-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
423 lines (356 loc) · 15.9 KB
/
main.go
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package main
import (
"context"
"fmt"
"log"
"math/big"
"time"
"github.com/XinFinOrg/XDC-Subnet/accounts/abi/bind"
"github.com/XinFinOrg/XDC-Subnet/common"
"github.com/XinFinOrg/XDC-Subnet/contracts/XDCx"
"github.com/XinFinOrg/XDC-Subnet/contracts/XDCx/simulation"
"github.com/XinFinOrg/XDC-Subnet/ethclient"
)
func main() {
fmt.Println("========================")
fmt.Println("mainAddr", simulation.MainAddr.Hex())
fmt.Println("relayerAddr", simulation.RelayerCoinbaseAddr.Hex())
fmt.Println("ownerRelayerAddr", simulation.OwnerRelayerAddr.Hex())
fmt.Println("========================")
client, err := ethclient.Dial(simulation.RpcEndpoint)
if err != nil {
fmt.Println(err, client)
}
nonce, _ := client.NonceAt(context.Background(), simulation.MainAddr, nil)
auth := bind.NewKeyedTransactor(simulation.MainKey)
auth.Value = big.NewInt(0) // in wei
auth.GasLimit = uint64(10000000) // in units
auth.GasPrice = big.NewInt(250000000000000)
// init trc21 issuer
auth.Nonce = big.NewInt(int64(nonce))
trc21IssuerAddr, trc21Issuer, err := XDCx.DeployTRC21Issuer(auth, client, simulation.MinTRC21Apply)
if err != nil {
log.Fatal("DeployTRC21Issuer", err)
}
trc21Issuer.TransactOpts.GasPrice = big.NewInt(250000000000000)
fmt.Println("===> trc21 issuer address", trc21IssuerAddr.Hex())
fmt.Println("wait 10s to execute init smart contract : TRC Issuer")
time.Sleep(2 * time.Second)
//init XDCX Listing in
auth.Nonce = big.NewInt(int64(nonce + 1))
XDCxListtingAddr, XDCxListing, err := XDCx.DeployXDCXListing(auth, client)
if err != nil {
log.Fatal("DeployXDCXListing", err)
}
XDCxListing.TransactOpts.GasPrice = big.NewInt(250000000000000)
fmt.Println("===> XDCx listing address", XDCxListtingAddr.Hex())
fmt.Println("wait 10s to execute init smart contract : XDCx listing ")
time.Sleep(2 * time.Second)
// init Relayer Registration
auth.Nonce = big.NewInt(int64(nonce + 2))
relayerRegistrationAddr, relayerRegistration, err := XDCx.DeployRelayerRegistration(auth, client, XDCxListtingAddr, simulation.MaxRelayers, simulation.MaxTokenList, simulation.MinDeposit)
if err != nil {
log.Fatal("DeployRelayerRegistration", err)
}
relayerRegistration.TransactOpts.GasPrice = big.NewInt(250000000000000)
fmt.Println("===> relayer registration address", relayerRegistrationAddr.Hex())
fmt.Println("wait 2s to execute init smart contract : relayer registration ")
time.Sleep(2 * time.Second)
auth.Nonce = big.NewInt(int64(nonce + 3))
lendingRelayerRegistrationAddr, lendingRelayerRegistration, err := XDCx.DeployLendingRelayerRegistration(auth, client, relayerRegistrationAddr, XDCxListtingAddr)
if err != nil {
log.Fatal("DeployLendingRelayerRegistration", err)
}
lendingRelayerRegistration.TransactOpts.GasPrice = big.NewInt(250000000000000)
fmt.Println("===> lending relayer registration address", lendingRelayerRegistrationAddr.Hex())
fmt.Println("wait 2s to execute init smart contract : lending relayer registration ")
time.Sleep(2 * time.Second)
currentNonce := nonce + 4
tokenList := initTRC21(auth, client, currentNonce, simulation.TokenNameList)
currentNonce = currentNonce + uint64(len(simulation.TokenNameList)) // init smartcontract
applyIssuer(trc21Issuer, tokenList, currentNonce)
currentNonce = currentNonce + uint64(len(simulation.TokenNameList))
applyXDCXListing(XDCxListing, tokenList, currentNonce)
// BTC Collateral
nonce = currentNonce + uint64(len(simulation.TokenNameList))
lendingRelayerRegistration.TransactOpts.Nonce = big.NewInt(int64(nonce))
_, err = lendingRelayerRegistration.AddCollateral(tokenList[0]["address"].(common.Address), simulation.CollateralDepositRate, simulation.CollateralLiquidationRate, simulation.CollateralRecallRate)
if err != nil {
log.Fatal("Lending add collateral", err)
}
// ETH Collateral
nonce = nonce + 1
lendingRelayerRegistration.TransactOpts.Nonce = big.NewInt(int64(nonce))
_, err = lendingRelayerRegistration.AddCollateral(tokenList[1]["address"].(common.Address), simulation.CollateralDepositRate, simulation.CollateralLiquidationRate, simulation.CollateralRecallRate)
if err != nil {
log.Fatal("Lending add collateral", err)
}
// XDC Collateral
nonce = nonce + 1
lendingRelayerRegistration.TransactOpts.Nonce = big.NewInt(int64(nonce))
_, err = lendingRelayerRegistration.AddCollateral(simulation.XDCNative, simulation.CollateralDepositRate, simulation.CollateralLiquidationRate, simulation.CollateralRecallRate)
if err != nil {
log.Fatal("Lending add collateral", err)
}
// XRP ILO Collateral
nonce = nonce + 1
lendingRelayerRegistration.TransactOpts.Nonce = big.NewInt(int64(nonce))
_, err = lendingRelayerRegistration.AddILOCollateral(tokenList[2]["address"].(common.Address), simulation.CollateralDepositRate, simulation.CollateralLiquidationRate, simulation.CollateralRecallRate)
if err != nil {
log.Fatal("Lending add ILO collateral", err)
}
// USD lending base
nonce = nonce + 1
lendingRelayerRegistration.TransactOpts.Nonce = big.NewInt(int64(nonce))
_, err = lendingRelayerRegistration.AddBaseToken(tokenList[9]["address"].(common.Address))
if err != nil {
log.Fatal("Lending add base token USD", err)
}
// XDC lending base
nonce = nonce + 1
lendingRelayerRegistration.TransactOpts.Nonce = big.NewInt(int64(nonce))
_, err = lendingRelayerRegistration.AddBaseToken(simulation.XDCNative)
if err != nil {
log.Fatal("Lending add base token XDC", err)
}
// BTC lending base
nonce = nonce + 1
lendingRelayerRegistration.TransactOpts.Nonce = big.NewInt(int64(nonce))
_, err = lendingRelayerRegistration.AddBaseToken(tokenList[0]["address"].(common.Address))
if err != nil {
log.Fatal("Lending add base token BTC", err)
}
// add term 1 minute for testing
for i := 0; i < len(simulation.Terms); i++ {
nonce = nonce + 1
lendingRelayerRegistration.TransactOpts.Nonce = big.NewInt(int64(nonce))
_, err = lendingRelayerRegistration.AddTerm(simulation.Terms[i])
if err != nil {
log.Fatal("Lending add terms", err)
}
fmt.Println("wait 2s to add term lending lending contract", simulation.Terms[i])
time.Sleep(2 * time.Second)
}
fmt.Println("wait 2s to setup lending contract")
time.Sleep(2 * time.Second)
currentNonce = nonce + 1
airdrop(auth, client, tokenList, simulation.TeamAddresses, currentNonce)
// relayer registration
ownerRelayer := bind.NewKeyedTransactor(simulation.OwnerRelayerKey)
nonce, _ = client.NonceAt(context.Background(), simulation.OwnerRelayerAddr, nil)
relayerRegistration, err = XDCx.NewRelayerRegistration(ownerRelayer, relayerRegistrationAddr, client)
if err != nil {
log.Fatal("NewRelayerRegistration", err)
}
relayerRegistration.TransactOpts.Nonce = big.NewInt(int64(nonce))
relayerRegistration.TransactOpts.Value = simulation.MinDeposit
relayerRegistration.TransactOpts.GasPrice = big.NewInt(250000000000000)
fromTokens := []common.Address{}
toTokens := []common.Address{}
/*
for _, token := range tokenList {
fromTokens = append(fromTokens, token["address"].(common.Address))
toTokens = append(toTokens, simulation.XDCNative)
}
*/
// XDC/BTC
fromTokens = append(fromTokens, simulation.XDCNative)
toTokens = append(toTokens, tokenList[0]["address"].(common.Address))
// XDC/USDT
fromTokens = append(fromTokens, simulation.XDCNative)
toTokens = append(toTokens, tokenList[9]["address"].(common.Address))
// ETH/XDC
fromTokens = append(fromTokens, tokenList[1]["address"].(common.Address))
toTokens = append(toTokens, simulation.XDCNative)
fromTokens = append(fromTokens, tokenList[2]["address"].(common.Address))
toTokens = append(toTokens, simulation.XDCNative)
fromTokens = append(fromTokens, tokenList[3]["address"].(common.Address))
toTokens = append(toTokens, simulation.XDCNative)
fromTokens = append(fromTokens, tokenList[4]["address"].(common.Address))
toTokens = append(toTokens, simulation.XDCNative)
fromTokens = append(fromTokens, tokenList[5]["address"].(common.Address))
toTokens = append(toTokens, simulation.XDCNative)
fromTokens = append(fromTokens, tokenList[6]["address"].(common.Address))
toTokens = append(toTokens, simulation.XDCNative)
fromTokens = append(fromTokens, tokenList[7]["address"].(common.Address))
toTokens = append(toTokens, simulation.XDCNative)
fromTokens = append(fromTokens, tokenList[8]["address"].(common.Address))
toTokens = append(toTokens, simulation.XDCNative)
// ETH/BTC
fromTokens = append(fromTokens, tokenList[1]["address"].(common.Address))
toTokens = append(toTokens, tokenList[0]["address"].(common.Address))
// XRP/BTC
fromTokens = append(fromTokens, tokenList[2]["address"].(common.Address))
toTokens = append(toTokens, tokenList[0]["address"].(common.Address))
// BTC/USDT
fromTokens = append(fromTokens, tokenList[0]["address"].(common.Address))
toTokens = append(toTokens, tokenList[9]["address"].(common.Address))
// ETH/USDT
fromTokens = append(fromTokens, tokenList[1]["address"].(common.Address))
toTokens = append(toTokens, tokenList[9]["address"].(common.Address))
_, err = relayerRegistration.Register(simulation.RelayerCoinbaseAddr, simulation.TradeFee, fromTokens, toTokens)
if err != nil {
log.Fatal("relayerRegistration Register ", err)
}
fmt.Println("wait 2s to apply token to list XDCx")
time.Sleep(2 * time.Second)
// Lending apply
nonce = nonce + 1
lendingRelayerRegistration, err = XDCx.NewLendingRelayerRegistration(ownerRelayer, lendingRelayerRegistrationAddr, client)
if err != nil {
log.Fatal("NewRelayerRegistration", err)
}
lendingRelayerRegistration.TransactOpts.Nonce = big.NewInt(int64(nonce))
lendingRelayerRegistration.TransactOpts.Value = big.NewInt(0)
lendingRelayerRegistration.TransactOpts.GasPrice = big.NewInt(250000000000000)
lendingRelayerRegistration.TransactOpts.GasLimit = uint64(10000000)
baseTokens := []common.Address{}
terms := []*big.Int{}
collaterals := []common.Address{}
// USD 1 minute for testing
baseTokens = append(baseTokens, tokenList[9]["address"].(common.Address))
terms = append(terms, big.NewInt(60))
collaterals = append(collaterals, common.HexToAddress("0x0"))
baseTokens = append(baseTokens, tokenList[9]["address"].(common.Address))
terms = append(terms, big.NewInt(60))
collaterals = append(collaterals, tokenList[2]["address"].(common.Address))
// USD 1 days
baseTokens = append(baseTokens, tokenList[9]["address"].(common.Address))
terms = append(terms, big.NewInt(86400))
collaterals = append(collaterals, common.HexToAddress("0x0"))
baseTokens = append(baseTokens, tokenList[9]["address"].(common.Address))
terms = append(terms, big.NewInt(86400))
collaterals = append(collaterals, tokenList[2]["address"].(common.Address))
// USD 7 days
baseTokens = append(baseTokens, tokenList[9]["address"].(common.Address))
terms = append(terms, big.NewInt(7*86400))
collaterals = append(collaterals, common.HexToAddress("0x0"))
// USD 30 days
baseTokens = append(baseTokens, tokenList[9]["address"].(common.Address))
terms = append(terms, big.NewInt(30*86400))
collaterals = append(collaterals, common.HexToAddress("0x0"))
// XDC 1 min
baseTokens = append(baseTokens, simulation.XDCNative)
terms = append(terms, big.NewInt(60))
collaterals = append(collaterals, common.HexToAddress("0x0"))
// XDC 1 day
baseTokens = append(baseTokens, simulation.XDCNative)
terms = append(terms, big.NewInt(86400))
collaterals = append(collaterals, common.HexToAddress("0x0"))
// XDC 7 days
baseTokens = append(baseTokens, simulation.XDCNative)
terms = append(terms, big.NewInt(7*86400))
collaterals = append(collaterals, common.HexToAddress("0x0"))
// XDC 30 days
baseTokens = append(baseTokens, simulation.XDCNative)
terms = append(terms, big.NewInt(30*86400))
collaterals = append(collaterals, common.HexToAddress("0x0"))
// BTC 1 min
baseTokens = append(baseTokens, tokenList[0]["address"].(common.Address))
terms = append(terms, big.NewInt(60))
collaterals = append(collaterals, common.HexToAddress("0x0"))
// BTC 1 day
baseTokens = append(baseTokens, tokenList[0]["address"].(common.Address))
terms = append(terms, big.NewInt(86400))
collaterals = append(collaterals, common.HexToAddress("0x0"))
// BTC 7 days
baseTokens = append(baseTokens, tokenList[0]["address"].(common.Address))
terms = append(terms, big.NewInt(7*86400))
collaterals = append(collaterals, common.HexToAddress("0x0"))
// BTC 30 days
baseTokens = append(baseTokens, tokenList[0]["address"].(common.Address))
terms = append(terms, big.NewInt(30*86400))
collaterals = append(collaterals, common.HexToAddress("0x0"))
_, err = lendingRelayerRegistration.Update(simulation.RelayerCoinbaseAddr, simulation.LendingTradeFee, baseTokens, terms, collaterals)
if err != nil {
log.Fatal("lendingRelayerRegistration Update", err)
}
fmt.Println("wait 2s to update lending contract")
time.Sleep(2 * time.Second)
}
func initTRC21(auth *bind.TransactOpts, client *ethclient.Client, nonce uint64, tokenNameList []string) []map[string]interface{} {
tokenListResult := []map[string]interface{}{}
for _, tokenName := range tokenNameList {
auth.Nonce = big.NewInt(int64(nonce))
d := uint8(18)
depositFee := big.NewInt(0)
withdrawFee := big.NewInt(0)
tokenCap := simulation.TRC21TokenCap
if tokenName == "ADA" {
d = 0
tokenCap = new(big.Int).Div(simulation.TRC21TokenCap, simulation.BaseXDC)
}
if tokenName == "USDT" {
d = 6
tokenCap = new(big.Int).Div(simulation.TRC21TokenCap, big.NewInt(1000000000000))
withdrawFee = big.NewInt(970000)
}
if tokenName == "BTC" {
d = 8
tokenCap = new(big.Int).Div(simulation.TRC21TokenCap, big.NewInt(10000000000))
withdrawFee = big.NewInt(40000)
}
if tokenName == "ETH" {
withdrawFee = big.NewInt(3000000000000000)
}
tokenAddr, _, err := XDCx.DeployTRC21(auth, client, simulation.Owners, simulation.Required, tokenName, tokenName, d, tokenCap, simulation.TRC21TokenFee, depositFee, withdrawFee)
if err != nil {
log.Fatal("DeployTRC21 ", tokenName, err)
}
fmt.Println(tokenName+" token address", tokenAddr.Hex(), "cap", tokenCap)
tokenListResult = append(tokenListResult, map[string]interface{}{
"name": tokenName,
"address": tokenAddr,
"decimals": d,
})
nonce = nonce + 1
}
time.Sleep(5 * time.Second)
return tokenListResult
}
func applyIssuer(trc21Issuer *XDCx.TRC21Issuer, tokenList []map[string]interface{}, nonce uint64) {
for _, token := range tokenList {
trc21Issuer.TransactOpts.Nonce = big.NewInt(int64(nonce))
trc21Issuer.TransactOpts.Value = simulation.MinTRC21Apply
_, err := trc21Issuer.Apply(token["address"].(common.Address))
if err != nil {
log.Fatal("trc21Issuer Apply ", token["name"].(string), err)
}
fmt.Println("applyIssuer ", token["name"].(string))
nonce = nonce + 1
}
time.Sleep(5 * time.Second)
}
func applyXDCXListing(XDCxListing *XDCx.XDCXListing, tokenList []map[string]interface{}, nonce uint64) {
for _, token := range tokenList {
XDCxListing.TransactOpts.Nonce = big.NewInt(int64(nonce))
XDCxListing.TransactOpts.Value = simulation.XDCXListingFee
_, err := XDCxListing.Apply(token["address"].(common.Address))
if err != nil {
log.Fatal("XDCxListing Apply ", token["name"].(string), err)
}
fmt.Println("applyXDCXListing ", token["name"].(string))
nonce = nonce + 1
}
time.Sleep(5 * time.Second)
}
func airdrop(auth *bind.TransactOpts, client *ethclient.Client, tokenList []map[string]interface{}, addresses []common.Address, nonce uint64) {
for _, token := range tokenList {
for _, address := range addresses {
trc21Contract, _ := XDCx.NewTRC21(auth, token["address"].(common.Address), client)
trc21Contract.TransactOpts.Nonce = big.NewInt(int64(nonce))
baseAmount := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(token["decimals"].(uint8))), nil)
amount := big.NewInt(0).Mul(baseAmount, big.NewInt(1000000))
_, err := trc21Contract.Transfer(address, amount)
if err == nil {
fmt.Printf("Transfer %v %v to %v successfully", amount.String(), token["name"].(string), address.String())
fmt.Println()
} else {
fmt.Printf("Transfer %v to %v failed!", token["name"].(string), address.String())
fmt.Println()
}
nonce = nonce + 1
}
}
time.Sleep(5 * time.Second)
}