【发布时间】:2018-10-04 17:57:49
【问题描述】:
我正在尝试从我的智能合约上使用“区块难度”和“区块时间戳”的函数中提取一个随机数。
solidity 代码如下所示:
pragma solidity ^0.4.18;
contract ApprovalContract {
function random() external view returns (uint8){
return uint8(uint256(keccak256(abi.encodePacked(block.timestamp,
block.difficulty)))%251);
}
...
}
用 JS 编写的应用程序代码如下所示:
ApprovalContract.methods.random().call(
function(error, result) {
if (error) {
console.log('error: ' + error);
}
else {
console.log('result: ' + JSON.stringify(result));
}
});
我需要做的就是将随机生成的数字记录到控制台。不幸的是,我一直看到“未捕获的 TypeError:ApprovalContract.methods.random 不是函数”
【问题讨论】:
-
将其更改为
ApprovalContract.methods.random.call(...)(random上没有括号) -
@AdamKipnis index.html:204 Uncaught TypeError: Cannot read property 'call' of undefined at HTMLFormElement.
(index.html:204) at HTMLFormElement.dispatch (jquery-3.2.1. slim.min.js:3) 在 HTMLFormElement.q.handle (jquery-3.2.1.slim.min.js:3) 我也看不到我的“随机”函数列为合约对象中的方法跨度>
标签: ethereum solidity smartcontracts web3 web3js