When trying to swap I get the following error.
Please help!
Kindly help?
I’m not getting proper error response object if balance is insufficient etc.
What is the code that you use for swap?
Using exact code that is available at Moralis’s ethereum-boilerplate for swapping tokens. If you need me to share exact snippet, then let me know.
You can share a snippet, I’m not familiar with that code. There is also a specific forum thread for ethereum boilerplate questions.
const { Moralis, account } = useMoralis();
const [tokenList, setTokenlist] = useState();
useEffect(() => {
if (!Moralis?.["Plugins"]?.["oneInch"]) return null;
Moralis.Plugins.oneInch
.getSupportedTokens({ chain })
.then((tokens) => setTokenlist(tokens.tokens));
}, [Moralis, Moralis.Plugins, chain]);
const getQuote = async (params) =>
await Moralis.Plugins.oneInch.quote({
chain: params.chain, // The blockchain you want to use (eth/bsc/polygon)
fromTokenAddress: params.fromToken.address, // The token you want to swap
toTokenAddress: params.toToken.address, // The token you want to receive
amount: Moralis.Units.Token(
params.fromAmount,
params.fromToken.decimals,
).toString(),
});
async function trySwap(params) {
const { fromToken, fromAmount, chain } = params;
const amount = Moralis.Units.Token(
fromAmount,
fromToken.decimals,
).toString();
if (fromToken.address !== "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee") {
await Moralis.Plugins.oneInch
.hasAllowance({
chain, // The blockchain you want to use (eth/bsc/polygon)
fromTokenAddress: fromToken.address, // The token you want to swap
fromAddress: account, // Your wallet address
amount,
})
.then(async (allowance) => {
console.log(allowance);
if (!allowance) {
await Moralis.Plugins.oneInch.approve({
chain, // The blockchain you want to use (eth/bsc/polygon)
tokenAddress: fromToken.address, // The token you want to swap
fromAddress: account, // Your wallet address
});
}
})
.catch((e) => alert(e.message));
}
await doSwap(params)
.then((receipt) => {
console.log("receipt status code: ", receipt);
if (receipt.statusCode !== 400) {
alert("Swap Complete");
setTimeout(() => {
window.location.reload("/");
}, 500);
}
// else {
// alert("Receipt - Not enough balance!");
// }
})
.catch((e) => {
console.log("error: ", e);
alert(e);
});
}
async function doSwap(params) {
return await Moralis.Plugins.oneInch.swap({
chain: params.chain, // The blockchain you want to use (eth/bsc/polygon)
fromTokenAddress: params.fromToken.address, // The token you want to swap
toTokenAddress: params.toToken.address, // The token you want to receive
amount: Moralis.Units.Token(
params.fromAmount,
params.fromToken.decimals,
).toString(),
fromAddress: account, // Your wallet address
slippage: 3,
});
}
return { getQuote, trySwap, tokenList };
It’s the Moralis component’s named useInchDex which is responsable for dex functionality. I don’t have much experience with Moralis package. But it’s not giving me proper error handling i.e. for insufficient balance etc when swapping tokens
Do you have latest version of 1Inch plugin? Not sure if it matters?
can you try to look in network tab in your browser to see what data is sent to 1Inch plugin and what is received?
can you also paste that payload?
it should be a response somewhere too, even if it is an error
{chain: "bsc", fromTokenAddress: "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",…}
amount: "1000000000000000000"
chain: "bsc"
fromAddress: "0xd6ab7b48a4637b3d7977b62773d481c47d09eadc"
fromTokenAddress: "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
slippage: 3
toTokenAddress: "0x55d398326f99059ff775485246999027b3197955"
_ApplicationId: "6Op9hq5BIhR2qqfpZipOgkw0lnGgSLxLcTEh8XcE"
_ClientVersion: "js1.3.1"
_InstallationId: "af996ae6-ed92-4e07-84fd-8fa3207c0818"
_SessionToken: "r:a54825beadfffebd3d2d87ac1b28602f"
error: Error: Something went wrong
"{\n \"statusCode\": 500,\n \"message\": \"Not enough 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE. Amount: 1000000000000000000. Balance: 0.\"\n}"
at MoralisWeb3.js:907:1
at tryCatch (runtime.js:63:1)
at Generator.invoke [as _invoke] (runtime.js:294:1)
at Generator.next (runtime.js:119:1)
at asyncGeneratorStep (asyncToGenerator.js:5:1)
at _next (asyncToGenerator.js:27:1)
The error is telling you that you don’t have enought native tokens to swap.
"Not enough 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE. Amount: 1000000000000000000. Balance: 0.
Indeed 0xd6ab7b48a4637b3d7977b62773d481c47d09eadc
seems to have 0 BNB
I understand that, but I’m not getting proper json response!? why is that? Seems like proper error handling at the backend is missing? I could be wrong though!
See, I’ve to show the exact error string on the frontend in any case what so ever which I should get from the backend no?
you need to fill you wallet with some NFT first man then the error will disappear when you swap~
Oh I see! Alright let me do that
it looks like here is the proper error message?