# Solidity Max % transaction

**URL:** <https://forum.openzeppelin.com/t/solidity-max-transaction/13574>\
**Category:** Support\
**Tags:** bep20\
**Created:** [August 6, 2021, 3:35am UTC](https://forum.openzeppelin.com/t/solidity-max-transaction/13574 "2021-08-06T03:35:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carlos\_De\_Gracia](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/carlos_de_gracia/32/5178_2.png) [@Carlos\_De\_Gracia](https://forum.openzeppelin.com/u/Carlos_De_Gracia)\
**Post date:** [August 6, 2021, 3:35am UTC](https://forum.openzeppelin.com/t/solidity-max-transaction/13574/1 "2021-08-06T03:35:43Z")

</div>

Good Night,

can anyone help me how to add : Sales are restricted to less than 0.2% of the total supply

into my contract. whats the source code for that?

---

<div class="post-metadata">

**Author:** ![gsx](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/gsx/32/5991_2.png) [@gsx](https://forum.openzeppelin.com/u/gsx)\
**Post date:** [August 6, 2021, 12:02pm UTC](https://forum.openzeppelin.com/t/solidity-max-transaction/13574/2 "2021-08-06T12:02:39Z")

</div>

well, you may want to place your source code here so people get better explanation.

but here is the general flow :

1. you should have a variable that hold `maximum amount of transaction`.
2. then before transfer, check the transferred amount. (you can place it in hook)

in code, it looks like this :

```auto
uint256 private _maxTrx;

function transfer(from, to, amount) {

  require(amount <= _maxTrx, "exceeds maximum transfer");

  /**
   * your code here
   */
}

```

but, i recommend you to learn Solidity Programming Language from scratch for better understanding.

---

<div class="post-metadata">

**Author:** ![Crypt0nacci](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/crypt0nacci/32/8162_2.png) [@Crypt0nacci](https://forum.openzeppelin.com/u/Crypt0nacci)\
**Post date:** [August 11, 2021, 3:53pm UTC](https://forum.openzeppelin.com/t/solidity-max-transaction/13574/3 "2021-08-11T15:53:57Z")

</div>

@gsx I found your reply helpful - thank you. Are you putting that here (at the bottom of the "Contract Token" area)?

````auto
pragma solidity ^0.8.2;

contract Token {
    mapping(address => uint) public balances;
    mapping(address => mapping(address => uint)) public allowance;
    uint public totalSupply = 1000000000000000 * 10 ** 18;
    string public name = "My Token";
    string public symbol = "TKN";
    uint public decimals = 18;
    uint256 public _maxTrx = 20000000000000 * 10 ** 18;

function transfer(from, to, amount) {

  require(amount <= _maxTrx, "exceeds maximum transfer");

  /**
   * your code here
   */
}
    
    event Transfer(address indexed from, address indexed to, uint value);
    event Approval(address indexed owner, address indexed spender, uint value);
    
    constructor() {
        balances[msg.sender] = totalSupply;
    }
    
    function balanceOf(address owner) public returns(uint) {
        return balances[owner];
    }
    
    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;   
    }
    
    function approve(address spender, uint value) public returns (bool) {
        allowance[msg.sender][spender] = value;
        emit Approval(msg.sender, spender, value);
        return true;   
    }
}```
````

---

<div class="post-metadata">

**Author:** ![gsx](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/gsx/32/5991_2.png) [@gsx](https://forum.openzeppelin.com/u/gsx)\
**Post date:** [August 11, 2021, 4:28pm UTC](https://forum.openzeppelin.com/t/solidity-max-transaction/13574/4 "2021-08-11T16:28:25Z")

</div>

in your case, there is two simple approach:

1. directly include that in `transfer()` and `transferFrom()` :

```auto
function transfer(to, value) {
   require(value <= _maxTx, "your message here");
   /* your code here */
}

function transferFrom(from, to, value) {
   require(value <= _maxTx, "your message here");
   /* your code here */
}

```

1. create a new internal `_transfer()` and move the transfer logic to there, then consume the function in `transfer()` and `transferFrom()` :

```auto
function _transfer(from, to, value) internal {
   require(value <= _maxTx, "your message here");
   /* your code here */
}

function transfer(to, value) {
   /* your code here */
   _transfer(msg.sender, to, value);
}

function transferFrom(from, to, value) {
   /* your code here */
   _transfer(from, to, value);
}

```

im recommending that you move transfer logic to an internal function. so the public `transfer` and `transferFrom` is only used for additional validation, take a look at [OZ : ERC20](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol)

---

<div class="post-metadata">

**Author:** ![Crypt0nacci](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/crypt0nacci/32/8162_2.png) [@Crypt0nacci](https://forum.openzeppelin.com/u/Crypt0nacci)\
**Post date:** [August 11, 2021, 5:25pm UTC](https://forum.openzeppelin.com/t/solidity-max-transaction/13574/5 "2021-08-11T17:25:31Z")

</div>

Very grateful, thanks, @gsx !
