Skip to content

Instantly share code, notes, and snippets.

@yuyasugano
Last active February 19, 2022 15:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yuyasugano/1169c60d91dafc2941c544350f9ef9fd to your computer and use it in GitHub Desktop.
Save yuyasugano/1169c60d91dafc2941c544350f9ef9fd to your computer and use it in GitHub Desktop.
arbitrage flashloan profit
...
const oneSplitRates = {
buy: new BigNumber(oneSplitResult1.returnAmount * unitRate).shiftedBy(-toTokenDecimals[j]).toString(),
sell: new BigNumber(oneSplitResult2.returnAmount).shiftedBy(-fromTokenDecimals[i]).toString()
}
console.log(`1inch Exchange ${targetTokens[j]}/${fromTokens[i]}: ${JSON.stringify(oneSplitRates)}`);
let profit = (oneSplitRates.sell - oneSplitRates.buy);
// profit needs to be eth price
if (fromTokens[i] != 'WETH') {
profit = profit / ethPrice;
console.log(`Profit: ${profit.toString()}`);
} else {
console.log(`Profit: ${profit.toString()}`);
}
if (profit > 0) {
const tx = flashloan.methods.initiateFlashLoan(
addresses.dydx.solo,
fromToken[i],
toToken[j],
amountFrom
);
const [gasPrice, gasCost] = await Promise.all([
web3.eth.getGasPrice(),
web3.eth.estimateGas({data: tx.encodeABI()})
]);
// a return cost
const txCost = gasCost * gasPrice / 10 ** 18 * 2 * ethPrice;
profit = profit - txCost;
if (profit > 0) {
console.log(`
Block # ${block.number}, ${targetTokens[j]}/${fromTokens[i]}
Arbitrage opportunity found! Expected profit: ${profit}
`);
const data = tx.encodeABI();
const txData = {
from: admin,
to: flashloan.options.address,
data,
gas: gasCost,
gasPrice
};
const receipt = await web3.eth.sendTransaction(txData);
console.log(`Transaction hash: ${receipt.transactionHash}`);
} else {
console.log(`
Block # ${block.number}, ${targetTokens[j]}/${fromTokens[i]}
Arbitrage opportunity not found. Expected profit: ${profit}
`);
}
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment