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:
- I've put the hexadecimal address directly in
getAllPendingRewardsByAddress("ADDRESS")
, no dice. - I've verified that the ABI is correct.
- 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!
CodePudding user response:
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!
CodePudding user response:
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!