-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdemo2.js
106 lines (78 loc) · 3.67 KB
/
demo2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
require("dotenv").config();
require("@nomiclabs/hardhat-etherscan");
require("@nomiclabs/hardhat-waffle");
require("hardhat-gas-reporter");
require("solidity-coverage");
// ABI Of the call we want to make
const FLASH_LOAN_ABI = require('./abis/flashLoan.json');
const TRANSFER_FROM_ABI = require('./abis/transferFrom.json');
const ONE_INCH_SWAP = require('./abis/oneInchSwap.json');
// POLYGON ADDRESS
const PULSAR = "0xaF54b4C42E26b6BD8f676911b98C6C4a07Ac5d00";
const DAI = "0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063";
const USDC = "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174";
const BNB = "0x3BA4c387f786bFEE076A58914F5Bd38d668B42c3";
const WETH = "0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619";
// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task(
"run-demo1",
"Execute the demo1 sequence",
async (_, {network, ethers, upgrades }) => {
let override = { gasLimit: 4000000 };
const signer = ethers.provider.getSigner(
process.env.ADDRESS
);
let oneDai = '1000000000000000000'
let dai = await ethers.getContractAt("IERC20", DAI, signer);
let balance = await dai.balanceOf(process.env.ADDRESS);
console.log("Initial DAI Balance : ",ethers.utils.formatUnits(balance,18))
let borrowedAssets = [DAI] // <-- must match withdrawn
let borrowedAmounts = ['1000000000000000000000']
let borrowedModes = [0]
let loanSequence = [
borrowedAssets,
borrowedAmounts,
borrowedModes
]
// Oninch swap call
// struct SwapDescription {
// IERC20 srcToken;
// IERC20 dstToken;
// address srcReceiver;
// address dstReceiver;
// uint256 amount;
// uint256 minReturnAmount;
// uint256 guaranteedAmount;
// uint256 flags;
// address referrer;
// bytes permit;
// }
// we would need to approve oneInch to pull the funds that we borrowed
let iface = new ethers.utils.Interface(JSON.stringify(ONE_INCH_SWAP));
let sig = "swap(address, (address,address,address,address,uint256,uint256,uint256,bytes), bytes)";
let data1 = iface.encodeFunctionData(sig,[process.env.ADDRESS,PULSAR,oneDai])
// transfer the loan to your wallet
// iface = new ethers.utils.Interface(JSON.stringify(TRANSFER_ABI));
// sig = "transfer(address, uint256)";
// let data2 = iface.encodeFunctionData(sig,[process.env.LOCAL_ADDRESS,trade1Amount])
// Target can't be Pulsar
let targets = [DAI]
let calls = [data1]
let withdraw = [DAI] //< --- must match the length of borrowed asset at least.
let tradeSequence = ethers.utils.defaultAbiCoder.encode([ "address[]", "bytes[]", "address[]" ], [ targets, calls, withdraw ])
// Full sequence
iface = new ethers.utils.Interface(JSON.stringify(FLASH_LOAN_ABI));
sig = "flashLoanCall((address[],uint256[],uint[]),bytes)";
let data = iface.encodeFunctionData(sig,[loanSequence,tradeSequence])
const call = await signer.sendTransaction({
to: PULSAR,
data: data, // change data here
value: ethers.utils.parseUnits("1.0", "ether")
});
call.wait(1)
console.log("Trade Executed")
await dai.approve(PULSAR, '0', override);
console.log("Approval reset.")
});
//swap(address, (address,address,address,address,uint256,uint256,uint256,bytes), bytes)