2

I have deployed a token contract, and now I want to use multiple transfers. I figured I can do this by deploying another contract which will run iteration over transfer function of my deployed token contract.

But I am getting failed transaction. I have run another test to clarify as well, below are the details:

Deployed Contract Sample:

contract MyToken {
    /* This creates an array with all balances */
    mapping (address => uint256) public balanceOf;

    /* Initializes contract with initial supply tokens to the creator of the contract */
    function MyToken(
        uint256 initialSupply
        ) {
        balanceOf[msg.sender] = initialSupply;              // Give the creator all initial tokens
    }

    /* Send coins */
    function transfer(address _to, uint256 _value) {
        require(balanceOf[msg.sender] >= _value);           // Check if the sender has enough
        require(balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows
        balanceOf[msg.sender] -= _value;                    // Subtract from the sender
        balanceOf[_to] += _value;                           // Add the same to the recipient
    }
}

Caller Contract Sample:

pragma solidity ^0.4.21;

contract MyToken {function transfer(address _to, uint256 _value) public; }

contract ADTest {

    function CallTransfer(address tokenAddress, address _to, uint256 _value) public {
        MyToken(tokenAddress).transfer(_to, _value);
    }

}

Can someone please help with the solution? I am not sure what I am doing wrong.

UPDATE I figured If I send some tokens to the caller contract, then caller contract sends to different addresses, without any problem. Not sure weather its a suitable solution for now, but might will help someone.

1

Try like this. Comments in-line.

pragma solidity ^0.4.21;

// added I as naming convention (Interface)
contract MyTokenI {function transfer(address _to, uint256 _value) public; }

contract ADTest {

    // will hold an instance cast as type MyToken
    MyTokenI myToken;

    // pass the deployed MyToken address into this constructor
    function ADTest(address _myToken) public {
        // instantiate the deployed contract at _myToken
        myToken = MyTokenI(_myToken);
    }

    function CallTransfer(address _to, uint256 _value) public {
        // use the instance
        myToken.transfer(_to, _value);
    }

}

Hope it helps.

  • Hi @Rob Hitchens Thank you for the quick response, but I am still getting the same error, Transaction Error. Exception thrown in contract code. Gas limit set dangerously high. Approving this transaction is likely to fail. – Wajid Khilji Mar 21 '18 at 21:39
0

Well, 1st you create MyToken with your user 0x1234 Then, address 0x1234... calls ADTest.CallTransfer(). MyToken deployed contract will check balance of msg.sender. But msg.sender is contract ADTest, not user "0x1234". So, require(balanceOf[msg.sender] >= _value); will fail and transaction will revert.

Here is my suggestion, using Rob´s suggestion also:

contract MyToken {
    /* This creates an array with all balances */
    mapping (address => uint256) public balanceOf;

    /* Initializes contract with initial supply tokens to the creator of the contract */
    function MyToken(
        uint256 initialSupply
        ) {
        balanceOf[msg.sender] = initialSupply;              // Give the creator all initial tokens
    }

    /* Send coins */
    function transfer(address sender, address _to, uint256 _value) {
        require(balanceOf[sender] >= _value);           // Check if the sender has enough
        require(balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows
        balanceOf[sender] -= _value;                    // Subtract from the sender
        balanceOf[_to] += _value;                           // Add the same to the recipient
    }
}

contract MyTokenI {function transfer(address sender, address _to, uint256 _value) public; }

contract ADTest {

    // will hold an instance cast as type MyToken
    MyTokenI myToken;

    // pass the deployed MyToken address into this constructor
    function ADTest(address _myToken) public {
        // instantiate the deployed contract at _myToken
        myToken = MyTokenI(_myToken);
    }

    function CallTransfer(address _to, uint256 _value) public {
        // use the instance
        myToken.transfer(msg.sender, _to, _value);
    }

}
  • Hi @luiz, I am looking for solution for a deployed contract, but this solution will require me to change signature of already deployed contract. Which I cannot do. – Wajid Khilji Mar 21 '18 at 21:42
  • Well, in that case, only who deployed MyToken will be able to transfer because of this require: balanceOf[msg.sender] = initialSupply; – Luiz Soares Mar 21 '18 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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