-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
138 lines (123 loc) · 3.8 KB
/
index.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const axios = require('axios')
const { ethers } = require('ethers')
const BigNumber = require('bignumber.js')
const qs = require('qs')
require('dotenv').config()
const ERC20_ABI = require('./abi.json')
const walletAddress = process.env.WALLET_ADDRESS
// // GOERLI DATA
// const ZERO_EX_ADDRESS = '0xf91bb752490473b8342a3e964e855b9f9a2a668e'
// const wethAddress = '0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6'
// const daiAddress = '0xdc31Ee1784292379Fbb2964b3B9C4124D8F89C60'
// const GOERLI_URL_TESTNET = process.env.GOERLI_URL_TESTNET
// // ROPSTEN DATA
// Tokens
const tokenFrom = {
name: 'Wrapped Ether',
address: '0xc778417E063141139Fce010982780140Aa0cD5Ab',
symbol: 'ETH',
decimals: 18,
}
const tokenTo = {
name: 'Wrapped BTC',
address: '0xBde8bB00A7eF67007A96945B3a3621177B615C44',
symbol: 'WBTC',
decimals: 18,
}
const ZERO_EX_ADDRESS = '0xdef1c0ded9bec7f1a1670819833240f027b25eff'
// const tokenFromAddress = '0xc778417E063141139Fce010982780140Aa0cD5Ab'
// const tokenToAddress = '0xaD6D458402F60fD3Bd25163575031ACDce07538D'
const ROPTSTEN_URL_TESTNET = process.env.ROPTSTEN_URL_TESTNET
const provider = new ethers.providers.JsonRpcProvider(ROPTSTEN_URL_TESTNET)
const signer = new ethers.Wallet(process.env.PRIVATE_KEY, provider)
const getPrice = async (amountFrom) => {
try {
const params = {
sellToken: tokenFrom.address,
buyToken: tokenTo.address,
sellAmount: amountFrom * 10 ** 18,
}
await axios
.get(`https://ropsten.api.0x.org/swap/v1/price?${qs.stringify(params)}`)
.then((res) => {
const estimatedGasPrice = res.data.estimatedGas
const buyAmount = res.data.buyAmount / 10 ** 18
// console.log(res.data);
console.log(`You get: ${buyAmount} DAI`)
console.log(`Gas: ${estimatedGasPrice} DAI`)
})
.catch((err) => {
console.log(err.response)
})
} catch (error) {
console.log(error.response)
}
}
const getQuote = async (amountFrom) => {
try {
const params = {
sellToken: tokenFrom.symbol,
buyToken: tokenTo.address,
sellAmount: amountFrom * 10 ** 18,
takerAddress: walletAddress,
slippagePercentage: 0.03,
// skipValidation: true,
}
const res = await axios.get(
`https://ropsten.api.0x.org/swap/v1/quote?${qs.stringify(params)}`
)
const estimatedGasPrice = res.data.estimatedGas
// const buyAmount = res.data.buyAmount / 10 ** 18
const buyAmount = res.data.buyAmount
console.log(`You get: ${buyAmount} DAI`)
console.log(`Gas: ${estimatedGasPrice} DAI`)
return res.data
} catch (error) {
console.log(error.response)
}
}
const approve = async () => {
try {
const erc20Contract = new ethers.Contract(
tokenFrom.address,
ERC20_ABI,
signer
)
const approvalAmount = ethers.utils.parseUnits('1', 18).toString()
const tx = await erc20Contract.approve(ZERO_EX_ADDRESS, approvalAmount)
await tx.wait(1)
console.log('Approved!')
} catch (error) {
console.log(error.response)
}
}
const swapTokens = async () => {
try {
const tx = await getQuote(0.001)
const txObj = {
from: tx.from,
to: tx.to,
data: tx.data,
value: tx.value,
gasPrice: tx.gasPrice,
gasLimit: ethers.utils.hexlify(1000000),
}
await approve()
console.log('Broadcasting....')
// Send the tx
const sentTx = await signer.sendTransaction(txObj)
await sentTx.wait(1)
console.log(sentTx)
console.log(`-------------------------------------------`)
} catch (error) {
console.log(error)
}
}
const getBalance = async () => {
const contract = new ethers.Contract(tokenTo.address, ERC20_ABI, signer)
const balance = await contract.balanceOf(walletAddress)
console.log(balance.toString() / 10 ** 18)
}
getQuote(0.001)
// swapTokens()
// getBalance()