|
| 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 | +} |
0 commit comments