Hello,
It seems that the table MaticTokenTransfers does not contain al transactions that we perform.
I have this code snipped that I used to check this. I calculated the balance of a address by summing up all transactions and then comparing it to the balance from MaticTokenBalance. And it does not add up. Also the balance from MaticTokenBalance seems to be correct, it is the same as the one shown in Metamask.
What seems to be happening is that when we perform multiple transactions in one of our contract methods, then only one of those transaction gets to be recorded in MaticTokenTransfers. Could this really be the case?
We are using on the Matic Mumbai network.
The code we use to check this is below:
let TokenTransfers = null;
let queryReceiver = null;
let querySender = null;
let query = null;
let erc20Transactions = null;
let tokenAddress = "0xb80ac2abc27dcc7695b8c60bc2800e5ee97fea7b";
let ownerAddress = "0x211773ea847a59ca889424a52835d24f8fdb79b2";
// Load all transaction send to / from the owner
TokenTransfers = Moralis.Object.extend("MaticTokenTransfers");
queryReceiver = new Moralis.Query(TokenTransfers)
.equalTo("token_address", tokenAddress)
.equalTo("to_address", ownerAddress);
querySender = new Moralis.Query(TokenTransfers)
.equalTo("token_address", tokenAddress)
.equalTo("from_address", ownerAddress);
query = Moralis.Query.or(queryReceiver, querySender);
erc20Transactions = await query.find();
let balance = web3.utils.toBN("0");
// Calculate the balance
erc20Transactions = erc20Transactions.map((t) => {
let value = web3.utils.toBN(t.get("value"));
if (t.get("to_address") === ownerAddress) {
balance = balance.add(value);
} else {
balance = balance.sub(value);
}
return {
from_address: t.get("from_address"),
to_address: t.get("to_address"),
value: t.get("value"),
}
})
console.log("DEBUG: erc20Transactions: ", erc20Transactions);
// Now load the balance from the table
let balanceQuery = await new Moralis.Query("MaticTokenBalance")
.equalTo("address", ownerAddress).find();
let otherBalance = balanceQuery[0].get("balance");
// The 2 values should be equal
console.log("DEBUG: calculated balance: ", web3.utils.fromWei(balance.toString(), 'ether'));
console.log("DEBUG: balance from MaticTokenBalance: ", web3.utils.fromWei(otherBalance, 'ether'));
Thanks,
Gerald