Skip to content

Commit 3b6572b

Browse files
committed
Merge branch 'erg2erg' into 'dev'
erg2erg See merge request gateway/gravity-node-data-extractor!1
2 parents 1ea2590 + 06ef26d commit 3b6572b

File tree

8 files changed

+684
-127
lines changed

8 files changed

+684
-127
lines changed

erg-erg.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"SourceNodeURL": "http://176.9.65.58:9016/",
3+
"SourceDecimals": 6
4+
"DestinationDecimals": 6,
5+
"DestinationNodeURL": "http://176.9.65.58:9016/",
6+
"IBPortAddress": "ibport",
7+
"LUPortAddress": "luport"
8+
}

extractors/susy/bridge/common.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package bridge
33
import (
44
"context"
55
"github.com/Gravity-Tech/gravity-node-data-extractor/v2/extractors"
6+
"github.com/Gravity-Tech/gravity-node-data-extractor/v2/helpers"
67
"github.com/ethereum/go-ethereum/common"
7-
wavescrypto "github.com/wavesplatform/go-lib-crypto"
88
"github.com/mr-tron/base58"
9+
wavescrypto "github.com/wavesplatform/go-lib-crypto"
910
"math/big"
1011
)
1112

@@ -80,4 +81,9 @@ func ValidateEthereumBasedAddress(address string) bool {
8081
func ValidateWavesAddress(address string, chainId byte) bool {
8182
instance := wavescrypto.NewWavesCrypto()
8283
return instance.VerifyAddress(wavescrypto.Address(address), wavescrypto.WavesChainID(chainId))
83-
}
84+
}
85+
86+
func ValidateErgoAddress(address string) bool {
87+
isValid , _ := helpers.CheckAddress(address)
88+
return isValid
89+
}

extractors/susy/bridge/ergo.go

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package bridge
2+
3+
import (
4+
"context"
5+
"encoding/base64"
6+
"fmt"
7+
"strconv"
8+
9+
"github.com/Gravity-Tech/gravity-node-data-extractor/v2/extractors"
10+
"github.com/Gravity-Tech/gravity-node-data-extractor/v2/helpers"
11+
"math/big"
12+
)
13+
14+
// TODO: Implement general queue iterator (Waves & ETH)
15+
// bc too muchs queues
16+
type ErgoRequestsState struct {
17+
requests map[string]interface{}
18+
}
19+
20+
type ErgoToErgoExtractionBridge struct {
21+
config ConfigureCommand
22+
configured bool
23+
24+
ergClientSource *helpers.ErgClient
25+
ergClientTarget *helpers.ErgClient
26+
}
27+
28+
func (provider *ErgoToErgoExtractionBridge) Configure(config ConfigureCommand) error {
29+
if provider.configured {
30+
return fmt.Errorf("bridge is configured already")
31+
}
32+
33+
provider.config = config
34+
35+
// Node clients instantiation
36+
var err error
37+
provider.ergClientSource, err = helpers.NewClient(helpers.ErgOptions{BaseUrl: config.SourceNodeUrl})
38+
if err != nil {
39+
return err
40+
}
41+
provider.ergClientTarget, err = helpers.NewClient(helpers.ErgOptions{BaseUrl: config.SourceNodeUrl})
42+
if err != nil {
43+
return err
44+
}
45+
provider.configured = true
46+
47+
return nil
48+
}
49+
50+
func (provider *ErgoToErgoExtractionBridge) pickRequestFromQueue(luStates []helpers.Request) (helpers.Request, error) {
51+
52+
ctx, cancel := context.WithCancel(context.Background())
53+
defer cancel()
54+
55+
ibStates, _ := provider.ergClientTarget.GetState(provider.config.IBPortAddress, ctx)
56+
57+
var rq helpers.Request
58+
59+
for _, luState := range luStates {
60+
61+
for _, ibState := range ibStates {
62+
if ibState.RequestId != luState.RequestId {
63+
continue
64+
}
65+
if !ValidateErgoAddress(luState.Receiver) {
66+
continue
67+
}
68+
rq = luState
69+
break
70+
}
71+
}
72+
73+
return rq, nil
74+
}
75+
76+
// Decoupling is aimed for tests management
77+
// It allows testing distinct functions
78+
//
79+
func (provider *ErgoToErgoExtractionBridge) ExtractDirectTransferRequest(ctx context.Context) (*extractors.Data, error) {
80+
luStates, err := provider.ergClientSource.GetState(provider.config.LUPortAddress, ctx)
81+
if err != nil {
82+
return nil, err
83+
}
84+
85+
rq, _ := provider.pickRequestFromQueue(luStates)
86+
87+
if rq.RequestId == "" {
88+
return nil, extractors.NotFoundErr
89+
}
90+
91+
rqInt, _ := strconv.ParseInt(rq.RequestId, 10, 64)
92+
rqBigInt := big.NewInt(rqInt)
93+
amount, _ := strconv.ParseInt(rq.Amount, 10, 64)
94+
95+
sourceDecimals := big.NewInt(10)
96+
sourceDecimals.Exp(sourceDecimals, big.NewInt(provider.config.SourceDecimals), nil)
97+
98+
destinationDecimals := big.NewInt(10)
99+
destinationDecimals.Exp(destinationDecimals, big.NewInt(provider.config.DestinationDecimals), nil)
100+
101+
bigIntAmount := big.NewInt(amount)
102+
103+
newAmount := bigIntAmount.
104+
Mul(bigIntAmount, sourceDecimals).
105+
Div(bigIntAmount, destinationDecimals)
106+
107+
result := []byte{'m'}
108+
var newAmountBytes [32]byte
109+
var RequestIdBytes [32]byte
110+
result = append(result, rqBigInt.FillBytes(RequestIdBytes[:])...)
111+
result = append(result, newAmount.FillBytes(newAmountBytes[:])...)
112+
result = append(result, []byte(rq.Receiver)...)
113+
114+
println(newAmount.String())
115+
println(base64.StdEncoding.EncodeToString(result))
116+
return &extractors.Data{
117+
Type: extractors.Base64,
118+
Value: base64.StdEncoding.EncodeToString(result),
119+
}, err
120+
}
121+
122+
func (provider *ErgoToErgoExtractionBridge) ExtractReverseTransferRequest(ctx context.Context) (*extractors.Data, error) {
123+
ibStates, err := provider.ergClientSource.GetState(provider.config.IBPortAddress, ctx)
124+
if err != nil {
125+
return nil, err
126+
}
127+
128+
rq, _ := provider.pickRequestFromQueue(ibStates)
129+
130+
if rq.RequestId == "" {
131+
return nil, extractors.NotFoundErr
132+
}
133+
134+
rqInt, _ := strconv.ParseInt(rq.RequestId, 10, 64)
135+
rqBigInt := big.NewInt(rqInt)
136+
amount, _ := strconv.ParseInt(rq.Amount, 10, 64)
137+
138+
sourceDecimals := big.NewInt(10)
139+
sourceDecimals.Exp(sourceDecimals, big.NewInt(provider.config.SourceDecimals), nil)
140+
141+
destinationDecimals := big.NewInt(10)
142+
destinationDecimals.Exp(destinationDecimals, big.NewInt(provider.config.DestinationDecimals), nil)
143+
144+
bigIntAmount := big.NewInt(amount)
145+
146+
newAmount := bigIntAmount.
147+
Mul(bigIntAmount, sourceDecimals).
148+
Div(bigIntAmount, destinationDecimals)
149+
150+
result := []byte{'u'}
151+
var newAmountBytes [32]byte
152+
var RequestIdBytes [32]byte
153+
result = append(result, rqBigInt.FillBytes(RequestIdBytes[:])...)
154+
result = append(result, newAmount.FillBytes(newAmountBytes[:])...)
155+
result = append(result, []byte(rq.Receiver)...)
156+
157+
println(newAmount.String())
158+
println(base64.StdEncoding.EncodeToString(result))
159+
return &extractors.Data{
160+
Type: extractors.Base64,
161+
Value: base64.StdEncoding.EncodeToString(result),
162+
}, err
163+
}

extractors/susy/common.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const (
1212
WavesToEthReverse extractors.ExtractorType = "waves-based-to-eth-reverse"
1313
EthToWavesDirect extractors.ExtractorType = "eth-based-to-waves-direct"
1414
EthToWavesReverse extractors.ExtractorType = "eth-based-to-waves-reverse"
15+
ErgToErgDirect extractors.ExtractorType = "erg-based-to-erg-direct"
16+
ErgToErgReverse extractors.ExtractorType = "erg-based-to-erg-reverse"
1517
)
1618

1719
type ExtractionProvider interface {
@@ -44,6 +46,9 @@ func New(sourceNodeUrl string, destinationNodeUrl string,
4446
case EthToWavesDirect, EthToWavesReverse: {
4547
delegate = &bridge.EthereumToWavesExtractionBridge{}
4648
}
49+
case ErgToErgDirect, ErgToErgReverse: {
50+
delegate = &bridge.ErgoToErgoExtractionBridge{}
51+
}
4752
}
4853

4954
if delegate == nil {
@@ -72,9 +77,9 @@ func (e *SourceExtractor) Info() *extractors.ExtractorInfo {
7277

7378
func (e *SourceExtractor) Extract(ctx context.Context) (*extractors.Data, error) {
7479
switch e.kind {
75-
case WavesToEthDirect, EthToWavesDirect:
80+
case WavesToEthDirect, EthToWavesDirect, ErgToErgDirect:
7681
return e.delegate.ExtractDirectTransferRequest(ctx)
77-
case WavesToEthReverse, EthToWavesReverse:
82+
case WavesToEthReverse, EthToWavesReverse, ErgToErgReverse:
7883
return e.delegate.ExtractReverseTransferRequest(ctx)
7984
}
8085

go.mod

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ go 1.14
44

55
require (
66
github.com/Gravity-Tech/gateway v0.0.0-20210307122541-e53fafdff20b
7-
github.com/Gravity-Tech/gravity-core v1.0.2-0.20201021214847-79e765399c5e // indirect
87
github.com/ethereum/go-ethereum v1.9.23
8+
github.com/go-kit/kit v0.10.0 // indirect
9+
github.com/gorilla/websocket v1.4.2 // indirect
10+
github.com/kr/pretty v0.2.0 // indirect
911
github.com/mr-tron/base58 v1.1.2
12+
github.com/pkg/errors v0.9.1 // indirect
13+
github.com/prometheus/tsdb v0.7.1 // indirect
14+
github.com/rs/cors v1.7.0 // indirect
1015
github.com/wavesplatform/go-lib-crypto v0.0.0-20190905125804-474f21517ad5
1116
github.com/wavesplatform/gowaves v0.7.0
1217
)

0 commit comments

Comments
 (0)