智能合约之间的交互不起作用



我部署了两个合约,一个是 Callee,另一个是 Caller。调用方使用被叫方提供的函数。直接调用被调用方的函数是成功的,但是,调用方不起作用。实际上,我从互联网上尝试了不同的案例,但没有一个有效。我错过了一些棘手的事情吗?以下是源代码:

  • Callee.sol

    pragma solidity ^0.4.6;
    contract Callee {
    uint[] public values;
    function getValue(uint initial) public pure returns(uint) {
    return initial + 150;
    }
    function storeValue(uint value) public {
    values.push(value);
    }
    function getValues() public view returns(uint) {
    return values.length;
    }
    }
    
  • Caller.sol

    pragma solidity ^0.4.6;
    contract Caller {
    function someAction(address addr) public returns(uint) {
    Callee c = Callee(addr);
    return c.getValue(100);
    }
    function storeAction(address addr) public returns(uint) {
    Callee c = Callee(addr);
    c.storeValue(100);
    return c.getValues();
    }
    function someUnsafeAction(address addr) public returns(bool){
    return addr.call(bytes4(keccak256("storeValue(uint256)")), 100);
    }
    }
    contract Callee {
    function getValue(uint initialValue) public returns(uint);
    function storeValue(uint value) public;
    function getValues() public returns(uint);
    }
    

Caller在技术上是有效的,但没有做你所期望的。

问题是Caller.someAction没有用pureview修饰符标记,因此您的函数调用正在触发新事务。事务调用不能返回值(它编译并运行,但不返回任何内容)。

如果您将Caller.someAction更改为pure函数(并且,还将界面中的Callee.getValue更改为pure以匹配合约),您将获得预期的 250 返回。

pragma solidity ^0.4.6;
contract Caller {
function someAction(address addr) public pure returns(uint) {
Callee  c = Callee(addr);
return c.getValue(100);
}
function storeAction(address addr) public returns(uint) {
Callee c = Callee(addr);
c.storeValue(100);
return c.getValues();
}
function someUnsafeAction(address addr) public returns(bool){
return addr.call(bytes4(keccak256("storeValue(uint256)")), 100);
}
}
contract Callee {
function getValue(uint initialValue) public pure returns(uint);
function storeValue(uint value) public;
function getValues() public returns(uint);
}

在终端中:

$ truffle migrate --reset
Compiling .contractsCallee.sol...
Compiling .contractsSimpleContract.sol...
Writing artifacts to .buildcontracts
Using network 'development'.
Running migration: 1_initial_migration.js
Replacing Migrations...
... 0x58a14c93acc733bb08e4bb56978d0bb466f8aca7659673426d989ee7e0e626f3
Migrations: 0x69ed5e4d6172639ed7d3c456ea8b2f2562c7dbcd
Saving successful migration to network...
... 0x9a3831076748cc6179ef3e4b3e466c3a4a871e196376bc22c984e91e95fdc567
Saving artifacts...
Running migration: 2_deploy_contracts.js
Replacing Caller...
... 0x7be6840c86a356decacc13967d50ef4fea30e86d30d69b19fb9998ce500c95e8
Caller: 0xb8c5e079af71813acac73bff9ca8e9e068660e86
Replacing Callee...
... 0xba7f588f9119e67968a7d6ab0110d79e1ecd3003e10cce3134018d42e966c8be
Callee: 0xd5c110c2f6566fd56749ec1a11328405f9935385
Saving successful migration to network...
... 0xc1526876e365189be1529a0943042e3a25e395a5cc19930ad907a694165104f1
Saving artifacts...
$ truffle console
truffle(development)> var caller = Caller.at('0xb8c5e079af71813acac73bff9ca8e9e068660e86');
undefined
truffle(development)> caller.someAction('0xd5c110c2f6566fd56749ec1a11328405f9935385');
{ [String: '250'] s: 1, e: 2, c: [ 250 ] }

最新更新