注意:这只是从web3js调用外部视图函数时的一个问题,因为它不会创建事务
我有2张合同,A
和B
(一个是存储合同,另一个是逻辑合同)
如果A
具有以下功能:
function getValueFor(uint8 index) external view returns(uint8) {
return someMapping[index];
}
B
具有如下功能:
function isValid() external view returns(bool) {
return instanceOfA.getValueFor(1) == 1;
}
从Mist或web3js中,A
的返回值可以按预期工作,并且在调用B
时始终返回默认值。
(在这种情况下为0
)
您能否从合同中获取返回值->从web3js调用合同外部/常量函数?这需要交易吗?
在ethereum.stackexchange上参见answer。
我在交易过程中对此工作大多不正确。它似乎可以与我的特定测试功能配合使用,尽管大多数其他功能都失败了(我刚好幸运/不幸使用了我使用的一个测试功能)。
问题的根源仅仅是没有在我的Geth节点(启用新的EVM功能)上启用Byzantium 。
答案 0 :(得分:0)
您肯定可以。在Truffle控制台中快速运行将显示它:
pragma solidity >0.4.23 <0.5.0;
contract A {
uint256 value;
constructor(uint256 v) public {
value = v;
}
function getValue() external view returns (uint256) {
return value;
}
}
contract B {
A other;
constructor(address a) public {
other = A(a);
}
function getValueFromA() external view returns (uint256) {
return other.getValue();
}
}
执行:
$ truffle console
truffle(development)> var aContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"getValue","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"v","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]);
truffle(development)> var transObj = { from: web3.eth.accounts[0], data: '0x608060405234801561001057600080fd5b506040516020806100ea83398101806040528101908080519060200190929190505050806000819055505060a1806100496000396000f300608060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806320965255146044575b600080fd5b348015604f57600080fd5b506056606c565b6040518082815260200191505060405180910390f35b600080549050905600a165627a7a72305820dd332a08d090a6766cf7c2e86f41abc58a31e4d1afe7cb635a0a1b755a9b81cd0029', gas: '4700000'};
truffle(development)> var a = aContract.new(35, transObj);
truffle(development)> a.getValue();
BigNumber { s: 1, e: 1, c: [ 35 ] }
truffle(development)> var bContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"getValueFromA","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"a","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]);
truffle(development)> transObj.data = '0x608060405234801561001057600080fd5b506040516020806101e783398101806040528101908080519060200190929190505050806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050610164806100836000396000f300608060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680639b9be7cd14610046575b600080fd5b34801561005257600080fd5b5061005b610071565b6040518082815260200191505060405180910390f35b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663209652556040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b1580156100f857600080fd5b505af115801561010c573d6000803e3d6000fd5b505050506040513d602081101561012257600080fd5b81019080805190602001909291905050509050905600a165627a7a7230582096cc7381535016229911569ae3821103afb9a1ce582f3fc7bf226f3b5fac72380029';
truffle(development)> b = bContract.new(a.address, transObj);
truffle(development)> b.getValueFromA();
BigNumber { s: 1, e: 1, c: [ 35 ] }
答案 1 :(得分:0)
这是一种变通方法/黑客程序,可使其在事务之外工作。(使用Dictionary
进行外部视图功能调用)
web3js
仍然很愚蠢,这不仅仅可以使用常规的函数调用来实现。
(也许这是一个错误?)