0

ERC20 Token Smart Contract:

contract ERC20token is  IERC20 {
   
    using SafeMath for uint256;
    using Address for address;
    
    mapping (address => uint256) public _balances;
    uint256 private _totalSupply;
    uint256 private _cap=10000;
    address public owner; 
    string private _name;
    string private _symbol;
    uint8 private _decimals;

    constructor (string memory name, string memory symbol) public {
        _name = name;
        _symbol = symbol;
        _decimals = 18;
        _mint(msg.sender, 2000);
        owner=msg.sender;
    }
    

    /* other functions */

    /**Creates `amount` tokens and assigns them to `account`, increasing
     * the total supply.
     * Requirements
     * - `to` cannot be the zero address.
     */
    function _mint(address account, uint256 _amount) public virtual {
        require(account != address(0), "ERC20: mint to the zero address");
        
        _beforeTokenTransfer(address(0), account, _amount);

        _totalSupply = _totalSupply.add(_amount);
        _balances[account] = _balances[account].add(_amount);
       
    }
   
    function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual {
         if (from == address(0)) { // When minting tokens
            require(totalSupply().add(amount) <= _cap, "ERC20Capped: cap exceeded");
        }
     }
}

Test Smart Contract:

contract Test 
   {
     
     address owner; 
     ERC20token ec;
     
      constructor () public {
        owner=msg.sender; 
     }

    function Asset_mint(uint _amount) public 
    {
         // some conditions 
         ec._mint(msg.sender, _amount);
         
    }
}

When function Asset_mint() is called with an amount more than msg.sender's balance, transaction is reverted with the error:

  • The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance.

is it not possible to call a function in a smart contract from another smart contract by passing a numeric value?

1
  • you mean like anotherContract.someFunction{value:<somevalue>}(); ?
    – sola24
    Sep 1, 2022 at 15:32

1 Answer 1

0

is it not possible to call a function in a smart contract from another smart contract by passing a numeric value?

I think this is what you are driving at.

contract testA {

    uint public testNum;

    function someFunction() public payable returns (uint) {
        return ++testNum;
    }
}


contract testB {

    testA anotherContract;

    constructor() payable {
        anotherContract = new testA();
        console.log(address(anotherContract));
    }

    receive() external payable {}

    function getBalance() external view returns (uint) {
        uint _bal = address(this).balance;
        console.log(_bal);
        return _bal;
    }

    function callOther(uint _amountToOther) external payable returns (uint) {
        console.log("contract balance", address(this).balance);
        console.log("calling other function passing %d wei", _amountToOther);
        uint result = anotherContract.someFunction{value: _amountToOther * 1 wei}();
        console.log("other contract function call returned result", result);
        console.log("contract balance", address(this).balance);
        return result;
    }
}
5
  • uint result = anotherContract.someFunction{value: _amountToOther * 1 wei}(); here value is passed to somefuntion() because its declared payable and it can receive wei or ether.
    – Monika
    Sep 1, 2022 at 16:54
  • The issue I am facing is I need to call _mint() ** in the Test contract without passing Wei or Ether to **_mint(). Can it be done?
    – Monika
    Sep 1, 2022 at 16:57
  • callOther() doesn't have to be payable. that's just optional. just for fun. someFunction() does have to be payable if you want to send value when you call that function, which I think you did, right?
    – sola24
    Sep 1, 2022 at 16:59
  • "The issue I am facing is I need to call _mint() ** in the Test contract without passing Wei or Ether to **_mint(). Can it be done?" ---- of course. why would you have to send eth?
    – sola24
    Sep 1, 2022 at 17:01
  • I forgot to initialize ERC20token ec; in Test contarct, that's why I was getting the following error: "The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance." Thank you.
    – Monika
    Sep 1, 2022 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.