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:
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:
getAllPendingRewardsByAddress("ADDRESS")
, no dice.Some hunches I have...
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.