Solidity Max % transaction

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?

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 :

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.

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

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;   
    }
}```

in your case, there is two simple approach:

  1. directly include that in transfer() and transferFrom() :
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() :
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

1 Like

Very grateful, thanks, @gsx !