Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cba321d

Browse files
committedSep 22, 2021
bugfix fix parse data
1 parent d01a04a commit cba321d

File tree

4 files changed

+91
-56
lines changed

4 files changed

+91
-56
lines changed
 

Diff for: ‎extractors/susy/bridge/common.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,11 @@ func ValidateSolanaAddress(address string) bool {
139139
}
140140

141141
func ValidateErgoAddress(address string) bool {
142-
isValid, _ := helpers.ValidateErgoAddress(address)
142+
isValid, err := helpers.ValidateErgoAddress(address)
143+
if err != nil {
144+
fmt.Printf("error: %v", err)
145+
return false
146+
}
143147
return isValid
144148
}
145149

Diff for: ‎extractors/susy/bridge/ergo.go

+72-40
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package bridge
33
import (
44
"context"
55
"encoding/base64"
6-
"encoding/hex"
6+
"encoding/binary"
77
"fmt"
88
"github.com/Gravity-Tech/gravity-node-data-extractor/v2/extractors"
99
"github.com/Gravity-Tech/gravity-node-data-extractor/v2/helpers"
10+
"github.com/spf13/cast"
1011
"math/big"
12+
"strconv"
1113
)
1214

1315
// TODO: Implement general queue iterator (Waves & ETH)
@@ -52,48 +54,70 @@ func (provider *ErgoToErgoExtractionBridge) Configure(config ConfigureCommand) e
5254

5355
func (provider *ErgoToErgoExtractionBridge) pickRequestFromLUPortQueue(luRequests []helpers.Request) (helpers.Request, error) {
5456
var rq helpers.Request
57+
allRequests, err := provider.ergClientTarget.GetAllRequest(provider.config.DestinationNodeUrl, provider.config.IBPortAddress)
58+
if err != nil {
59+
return rq, nil
60+
}
5561

56-
for _, luRequest := range luRequests {
57-
_, status, err := provider.ergClientTarget.GetRequest(provider.config.DestinationNodeUrl, provider.config.IBPortAddress, luRequest.RequestId)
58-
if err != nil {
59-
continue
62+
isExist := func(luRequest helpers.Request) bool {
63+
flag := false
64+
for _, request := range allRequests {
65+
if luRequest.RequestId == request.RequestId {
66+
flag = true
67+
break
68+
}
6069
}
61-
62-
if status == ErgoRequestStatusSuccess {
70+
return flag
71+
}
72+
for _, luRequest := range luRequests {
73+
if isExist(luRequest) {
6374
continue
6475
}
65-
if !ValidateErgoAddress(luRequest.Receiver) {
76+
isValid := ValidateErgoAddress(luRequest.Receiver)
77+
if !isValid {
6678
continue
6779
}
6880
rq = luRequest
6981
break
7082
}
83+
7184
return rq, nil
7285
}
7386

7487
func (provider *ErgoToErgoExtractionBridge) pickRequestFromIBPortQueue(ibRequests []helpers.Request) (helpers.Request, error) {
7588
var rq helpers.Request
89+
allRequests, err := provider.ergClientTarget.GetAllRequest(provider.config.SourceNodeUrl, provider.config.LUPortAddress)
90+
if err != nil {
91+
return rq, nil
92+
}
7693

77-
for _, ibRequest := range ibRequests {
78-
request, status, err := provider.ergClientSource.GetRequest(provider.config.SourceNodeUrl, provider.config.LUPortAddress, ibRequest.RequestId)
79-
if err != nil {
80-
continue
94+
isExist := func(ibRequest helpers.Request) bool {
95+
flag := false
96+
for _, request := range allRequests {
97+
if ibRequest.RequestId == request.RequestId {
98+
flag = true
99+
break
100+
}
81101
}
82-
if status != ErgoRequestStatusSuccess {
102+
return flag
103+
}
104+
for _, ibRequest := range ibRequests {
105+
if isExist(ibRequest) {
83106
continue
84107
}
85-
if !ValidateErgoAddress(request.Receiver) {
108+
isValid := ValidateErgoAddress(ibRequest.Receiver)
109+
if !isValid {
86110
continue
87111
}
88-
rq = request
112+
rq = ibRequest
89113
break
90114
}
115+
91116
return rq, nil
92117
}
93118

94-
95119
func (provider *ErgoToErgoExtractionBridge) ExtractDirectTransferRequest(ctx context.Context) (*extractors.Data, error) {
96-
luRequests, err := provider.ergClientSource.GetRequestsList(provider.config.SourceNodeUrl, provider.config.LUPortAddress,ctx)
120+
luRequests, err := provider.ergClientSource.GetRequestsList(provider.config.SourceNodeUrl, provider.config.LUPortAddress, ctx)
97121
if err != nil {
98122
return nil, err
99123
}
@@ -106,29 +130,33 @@ func (provider *ErgoToErgoExtractionBridge) ExtractDirectTransferRequest(ctx con
106130
rqId := new(big.Int)
107131
rqId, _ = rqId.SetString(rq.RequestId, 10)
108132

109-
amount := new(big.Int)
110-
amount, _ = amount.SetString(rq.Amount, 10)
133+
//amount := new(big.Int)
134+
amount, err := strconv.ParseInt(rq.Amount, 10, 64)
135+
bigAmount := big.NewInt(amount)
136+
//amount, _ = amount.SetString(rq.Amount, 10)
111137

112138
sourceDecimals := big.NewInt(10)
113139
sourceDecimals.Exp(sourceDecimals, big.NewInt(provider.config.SourceDecimals), nil)
114-
140+
//sourceDecimals := math.Pow(10, float64(provider.config.DestinationDecimals))
115141
destinationDecimals := big.NewInt(10)
116142
destinationDecimals.Exp(destinationDecimals, big.NewInt(provider.config.DestinationDecimals), nil)
143+
//destinationDecimals := math.Pow(10, float64(provider.config.DestinationDecimals))
117144

118-
newAmount := amount.
119-
Mul(amount, sourceDecimals).
120-
Div(amount, destinationDecimals)
145+
//newAmount := int64(float64(amount) * sourceDecimals / destinationDecimals)
146+
newAmount := cast.ToInt64(bigAmount.
147+
Mul(bigAmount, destinationDecimals).
148+
Div(bigAmount, sourceDecimals))
121149

122150
result := []byte{'m'}
123-
var newAmountBytes [32]byte
151+
newAmountBytes := make([]byte, 32)
124152
var RequestIdBytes [32]byte
125-
153+
binary.BigEndian.PutUint64(newAmountBytes, uint64(newAmount))
126154
result = append(result, rqId.FillBytes(RequestIdBytes[:])...)
127-
result = append(result, newAmount.FillBytes(newAmountBytes[:])...)
128-
receiver, _ := hex.DecodeString(rq.Receiver)
155+
result = append(result, newAmountBytes[:]...)
156+
receiver := []byte(rq.Receiver)
129157
result = append(result, receiver[:]...)
130158

131-
println(newAmount.String())
159+
println(newAmount)
132160
println(base64.StdEncoding.EncodeToString(result))
133161
return &extractors.Data{
134162
Type: extractors.Base64,
@@ -151,29 +179,33 @@ func (provider *ErgoToErgoExtractionBridge) ExtractReverseTransferRequest(ctx co
151179
rqId := new(big.Int)
152180
rqId, _ = rqId.SetString(rq.RequestId, 10)
153181

154-
amount := new(big.Int)
155-
amount, _ = amount.SetString(rq.Amount, 10)
182+
//amount := new(big.Int)
183+
amount, err := strconv.ParseInt(rq.Amount, 10, 64)
184+
bigAmount := big.NewInt(amount)
185+
//amount, _ = amount.SetString(rq.Amount, 10)
156186

157187
sourceDecimals := big.NewInt(10)
158188
sourceDecimals.Exp(sourceDecimals, big.NewInt(provider.config.SourceDecimals), nil)
159-
189+
//sourceDecimals := math.Pow(10, float64(provider.config.DestinationDecimals))
160190
destinationDecimals := big.NewInt(10)
161191
destinationDecimals.Exp(destinationDecimals, big.NewInt(provider.config.DestinationDecimals), nil)
192+
//destinationDecimals := math.Pow(10, float64(provider.config.DestinationDecimals))
162193

163-
newAmount := amount.
164-
Mul(amount, sourceDecimals).
165-
Div(amount, destinationDecimals)
194+
//newAmount := int64(float64(amount) * sourceDecimals / destinationDecimals)
195+
newAmount := cast.ToInt64(bigAmount.
196+
Mul(bigAmount, sourceDecimals).
197+
Div(bigAmount, destinationDecimals))
166198

167-
result := []byte{'u'}
168-
var newAmountBytes [32]byte
199+
result := []byte{'m'}
200+
newAmountBytes := make([]byte, 32)
169201
var RequestIdBytes [32]byte
170-
202+
binary.BigEndian.PutUint64(newAmountBytes, uint64(newAmount))
171203
result = append(result, rqId.FillBytes(RequestIdBytes[:])...)
172-
result = append(result, newAmount.FillBytes(newAmountBytes[:])...)
173-
receiver, _ := hex.DecodeString(rq.Receiver)
204+
result = append(result, newAmountBytes[:]...)
205+
receiver := []byte(rq.Receiver)
174206
result = append(result, receiver[:]...)
175207

176-
println(newAmount.String())
208+
fmt.Println(newAmount)
177209
println(base64.StdEncoding.EncodeToString(result))
178210
return &extractors.Data{
179211
Type: extractors.Base64,

Diff for: ‎go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ require (
1414
github.com/rs/cors v1.7.0 // indirect
1515
github.com/wavesplatform/go-lib-crypto v0.0.0-20190905125804-474f21517ad5
1616
github.com/wavesplatform/gowaves v0.7.0
17+
go.uber.org/zap v1.13.0 // indirect
1718
)

Diff for: ‎helpers/ergo.go

+13-15
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type ErgOptions struct {
2727

2828
var DefaultOptions = ErgOptions{
2929
BaseUrl: "http://176.9.65.58:9016",
30-
Doer: &http.Client{Timeout: 3 * time.Second},
30+
Doer: &http.Client{Timeout: 30 * time.Second},
3131
}
3232

3333
type ErgClient struct {
@@ -141,12 +141,11 @@ func ValidateErgoAddress(address string) (bool, error) {
141141
IsValid bool `json:"isValid"`
142142
}
143143

144-
url := fmt.Sprintf("%s/%s", DefaultOptions.BaseUrl, "/adaptor/validateAddress")
144+
url := fmt.Sprintf("%s/%s", DefaultOptions.BaseUrl, "adaptor/validateAddress")
145145

146146
values := map[string]string{"address": address}
147147
jsonValue, _ := json.Marshal(values)
148148
res, err := http.Post(url, "application/json", bytes.NewBuffer(jsonValue))
149-
150149
if err != nil {
151150
return false, err
152151
}
@@ -159,14 +158,14 @@ func ValidateErgoAddress(address string) (bool, error) {
159158
if err != nil {
160159
return false, err
161160
}
162-
163161
if !responseObject.Success {
164162
err = fmt.Errorf("proxy connection problem")
165163
return false, err
166164
}
167165
if err != nil {
168166
return false, err
169167
}
168+
170169
return responseObject.IsValid, nil
171170
}
172171

@@ -197,32 +196,31 @@ func (client *ErgClient) GetRequestsList(address string, route string, ctx conte
197196
return out.Requests, nil
198197
}
199198

200-
func (client *ErgClient) GetRequest(address string, route string, requestId string) (Request ,bool , error) {
199+
func (client *ErgClient) GetAllRequest(address string, route string) ([]Request, error) {
201200
type Result struct {
202201
Success bool `json:"success"`
203-
Request Request `json:"request"`
202+
Requests []Request `json:"requests"`
204203
}
205204

206-
url := fmt.Sprintf("%s/%s/getRequest", address, route)
205+
url := fmt.Sprintf("%s/%s/getRequestsList", address, route)
207206

208-
jsonValue, _ := json.Marshal(map[string]string{"requestId": requestId})
209-
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonValue))
207+
req, err := http.NewRequest("GET", url, nil)
210208
if err != nil {
211-
return Request{}, false, err
209+
return nil, err
212210
}
213211

214212
var out Result
215213
_, err = client.Do(req, &out)
216214
if err != nil {
217-
return Request{}, false, err
215+
return nil, err
218216
}
219217
if !out.Success{
220-
return Request{}, false, err
218+
return nil, err
221219
}
222-
if out.Request.RequestId == "" {
223-
return Request{}, false, err
220+
if len(out.Requests) == 0 {
221+
return nil, nil
224222
}
225-
return out.Request, true, nil
223+
return out.Requests, nil
226224
}
227225

228226
func (a *ErgClient) Do(req *http.Request, v interface{}) (*Response, error) {

0 commit comments

Comments
 (0)
Please sign in to comment.