-
Notifications
You must be signed in to change notification settings - Fork 2
/
apiHistoryTransaction.go
212 lines (197 loc) · 5.74 KB
/
apiHistoryTransaction.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
package goVsysSdk
import (
"strconv"
"encoding/json"
"errors"
"strings"
)
type HistoryTransaction struct{
Type int `json:"type,omitempty"` // TX_TYPE
Id string `json:"id,omitempty"`
Fee int64 `json:"fee,omitempty"`
FeeScale int64 `json:"feeScale,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"`
Proofs []Proof `json:"proofs,omitempty"`
Recipient string `json:"recipient,omitempty"`
Amount int64 `json:"amount,omitempty"`
Attachment string `json:"attachmen,omitempty"`
ContractId string `json:"contractId,omitempty"`
FunctionIndex int `json:"functionIndex,omitempty"` // FuncidxSplit,...
FunctionData string `json:"functionData,omitempty"`
Contract *HtContract `json:"contract,omitempty"`
InitData string `json:"initData,omitempty"`
Description string `json:"description,omitempty"`
Status string `json:"status,omitempty"`
FeeCharged int `json:"feeCharged,omitempty"`
Height int `json:"height,omitempty"`
CurrentBlockHeight int `json:"currentBlockHeight,omitempty"`
}
type HtContract struct{
LanguageCode string `json:"languageCode,omitempty"`
LanguageVersion int `json:"languageVersion,omitempty"`
Triggers []string `json:"triggers,omitempty"`
Descriptors []string `json:"descriptors,omitempty"`
StateVariables []string `json:"stateVariables,omitempty"`
StateMaps []string `json:"stateMaps,omitempty"`
Textual struct {
Triggers string `json:"triggers,omitempty"`
Descriptors string `json:"descriptors,omitempty"`
StateVariables string `json:"stateVariables,omitempty"`
StateMaps string `json:"stateMaps,omitempty"`
} `json:"textual,omitempty"`
}
func (ht *HistoryTransaction) IsContractSend() bool{
return ht.Type==TxTypeContractExecute && ht.ContractId!="" && ht.FunctionIndex==3
}
func (ht *HistoryTransaction) MustDecodeContractSend() HtContractSend{
if ht.IsContractSend()==false{
panic("rvxzrh2rqz")
}
de := DataEncoder{}
bytes := MustBase58Decode(ht.FunctionData)
list := de.Decode(bytes)
if len(list)!=2{
panic("5xrrhxtuc6")
}
return HtContractSend{
AddressSend: ht.Proofs[0].Address,
AddressRecv: list[0].Value.(string),
Amount: list[1].Value.(int64),
}
}
type HtContractSend struct{
AddressSend string
AddressRecv string
Amount int64
}
func (sdk *VsysApi)GetAccountHistoryTransactionList(address string,limit int) (list []HistoryTransaction,err error){
b,err:= sdk.httpGet("/transactions/address/"+ urlv(address)+"/limit/"+ urlv(strconv.Itoa(limit)))
if err!=nil{
return nil,err
}
//fmt.Println(string(b))
type respT [][]HistoryTransaction
var respO respT
err = json.Unmarshal(b,&respO)
if err!=nil{
return nil,err
}
if len(respO)!=1{
return nil,errors.New("dnv4ub5faz ["+strconv.Itoa(int(len(respO))) )
}
return respO[0],nil
}
// TxType 0 to return all transaction
func (sdk *VsysApi)GetAccountAllHistoryTransactionCallback(address string,TxType int,visitor func(tran HistoryTransaction)) (err error){
const itemPerPage = 10000
offset:=0
for {
list2,err:=sdk.GetAccountHistoryTransactionList2(TransactionList2Req{
Address: address,
TxType: TxType,
Limit: itemPerPage,
Offset: offset,
})
if err!=nil{
return err
}
for _,ht:=range list2{
visitor(ht)
}
offset+=len(list2)
if len(list2)<itemPerPage{
return nil
}
}
}
type TransactionList2Req struct{
Address string
Limit int
Offset int
TxType int
}
func (sdk *VsysApi)GetAccountHistoryTransactionList2(req TransactionList2Req) (list []HistoryTransaction,err error){
type respT struct {
TotalCount int `json:"totalCount"`
Size int `json:"size"`
Transactions []HistoryTransaction `json:"transactions"`
}
uS:="/transactions/list?address="+ urlv(req.Address)+"&limit="+ urlv(strconv.Itoa(req.Limit))
if req.Offset!=0{
uS+="&offset="+ urlv(strconv.Itoa(req.Offset))
}
if req.TxType!=0{
uS+="&txType="+ urlv(strconv.Itoa(req.TxType))
}
b,err:= sdk.httpGet(uS)
if err!=nil{
return nil,errors.New("rs2u4qna66 "+err.Error())
}
var respO respT
err = json.Unmarshal(b,&respO)
if err!=nil{
return nil,errors.New("9mtxu65dyt "+string(b)+" "+err.Error())
}
return respO.Transactions,nil
}
func (sdk *VsysApi)MustGetAccountHistoryTransactionList(address string,limit int) (list []HistoryTransaction) {
list,err:=sdk.GetAccountHistoryTransactionList(address,limit)
if err!=nil{
panic(err)
}
return list
}
func (sdk *VsysApi)GetUnconfirmedHistoryTransactionList() (list []HistoryTransaction,err error){
b,err:= sdk.httpGet("/transactions/unconfirmed")
if err!=nil{
return nil,err
}
var respO []HistoryTransaction
err = json.Unmarshal(b,&respO)
if err!=nil{
return nil,err
}
return respO,nil
}
func (sdk *VsysApi)GetHistoryTransactionById(txId string) (ht HistoryTransaction,err error){
b,err:= sdk.httpGet("/transactions/info/"+ urlv(txId))
if err!=nil{
return ht,errors.New("qkw9bg6ufq ["+err.Error()+"]")
}
var respO HistoryTransaction
err = json.Unmarshal(b,&respO)
if err!=nil{
return ht,errors.New("w4wm8yw2wb ["+err.Error()+"]")
}
if respO.Id !=txId{
return ht,errors.New("fbhs64quz4 ["+respO.Id +"]")
}
return respO,nil
}
// "Transaction is not in blockchain"
func IsErrTransactionNotInBlockChain(err error) bool{
if err==nil{
return false
}
errS:=err.Error()
return strings.Contains(errS,`"Transaction is not in blockchain"`)
}
func (sdk *VsysApi) MustGetHistoryTransactionById(txId string)(ht HistoryTransaction){
ht,err:=sdk.GetHistoryTransactionById(txId)
panicIfError(err)
return ht
}
func (sdk *VsysApi)MustGetTransactionUnconfirmedSize() int64{
b,err:= sdk.httpGet("/transactions/unconfirmed/size")
if err!=nil{
panic(err)
}
var respO struct{
Size int64 `json:"size"`
}
err = json.Unmarshal(b,&respO)
if err!=nil{
panic(err)
}
return respO.Size
}