/** Connect to Moralis server */
const serverUrl = "";
const appId = "";
const NETWORK = "bsc"; // 'eth'
const accountAddress = "0x18D6E89564762Da0c57c61c919da10208672971D";
const NATIVE_ADDRESS = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
const ONEINCH_ADDRESS = "0x111111111117dc0aa78b770fa6a738034120c302"; //0x1111111254fb6c44bac0bed2854e76f90643097d
// connect to Moralis server
Moralis.initialize(appId);
Moralis.serverURL = serverUrl;
let dex;
(async function () {
await Moralis.initPlugins();
dex = Moralis.Plugins.oneInch;
const info = await Moralis.enableWeb3();
if (!Moralis.User.current()) {
await Moralis.authenticate(); //await Moralis.authenticate({ signingMessage: "Hello World!" })
console.log(Moralis.User.current());
}
console.log("Moralis enable Web3: ", info);
})();
async function swap() {
console.log("Process Initiated");
console.log("Dex: ", Moralis.Plugins.oneInch);
try {
const receipt = await Moralis.Plugins.oneInch.swap({
chain: NETWORK, // The blockchain you want to use (eth/bsc/polygon)
fromTokenAddress: NATIVE_ADDRESS, // The token you want to swap
toTokenAddress: ONEINCH_ADDRESS, // The token you want to receive
amount: Number(Moralis.Units.ETH("0.01")),
fromAddress: accountAddress, // Your wallet address
slippage: 20,
});
console.log(receipt);
} catch (err) {
console.log(err);
}
}
async function getSupportedTokens() {
const tokens = await Moralis.Plugins.oneInch.getSupportedTokens({
chain: NETWORK, // The blockchain you want to use (eth/bsc/polygon)
});
console.log(tokens);
return tokens;
}
async function getQuote() {
const quote = await Moralis.Plugins.oneInch.quote({
chain: NETWORK, // The blockchain you want to use (eth/bsc/polygon)
fromTokenAddress: NATIVE_ADDRESS, // The token you want to swap
toTokenAddress: ONEINCH_ADDRESS, // The token you want to receive
amount: Number(Moralis.Units.ETH("0.01")),
});
console.log(quote);
}
async function hasAllowance() {
const allowance = await Moralis.Plugins.oneInch.hasAllowance({
chain: NETWORK, // The blockchain you want to use (eth/bsc/polygon)
fromTokenAddress: NATIVE_ADDRESS, // The token you want to swap
toTokenAddress: ONEINCH_ADDRESS, // The token you want to receive
amount: Number(Moralis.Units.ETH("0.01")),
});
console.log(`The user has enough allowance: ${allowance}`);
console.log(allowance);
}
async function approve() {
const approve = await Moralis.Plugins.oneInch.approve({
chain: NETWORK, // The blockchain you want to use (eth/bsc/polygon)
fromTokenAddress: NATIVE_ADDRESS, // The token you want to swap
toTokenAddress: ONEINCH_ADDRESS, // The token you want to receive
});
console.log(approve);
}