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;