@@ -3,11 +3,13 @@ package bridge
3
3
import (
4
4
"context"
5
5
"encoding/base64"
6
- "encoding/hex "
6
+ "encoding/binary "
7
7
"fmt"
8
8
"github.com/Gravity-Tech/gravity-node-data-extractor/v2/extractors"
9
9
"github.com/Gravity-Tech/gravity-node-data-extractor/v2/helpers"
10
+ "github.com/spf13/cast"
10
11
"math/big"
12
+ "strconv"
11
13
)
12
14
13
15
// TODO: Implement general queue iterator (Waves & ETH)
@@ -52,48 +54,70 @@ func (provider *ErgoToErgoExtractionBridge) Configure(config ConfigureCommand) e
52
54
53
55
func (provider * ErgoToErgoExtractionBridge ) pickRequestFromLUPortQueue (luRequests []helpers.Request ) (helpers.Request , error ) {
54
56
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
+ }
55
61
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
+ }
60
69
}
61
-
62
- if status == ErgoRequestStatusSuccess {
70
+ return flag
71
+ }
72
+ for _ , luRequest := range luRequests {
73
+ if isExist (luRequest ) {
63
74
continue
64
75
}
65
- if ! ValidateErgoAddress (luRequest .Receiver ) {
76
+ isValid := ValidateErgoAddress (luRequest .Receiver )
77
+ if ! isValid {
66
78
continue
67
79
}
68
80
rq = luRequest
69
81
break
70
82
}
83
+
71
84
return rq , nil
72
85
}
73
86
74
87
func (provider * ErgoToErgoExtractionBridge ) pickRequestFromIBPortQueue (ibRequests []helpers.Request ) (helpers.Request , error ) {
75
88
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
+ }
76
93
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
+ }
81
101
}
82
- if status != ErgoRequestStatusSuccess {
102
+ return flag
103
+ }
104
+ for _ , ibRequest := range ibRequests {
105
+ if isExist (ibRequest ) {
83
106
continue
84
107
}
85
- if ! ValidateErgoAddress (request .Receiver ) {
108
+ isValid := ValidateErgoAddress (ibRequest .Receiver )
109
+ if ! isValid {
86
110
continue
87
111
}
88
- rq = request
112
+ rq = ibRequest
89
113
break
90
114
}
115
+
91
116
return rq , nil
92
117
}
93
118
94
-
95
119
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 )
97
121
if err != nil {
98
122
return nil , err
99
123
}
@@ -106,29 +130,33 @@ func (provider *ErgoToErgoExtractionBridge) ExtractDirectTransferRequest(ctx con
106
130
rqId := new (big.Int )
107
131
rqId , _ = rqId .SetString (rq .RequestId , 10 )
108
132
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)
111
137
112
138
sourceDecimals := big .NewInt (10 )
113
139
sourceDecimals .Exp (sourceDecimals , big .NewInt (provider .config .SourceDecimals ), nil )
114
-
140
+ //sourceDecimals := math.Pow(10, float64(provider.config.DestinationDecimals))
115
141
destinationDecimals := big .NewInt (10 )
116
142
destinationDecimals .Exp (destinationDecimals , big .NewInt (provider .config .DestinationDecimals ), nil )
143
+ //destinationDecimals := math.Pow(10, float64(provider.config.DestinationDecimals))
117
144
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 ))
121
149
122
150
result := []byte {'m' }
123
- var newAmountBytes [ 32 ]byte
151
+ newAmountBytes := make ([ ]byte , 32 )
124
152
var RequestIdBytes [32 ]byte
125
-
153
+ binary . BigEndian . PutUint64 ( newAmountBytes , uint64 ( newAmount ))
126
154
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 )
129
157
result = append (result , receiver [:]... )
130
158
131
- println (newAmount . String () )
159
+ println (newAmount )
132
160
println (base64 .StdEncoding .EncodeToString (result ))
133
161
return & extractors.Data {
134
162
Type : extractors .Base64 ,
@@ -151,29 +179,33 @@ func (provider *ErgoToErgoExtractionBridge) ExtractReverseTransferRequest(ctx co
151
179
rqId := new (big.Int )
152
180
rqId , _ = rqId .SetString (rq .RequestId , 10 )
153
181
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)
156
186
157
187
sourceDecimals := big .NewInt (10 )
158
188
sourceDecimals .Exp (sourceDecimals , big .NewInt (provider .config .SourceDecimals ), nil )
159
-
189
+ //sourceDecimals := math.Pow(10, float64(provider.config.DestinationDecimals))
160
190
destinationDecimals := big .NewInt (10 )
161
191
destinationDecimals .Exp (destinationDecimals , big .NewInt (provider .config .DestinationDecimals ), nil )
192
+ //destinationDecimals := math.Pow(10, float64(provider.config.DestinationDecimals))
162
193
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 ))
166
198
167
- result := []byte {'u ' }
168
- var newAmountBytes [ 32 ]byte
199
+ result := []byte {'m ' }
200
+ newAmountBytes := make ([ ]byte , 32 )
169
201
var RequestIdBytes [32 ]byte
170
-
202
+ binary . BigEndian . PutUint64 ( newAmountBytes , uint64 ( newAmount ))
171
203
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 )
174
206
result = append (result , receiver [:]... )
175
207
176
- println ( newAmount . String () )
208
+ fmt . Println ( newAmount )
177
209
println (base64 .StdEncoding .EncodeToString (result ))
178
210
return & extractors.Data {
179
211
Type : extractors .Base64 ,
0 commit comments