-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
71 lines (57 loc) · 2.41 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
require('dotenv').config()
const ethers = require('ethers');
const {BigNumber, utils} = ethers
const provider = new ethers.providers.WebSocketProvider(
`wss://rinkeby.infura.io/ws/v3/${process.env.INFURA_ID}`,
'rinkeby',
)
const depositWallet = new ethers.Wallet(
process.env.DEPOSIT_WALLET_PRIVATE_KEY,
provider,
)
const main = async () => {
const depositWalletAddress = await depositWallet.getAddress()
console.log(`Watching for incoming tx to ${depositWalletAddress}...`)
provider.on('pending', (txHash) => {
try {
provider.getTransaction(txHash).then((tx) => {
if (tx === null) return
const {from, to, value} = tx
if (to === depositWalletAddress) {
console.log(`Receiving ${utils.formatEther(value)} ETH from ${from}..`)
tx.wait(process.env.CONFIRMATIONS_BEFORE_WITHDRAWAL).then(async (_receipt) => {
const currentBalance = await depositWallet.getBalance('latest')
const gasPrice = await provider.getGasPrice()
const gasLimit = 21000
const maxGasFee = BigNumber.from(gasLimit).mul(gasPrice)
const tx = {
to: process.env.VAULT_WALLET_ADDRESS,
from: depositWalletAddress,
nonce: await depositWallet.getTransactionCount(),
value: currentBalance.sub(maxGasFee),
chainId: 4,
gasPrice: gasPrice,
gasLimit: gasLimit
}
depositWallet.sendTransaction(tx).then((_receipt) => {
console.log(
`Withdrew ${utils.formatEther(
currentBalance.sub(maxGasFee)
)} ETH to VAULT ${process.env.VAULT_WALLET_ADDRESS} `
)
}, (reason) => {
console.error("Withdrawal failed", reason)
})
}, (reason) => {
console.error("Receival failed", reason)
})
}
})
} catch (e) {
console.error(e)
}
})
}
if (require.main === module) {
main()
}