智能合约可以在混音上正常运行,但不能在Web3上正常运行

时间:2018-07-26 17:28:44

标签: javascript html solidity web3 remix

我的智能合约有问题。

调用该方法时,从元硬币合同转移以将令牌发送到另一个合同。

当我尝试从getAddress函数读取时,全零显示。

从Remix IDE调用传递函数时,从getAddress函数读取后,它将正确返回地址。

Web3版本0.20.6

松露版本v4.1.13

Geth版本1.8.12

MetaCoin合约:

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();
    }

}

我要发送令牌的合同:

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;



    }




}

附加到html页面的JavaScript代码,用于从合同调用方法:

 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(
//abi
).at(MetaCoinAddress);


        var tradeCenter = web3.eth.contract(

//abi

        ).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);




    }

When calling transfer function from web3 on web page.

When calling transfer function from Remix Ide.

0 个答案:

没有答案