This question looks silly but it drives me crazy because I cant find a solution. I will make it clear below.
I use useDApp framework and with useCall hook. I can get correctly values returned from Solidity functions and everything works very perfectly. But wrong with these 2 functions:
function showRemainingDate(uint _id) public view returns(uint) {
for (uint i = 0; i < users[msg.sender].numberOfCards; i++) {
if (users[msg.sender].purchase[i].card.id == cards[_id].id) {
return 30*86400 - (block.timestamp - users[msg.sender].purchase[i].purchaseDate);
}
}
return 0;
}
function checkPurchase(uint _id) public view returns (bool) {
for (uint i = 0; i < users[msg.sender].numberOfCards; i++) {
if (users[msg.sender].purchase[i].card.id == cards[_id].id) {
return true;
}
}
return false;
}
After having deployed on Remix, I imported its address and ABI
for my dApp project then used useCall hook like this:
export function useCheckPurchase(index) {
const { value: checkPurchase } = useCall({
contract: contract,
method: "checkPurchase",
args: [index],
}) ?? {};
return checkPurchase;
}
export function useShowRemainingDate(index) {
const { value: showRemainingDate } = useCall({
contract: contract,
method: "showRemainingDate",
args: [index],
}) ?? {};
return showRemainingDate;
}
The weird points start here, if I use purchaseCard
hook/function (no matter on JS or Remix) and then console.log
the values (just for example):
console.log(useCheckPurchase(3))
console.log(parseInt(useShowRemainingDate(3)))
It would always return false
and 0
for all parameter:
While the values would always return correctly on Remix:
This is a very weird bug, Im sure there arent any typo in the names.
This is the contract address 0x29588f4714223bd492ce6500D6CEE0eCD4f1c51e
in my examples and this is my Solidity code.
Thank you everyone, any answer from you guys are all appreciated.
Okay, I finally know the reason.
After changing msg.sender
into my wallet address on Solidity code, it worked perfectly.
From here, I can show that the problem is with msg.sender
.
And this is the solution post, you guys can read more about it: Solidity function returns empty array on ethersjs/hardhat but not in remix ide?
Hope this will help someone encountering my question.