-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathmain.go
More file actions
703 lines (586 loc) · 18.3 KB
/
main.go
File metadata and controls
703 lines (586 loc) · 18.3 KB
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
package main
import (
"encoding/hex"
"fmt"
"sync"
"time"
"unsafe"
"github.com/elliottech/lighter-go/client"
"github.com/elliottech/lighter-go/client/http"
"github.com/elliottech/lighter-go/types"
"github.com/elliottech/lighter-go/types/txtypes"
"github.com/ethereum/go-ethereum/common/hexutil"
)
/*
#include <stdlib.h>
#include <stdint.h>
typedef struct {
char* str;
char* err;
} StrOrErr;
typedef struct {
uint8_t txType;
char* txInfo;
char* txHash;
char* messageToSign;
char* err;
} SignedTxResponse;
typedef struct {
char* privateKey;
char* publicKey;
char* err;
} ApiKeyResponse;
typedef struct {
uint8_t MarketIndex;
int64_t ClientOrderIndex;
int64_t BaseAmount;
uint32_t Price;
uint8_t IsAsk;
uint8_t Type;
uint8_t TimeInForce;
uint8_t ReduceOnly;
uint32_t TriggerPrice;
int64_t OrderExpiry;
} CreateOrderTxReq;
*/
import "C"
var (
chainIdMu sync.Mutex
chainId uint32
)
func wrapErr(err any) *C.char {
if err == nil {
return nil
}
return C.CString(fmt.Sprintf("%v", err))
}
func messageToSign(txInfo txtypes.TxInfo) string {
chainIdMu.Lock()
localChainId := chainId
chainIdMu.Unlock()
switch typed := txInfo.(type) {
case *txtypes.L2ChangePubKeyTxInfo:
return typed.GetL1SignatureBody()
case *txtypes.L2TransferTxInfo:
return typed.GetL1SignatureBody(localChainId)
default:
return ""
}
}
func signedTxResponseErr(err any) C.SignedTxResponse {
return C.SignedTxResponse{err: wrapErr(err)}
}
func signedTxResponsePanic(err any) C.SignedTxResponse {
return signedTxResponseErr(fmt.Errorf("panic: %v", err))
}
func convertTxInfoToResponse(txInfo txtypes.TxInfo, err error) C.SignedTxResponse {
if err != nil {
return signedTxResponseErr(err)
}
if txInfo == nil {
return signedTxResponseErr("nil transaction info")
}
txInfoStr, err := txInfo.GetTxInfo()
if err != nil {
return signedTxResponseErr(err)
}
resp := C.SignedTxResponse{
txType: C.uint8_t(txInfo.GetTxType()),
txInfo: C.CString(txInfoStr),
txHash: C.CString(txInfo.GetTxHash()),
}
if msg := messageToSign(txInfo); msg != "" {
resp.messageToSign = C.CString(msg)
}
return resp
}
// getClient returns the go TxClient from the specified cApiKeyIndex and cAccountIndex
func getClient(cApiKeyIndex C.int, cAccountIndex C.longlong) (*client.TxClient, error) {
apiKeyIndex := uint8(cApiKeyIndex)
accountIndex := int64(cAccountIndex)
return client.GetClient(apiKeyIndex, accountIndex)
}
func getTransactOpts(cNonce C.longlong) *types.TransactOpts {
nonce := int64(cNonce)
return &types.TransactOpts{
Nonce: &nonce,
}
}
//export GenerateAPIKey
func GenerateAPIKey(cSeed *C.char) (ret C.ApiKeyResponse) {
defer func() {
if r := recover(); r != nil {
ret = C.ApiKeyResponse{err: wrapErr(fmt.Errorf("panic: %v", r))}
}
}()
seed := C.GoString(cSeed)
privateKeyStr, publicKeyStr, err := client.GenerateAPIKey(seed)
if err != nil {
return C.ApiKeyResponse{err: wrapErr(err)}
}
return C.ApiKeyResponse{
privateKey: C.CString(privateKeyStr),
publicKey: C.CString(publicKeyStr),
}
}
//export CreateClient
func CreateClient(cUrl *C.char, cPrivateKey *C.char, cChainId C.int, cApiKeyIndex C.int, cAccountIndex C.longlong) (ret *C.char) {
defer func() {
if r := recover(); r != nil {
ret = wrapErr(fmt.Errorf("panic: %v", r))
}
}()
url := C.GoString(cUrl)
privateKey := C.GoString(cPrivateKey)
localChainId := uint32(cChainId)
apiKeyIndex := uint8(cApiKeyIndex)
accountIndex := int64(cAccountIndex)
httpClient := http.NewClient(url)
_, err := client.CreateClient(httpClient, privateKey, localChainId, apiKeyIndex, accountIndex)
chainIdMu.Lock()
chainId = localChainId
chainIdMu.Unlock()
return wrapErr(err)
}
//export CheckClient
func CheckClient(cApiKeyIndex C.int, cAccountIndex C.longlong) (ret *C.char) {
defer func() {
if r := recover(); r != nil {
ret = wrapErr(fmt.Errorf("panic: %v", r))
}
}()
c, err := getClient(cApiKeyIndex, cAccountIndex)
if err != nil {
return wrapErr(err)
}
return wrapErr(c.Check())
}
//export SignChangePubKey
func SignChangePubKey(cPubKey *C.char, cNonce C.longlong, cApiKeyIndex C.int, cAccountIndex C.longlong) (ret C.SignedTxResponse) {
defer func() {
if r := recover(); r != nil {
ret = signedTxResponsePanic(r)
}
}()
c, err := getClient(cApiKeyIndex, cAccountIndex)
if err != nil {
return signedTxResponseErr(err)
}
pubKeyStr := C.GoString(cPubKey)
pubKeyBytes, err := hexutil.Decode(pubKeyStr)
if err != nil {
return signedTxResponseErr(err)
}
if len(pubKeyBytes) != 40 {
return signedTxResponseErr(fmt.Errorf("invalid pub key length. expected 40 but got %v", len(pubKeyBytes)))
}
var pubKey [40]byte
copy(pubKey[:], pubKeyBytes)
tx := &types.ChangePubKeyReq{
PubKey: pubKey,
}
ops := getTransactOpts(cNonce)
txInfo, err := c.GetChangePubKeyTransaction(tx, ops)
return convertTxInfoToResponse(txInfo, err)
}
//export SignCreateOrder
func SignCreateOrder(cMarketIndex C.int, cClientOrderIndex C.longlong, cBaseAmount C.longlong, cPrice C.int, cIsAsk C.int, cOrderType C.int, cTimeInForce C.int, cReduceOnly C.int, cTriggerPrice C.int, cOrderExpiry C.longlong, cNonce C.longlong, cApiKeyIndex C.int, cAccountIndex C.longlong) (ret C.SignedTxResponse) {
defer func() {
if r := recover(); r != nil {
ret = signedTxResponsePanic(r)
}
}()
c, err := getClient(cApiKeyIndex, cAccountIndex)
if err != nil {
return signedTxResponseErr(err)
}
marketIndex := int16(cMarketIndex)
clientOrderIndex := int64(cClientOrderIndex)
baseAmount := int64(cBaseAmount)
price := uint32(cPrice)
isAsk := uint8(cIsAsk)
orderType := uint8(cOrderType)
timeInForce := uint8(cTimeInForce)
reduceOnly := uint8(cReduceOnly)
triggerPrice := uint32(cTriggerPrice)
orderExpiry := int64(cOrderExpiry)
if orderExpiry == -1 {
orderExpiry = time.Now().Add(time.Hour * 24 * 28).UnixMilli() // 28 days
}
tx := &types.CreateOrderTxReq{
MarketIndex: marketIndex,
ClientOrderIndex: clientOrderIndex,
BaseAmount: baseAmount,
Price: price,
IsAsk: isAsk,
Type: orderType,
TimeInForce: timeInForce,
ReduceOnly: reduceOnly,
TriggerPrice: triggerPrice,
OrderExpiry: orderExpiry,
}
ops := getTransactOpts(cNonce)
txInfo, err := c.GetCreateOrderTransaction(tx, ops)
return convertTxInfoToResponse(txInfo, err)
}
//export SignCreateGroupedOrders
func SignCreateGroupedOrders(cGroupingType C.uint8_t, cOrders *C.CreateOrderTxReq, cLen C.int, cNonce C.longlong, cApiKeyIndex C.int, cAccountIndex C.longlong) (ret C.SignedTxResponse) {
defer func() {
if r := recover(); r != nil {
ret = signedTxResponsePanic(r)
}
}()
c, err := getClient(cApiKeyIndex, cAccountIndex)
if err != nil {
return signedTxResponseErr(err)
}
length := int(cLen)
orders := make([]*types.CreateOrderTxReq, length)
size := unsafe.Sizeof(*cOrders)
for i := 0; i < length; i++ {
order := (*C.CreateOrderTxReq)(unsafe.Pointer(uintptr(unsafe.Pointer(cOrders)) + uintptr(i)*uintptr(size)))
orderExpiry := int64(order.OrderExpiry)
if orderExpiry == -1 {
orderExpiry = time.Now().Add(time.Hour * 24 * 28).UnixMilli()
}
orders[i] = &types.CreateOrderTxReq{
MarketIndex: int16(order.MarketIndex),
ClientOrderIndex: int64(order.ClientOrderIndex),
BaseAmount: int64(order.BaseAmount),
Price: uint32(order.Price),
IsAsk: uint8(order.IsAsk),
Type: uint8(order.Type),
TimeInForce: uint8(order.TimeInForce),
ReduceOnly: uint8(order.ReduceOnly),
TriggerPrice: uint32(order.TriggerPrice),
OrderExpiry: orderExpiry,
}
}
tx := &types.CreateGroupedOrdersTxReq{
GroupingType: uint8(cGroupingType),
Orders: orders,
}
ops := getTransactOpts(cNonce)
txInfo, err := c.GetCreateGroupedOrdersTransaction(tx, ops)
return convertTxInfoToResponse(txInfo, err)
}
//export SignCancelOrder
func SignCancelOrder(cMarketIndex C.int, cOrderIndex C.longlong, cNonce C.longlong, cApiKeyIndex C.int, cAccountIndex C.longlong) (ret C.SignedTxResponse) {
defer func() {
if r := recover(); r != nil {
ret = signedTxResponsePanic(r)
}
}()
c, err := getClient(cApiKeyIndex, cAccountIndex)
if err != nil {
return signedTxResponseErr(err)
}
marketIndex := int16(cMarketIndex)
orderIndex := int64(cOrderIndex)
tx := &types.CancelOrderTxReq{
MarketIndex: marketIndex,
Index: orderIndex,
}
ops := getTransactOpts(cNonce)
txInfo, err := c.GetCancelOrderTransaction(tx, ops)
return convertTxInfoToResponse(txInfo, err)
}
//export SignWithdraw
func SignWithdraw(cAssetIndex C.int, cRouteType C.int, cAmount C.ulonglong, cNonce C.longlong, cApiKeyIndex C.int, cAccountIndex C.longlong) (ret C.SignedTxResponse) {
defer func() {
if r := recover(); r != nil {
ret = signedTxResponsePanic(r)
}
}()
c, err := getClient(cApiKeyIndex, cAccountIndex)
if err != nil {
return signedTxResponseErr(err)
}
assetIndex := int16(cAssetIndex)
routeType := uint8(cRouteType)
amount := uint64(cAmount)
tx := &types.WithdrawTxReq{
AssetIndex: assetIndex,
RouteType: routeType,
Amount: amount,
}
ops := getTransactOpts(cNonce)
txInfo, err := c.GetWithdrawTransaction(tx, ops)
return convertTxInfoToResponse(txInfo, err)
}
//export SignCreateSubAccount
func SignCreateSubAccount(cNonce C.longlong, cApiKeyIndex C.int, cAccountIndex C.longlong) (ret C.SignedTxResponse) {
defer func() {
if r := recover(); r != nil {
ret = signedTxResponsePanic(r)
}
}()
c, err := getClient(cApiKeyIndex, cAccountIndex)
if err != nil {
return signedTxResponseErr(err)
}
ops := getTransactOpts(cNonce)
txInfo, err := c.GetCreateSubAccountTransaction(ops)
return convertTxInfoToResponse(txInfo, err)
}
//export SignCancelAllOrders
func SignCancelAllOrders(cTimeInForce C.int, cTime C.longlong, cNonce C.longlong, cApiKeyIndex C.int, cAccountIndex C.longlong) (ret C.SignedTxResponse) {
defer func() {
if r := recover(); r != nil {
ret = signedTxResponsePanic(r)
}
}()
c, err := getClient(cApiKeyIndex, cAccountIndex)
if err != nil {
return signedTxResponseErr(err)
}
timeInForce := uint8(cTimeInForce)
t := int64(cTime)
tx := &types.CancelAllOrdersTxReq{
TimeInForce: timeInForce,
Time: t,
}
ops := getTransactOpts(cNonce)
txInfo, err := c.GetCancelAllOrdersTransaction(tx, ops)
return convertTxInfoToResponse(txInfo, err)
}
//export SignModifyOrder
func SignModifyOrder(cMarketIndex C.int, cIndex C.longlong, cBaseAmount C.longlong, cPrice C.longlong, cTriggerPrice C.longlong, cNonce C.longlong, cApiKeyIndex C.int, cAccountIndex C.longlong) (ret C.SignedTxResponse) {
defer func() {
if r := recover(); r != nil {
ret = signedTxResponsePanic(r)
}
}()
c, err := getClient(cApiKeyIndex, cAccountIndex)
if err != nil {
return signedTxResponseErr(err)
}
marketIndex := int16(cMarketIndex)
index := int64(cIndex)
baseAmount := int64(cBaseAmount)
price := uint32(cPrice)
triggerPrice := uint32(cTriggerPrice)
tx := &types.ModifyOrderTxReq{
MarketIndex: marketIndex,
Index: index,
BaseAmount: baseAmount,
Price: price,
TriggerPrice: triggerPrice,
}
ops := getTransactOpts(cNonce)
txInfo, err := c.GetModifyOrderTransaction(tx, ops)
return convertTxInfoToResponse(txInfo, err)
}
//export SignTransfer
func SignTransfer(cToAccountIndex C.longlong, cAssetIndex C.int16_t, cFromRouteType, cToRouteType C.uint8_t, cAmount, cUsdcFee C.longlong, cMemo *C.char, cNonce C.longlong, cApiKeyIndex C.int, cAccountIndex C.longlong) (ret C.SignedTxResponse) {
defer func() {
if r := recover(); r != nil {
ret = signedTxResponsePanic(r)
}
}()
c, err := getClient(cApiKeyIndex, cAccountIndex)
if err != nil {
return signedTxResponseErr(err)
}
toAccountIndex := int64(cToAccountIndex)
assetIndex := int16(cAssetIndex)
fromRouteType := uint8(cFromRouteType)
toRouteType := uint8(cToRouteType)
amount := int64(cAmount)
usdcFee := int64(cUsdcFee)
memo := [32]byte{}
memoStr := C.GoString(cMemo)
if len(memoStr) == 66 {
if memoStr[0:2] == "0x" {
memoStr = memoStr[2:66]
} else {
return signedTxResponseErr(fmt.Sprintf("memo expected to be 32 bytes or 64 hex encoded or 66 if 0x hex encoded -- long but received %v", len(memoStr)))
}
}
// assume hex encoded here
if len(memoStr) == 64 {
b, err := hex.DecodeString(memoStr)
if err != nil {
return signedTxResponseErr(fmt.Sprintf("failed to decode hex string. err: %v", err))
}
for i := 0; i < 32; i += 1 {
memo[i] = b[i]
}
} else if len(memoStr) == 32 {
for i := 0; i < 32; i++ {
memo[i] = byte(memoStr[i])
}
} else {
return signedTxResponseErr(fmt.Sprintf("memo expected to be 32 bytes or 64 hex encoded or 66 if 0x hex encoded -- long but received %v", len(memoStr)))
}
tx := &types.TransferTxReq{
ToAccountIndex: toAccountIndex,
AssetIndex: assetIndex,
FromRouteType: fromRouteType,
ToRouteType: toRouteType,
Amount: amount,
USDCFee: usdcFee,
Memo: memo,
}
ops := getTransactOpts(cNonce)
txInfo, err := c.GetTransferTransaction(tx, ops)
return convertTxInfoToResponse(txInfo, err)
}
//export SignCreatePublicPool
func SignCreatePublicPool(cOperatorFee C.longlong, cInitialTotalShares C.int, cMinOperatorShareRate C.longlong, cNonce C.longlong, cApiKeyIndex C.int, cAccountIndex C.longlong) (ret C.SignedTxResponse) {
defer func() {
if r := recover(); r != nil {
ret = signedTxResponsePanic(r)
}
}()
c, err := getClient(cApiKeyIndex, cAccountIndex)
if err != nil {
return signedTxResponseErr(err)
}
operatorFee := int64(cOperatorFee)
initialTotalShares := int64(cInitialTotalShares)
minOperatorShareRate := uint16(cMinOperatorShareRate)
tx := &types.CreatePublicPoolTxReq{
OperatorFee: operatorFee,
InitialTotalShares: initialTotalShares,
MinOperatorShareRate: minOperatorShareRate,
}
ops := getTransactOpts(cNonce)
txInfo, err := c.GetCreatePublicPoolTransaction(tx, ops)
return convertTxInfoToResponse(txInfo, err)
}
//export SignUpdatePublicPool
func SignUpdatePublicPool(cPublicPoolIndex C.longlong, cStatus C.int, cOperatorFee C.longlong, cMinOperatorShareRate C.int, cNonce C.longlong, cApiKeyIndex C.int, cAccountIndex C.longlong) (ret C.SignedTxResponse) {
defer func() {
if r := recover(); r != nil {
ret = signedTxResponsePanic(r)
}
}()
c, err := getClient(cApiKeyIndex, cAccountIndex)
if err != nil {
return signedTxResponseErr(err)
}
publicPoolIndex := int64(cPublicPoolIndex)
status := uint8(cStatus)
operatorFee := int64(cOperatorFee)
minOperatorShareRate := uint16(cMinOperatorShareRate)
tx := &types.UpdatePublicPoolTxReq{
PublicPoolIndex: publicPoolIndex,
Status: status,
OperatorFee: operatorFee,
MinOperatorShareRate: minOperatorShareRate,
}
ops := getTransactOpts(cNonce)
txInfo, err := c.GetUpdatePublicPoolTransaction(tx, ops)
return convertTxInfoToResponse(txInfo, err)
}
//export SignMintShares
func SignMintShares(cPublicPoolIndex C.longlong, cShareAmount C.longlong, cNonce C.longlong, cApiKeyIndex C.int, cAccountIndex C.longlong) (ret C.SignedTxResponse) {
defer func() {
if r := recover(); r != nil {
ret = signedTxResponsePanic(r)
}
}()
c, err := getClient(cApiKeyIndex, cAccountIndex)
if err != nil {
return signedTxResponseErr(err)
}
publicPoolIndex := int64(cPublicPoolIndex)
shareAmount := int64(cShareAmount)
tx := &types.MintSharesTxReq{
PublicPoolIndex: publicPoolIndex,
ShareAmount: shareAmount,
}
ops := getTransactOpts(cNonce)
txInfo, err := c.GetMintSharesTransaction(tx, ops)
return convertTxInfoToResponse(txInfo, err)
}
//export SignBurnShares
func SignBurnShares(cPublicPoolIndex C.longlong, cShareAmount C.longlong, cNonce C.longlong, cApiKeyIndex C.int, cAccountIndex C.longlong) (ret C.SignedTxResponse) {
defer func() {
if r := recover(); r != nil {
ret = signedTxResponsePanic(r)
}
}()
c, err := getClient(cApiKeyIndex, cAccountIndex)
if err != nil {
return signedTxResponseErr(err)
}
publicPoolIndex := int64(cPublicPoolIndex)
shareAmount := int64(cShareAmount)
tx := &types.BurnSharesTxReq{
PublicPoolIndex: publicPoolIndex,
ShareAmount: shareAmount,
}
ops := getTransactOpts(cNonce)
txInfo, err := c.GetBurnSharesTransaction(tx, ops)
return convertTxInfoToResponse(txInfo, err)
}
//export SignUpdateLeverage
func SignUpdateLeverage(cMarketIndex C.int, cInitialMarginFraction C.int, cMarginMode C.int, cNonce C.longlong, cApiKeyIndex C.int, cAccountIndex C.longlong) (ret C.SignedTxResponse) {
defer func() {
if r := recover(); r != nil {
ret = signedTxResponsePanic(r)
}
}()
c, err := getClient(cApiKeyIndex, cAccountIndex)
if err != nil {
return signedTxResponseErr(err)
}
marketIndex := int16(cMarketIndex)
initialMarginFraction := uint16(cInitialMarginFraction)
marginMode := uint8(cMarginMode)
tx := &types.UpdateLeverageTxReq{
MarketIndex: marketIndex,
InitialMarginFraction: initialMarginFraction,
MarginMode: marginMode,
}
ops := getTransactOpts(cNonce)
txInfo, err := c.GetUpdateLeverageTransaction(tx, ops)
return convertTxInfoToResponse(txInfo, err)
}
//export CreateAuthToken
func CreateAuthToken(cDeadline C.longlong, cApiKeyIndex C.int, cAccountIndex C.longlong) (ret C.StrOrErr) {
defer func() {
if r := recover(); r != nil {
ret = C.StrOrErr{err: wrapErr(fmt.Errorf("panic: %v", r))}
}
}()
c, err := getClient(cApiKeyIndex, cAccountIndex)
if err != nil {
return C.StrOrErr{err: wrapErr(err)}
}
deadline := int64(cDeadline)
if deadline == 0 {
deadline = time.Now().Add(time.Hour * 7).Unix()
}
authToken, err := c.GetAuthToken(time.Unix(deadline, 0))
if err != nil {
return C.StrOrErr{err: wrapErr(err)}
}
return C.StrOrErr{str: C.CString(authToken)}
}
//export SignUpdateMargin
func SignUpdateMargin(cMarketIndex C.int, cUSDCAmount C.longlong, cDirection C.int, cNonce C.longlong, cApiKeyIndex C.int, cAccountIndex C.longlong) (ret C.SignedTxResponse) {
defer func() {
if r := recover(); r != nil {
ret = signedTxResponsePanic(r)
}
}()
c, err := getClient(cApiKeyIndex, cAccountIndex)
if err != nil {
return signedTxResponseErr(err)
}
marketIndex := int16(cMarketIndex)
usdcAmount := int64(cUSDCAmount)
direction := uint8(cDirection)
tx := &types.UpdateMarginTxReq{
MarketIndex: marketIndex,
USDCAmount: usdcAmount,
Direction: direction,
}
ops := getTransactOpts(cNonce)
txInfo, err := c.GetUpdateMarginTransaction(tx, ops)
return convertTxInfoToResponse(txInfo, err)
}
func main() {}