Skip to content

Instantly share code, notes, and snippets.

@kn9ts
Last active August 26, 2020 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kn9ts/ed7f65a55b586a0a43defc72ede5dadf to your computer and use it in GitHub Desktop.
Save kn9ts/ed7f65a55b586a0a43defc72ede5dadf to your computer and use it in GitHub Desktop.
Short SUSD with USDC
const Web3 = require('web3');
const DSA = require('dsa-sdk');
const ETH_ADDRESS = 'XXXX';
const ADDRESS_PVT_KEY = 'XXXX'
const TARGET_ENDPOINT = 'https://mainnet.infura.io/v3/XXXX';
const mainnetP = new Web3.providers.HttpProvider(TARGET_ENDPOINT);
const web3 = new Web3(mainnetP);
const dsa = new DSA({
web3,
mode: 'node',
privateKey: ADDRESS_PVT_KEY,
});
dsa
.getAccounts(ETH_ADDRESS)
.then(async ([ dsaAccount ]) => {
// console.log(dsaAccount);
dsa.setInstance(dsaAccount.id);
// DSA related
const spells = dsa.Spell();
const { dai, usdc, susd } = dsa.tokens.info;
const [ notBeingUsed, { address: USDC }, { address: SUSD } ] = [ dai, usdc, susd ];
// console.log(notBeingUsed, USDC, SUSD);
// Static values
const personalAmountInWallet = 100;
const amountFlashBorrowed = 300;
const combinedAmountInWallet = personalAmountInWallet + amountFlashBorrowed;
const slippageAllowed = 0.5;
const zeroValue = 0;
const maxValue = -1;
// FIND OUT HOW MUCH USDC you need to borrow,
// by checking swap rate between SUSD and USDC
// You want to BORROW the PROFITS you would have made by BORROWING SUSD & SWAPING it for USDC
const swappedSUSDforUSDC = await dsa.curve_susd.getBuyAmount(
...[ 'USDC', 'SUSD', combinedAmountInWallet, slippageAllowed ]
);
// 1. BORROW x3 of the amount you have in your wallet
// Note: BORROW DAI due to lower slippage when swapping with SUSD
let amountFlashBorrowedInWei = dsa.tokens.fromDecimal(swappedSUSDforUSDC.buyAmt, 'usdc');
spells.add({
connector: 'instapool',
method: 'flashBorrow',
args: [ USDC, amountFlashBorrowedInWei, zeroValue, zeroValue ]
});
// 5. Payback the borrowed amount
spells.add({
connector: 'instapool',
method: 'flashPayback',
args: [ USDC, zeroValue, zeroValue ]
});
// const results = await dsa.cast({ spells });
// console.info(0, results);
const BN = web3.utils.BN;
const gas = 1.5e6; // web3.utils.toBN(gasLimit);
const currentGasPrice = await web3.eth.getGasPrice();
const gasPrice = web3.utils.toWei(web3.utils.toBN(100), 'gwei');
// console.log('GAS PRICE', currentGasPrice, gasPrice);
const gasAndGasPrice = {
gasPrice: currentGasPrice.toString(),
gas: gas.toString()
};
let spellsEncoded = dsa.internal.encodeSpells({
spells,
...gasAndGasPrice
});
const to = dsaAccount.address;
const from = ETH_ADDRESS;
const origin = dsa.origin;
const contract = new dsa.web3.eth.Contract(dsa.ABI.core.account, dsaAccount.address);
const callData = contract.methods.cast(...spellsEncoded, origin).encodeABI();
console.log(
await dsa.internal.getTxObj({
spells,
to,
from,
origin,
callData,
...gasAndGasPrice
})
);
})
.catch(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment