forked from MixinNetwork/bot-api-go-client
/
withdrawal.go
72 lines (64 loc) · 1.7 KB
/
withdrawal.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
package bot
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/MixinNetwork/go-number"
)
type Withdrawal struct {
Type string `json:"type"`
SnapshotId string `json:"snapshot_id"`
Receiver string `json:"receiver"`
TransactionHash string `json:"transaction_hash"`
AssetId string `json:"asset_id"`
Amount string `json:"amount"`
TraceId string `json:"trace_id"`
Memo string `json:"memo"`
CreatedAt time.Time `json:"created_at"`
}
type WithdrawalInput struct {
AddressId string
Amount number.Decimal
TraceId string
Memo string
}
func CreateWithdrawal(ctx context.Context, in *WithdrawalInput, uid, sid, sessionKey, pin, pinToken string) (*Withdrawal, error) {
if in.Amount.Exhausted() {
return nil, fmt.Errorf("Amount negative")
}
encryptedPIN, err := EncryptPIN(ctx, pin, pinToken, sid, sessionKey, uint64(time.Now().UnixNano()))
if err != nil {
return nil, err
}
data, err := json.Marshal(map[string]interface{}{
"address_id": in.AddressId,
"amount": in.Amount.Persist(),
"trace_id": in.TraceId,
"memo": in.Memo,
"pin": encryptedPIN,
})
if err != nil {
return nil, err
}
token, err := SignAuthenticationToken(uid, sid, sessionKey, "POST", "/withdrawals", string(data))
if err != nil {
return nil, err
}
body, err := Request(ctx, "POST", "/withdrawals", data, token)
if err != nil {
return nil, err
}
var resp struct {
Error Error `json:"error"`
Data Withdrawal `json:"data,omitempty"`
}
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
if resp.Error.Code > 0 {
return nil, resp.Error
}
return &resp.Data, nil
}