# How to add a transaction fee on my token?

**URL:** <https://forum.openzeppelin.com/t/how-to-add-a-transaction-fee-on-my-token/27897>\
**Category:** Smart Contracts\
**Created:** [April 22, 2022, 6:38pm UTC](https://forum.openzeppelin.com/t/how-to-add-a-transaction-fee-on-my-token/27897 "2022-04-22T18:38:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ered](https://avatars.discourse-cdn.com/v4/letter/e/3ab097/32.png) [@Ered](https://forum.openzeppelin.com/u/Ered)\
**Post date:** [April 22, 2022, 6:38pm UTC](https://forum.openzeppelin.com/t/how-to-add-a-transaction-fee-on-my-token/27897/1 "2022-04-22T18:38:30Z")

</div>

Hello

I want to create a BEP20 token which would have say 2% transaction fee on a seperate wallet. So when ever the token is bought or sold, 2% would go to a given wallet. I am deploying the contract using Remix. How can I make this happen?

---

<div class="post-metadata">

**Author:** ![cryptodev77](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@cryptodev77](https://forum.openzeppelin.com/u/cryptodev77)\
**Post date:** [April 22, 2022, 7:28pm UTC](https://forum.openzeppelin.com/t/how-to-add-a-transaction-fee-on-my-token/27897/2 "2022-04-22T19:28:02Z")

</div>

you need to add fee in transferFrom function in the contract

---

<div class="post-metadata">

**Author:** ![Team\_X](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/team_x/32/10471_2.png) [@Team\_X](https://forum.openzeppelin.com/u/Team_X)\
**Post date:** [April 23, 2022, 4:01pm UTC](https://forum.openzeppelin.com/t/how-to-add-a-transaction-fee-on-my-token/27897/3 "2022-04-23T16:01:55Z")

</div>

Go to your transfer function and check if the msg.sender is router or if the receipient is router. If so there is a buy or sell happening. You can then subtract the fee from the amount and send the fee to the external wallet

---

<div class="post-metadata">

**Author:** ![Ered](https://avatars.discourse-cdn.com/v4/letter/e/3ab097/32.png) [@Ered](https://forum.openzeppelin.com/u/Ered)\
**Post date:** [April 23, 2022, 5:14pm UTC](https://forum.openzeppelin.com/t/how-to-add-a-transaction-fee-on-my-token/27897/5 "2022-04-23T17:14:21Z")

</div>

```
function transfer(address to, uint value) public returns(bool) {
    require(balanceOf(msg.sender) >= value, 'balance too low');
    balances[to] += value;
    balances[msg.sender] -= value;
   emit Transfer(msg.sender, to, value);
    return true;
}

function transferFrom(address from, address to, uint value) public returns(bool) {
    require(balanceOf(from) >= value, 'balance too low');
    require(allowance[from][msg.sender] >= value, 'allowance too low');
    balances[to] += value;
    balances[from] -= value;
    emit Transfer(from, to, value);
    return true;   
}

```

Is this the transfer function? I get this contract watching a tutorial on YouTube

---

<div class="post-metadata">

**Author:** ![Team\_X](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/team_x/32/10471_2.png) [@Team\_X](https://forum.openzeppelin.com/u/Team_X)\
**Post date:** [April 23, 2022, 6:07pm UTC](https://forum.openzeppelin.com/t/how-to-add-a-transaction-fee-on-my-token/27897/6 "2022-04-23T18:07:50Z")

</div>

yes.

```auto
function transfer(address to, uint value) public returns(bool) {
    require(balanceOf(msg.sender) >= value, 'balance too low');
    uint256 fee = 2;
    uint256 feeAmount = value / 100 * fee;
    value -= feeAmount;
    balances[FEERECEIVER] += feeAmount;
    balances[to] += value;
    balances[msg.sender] -= value;
   emit Transfer(msg.sender, to, value);
    return true;
}

```

this is how the fee could look like

---

<div class="post-metadata">

**Author:** ![Ered](https://avatars.discourse-cdn.com/v4/letter/e/3ab097/32.png) [@Ered](https://forum.openzeppelin.com/u/Ered)\
**Post date:** [April 24, 2022, 3:25am UTC](https://forum.openzeppelin.com/t/how-to-add-a-transaction-fee-on-my-token/27897/7 "2022-04-24T03:25:11Z")

</div>

Okay so I would put the recipents wallet address inside [FEERECEIVER]?

---

<div class="post-metadata">

**Author:** ![Ered](https://avatars.discourse-cdn.com/v4/letter/e/3ab097/32.png) [@Ered](https://forum.openzeppelin.com/u/Ered)\
**Post date:** [April 24, 2022, 4:19am UTC](https://forum.openzeppelin.com/t/how-to-add-a-transaction-fee-on-my-token/27897/8 "2022-04-24T04:19:52Z")

</div>

And does that apply both, buying and selling?

---

<div class="post-metadata">

**Author:** ![Team\_X](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/team_x/32/10471_2.png) [@Team\_X](https://forum.openzeppelin.com/u/Team_X)\
**Post date:** [April 24, 2022, 12:31pm UTC](https://forum.openzeppelin.com/t/how-to-add-a-transaction-fee-on-my-token/27897/9 "2022-04-24T12:31:39Z")

</div>

yes and yes. It applies on both

---

<div class="post-metadata">

**Author:** ![Ered](https://avatars.discourse-cdn.com/v4/letter/e/3ab097/32.png) [@Ered](https://forum.openzeppelin.com/u/Ered)\
**Post date:** [April 24, 2022, 1:49pm UTC](https://forum.openzeppelin.com/t/how-to-add-a-transaction-fee-on-my-token/27897/10 "2022-04-24T13:49:48Z")

</div>

Thanks for the help. However, I tested the code on testnet and when ever I send tokens from wallet to wallet, it leaves 2% on the sender wallet. People should be able to freely send tokens between wallets without fees

Is it possible to make so that only when people buy or sell the token (from liquidity pool), it would send 2% to a given wallet?

---

<div class="post-metadata">

**Author:** ![Ered](https://avatars.discourse-cdn.com/v4/letter/e/3ab097/32.png) [@Ered](https://forum.openzeppelin.com/u/Ered)\
**Post date:** [April 24, 2022, 4:47pm UTC](https://forum.openzeppelin.com/t/how-to-add-a-transaction-fee-on-my-token/27897/11 "2022-04-24T16:47:04Z")

</div>

I would really appreciate if you could help me with this. This is the only thing holding me back to launch the token

---

<div class="post-metadata">

**Author:** ![Team\_X](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/team_x/32/10471_2.png) [@Team\_X](https://forum.openzeppelin.com/u/Team_X)\
**Post date:** [April 24, 2022, 5:02pm UTC](https://forum.openzeppelin.com/t/how-to-add-a-transaction-fee-on-my-token/27897/12 "2022-04-24T17:02:09Z")

</div>

oh yeah forgot one thing 😅

```auto
function transfer(address to, uint value) public returns(bool) {
    require(balanceOf(msg.sender) >= value, 'balance too low');
    if(msg.sender == ROUTER || to == ROUTER) {
       uint256 fee = 2;
       uint256 feeAmount = value / 100 * fee;
       value -= feeAmount;
       balances[FEERECEIVER] += feeAmount;
    }    
    balances[to] += value;
    balances[msg.sender] -= value;
   emit Transfer(msg.sender, to, value);
    return true;
}

```

---

<div class="post-metadata">

**Author:** ![Ered](https://avatars.discourse-cdn.com/v4/letter/e/3ab097/32.png) [@Ered](https://forum.openzeppelin.com/u/Ered)\
**Post date:** [April 24, 2022, 5:17pm UTC](https://forum.openzeppelin.com/t/how-to-add-a-transaction-fee-on-my-token/27897/13 "2022-04-24T17:17:33Z")

</div>

![err](https://us1.discourse-cdn.com/flex022/uploads/zeppelin/original/2X/0/0a6555f2d05836f235a76db223b7103aadb8c1b9.jpeg)

Gives me this error

---

<div class="post-metadata">

**Author:** ![Team\_X](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/team_x/32/10471_2.png) [@Team\_X](https://forum.openzeppelin.com/u/Team_X)\
**Post date:** [April 24, 2022, 5:23pm UTC](https://forum.openzeppelin.com/t/how-to-add-a-transaction-fee-on-my-token/27897/14 "2022-04-24T17:23:37Z")

</div>

yeah for ROUTER you need to input the address of the router

---

<div class="post-metadata">

**Author:** ![Ered](https://avatars.discourse-cdn.com/v4/letter/e/3ab097/32.png) [@Ered](https://forum.openzeppelin.com/u/Ered)\
**Post date:** [April 24, 2022, 5:23pm UTC](https://forum.openzeppelin.com/t/how-to-add-a-transaction-fee-on-my-token/27897/15 "2022-04-24T17:23:59Z")

</div>

Just figured this.. I will try, thanks alot for help

---

<div class="post-metadata">

**Author:** ![Ered](https://avatars.discourse-cdn.com/v4/letter/e/3ab097/32.png) [@Ered](https://forum.openzeppelin.com/u/Ered)\
**Post date:** [April 24, 2022, 9:03pm UTC](https://forum.openzeppelin.com/t/how-to-add-a-transaction-fee-on-my-token/27897/16 "2022-04-24T21:03:51Z")

</div>

Okay so I tried this on mainnet and I cant understand one thing.. The liquidity pool address is made AFTER the token deployment with Pancakeswap so I can't know the address beforehand, so how is it even possible to put Pancakeswap LP address when creating the contract?

---

<div class="post-metadata">

**Author:** ![Team\_X](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/team_x/32/10471_2.png) [@Team\_X](https://forum.openzeppelin.com/u/Team_X)\
**Post date:** [April 25, 2022, 6:50pm UTC](https://forum.openzeppelin.com/t/how-to-add-a-transaction-fee-on-my-token/27897/17 "2022-04-25T18:50:54Z")

</div>

why would you need the LP token? just the pancakeswap router address.

---

<div class="post-metadata">

**Author:** ![Ered](https://avatars.discourse-cdn.com/v4/letter/e/3ab097/32.png) [@Ered](https://forum.openzeppelin.com/u/Ered)\
**Post date:** [April 27, 2022, 12:02am UTC](https://forum.openzeppelin.com/t/how-to-add-a-transaction-fee-on-my-token/27897/18 "2022-04-27T00:02:51Z")

</div>

I did put the Pancakeswap router address and it still doesnt work. For what I understand, people are interacting with the Token's LP address, not the router address

---

<div class="post-metadata">

**Author:** ![Team\_X](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/team_x/32/10471_2.png) [@Team\_X](https://forum.openzeppelin.com/u/Team_X)\
**Post date:** [April 27, 2022, 12:18pm UTC](https://forum.openzeppelin.com/t/how-to-add-a-transaction-fee-on-my-token/27897/19 "2022-04-27T12:18:35Z")

</div>

when you swap a token you are interacting with the router of pancakeswap.

---

<div class="post-metadata">

**Author:** ![Ered](https://avatars.discourse-cdn.com/v4/letter/e/3ab097/32.png) [@Ered](https://forum.openzeppelin.com/u/Ered)\
**Post date:** [April 28, 2022, 3:58pm UTC](https://forum.openzeppelin.com/t/how-to-add-a-transaction-fee-on-my-token/27897/20 "2022-04-28T15:58:49Z")

</div>

Well I tried that code many times and it doesn't seem to work. No fee's are transferred to desired account

---

<div class="post-metadata">

**Author:** ![demetri](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@demetri](https://forum.openzeppelin.com/u/demetri)\
**Post date:** [April 30, 2022, 6:45am UTC](https://forum.openzeppelin.com/t/how-to-add-a-transaction-fee-on-my-token/27897/21 "2022-04-30T06:45:50Z")

</div>

I'm also seeking help with this issue. Maybe if openzeppelin could write the function into the Wizard in docs? that would be very neat!!

[Next page](https://forum.openzeppelin.com/t/how-to-add-a-transaction-fee-on-my-token/27897.md?page=2)
