forked from fbsobreira/gotron-sdk
/
exchange.go
184 lines (161 loc) · 4.31 KB
/
exchange.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
package client
import (
"encoding/binary"
"fmt"
"github.com/camel98/gotron-sdk/pkg/common"
"github.com/camel98/gotron-sdk/pkg/proto/api"
"github.com/camel98/gotron-sdk/pkg/proto/core"
"google.golang.org/protobuf/proto"
)
// ExchangeList of bancor TRC10, use page -1 to list all
func (g *GrpcClient) ExchangeList(page int64, limit ...int) (*api.ExchangeList, error) {
ctx, cancel := g.getContext()
defer cancel()
if page == -1 {
return g.Client.ListExchanges(ctx, new(api.EmptyMessage))
}
useLimit := int64(10)
if len(limit) == 1 {
useLimit = int64(limit[0])
}
return g.Client.GetPaginatedExchangeList(ctx, GetPaginatedMessage(page*useLimit, useLimit))
}
// ExchangeByID returns exchangeDetails
func (g *GrpcClient) ExchangeByID(id int64) (*core.Exchange, error) {
ctx, cancel := g.getContext()
defer cancel()
bID := make([]byte, 8)
binary.BigEndian.PutUint64(bID, uint64(id))
result, err := g.Client.GetExchangeById(ctx, GetMessageBytes(bID))
if err != nil {
return nil, err
}
if result.ExchangeId != id {
return nil, fmt.Errorf("Exchange does not exists")
}
return result, nil
}
// ExchangeCreate from two tokens (TRC10/TRX) only
func (g *GrpcClient) ExchangeCreate(
from string,
tokenID1 string,
amountToken1 int64,
tokenID2 string,
amountToken2 int64,
) (*api.TransactionExtention, error) {
var err error
contract := &core.ExchangeCreateContract{
FirstTokenId: []byte(tokenID1),
FirstTokenBalance: amountToken1,
SecondTokenId: []byte(tokenID2),
SecondTokenBalance: amountToken2,
}
if contract.OwnerAddress, err = common.DecodeCheck(from); err != nil {
return nil, err
}
ctx, cancel := g.getContext()
defer cancel()
tx, err := g.Client.ExchangeCreate(ctx, contract)
if err != nil {
return nil, err
}
if proto.Size(tx) == 0 {
return nil, fmt.Errorf("bad transaction")
}
if tx.GetResult().GetCode() != 0 {
return nil, fmt.Errorf("%s", tx.GetResult().GetMessage())
}
return tx, nil
}
// ExchangeInject both tokens into banco pair (the second token is taken info transaction process)
func (g *GrpcClient) ExchangeInject(
from string,
exchangeID int64,
tokenID string,
amountToken int64,
) (*api.TransactionExtention, error) {
var err error
contract := &core.ExchangeInjectContract{
ExchangeId: exchangeID,
TokenId: []byte(tokenID),
Quant: amountToken,
}
if contract.OwnerAddress, err = common.DecodeCheck(from); err != nil {
return nil, err
}
ctx, cancel := g.getContext()
defer cancel()
tx, err := g.Client.ExchangeInject(ctx, contract)
if err != nil {
return nil, err
}
if proto.Size(tx) == 0 {
return nil, fmt.Errorf("bad transaction")
}
if tx.GetResult().GetCode() != 0 {
return nil, fmt.Errorf("%s", tx.GetResult().GetMessage())
}
return tx, nil
}
// ExchangeWithdraw both tokens into banco pair (the second token is taken info transaction process)
func (g *GrpcClient) ExchangeWithdraw(
from string,
exchangeID int64,
tokenID string,
amountToken int64,
) (*api.TransactionExtention, error) {
var err error
contract := &core.ExchangeWithdrawContract{
ExchangeId: exchangeID,
TokenId: []byte(tokenID),
Quant: amountToken,
}
if contract.OwnerAddress, err = common.DecodeCheck(from); err != nil {
return nil, err
}
ctx, cancel := g.getContext()
defer cancel()
tx, err := g.Client.ExchangeWithdraw(ctx, contract)
if err != nil {
return nil, err
}
if proto.Size(tx) == 0 {
return nil, fmt.Errorf("bad transaction")
}
if tx.GetResult().GetCode() != 0 {
return nil, fmt.Errorf("%s", tx.GetResult().GetMessage())
}
return tx, nil
}
// ExchangeTrade on bancor TRC10
func (g *GrpcClient) ExchangeTrade(
from string,
exchangeID int64,
tokenID string,
amountToken int64,
amountExpected int64,
) (*api.TransactionExtention, error) {
var err error
contract := &core.ExchangeTransactionContract{
ExchangeId: exchangeID,
TokenId: []byte(tokenID),
Quant: amountToken,
Expected: amountExpected,
}
if contract.OwnerAddress, err = common.DecodeCheck(from); err != nil {
return nil, err
}
ctx, cancel := g.getContext()
defer cancel()
tx, err := g.Client.ExchangeTransaction(ctx, contract)
if err != nil {
return nil, err
}
if proto.Size(tx) == 0 {
return nil, fmt.Errorf("bad transaction")
}
if tx.GetResult().GetCode() != 0 {
return nil, fmt.Errorf("%s", tx.GetResult().GetMessage())
}
return tx, nil
}