0

the balance not change from another contract

pragma solidity ^0.8.10;


interface ICounter {
    function balanceOf(address owner) external view returns (uint);
    function transfer(address to, uint value) external view returns (bool);
    function balances(address owner) external returns (uint);
}

contract MyContract {
    uint public theBalance;
    function incrementCounter(address _counter,uint value) external {
        theBalance = ICounter(_counter).balances(msg.sender);
        theBalance = value;
    }

    function getCount(address _counter) external view returns (uint) {
        return ICounter(_counter).balanceOf(msg.sender);
    }

}
2
  • 1
    Please, can you share all of smart contract code including interface and other contracts? Mar 9, 2022 at 17:56
  • ICounter doesn't provide a method to modify its state. Both balanceOf and transfer are views so they won't change the contract state. And balances only accepts an address so there's no way to change the internal counter value.
    – Ismael
    Mar 10, 2022 at 0:11

1 Answer 1

0

this another contract

pragma solidity ^0.8.2;

contract Token {
    mapping(address => uint) public balances;
    mapping(address => mapping(address => uint)) public allowance;
    uint public totalSupply = 120000000 * 10 ** 18;
    string public name = "Test";
    string public symbol = "TST";
    uint public decimals = 18;
    
    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;   
    }
}
1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Mar 11, 2022 at 11:02

Your Answer

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

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