函数调用在 Remix 中工作正常,但在 JS 中不行

发布于 2025-01-09 09:47:53 字数 1034 浏览 3 评论 0原文

我在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰◇生如夏花灿烂 2025-01-16 09:47:53

我认为您遇到了一个问题,即已调用 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 to fulfillRandomness 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文