[英]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 调用相同的视图时,我得到以下信息:
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:我已经尝试了以下调试步骤:
getAllPendingRewardsByAddress("ADDRESS")
, no dice.getAllPendingRewardsByAddress("ADDRESS")
中,没有骰子。 Some hunches I have...我有一些预感...
pendingRewardsFromSnapshot
values that is causing the TX to not have enough gas? 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.