函数调用在 Remix 中工作正常,但在 JS 中不行
我在将 chainlink VFR 映射链接到 Javascript 时遇到了麻烦。 我已将 VRF 的结果映射到调用者的地址,以便结果取决于调用者。
这是 Solidity 代码:
mapping(address => bytes32) private addressToId;
mapping(bytes32 => uint256) private IdToRandom;
function getRandomNumber() public returns (bytes32 requestId) {
require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK - fill contract with faucet");
requestId = requestRandomness(keyHash, fee);
addressToId[msg.sender] = requestId;
}
function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
IdToRandom[requestId] = randomness;
getResult();
}
function getResult() public view returns (uint randomnombre) {
randomnombre = IdToRandom[addressToId[msg.sender]];
}
当我在 Solidity 函数中调用 getResult() 来确定地址是否获胜时,它在 remix 上工作正常,但在 JS 上不起作用。
这是我的 JS 调用:
contract.methods.getResult().call().then(function(bal) { console.log(bal) })
它向我发送回 0,我不知道如何处理它......
I've been experiencing trouble to link my chainlink VFR mapping to my Javascript.
I've mapped the result of the VRF to the address of the caller so that the result depends on the caller.
Here is the solidity code:
mapping(address => bytes32) private addressToId;
mapping(bytes32 => uint256) private IdToRandom;
function getRandomNumber() public returns (bytes32 requestId) {
require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK - fill contract with faucet");
requestId = requestRandomness(keyHash, fee);
addressToId[msg.sender] = requestId;
}
function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
IdToRandom[requestId] = randomness;
getResult();
}
function getResult() public view returns (uint randomnombre) {
randomnombre = IdToRandom[addressToId[msg.sender]];
}
When I call getResult() in a solidity function to determine if the address won or not, it works fine on remix but won't work on JS.
Here is my JS call:
contract.methods.getResult().call().then(function(bal) { console.log(bal) })
It sends me back 0 and I don't know how to handle it...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您遇到了一个问题,即已调用
getRandomNumber()
并且事务已完成,但尚未发生对fulfillRandomness
的回调。在调用 getResult() 之前,您需要监听该事件,
以下方法之一应该可以完成此
web3:
https://web3js.readthedocs.io/ en/v1.2.11/web3-eth-contract.html#contract-events
以太币:
https://docs.ethers.io/v5/ api/providers/provider/#Provider--event-methods
I think you are running into an issue where the
getRandomNumber()
has been called and that transaction is complete but the callback tofulfillRandomness
hasn't occurred yet.You will need to listen for the event before calling
getResult()
One of the methods below should accomplish this
web3:
https://web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html#contract-events
ethers:
https://docs.ethers.io/v5/api/providers/provider/#Provider--event-methods