This repository has been archived by the owner on Apr 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path28.ammWithdrawal.js
executable file
·108 lines (87 loc) · 2.83 KB
/
28.ammWithdrawal.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
#!/usr/bin/env -S yarn node
/* eslint-disable no-unused-vars */
/*
DO NOT EDIT THIS FILE BY HAND!
Examples are generated using helpers/buildExamples.js script.
Check README.md for more details.
*/
const sw = require('@rhino.fi/starkware-crypto')
const getWeb3 = require('./helpers/getWeb3')
const RhinofiClientFactory = require('../src')
const envVars = require('./helpers/loadFromEnvOrConfig')(
process.env.CONFIG_FILE_NAME
)
const logExampleResult = require('./helpers/logExampleResult')(__filename)
const ethPrivKey = envVars.ETH_PRIVATE_KEY
// NOTE: you can also generate a new key using:`
// const starkPrivKey = rhinofi.stark.createPrivateKey()
const starkPrivKey = envVars.STARK_PRIVATE_KEY
const rpcUrl = envVars.RPC_URL
const { web3, provider } = getWeb3(ethPrivKey, rpcUrl)
const rhinofiConfig = {
api: envVars.API_URL,
dataApi: envVars.DATA_API_URL,
useAuthHeader: true,
wallet: {
type: 'tradingKey',
meta: {
starkPrivateKey: starkPrivKey
}
}
// Add more variables to override default values
}
;(async () => {
const rhinofi = await RhinofiClientFactory(web3, rhinofiConfig)
const waitForDepositCreditedOnChain = require('./helpers/waitForDepositCreditedOnChain')
const token1 = 'ETH'
const token2 = 'USDT'
if (process.env.DEPOSIT_FIRST === 'true') {
const depositETHResponse = await rhinofi.deposit(token1, 0.1, starkPrivKey)
const depositUSDTResponse = await rhinofi.deposit(token2, 1000, starkPrivKey)
if (process.env.WAIT_FOR_DEPOSIT_READY === 'true') {
await waitForDepositCreditedOnChain(rhinofi, depositETHResponse)
await waitForDepositCreditedOnChain(rhinofi, depositUSDTResponse)
}
}
const pool = `${token1}${token2}`
const ammDepositOrderData = await rhinofi.getAmmFundingOrderData({
pool,
token: token1,
amount: 0.1
})
const ammDeposit = await rhinofi.postAmmFundingOrders(
ammDepositOrderData
)
const P = require('aigle')
await P.retry(
{ times: 360, interval: 1000 },
async () => {
const existingDeposit = await rhinofi.getAmmFundingOrders(
null,
null,
{ ammFundingOrderId: ammDeposit._id }
)
if (existingDeposit.pending) {
throw new Error('funding order for amm deposit still pending')
}
}
)
const { toBN } = require('@rhino.fi/dvf-utils')
const ammWithdrawalOrderData = await rhinofi.getAmmFundingOrderData({
pool,
token: `LP-${pool}`,
// Withdraw previously deposited liquidity by returning all LP tokens.
amount: ammDeposit.orders.reduce(
(sum, order) => sum.plus(toBN(order.amountBuy)),
toBN(0)
)
})
const ammWithdrawal = await rhinofi.postAmmFundingOrders(
await rhinofi.applyFundingOrderDataSlippage(ammWithdrawalOrderData, 0.05)
)
logExampleResult(ammWithdrawal)
})()
.catch(error => {
console.error(error)
process.exit(1)
})