简体   繁体   English

EthersJS 未正确从 function 调用的智能合约返回值

[英]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.我正在以太坊网络上开发 DApp,并在反应前端使用 EthersJS 与智能合约进行交互。 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.我已经部署了我的智能合约,我的大部分function 调用似乎一切正常,但是由于某种原因,我无法从 EtherJS 正确调用几个合约函数。 Here's what I mean...这就是我的意思...

In Remix (where I deployed the smart contract) I am able to call the view no problem;在 Remix(我部署了智能合约的地方)中,我可以调用视图没有问题; I'm getting the result I expect.我得到了我期望的结果。 重新混合显示正确的值

However, when I call the same view from EthersJS, I'm getting the following:但是,当我从 EthersJS 调用相同的视图时,我得到以下信息: EthersJS 只为这个视图和这个视图返回任何内容。

As you can see in the screenshot, the view call from EthersJS returns 0x00 instead of the desired value.正如您在屏幕截图中看到的,来自 EthersJS 的视图调用返回0x00而不是所需的值。

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.我已经将十六进制地址直接放在getAllPendingRewardsByAddress("ADDRESS")中,没有骰子。
  2. I've verified that the ABI is correct.我已经验证了 ABI 是正确的。
  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?这与视图中的 for 循环有关吗? Maybe EthersJS needs more gas to properly execute the for loop, or something along those lines?也许 EthersJS 需要更多的气体来正确执行 for 循环,或者类似的东西?
  • ^ 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? ^ 以此为基础......也许这与分配一个导致 TX 没有足够气体的局部变量pendingRewardsFromSnapshot值有关?

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).我发现了这个问题,显然 EthersJS 使用单独的帐户调用视图,而不是我调用视图的帐户(真的很奇怪)。 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.无论如何,我不得不更改我的一个支持视图( getBalanceOfCallerWithPrecisionPerSnapshot )以使用传递的地址而不是 msg.sender,这解决了我的问题。 If you have a similar problem, check there.如果您有类似的问题,请检查那里。 Thanks for being my rubber duck @sms!谢谢你成为我的橡皮鸭@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!你能告诉我 getPendingRewardPerSnapshot() function 也许问题出在 function 或者你从 1 开始你的 for 循环,也许你应该重新检查它!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何调用 Solidity 函数从智能合约返回 Ether? - How to call Solidity Function to return Ether from Smart Contract? 无法使用 web3.js 从智能合约调用 function - Unable to call function from smart contract using web3.js 通过javascript(web3.js)从部署的智能合约中调用特定的solidity函数 - Call specific solidity function from deployed smart contract within react through javascript (web3.js) 似乎无法在 UI 中调用智能合约功能 - Can't seem to call a smart contract function in the UI Solidity:从我的智能合约中显示价值以做出反应的问题 - Solidity : Problem to display value from my smart contract to react 如何用WalletConnect调用智能合约function? (React.js, Node.js) - How to call a smart contract function with WalletConnect? (React.js, Node.js) 尝试从 react js 中获取 Solidity 智能合约中的数据时返回 NULL 值 - NULL value returned when trying to fetch data from solidity smart contract from react js ReactJS 不从产品上的智能合约(NEAR)调用方法(查看时测试网不存在) - ReactJS not call method from Smart Contract (NEAR) on product (testnet does not exist while viewing) 从智能合约数组中获取数据 - Fetch data from a smart contract array 你可以从网页部署智能合约吗? - Can you deploy a smart contract from a webpage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM