After deleting and reinstalling the 1inch Plugin, and updating my server, it still doesn’t work for me.
Here’s my code so far:
/* Moralis init code */
// init is short for initiation btw...
const serverUrl = "";
const appId = "";
Moralis.start({ serverUrl, appId });
Moralis
.initPlugins()
.then(() => console.log('Plugins have been initiated'));
const $tokenBalanceTBody = document.querySelector('.js-token-balances');
const $selectedToken = document.querySelector('.js-from-token');
const $amountInput = document.querySelector('.js-from-amount');
/** Utilities */
//Convert token value to ETH style with 18 decimals
//If you do not specify decimals, 18 decimals will be automatically used
const tokenValue = Moralis.Units.FromWei("2000000000000000000");
/** Login/Logout and initialization */
/* Authentication code */
async function login() {
let user = Moralis.User.current();
if (!user) {
user = await Moralis.authenticate({
signingMessage: "Log in using Moralis",
})
.then(function (user) {
console.log("logged in user:", user);
console.log(user.get("ethAddress"));
})
.catch(function (error) {
console.log(error);
});
}
getStats();
}
async function initSwapForm(event) {
event.preventDefault();
$selectedToken.innerText = event.target.dataset.symbol;
$selectedToken.dataset.address = event.target.dataset.address;
$selectedToken.dataset.decimals = event.target.dataset.decimals;
$selectedToken.dataset.max = event.target.dataset.max;
$amountInput.removeAttribute('disabled');
$amountInput.value = '';
document.querySelector('.js-submit').removeAttribute('disabled');
document.querySelector('.js-cancel').removeAttribute('disabled');
document.querySelector('.js-quote-container').innerHTML = '';
document.querySelector('.js-amount-error').innerText = '';
}
// Getting the balances of the token!
async function getStats() {
const balances = await Moralis.Web3API.account.getTokenBalances({ chain: "polygon" });
console.log(balances);
$tokenBalanceTBody.innerHTML = balances.map((token, index) => `
<tr>
<td>${index + 1}.</td>
<td>${token.symbol}</td>
<td>${Moralis.Units.FromWei(token.balance, token.decimals)}</td>
<td>
<button
class="js-swap btn btn-success"
data-address="${token.token_address}"
data-symbol="${token.symbol}"
data-decimals="${token.decimals}"
data-max="${Moralis.Units.FromWei(token.balance, token.decimals)}"
>
Swap
</button>
</td>
</tr>
`).join('');
for (let $btn of $tokenBalanceTBody.querySelectorAll('.js-swap')) {
$btn.addEventListener('click', initSwapForm);
}
}
// Fiat onramper function
async function buyCrypto() {
Moralis.Plugins.fiat.buy();
}
async function logOut() {
await Moralis.User.logOut();
console.log("logged out");
}
document.querySelector("#btn-login").addEventListener('click', login);
document
.getElementById("btn-buy-crypto")
.addEventListener('click', buyCrypto);
document.getElementById("btn-logout").addEventListener('click', logOut);
/** Quote / Swap */
async function formSubmitted(event) {
const fromAmount = Number.parseFloat($amountInput.value);
const fromMaxValue = Number.parseFloat($selectedToken.dataset.max);
if (Number.isNaN(fromAmount) || fromAmount > fromMaxValue) {
// Invalid input
document.querySelector('.js-amount-error').innerText = 'Invalid amount';
} else {
document.querySelector('.js-amount-error').innerText = '';
}
// Submission of the quote request
const fromDecimals = $selectedToken.dataset.decimals;
const fromTokenAddress = $selectedToken.dataset.address;
const [toTokenAddress, toDecimals] = document.querySelector('[name=to-token]').value.split('-');
try {
const quote = await Moralis.Plugins.oneInch.quote({
chain: 'polygon', // The blockchain you want to use (eth/bsc/polygon)
fromTokenAddress, // The token you want to swap
toTokenAddress, // The token you want to receive
amount: Moralis.Units.Token(fromAmount, fromDecimals),
});
console.log(quote);
// const toAmount (, toDecimals)
} catch (e) {
document.querySelector('.js-quote-container').innerHTML = `
<p>Unsuccessful conversion</p>
`;
}
}
async function formCancelled(event) {
event.preventDefault();
document.querySelector('.js-submit').setAttribute('disabled', '');
document.querySelector('.js-cancel').setAttribute('disabled', '');
$amountInput.value = '';
$amountInput.setAttribute('disabled');
delete $selectedToken.dataset.address;
delete $selectedToken.dataset.decimals;
delete $selectedToken.dataset.max;
document.querySelector('.js-quote-container').innerHTML = '';
document.querySelector('.js-amount-error').innerHTML = '';
}
document.querySelector('.js-submit').addEventListener('click', formSubmitted)
document.querySelector('.js-cancel').addEventListener('click', formCancelled)
// To Token dropdown preparation
async function getTop10Coins() {
const response = await fetch('https://api.coinpaprika.com/v1/coins')
const tokens = await response.json();
return tokens
.filter(token => token.rank <= 20 && token.rank >= 1)
.map(token => token.symbol);
}
async function getTickerData(tickerList) {
const tokens = await Moralis.Plugins.oneInch.getSupportedTokens({
chain: 'polygon', // The blockchain you want to use (eth/bsc/polygon)
});
const tokenList = Object.values(tokens.tokens)
return tokenList.filter(token => tickerList.includes(token.symbol));
}
function renderTokenDropdown(tokens) {
const options = tokens.map(token => `
<option value="${token.address}-${token.decimals}">
${token.name}
</option>
`).join('');
document.querySelector('[name=to-token]').innerHTML = options;
}
getTop10Coins()
.then(getTickerData)
.then(renderTokenDropdown);```