1

before I start just wanted to say that I started a few time ago with solidity and I'm still noob, but I've been reading and researching about this and can't get it working.

I'm using ethereum wallet with ropsten network.

Let's say I have X tokens from a standar ERC20 token already deployed, I know its contract address. I want to create a contract to transfer that to another account. I want to do this so in the future I could send to multiple accounts at once, but wanted to go step by step.

Should something like this work?

    pragma solidity ^0.4.18;

contract Token {
    function totalSupply() constant public returns (uint256 supply);
    function balanceOf(address _owner) constant public returns (uint256 balance);
    function transfer(address _to, uint256 _value) public returns (bool success) ;
    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) ;
    function approve(address _spender, uint256 _value) public returns (bool success) ;
    function allowance(address _owner, address _spender) constant public returns (uint256 remaining) ;
}

contract Envio{

    function Envio() public{
         Token miToken = Token(0x87458903eB3A37775A9CC097f80eb703D1cd0231); //Contract address
         uint8   decimals = 18;
         uint256   decimalFactor = 10 ** uint256(decimals);
        miToken.transfer(0xD49E71da9b64088BA1Ea1535c97f2985F9D3B1Bb,5000*decimalFactor); 
    } 
}

The result I'm getting after the transaction completes is: no tokens are transferred and it says gas used = 0. Also tried using transferFrom, same results.

I know that what I'm doing might be completely wrong, just looking for some guidance.

1

The contract can't successfully call transfer because it doesn't own any tokens. It presumably can't successfully call transferFrom because it wasn't approved by the token owner (you) to transfer your tokens.

You'll need to deploy the contract first (without trying to do a transfer in the constructor) and then either:

  1. Call approve on the token contract to allow your new contract to use transferFrom, or
  2. Call transfer on the token contract to transfer tokens to the new contract.

Then you can call a function on your new contract that transfers those tokens somewhere else.

  • Could you add any code snippet? As I said still learning and don't want to mess it up. Thank you – emiliomarin Feb 21 '18 at 22:54

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.