-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevent_test.go.bak
289 lines (250 loc) · 11.2 KB
/
event_test.go.bak
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
package ethadaptor
import (
"encoding/base64"
"encoding/hex"
"fmt"
"math/big"
"strconv"
"strings"
"testing"
"github.com/palletone/adaptor"
)
func processResult(getevent *adaptor.GetEventByAddressResult) {
for _, result := range getevent.Events {
fmt.Println(result)
// var strA1 []string
// err = json.Unmarshal([]byte(result), &strA1) //json: cannot unmarshal number into Go value of type string
// if err != nil {
// fmt.Println(err.Error())
// } else {
// for _, str := range strA1 {
// fmt.Println(str)
// }
// str3 := strA1[3][1 : len(strA1[3])-2]
// fmt.Println(str3)
// redeemBytes, err := base64.StdEncoding.DecodeString(str3)
// if err != nil {
// fmt.Println(err.Error())
// } else {
// redeemHex := hex.EncodeToString(redeemBytes)
// fmt.Println(redeemHex)
// }
// }
strArray := strings.SplitAfter(result, ",")
for _, str := range strArray {
fmt.Println(str)
}
str3 := strArray[3][1 : len(strArray[3])-2]
//str3 = str3[1 : len(strArray[3])-2]
fmt.Println(str3)
redeemBytes, err := base64.StdEncoding.DecodeString(str3)
if err != nil {
fmt.Println(err.Error())
} else {
redeemHex := hex.EncodeToString(redeemBytes)
fmt.Println(redeemHex)
}
}
}
func processResultBalance(ethAddr string, geteventresult *adaptor.GetEventByAddressResult) error {
//
eth_redeem := "7d7116a8706ae08baa7f4909e26728fa7a5f0365aaa919a7c465be9b053673c567d73be8603179636c7110482920e0af149a82189251f292a84148a85b7cd70d"
//eth_redeem base64
eth_redeem_bytes, err := hex.DecodeString(string(eth_redeem))
if err != nil {
return err
}
eth_redeem_base64 := base64.StdEncoding.EncodeToString(eth_redeem_bytes)
//
ethAddr = strings.ToLower(ethAddr)
bigIntAmount := new(big.Int)
for i, event := range geteventresult.Events {
fmt.Println(event)
fmt.Println(geteventresult.Txhashs[i])
strArray := strings.Split(event, ",")
//token 0x0 is eth ["0x0000000000000000000000000000000000000000"
str0 := strArray[0][2 : len(strArray[0])-1]
if strings.Compare(str0, "0x0000000000000000000000000000000000000000") != 0 {
continue
}
//user is eth sender "0x7d7116a8706ae08baa7f4909e26728fa7a5f0365"
str1 := strArray[1][1 : len(strArray[1])-1]
if strings.Compare(str1, ethAddr) != 0 {
continue
}
//eth_redeem base64 "fXEWqHBq4Iuqf0kJ4mco+npfA2WqqRmnxGW+mwU2c8Vn1zvoYDF5Y2xxEEgpIOCvFJqCGJJR8pKoQUioW3zXDQ=="]
str3 := strArray[3][1 : len(strArray[3])-2]
if strings.Compare(str3, eth_redeem_base64) != 0 {
continue
}
//deposit amount 1000000000000000000
str2 := strArray[2]
bigInt := new(big.Int)
bigInt.SetString(str2, 10)
bigIntAmount = bigIntAmount.Add(bigIntAmount, bigInt)
}
return nil
//test
/*bigIntAmount =*/
bigIntAmount.Add(bigIntAmount, big.NewInt(1000000000000000000))
fmt.Println(bigIntAmount)
/*bigIntAmount =*/
bigIntAmount.Sub(bigIntAmount, big.NewInt(1000000000000000000))
fmt.Println(bigIntAmount)
//
if bigIntAmount.Cmp(big.NewInt(1*1e18)) > 0 {
fmt.Println("bigger")
} else {
fmt.Println("smaller or equal")
}
rateAmount := 0.8
weiAmount := rateAmount * 1e18
bigFloat := new(big.Float)
bigFloat.SetInt(bigIntAmount)
if bigFloat.Cmp(big.NewFloat(weiAmount)) >= 0 {
fmt.Println("bigger or equal")
} else {
fmt.Println("smaller")
}
return nil
}
type DepositETHInfo struct {
Txhash string
Amount uint64
}
//need check confirms
func getDepositETHInfo(geteventresult *adaptor.GetEventByAddressResult) ([]DepositETHInfo, error) {
endHeight := "5784143"
//event Deposit(address token, address user, uint amount, string ptnaddr);
endBlockNum, _ := strconv.ParseUint(endHeight, 10, 64)
var depositInfo []DepositETHInfo
for i, event := range geteventresult.Events {
//Event example : ["0x0000000000000000000000000000000000000000","0x7d7116a8706ae08baa7f4909e26728fa7a5f0365",500000000000000000,"P1DXLJmJh9j3LFNUZ7MmfLVNWHoLzDUHM9A"]
strArray := strings.Split(event, ",")
if len(strArray) != 4 {
fmt.Printf("len(strArray) %d\n", len(strArray))
continue
}
//confirm
if geteventresult.Blocknums[i]+10 > endBlockNum {
fmt.Printf("geteventresult.Blocknums[i] %d, endBlockNum %d", geteventresult.Blocknums[i], endBlockNum)
continue
}
//deposit amount, example : 500000000000000000
str2 := strArray[2]
bigInt := new(big.Int)
bigInt.SetString(str2, 10)
bigInt = bigInt.Div(bigInt, big.NewInt(10000000000)) //ethToken's decimal is 8
//
depositInfo = append(depositInfo, DepositETHInfo{geteventresult.Txhashs[i], bigInt.Uint64()})
}
if len(depositInfo) == 0 {
fmt.Println("len(depositInfo) is 0")
return nil, nil
}
return depositInfo, nil
}
func processWithdrawResult(ethAddr string, geteventresult *adaptor.GetEventByAddressResult) error {
//
eth_redeem := "7d7116a8706ae08baa7f4909e26728fa7a5f0365aaa919a7c465be9b053673c567d73be8603179636c7110482920e0af149a82189251f292a84148a85b7cd70d"
//eth_redeem base64
eth_redeem_bytes, err := hex.DecodeString(string(eth_redeem))
if err != nil {
return err
}
eth_redeem_base64 := base64.StdEncoding.EncodeToString(eth_redeem_bytes)
//
ethAddr = strings.ToLower(ethAddr)
//event Withdraw(address token, address user, bytes redeem, address recver, uint amount, uint confirmvalue, string state);
bigIntAmount := new(big.Int)
for _, event := range geteventresult.Events {
//example : ["0x0000000000000000000000000000000000000000","0xaaa919a7c465be9b053673c567d73be860317963","fXEWqHBq4Iuqf0kJ4mco+npfA2WqqRmnxGW+mwU2c8Vn1zvoYDF5Y2xxEEgpIOCvFJqCGJJR8pKoQUioW3zXDQ==","0xaaa919a7c465be9b053673c567d73be860317963",1000000000000000000,2,"withdraw"]
strArray := strings.Split(event, ",")
//token 0x0 is ETH, example : ["0x0000000000000000000000000000000000000000"
str0 := strArray[0][2 : len(strArray[0])-1]
if strings.Compare(str0, "0x0000000000000000000000000000000000000000") != 0 {
continue
}
//user is recver, example : "0xaaa919a7c465be9b053673c567d73be860317963"
str3 := strArray[1][1 : len(strArray[3])-1]
if strings.Compare(str3, ethAddr) != 0 {
continue
}
//eth_redeem's base64, example : "fXEWqHBq4Iuqf0kJ4mco+npfA2WqqRmnxGW+mwU2c8Vn1zvoYDF5Y2xxEEgpIOCvFJqCGJJR8pKoQUioW3zXDQ=="
str2 := strArray[2][1 : len(strArray[2])-1]
if strings.Compare(str2, eth_redeem_base64) != 0 {
continue
}
//deposit amount, example : 1000000000000000000
str4 := strArray[4]
fmt.Println(str4)
bigInt := new(big.Int)
bigInt.SetString(str4, 10)
bigIntAmount = bigIntAmount.Add(bigIntAmount, bigInt)
}
//test
/*bigIntAmount =*/
bigIntAmount.Add(bigIntAmount, big.NewInt(1000000000000000000))
fmt.Println("+1", bigIntAmount)
/*bigIntAmount =*/
bigIntAmount.Sub(bigIntAmount, big.NewInt(1000000000000000000))
fmt.Println("-1", bigIntAmount)
//
if bigIntAmount.Cmp(big.NewInt(1*1e18)) > 0 {
fmt.Println("bigger")
} else {
fmt.Println("smaller or equal")
}
rateAmount := 0.8
weiAmount := rateAmount * 1e18
bigFloat := new(big.Float)
bigFloat.SetInt(bigIntAmount)
if bigFloat.Cmp(big.NewFloat(weiAmount)) >= 0 {
fmt.Println("bigger or equal")
} else {
fmt.Println("smaller")
}
return nil
}
func TestGetEventByAddress(t *testing.T) {
rpcParams := RPCParams{
Rawurl: "https://ropsten.infura.io/", //"\\\\.\\pipe\\geth.ipc",
}
const contractABI = "[{\"constant\":true,\"inputs\":[{\"name\":\"reqid\",\"type\":\"string\"}],\"name\":\"getmultisig\",\"outputs\":[{\"name\":\"\",\"type\":\"uint8\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"addr\",\"type\":\"address\"}],\"name\":\"suicideto\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"ptnaddr\",\"type\":\"string\"}],\"name\":\"deposit\",\"outputs\":[],\"payable\":true,\"stateMutability\":\"payable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"my_eth_bal\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"addra\",\"type\":\"address\"},{\"name\":\"addrb\",\"type\":\"address\"},{\"name\":\"addrc\",\"type\":\"address\"},{\"name\":\"addrd\",\"type\":\"address\"}],\"name\":\"setaddrs\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"recver\",\"type\":\"address\"},{\"name\":\"amount\",\"type\":\"uint256\"},{\"name\":\"reqid\",\"type\":\"string\"},{\"name\":\"sigstr1\",\"type\":\"bytes\"},{\"name\":\"sigstr2\",\"type\":\"bytes\"},{\"name\":\"sigstr3\",\"type\":\"bytes\"}],\"name\":\"withdraw\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"name\":\"addra\",\"type\":\"address\"},{\"name\":\"addrb\",\"type\":\"address\"},{\"name\":\"addrc\",\"type\":\"address\"},{\"name\":\"addrd\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"fallback\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"token\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"user\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"amount\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"ptnaddr\",\"type\":\"string\"}],\"name\":\"Deposit\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"token\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"user\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"recver\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"amount\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"reqid\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"confirmvalue\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"state\",\"type\":\"string\"}],\"name\":\"Withdraw\",\"type\":\"event\"}]"
contractAddr := "0x5b8c8B8Aa705bF555F0B8E556Bf0d58956eCD6e9"
var getEventByAddressParams adaptor.GetEventByAddressParams
getEventByAddressParams.ContractABI = contractABI
getEventByAddressParams.ContractAddr = contractAddr
getEventByAddressParams.ConcernAddr = "P1BsMFaiGD1WGdTr6ARz1UKDejDbVumwwzm" //"0x588eB98f8814aedB056D549C0bafD5Ef4963069C"
getEventByAddressParams.EventName = "Deposit"
getEventByAddressParams.StartHeight = "5611499"
getEventByAddressParams.EndHeight = "5784293"
result1, err := GetEventByAddress(&getEventByAddressParams, &rpcParams, NETID_MAIN)
if err != nil {
fmt.Println(err.Error())
} else {
//fmt.Println(result1)
//err = processResultBalance("0x7d7116a8706ae08baa7f4909e26728fa7a5f0365", &getevent)
depositInfo, err := getDepositETHInfo(result1)
fmt.Printf("len(depositInfo) is %d\n", len(depositInfo))
if err != nil {
fmt.Println(err.Error())
}
}
//return
fmt.Println("==== ===== ==== =====")
getEventByAddressParams.ConcernAddr = "0xaAA919a7c465be9b053673C567D73Be860317963"
getEventByAddressParams.EventName = "Withdraw"
getEventByAddressParams.StartHeight = "3885311"
getEventByAddressParams.EndHeight = "3886333"
result2, err := GetEventByAddress(&getEventByAddressParams, &rpcParams, NETID_MAIN)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println(result2)
err = processWithdrawResult("0xaAA919a7c465be9b053673C567D73Be860317963", result2)
if err != nil {
fmt.Println(err.Error())
}
}
}