[SOLVED] DEX exchange is not working

my dex is not working… this is error code i get from console…
Uncaught (in promise) ReferenceError: serverUrl is not defined
at init (main.js:11:25)
at main.js:165:1
2main.js:128 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ā€˜decimals’)
at HTMLButtonElement.trySwap (main.js:128:31)

see code below…
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;

tokens[ā€œ0xacfc95585d80ab62f67a14c566c1b7a49fe91167ā€] = {

address: "0xacfc95585d80ab62f67a14c566c1b7a49fe91167",

decimals: 18,

logoURI: "https://s2.coinmarketcap.com/static/img/coins/64x64/8397.png",

name: "FEG Token",

symbol: "FEG",

};

tokens[ā€œ0x42981d0bfbAf196529376EE702F2a9Eb9092fcB5ā€] = {

address: "0x42981d0bfbAf196529376EE702F2a9Eb9092fcB5",

decimals: 18,

logoURI: "https://s2.coinmarketcap.com/static/img/coins/64x64/16162.png",

name: "SafeMoon V2",

symbol: "SafeMoon",

};

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 !== ā€œ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

  });

}

}

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;

Did you define serverUrl and appId before using the Moralis.start function

const serverUrl = "your server URL here";
const appId = "your app Id here";

As per this error, there is no decimal value to read at currentTrade.from in trySwap function.

Try fixing the serverUrl and appId error to see if it also fixes the other error.

Thanks man… I didnt define serverUrl and appId. Thanks

1 Like