I’m having a hard time making hasAllowance
work for me. My understanding is that I’m supposed to check and make sure the user has the token balance available before swapping which makes sense. If they do, I’m supposed to ask for an Approval signature to verify interaction with their tokens. Once I have a valid balance and signature, I proceed to swap.
The token I’m working with is 9 decimals and I have ~1350 in the wallet. I don’t see a way to pass decimal information into hasAllowance
.
When I run this in my console, it returns TRUE
:
await Moralis.Plugins.oneInch.hasAllowance({
chain: 'bsc', // The blockchain you want to use (eth/bsc/polygon)
fromTokenAddress: '0x0581fafb89bc15f13afe3ee7348fd667262b2732', // The token you want to swap
fromAddress: '0xc70ef3240ff891014538ca54ea2f7cd8cacc872a', // Your wallet address
amount: '11',
});
If I change (amount: '11'
) to (amount: '12'
) it will return FALSE
await Moralis.Plugins.oneInch.hasAllowance({
chain: 'bsc', // The blockchain you want to use (eth/bsc/polygon)
fromTokenAddress: '0x0581fafb89bc15f13afe3ee7348fd667262b2732', // The token you want to swap
fromAddress: '0xc70ef3240ff891014538ca54ea2f7cd8cacc872a', // Your wallet address
amount: '12',
});
Now if I change (amount: '12'
) to (amount: ''
) it will return TRUE
again.
await Moralis.Plugins.oneInch.hasAllowance({
chain: 'bsc', // The blockchain you want to use (eth/bsc/polygon)
fromTokenAddress: '0x0581fafb89bc15f13afe3ee7348fd667262b2732', // The token you want to swap
fromAddress: '0xc70ef3240ff891014538ca54ea2f7cd8cacc872a', // Your wallet address
amount: '',
});
Finally, if I change (amount: ''
) to any integer (amount: 9999999999999
) it will return TRUE
await Moralis.Plugins.oneInch.hasAllowance({
chain: 'bsc', // The blockchain you want to use (eth/bsc/polygon)
fromTokenAddress: '0x0581fafb89bc15f13afe3ee7348fd667262b2732', // The token you want to swap
fromAddress: '0xc70ef3240ff891014538ca54ea2f7cd8cacc872a', // Your wallet address
amount: 9999999999999,
});
Is this how hasAllowance
is supposed to work? Does hasAllowance
check to make sure the signature has been received as well as check the balance? Because what’s happening is hasAllowance
will sometimes return false and it will continue to prompt Moralis.Plugins.oneInch.approve
when it already has premission. I only want to prompt for Moralis.Plugins.oneInch.approve
once. How can I do this?
Here is the flow taken from the example:
if (currentTrade.from.symbol !== "BNB") {
const allowance = await Moralis.Plugins.oneInch.hasAllowance({
chain: "bsc", // The blockchain you want to use (eth/bsc/polygon)
fromTokenAddress: currentTrade.from.address, // The token you want to swap
fromAddress: address, // Your wallet address
amount: amount,
});
console.log(allowance);
if (!allowance) {
await Moralis.Plugins.oneInch.approve({
chain: "bsc", // The blockchain you want to use (eth/bsc/polygon)
tokenAddress: currentTrade.from.address, // The token you want to swap
fromAddress: address, // Your wallet address
});
}
}
As you can see if allowance
returns FALSE
, it will constantly prompt for approval.
I also tried making this work for me with no luck:
https://docs.moralis.io/moralis-server/web3-sdk/token#gettokenallowance
The result always returns a string "0"
for me no matter what enter. I had to enter chain
as well even though it was optional.
Any help would just be amazing at this point. I’ve been stuck too long on this…