[SOLVED] 1Inch plugin, errors when using bsc network

Hi everyone,

i use
const serverUrl = “https://uqppy6hj42wj.usemoralis.com:2053/server”;
const appId = “Vekfg0LulQYGrKA1CvfgRjhktqCQD7wMXNIHmoqv”;

I downloaded the code from https://moralis.io/how-to-create-a-dex-in-5-steps/, I created a bsc minnet server, while with chian eth it works, changing to bsc gives me the error

error = new _ParseError.default(errorJSON.code, errorJSON.error);

Uncaught (in promise) Error: [object Object]
at handleError (moralis.js:26415:17)

2022-05-24 21_49_10-Swap Space D

Can you share which 1Inch function is causing that error?

Where did you change the chain?

hi, in this function generate error

hi, i change in this point

this is my code

const serverUrl = “https://uqppy6hj42wj.usemoralis.com:2053/server”;
const appId = “Vekfg0LulQYGrKA1CvfgRjhktqCQD7wMXNIHmoqv”;

let currentTrade = {};
let currentSelectSide;
let tokens;

async function init() {
await Moralis.start({ serverUrl, appId });
await Moralis.enableWeb3();
await listAvailableTokens();
currentUser = Moralis.User.current();
if (currentUser) {
document.getElementById(“swap_button”).disabled = false;
}
}

async function listAvailableTokens() {
const result = await Moralis.Plugins.oneInch.getSupportedTokens({
chain: “bsc”, // The blockchain you want to use (eth/bsc/polygon)
});
tokens = result.tokens;
let parent = document.getElementById(“token_list”);
for (const address in tokens) {
let token = tokens[address];
let div = document.createElement(“div”);
div.setAttribute(“data-address”, address);
div.className = “token_row”;
let html = <img class="token_list_img" src="${token.logoURI}"> <span class="token_list_text">${token.symbol}</span> ;
div.innerHTML = html;
div.onclick = () => {
selectToken(address);
};
parent.appendChild(div);
}
}

function selectToken(address) {
closeModal();
console.log(tokens);
currentTrade[currentSelectSide] = tokens[address];
console.log(currentTrade);
renderInterface();
getQuote();
}

function renderInterface() {
if (currentTrade.from) {
document.getElementById(“from_token_img”).src = currentTrade.from.logoURI;
document.getElementById(“from_token_text”).innerHTML = currentTrade.from.symbol;
}
if (currentTrade.to) {
document.getElementById(“to_token_img”).src = currentTrade.to.logoURI;
document.getElementById(“to_token_text”).innerHTML = currentTrade.to.symbol;
}
}

async function login() {
try {
currentUser = Moralis.User.current();
if (!currentUser) {
currentUser = await Moralis.authenticate();
}
document.getElementById(“swap_button”).disabled = false;
} catch (error) {
console.log(error);
}
}

function openModal(side) {
currentSelectSide = side;
document.getElementById(“token_modal”).style.display = “block”;
}
function closeModal() {
document.getElementById(“token_modal”).style.display = “none”;
}

async function getQuote() {
if (!currentTrade.from || !currentTrade.to || !document.getElementById(“from_amount”).value) return;

let amount = Number(document.getElementById(“from_amount”).value * 10 ** currentTrade.from.decimals);

const quote = await Moralis.Plugins.oneInch.quote({
chain: “bsc”, // The blockchain you want to use (eth/bsc/polygon)
fromTokenAddress: currentTrade.from.address, // The token you want to swap
toTokenAddress: currentTrade.to.address, // The token you want to receive
amount: amount,
});
console.log(quote);
document.getElementById(“gas_estimate”).innerHTML = quote.estimatedGas;
document.getElementById(“to_amount”).value = quote.toTokenAmount / 10 ** quote.toToken.decimals;
}

async function trySwap() {
let address = Moralis.User.current().get(“ethAddress”);
let amount = Number(document.getElementById(“from_amount”).value * 10 ** currentTrade.from.decimals);
if (currentTrade.from.symbol !== “ETH”) {
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
});
}
}
try {
let receipt = await doSwap(address, amount);
alert(“Swap Complete”);
} catch (error) {
console.log(error);
}
}

function doSwap(userAddress, amount) {
return Moralis.Plugins.oneInch.swap({
chain: “bsc”, // The blockchain you want to use (eth/bsc/polygon)
fromTokenAddress: currentTrade.from.address, // The token you want to swap
toTokenAddress: currentTrade.to.address, // The token you want to receive
amount: amount,
fromAddress: userAddress, // Your wallet address
slippage: 1,
});
}

init();

document.getElementById(“modal_close”).onclick = closeModal;
document.getElementById(“from_token_select”).onclick = () => {
openModal(“from”);
};
document.getElementById(“to_token_select”).onclick = () => {
openModal(“to”);
};
document.getElementById(“login_button”).onclick = login;
document.getElementById(“from_amount”).onblur = getQuote;
document.getElementById(“swap_button”).onclick = trySwap;

can you share the parameters that you used for hasAllowance function?

hi, this

5

3 1 2

solved by putting fixed tokens, but am I wrong something that gets me wrong in the function?

the problem is BNB token

what is the problem with BNB token?

gives me this address to swap 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

this is list token function

async function listAvailableTokens() {
const result = await Moralis.Plugins.oneInch.getSupportedTokens({
chain: “bsc”, // The blockchain you want to use (eth/bsc/polygon)
});
tokens = result.tokens;
let parent = document.getElementById(“token_list”);
for (const address in tokens) {
let token = tokens[address];
let div = document.createElement(“div”);
div.setAttribute(“data-address”, address);
div.className = “token_row”;
let html = <img class="token_list_img" src="${token.logoURI}"> <span class="token_list_text">${token.symbol}</span> ;
div.innerHTML = html;
div.onclick = () => {
selectToken(address);
};
parent.appendChild(div);
}
}

this could be the address used for 1Inch as a way to represent BNB that is a native currency

ok thanks, is there a function to convert it?

You need to fix this by replacing it with this

if (currentTrade.from.symbol !== "BNB") {

I tried, it goes wrong

Which token are you swapping from and which token are you swapping to ?

For example if I use cake and usdt, everything is ok, if I use bnb from what I understand it gives me the native address and goes into error

1Inch plugin works fine with BSC. You can try check This code and compare with yours.

1 Like