-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer.go
87 lines (73 loc) · 2.59 KB
/
transfer.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
package bc
import (
"context"
"fmt"
"time"
"github.com/shopspring/decimal"
)
// Transfer Transfer
type Transfer struct {
ID int64 `json:"id,omitempty"`
SnapshotID string `json:"snapshot_id,omitempty"`
TraceID string `json:"trace_id,omitempty"`
OpponentID string `json:"opponent_id,omitempty"`
UserID string `json:"user_id,omitempty"`
AssetID string `json:"asset_id,omitempty"`
Amount decimal.Decimal `json:"amount,omitempty"`
Memo string `json:"memo,omitempty"`
Type string `json:"type,omitempty"`
Status string `json:"status,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
}
// TransferInput TransferInput
type TransferInput struct {
AssetID string `json:"asset_id,omitempty" binding:"required"`
OpponentID string `json:"opponent_id,omitempty" binding:"required"`
Amount decimal.Decimal `json:"amount,omitempty" binding:"required"`
TraceID string `json:"trace_id,omitempty"`
Memo string `json:"memo,omitempty"`
}
// GenerateTransferInput GenerateTransferInput
type GenerateTransferInput struct {
AssetID string `json:"asset_id,omitempty" binding:"required"`
UserID string `json:"user_id,omitempty" binding:"required"`
Amount decimal.Decimal `json:"amount,omitempty" binding:"required"`
TraceID string `json:"trace_id,omitempty"`
Memo string `json:"memo,omitempty"`
}
// Transfer Transfer
func (c *Client) Transfer(ctx context.Context, input *TransferInput) (*Transfer, error) {
var transfer Transfer
var body struct {
TransferInput
UserID string `json:"user_id,omitempty"`
}
body.TransferInput = *input
body.UserID = c.ClientID
if err := c.Post(ctx, "/app/transfers", body, &transfer); err != nil {
return nil, err
}
return &transfer, nil
}
// GenerateTransfer GenerateTransfer
func (c *Client) GenerateTransfer(ctx context.Context, input *GenerateTransferInput) (*Transfer, error) {
var transfer Transfer
var body struct {
GenerateTransferInput
OpponentID string `json:"opponent_id,omitempty"`
}
body.GenerateTransferInput = *input
body.OpponentID = c.ClientID
if err := c.Post(ctx, "/app/transfers/generate", body, &transfer); err != nil {
return nil, err
}
return &transfer, nil
}
func (c *Client) ReadTransfer(ctx context.Context, traceID string) (*Transfer, error) {
uri := fmt.Sprintf("/app/transfers/trace/%s", traceID)
var transfer Transfer
if err := c.Get(ctx, uri, nil, &transfer); err != nil {
return nil, err
}
return &transfer, nil
}