1

I have a problem with my smart contract.

When calling the method, transfer from the metacoin contract to send tokens to another contract.

When I try to read from the getAddress function, all zeros display. When I do the same with Remix everything works fine.

Web3 version 0.20.6

Truffle version v4.1.13

Geth version 1.8.12

MetaCoin contract:

pragma solidity ^0.4.4;

import "./ERC223_interface.sol";
import "./SafeMath.sol";
import "./ERC223_receiving_contract.sol";
import "./ERC20_interface.sol";

//ERC223Interface

contract MetaCoin is ERC20Interface, ERC223Interface { 
    using SafeMath for uint;

    string public constant symbol = "MC";
    string public constant name = "MetaCoin";
    uint256 public constant decimals = 18;

    uint256 private constant _totalSupply = 1000000;

    address private _owner;


    mapping(address => uint) private balances;

    mapping(address => mapping(address => uint)) private allowed;

    constructor() public{
        _owner = msg.sender;
        balances[_owner] = _totalSupply;
        emit Transfer(address(0), _owner, _totalSupply);
    }

    function totalSupply() public view returns (uint) {
        return _totalSupply;
    }

    function balanceOf(address takeOwner) public view returns (uint balance){
        return balances[takeOwner];

    }

    function allowance(address tokenOwner, address spender) public view returns (uint remaining) {
        return allowed[tokenOwner][spender];

    }

    function transfer(address to, uint tokens) public returns (bool success){
        if(tokens <= balanceOf(msg.sender) && tokens>0 && !checkContract(to)){
            balances[msg.sender] = balances[msg.sender].sub(tokens);
            balances[to] = balances[to].add(tokens);
            emit Transfer(msg.sender, to, tokens);
            return true;

        }

        return false;



    }

    function approve(address spender,uint tokens) public returns(bool success){
        allowed[msg.sender][spender] = tokens;
        emit Approval(msg.sender, spender, tokens);
        return true;

    }

    function transferFrom(address from, address to, uint tokens) public returns (bool success){
        if(allowed[from][msg.sender] > 0 &&
            tokens > 0 && 
            allowed[from][msg.sender] >= tokens && 
            balances[from]>=tokens){


            balances[from] = balances[from].sub(tokens);
            allowed[from][msg.sender] = allowed[from][msg.sender].sub(tokens);
            balances[to] = balances[to].add(tokens);
            emit Transfer(from, to, tokens);
            return true;


        }
        return false;

    }

    function transfer(address to, uint tokens, bytes data) public returns (bool){
        if(tokens <= balanceOf(msg.sender) && tokens>0 && checkContract(to)){
            balances[msg.sender] = balances[msg.sender].sub(tokens);
            balances[to] = balances[to].add(tokens);
            ERC223ReceivingContract _contract = ERC223ReceivingContract(to);
            _contract.tokenFallback(msg.sender,tokens,data);
            emit Transfer(msg.sender, to, tokens,data);
            return true;

        }

        return false;



    }

    function checkContract(address addr) private view returns(bool){
        uint size;
        assembly {
            size := extcodesize(addr)
        }
        return size > 0;

    }



    function () public payable {
        revert();
    }

}

The contract I am trying to send tokens:

pragma solidity ^0.4.4;

import "./ERC223_receiving_contract.sol";



contract TradeCenter is ERC223ReceivingContract{


    struct Rec{
        address from;
        uint tokens;
        bytes data;

    }

    mapping(address => uint) private _indexRec;
    mapping(uint => Rec) private _recs;
    uint private _index = 0;

    function tokenFallback(address _from, uint _value, bytes _data) public {

        _recs[_index] = Rec(_from,_value,_data);
        _indexRec[_from] = _index;
        _index++;


    }

    function getAddress() public view returns(address){

        Rec memory _rec = _recs[_indexRec[msg.sender]];

        return _rec.from;



    }




}

And JavaScript code for calling methods from contract:

const MetaCoinAddress = "0x64413e3d7775003c801ac50d4becedf038bad5b1";
const tradecenterAddress = "0x645b70b1f5f1df0e4c0e8efb23025ccace971d3b";





if(typeof web3 !== 'undefined'){

    web3 = new Web3(web3.currentProvider);

}else{
    //TODO change Http provider to mainnet

    web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

    //main account[0] only for tests

    web3.eth.defaultAccount = web3.eth.accounts[0];

    var tokenContract = web3.eth.contract([
    {
      "constant": true,
      "inputs": [],
      "name": "name",
      "outputs": [
        {
          "name": "",
          "type": "string"
        }
      ],
      "payable": false,
      "stateMutability": "view",
      "type": "function"
    },
    {
      "constant": true,
      "inputs": [],
      "name": "decimals",
      "outputs": [
        {
          "name": "",
          "type": "uint256"
        }
      ],
      "payable": false,
      "stateMutability": "view",
      "type": "function"
    },
    {
      "constant": true,
      "inputs": [],
      "name": "symbol",
      "outputs": [
        {
          "name": "",
          "type": "string"
        }
      ],
      "payable": false,
      "stateMutability": "view",
      "type": "function"
    },
    {
      "inputs": [],
      "payable": false,
      "stateMutability": "nonpayable",
      "type": "constructor"
    },
    {
      "payable": true,
      "stateMutability": "payable",
      "type": "fallback"
    },
    {
      "anonymous": false,
      "inputs": [
        {
          "indexed": true,
          "name": "from",
          "type": "address"
        },
        {
          "indexed": true,
          "name": "to",
          "type": "address"
        },
        {
          "indexed": false,
          "name": "value",
          "type": "uint256"
        },
        {
          "indexed": false,
          "name": "data",
          "type": "bytes"
        }
      ],
      "name": "Transfer",
      "type": "event"
    },
    {
      "anonymous": false,
      "inputs": [
        {
          "indexed": true,
          "name": "from",
          "type": "address"
        },
        {
          "indexed": true,
          "name": "to",
          "type": "address"
        },
        {
          "indexed": false,
          "name": "tokens",
          "type": "uint256"
        }
      ],
      "name": "Transfer",
      "type": "event"
    },
    {
      "anonymous": false,
      "inputs": [
        {
          "indexed": true,
          "name": "tokenOwner",
          "type": "address"
        },
        {
          "indexed": true,
          "name": "spender",
          "type": "address"
        },
        {
          "indexed": false,
          "name": "tokens",
          "type": "uint256"
        }
      ],
      "name": "Approval",
      "type": "event"
    },
    {
      "constant": true,
      "inputs": [],
      "name": "totalSupply",
      "outputs": [
        {
          "name": "",
          "type": "uint256"
        }
      ],
      "payable": false,
      "stateMutability": "view",
      "type": "function"
    },
    {
      "constant": true,
      "inputs": [
        {
          "name": "takeOwner",
          "type": "address"
        }
      ],
      "name": "balanceOf",
      "outputs": [
        {
          "name": "balance",
          "type": "uint256"
        }
      ],
      "payable": false,
      "stateMutability": "view",
      "type": "function"
    },
    {
      "constant": true,
      "inputs": [
        {
          "name": "tokenOwner",
          "type": "address"
        },
        {
          "name": "spender",
          "type": "address"
        }
      ],
      "name": "allowance",
      "outputs": [
        {
          "name": "remaining",
          "type": "uint256"
        }
      ],
      "payable": false,
      "stateMutability": "view",
      "type": "function"
    },
    {
      "constant": false,
      "inputs": [
        {
          "name": "spender",
          "type": "address"
        },
        {
          "name": "tokens",
          "type": "uint256"
        }
      ],
      "name": "approve",
      "outputs": [
        {
          "name": "success",
          "type": "bool"
        }
      ],
      "payable": false,
      "stateMutability": "nonpayable",
      "type": "function"
    },
    {
      "constant": false,
      "inputs": [
        {
          "name": "from",
          "type": "address"
        },
        {
          "name": "to",
          "type": "address"
        },
        {
          "name": "tokens",
          "type": "uint256"
        }
      ],
      "name": "transferFrom",
      "outputs": [
        {
          "name": "success",
          "type": "bool"
        }
      ],
      "payable": false,
      "stateMutability": "nonpayable",
      "type": "function"
    },
    {
      "constant": false,
      "inputs": [
        {
          "name": "to",
          "type": "address"
        },
        {
          "name": "tokens",
          "type": "uint256"
        }
      ],
      "name": "transfer",
      "outputs": [
        {
          "name": "success",
          "type": "bool"
        }
      ],
      "payable": false,
      "stateMutability": "nonpayable",
      "type": "function"
    },
    {
      "constant": false,
      "inputs": [
        {
          "name": "to",
          "type": "address"
        },
        {
          "name": "tokens",
          "type": "uint256"
        },
        {
          "name": "data",
          "type": "bytes"
        }
      ],
      "name": "transfer",
      "outputs": [
        {
          "name": "",
          "type": "bool"
        }
      ],
      "payable": false,
      "stateMutability": "nonpayable",
      "type": "function"
    }
    ]).at(MetaCoinAddress);


    var tradeCenter = web3.eth.contract(
      [
        {
          "constant": false,
          "inputs": [
            {
              "name": "_from",
              "type": "address"
            },
            {
              "name": "_value",
              "type": "uint256"
            },
            {
              "name": "_data",
              "type": "bytes"
            }
          ],
          "name": "tokenFallback",
          "outputs": [],
          "payable": false,
          "stateMutability": "nonpayable",
          "type": "function"
        },
        {
          "constant": true,
          "inputs": [],
          "name": "getAddress",
          "outputs": [
            {
              "name": "",
              "type": "address"
            }
          ],
          "payable": false,
          "stateMutability": "view",
          "type": "function"
        }
      ]
    ).at(tradecenterAddress);



    var account0 = web3.eth.accounts[0];

    var account1 = web3.eth.accounts[1];


    console.log(tokenContract.transfer['address,uint256,bytes'](tradecenterAddress,web3.toWei(20,'ether'),web3.toAscii("te"),{from:account0}));

        //("0xab327cdaf135c3dcacf58a4686f11dcd0ef1d031",web3.toWei(20,'ether'),'tes'));

    console.log(tradeCenter.getAddress());

    console.log(web3.version);




}

Your Answer

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

Browse other questions tagged or ask your own question.