Skip to content

Instantly share code, notes, and snippets.

@leckylao
Last active Sep 9, 2020
Embed
What would you like to do?
instadapp
async function oneInchSwapQuote(fromToken, toToken, dsa){
const oneInchSwapQuote = await (await fetch(`https://api.1inch.exchange/v1.1/swapQuote?fromTokenSymbol=${fromToken.symbol}&toTokenSymbol=${toToken.symbol}&amount=${fromToken.balance.toString()}&fromAddress=${dsa.instance.address}&slippage=1&disableEstimate=true`)).json();
console.log(`Estimate exchange: ${oneInchSwapQuote.toTokenAmount / (10 ** toToken.decimals)}`)
return oneInchSwapQuote;
}
// Set DSA instance
await dsa.setInstance((await dsa.getAccounts())[0].id);
// Set up tokens
const info = dsa.erc20.tokens.info;
const usdtInfo = info.usdt;
const batInfo = info.bat;
const wbtcInfo = info.wbtc;
const zrxInfo = info.zrx;
// Get balances
const balances = await dsa.balances.getBalances();
const usdtBalance = balances.usdt;
console.log(`USDT balance: ${usdtBalance}`);
// Get 1inch swapQuote USDT to BAT
let fromToken = {...usdtInfo, ...{balance: BigInt(usdtBalance * 10 ** usdtInfo.decimals)}}
let toToken = batInfo;
const usdtBat = await oneInchSwapQuote(fromToken, toToken, dsa)
// Set up Spell
let spells = dsa.Spell();
// USDT to BAT
spells.add({
connector: "oneInch",
method: "sellThree",
args: [toToken.address, fromToken.address, fromToken.balance.toString(), ((usdtBat.toTokenAmount / (10 ** usdtBat.toToken.decimals)) / (usdtBat.fromTokenAmount / (10 ** usdtBat.fromToken.decimals)) * 1e18).toString(), usdtBat.data, 0]
})
// Deposit BAT
spells.add({
connector: "compound",
method: "deposit",
args: [batInfo.address, -1, 0, 0]
})
// Get 1inch swapQuote 30% BAT to WBTC to borrow
fromToken = {...batInfo, ...{balance: BigInt(usdtBat.toTokenAmount * 0.3)}}
toToken = wbtcInfo;
const batWbtc = await oneInchSwapQuote(fromToken, toToken, dsa);
// Borrow WBTC
spells.add({
connector: "compound",
method: "borrow",
args: [wbtcInfo.address, batWbtc.toTokenAmount, 0, 0]
})
let estimateGas = await dsa.estimateCastGas(spells);
console.log(`Estimate Gas: ${estimateGas}`);
let tx = await dsa.cast(spells);
console.log(tx);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment