简体   繁体   中英

EthersJS not properly returning a value from a smart contract on function call

I'm working on a DApp on the Ethereum network, and am using EthersJS on a react frontend to interact with a smart contract. I've deployed my smart contract and all seems to be going fine with most of my function calls, however for some reason I'm unable to call a couple of the contract functions from EtherJS properly. Here's what I mean...

In Remix (where I deployed the smart contract) I am able to call the view no problem; I'm getting the result I expect. 重新混合显示正确的值

However, when I call the same view from EthersJS, I'm getting the following: EthersJS 只为这个视图和这个视图返回任何内容。

As you can see in the screenshot, the view call from EthersJS returns 0x00 instead of the desired value.

Here's the snippet of the smart contract (in solidity) that I'm calling:

function getAllPendingRewardsByAddress(address _addr) public view returns (uint256) {
    uint256 pendingRewardsFromSnapshot;
    for (uint256 i = 1; i < currentMonth; i++) {
        pendingRewardsFromSnapshot += getPendingRewardPerSnapshot(i, _addr);
    }

    return pendingRewardsFromSnapshot;
}

... and here's my frontend code that I'm calling to access the data:

  const provider = new ethers.providers.Web3Provider(window.ethereum)
  const accounts = await provider.send("eth_requestAccounts", []);
  const pendingRewards = await dividendContract.getAllPendingRewardsByAddress(accounts[0])
  console.log("PENDING", pendingRewards)

I've already tried the following debugging steps:

  1. I've put the hexadecimal address directly in getAllPendingRewardsByAddress("ADDRESS") , no dice.
  2. I've verified that the ABI is correct.
  3. I've tried calling other views, they all seem to be working fine.

Some hunches I have...

  • Is this something to do with having a for loop in a view? Maybe EthersJS needs more gas to properly execute the for loop, or something along those lines?
  • ^ building off this... maybe it's something to do with assigning a local variable pendingRewardsFromSnapshot values that is causing the TX to not have enough gas?

I'm scratching my head with this one, never seen an issue like this before. Was wondering if anyone has had a similar problem, please let me know and thanks so much!

I found the issue, apparently EthersJS calls views with a separate account than the one I was calling the view from (really weird). Anyways, I had to change one of my supporting views ( getBalanceOfCallerWithPrecisionPerSnapshot ) to use a passed address instead of msg.sender, and that fixed my problem. If you have a similar problem, check there. Thanks for being my rubber duck @sms!

can you show me the getPendingRewardPerSnapshot() function maybe the problem is in that function or maybe you started your for loop from 1, maybe you should recheck it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM