Skip to content

Commit

Permalink
Fix: tezos
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Jan 19, 2022
1 parent 86da0ac commit ececf15
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 112 deletions.
2 changes: 1 addition & 1 deletion cmd/watch_tower/watch_tower.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (wt *WatchTower) checkRefundTime(ctx context.Context) {
}

func (wt *WatchTower) redeem(ctx context.Context, swap *Swap) error {
if leg := swap.Leg(); leg != nil {
if leg := swap.Leg(); leg != nil && swap.RefundTime.UTC().After(time.Now().UTC()) {
swap.RetryCount++
return wt.tracker.Redeem(ctx, swap.Swap, *leg)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/btcsuite/btcd v0.22.0-beta // indirect
github.com/deckarep/golang-set v1.7.1 // indirect
github.com/dipdup-net/go-lib v0.1.52
github.com/dipdup-net/go-lib v0.1.54
github.com/ebellocchia/go-base58 v0.1.0
github.com/ethereum/go-ethereum v1.10.12
github.com/go-playground/validator/v10 v10.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ github.com/deepmap/oapi-codegen v1.8.2/go.mod h1:YLgSKSDv/bZQB7N4ws6luhozi3cEdRk
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-bitstream v0.0.0-20180413035011-3522498ce2c8/go.mod h1:VMaSuZ+SZcx/wljOQKvp5srsbCiKDEb6K2wC4+PiBmQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/dipdup-net/go-lib v0.1.52 h1:1WrO/tfIdJCDW4a05KuO2vlnILJyOWkDoiDLtvpSHFw=
github.com/dipdup-net/go-lib v0.1.52/go.mod h1:ZOAyo2sqNLCCVdvHvPbiWfgImvmrMW7/sdQiSsGrD44=
github.com/dipdup-net/go-lib v0.1.54 h1:cNrTN+3OakHZTrCb1QpLVB9wpva/ldxwsoCqV6rTMb0=
github.com/dipdup-net/go-lib v0.1.54/go.mod h1:ZOAyo2sqNLCCVdvHvPbiWfgImvmrMW7/sdQiSsGrD44=
github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dlclark/regexp2 v1.4.1-0.20201116162257-a2a8dda75c91/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/docker/docker v1.4.2-0.20180625184442-8e610b2b55bf/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
Expand Down
5 changes: 3 additions & 2 deletions internal/chain/tezos/atomex_tez/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package atomextez

import (
"context"
"encoding/hex"
"fmt"
"log"
"strconv"
Expand Down Expand Up @@ -167,8 +168,8 @@ func (contract *Atomextez) listen(ctx context.Context) {
for i := range items {
switch items[i].Path {
case "big_map":
var key KeyBigMap
if err := json.Unmarshal([]byte(items[i].Content.Key), &key); err != nil {
key, err := hex.DecodeString(items[i].Content.Key)
if err != nil {
log.Println(err)
continue
}
Expand Down
5 changes: 3 additions & 2 deletions internal/chain/tezos/atomex_tez_token/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"log"
"encoding/hex"
"strconv"
"sync"

Expand Down Expand Up @@ -152,8 +153,8 @@ func (contract *Atomexteztoken) listen(ctx context.Context) {
for i := range items {
switch items[i].Path {
case "0":
var key Key0
if err := json.Unmarshal([]byte(items[i].Content.Key), &key); err != nil {
key, err := hex.DecodeString(items[i].Content.Key)
if err != nil {
log.Println(err)
continue
}
Expand Down
135 changes: 31 additions & 104 deletions internal/chain/tezos/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func (t *Tezos) Redeem(ctx context.Context, hashedSecret, secret chain.Hex, cont
Amount: "0",
StorageLimit: operationParams.StorageLimit.Redeem,
GasLimit: operationParams.GasLimit.Redeem,
Fee: "1000",
Fee: "3000",
Destination: contract,
Parameters: &node.Parameters{
Entrypoint: "redeem",
Expand Down Expand Up @@ -398,7 +398,7 @@ func (t *Tezos) Refund(ctx context.Context, hashedSecret chain.Hex, contract str
Amount: "0",
StorageLimit: operationParams.StorageLimit.Refund,
GasLimit: operationParams.GasLimit.Refund,
Fee: "1000",
Fee: "10000",
Destination: contract,
Parameters: &node.Parameters{
Entrypoint: "refund",
Expand Down Expand Up @@ -458,10 +458,6 @@ func (t *Tezos) restoreFromBigMap(ctx context.Context, bm api.BigMap) error {
}

for i := range keys {
if err := t.handleBigMapKey(keys[i], bm.Contract.Address); err != nil {
return err
}

if err := t.restoreFinilizationSwap(ctx, bm, keys[i]); err != nil {
return err
}
Expand All @@ -474,49 +470,50 @@ func (t *Tezos) restoreFromBigMap(ctx context.Context, bm api.BigMap) error {
}

func (t *Tezos) restoreFinilizationSwap(ctx context.Context, bm api.BigMap, key api.BigMapKey) error {
updates, err := t.api.GetBigmapKeyUpdates(ctx, uint64(bm.Ptr), key.Key, map[string]string{
"sort.desc": "id",
})
updates, err := t.api.GetBigmapKeyUpdates(ctx, uint64(bm.Ptr), key.Key, nil)
if err != nil {
return err
}
switch len(updates) {
case 2:

for i := range updates {
if t.cfg.Contract == bm.Contract.Address {
var bmUpdate atomextez.BigMapUpdate
if err := json.Unmarshal(updates[0].Value, &bmUpdate.BigMap.Value); err != nil {
if err := json.Unmarshal(updates[i].Value, &bmUpdate.BigMap.Value); err != nil {
return err
}
bmUpdate.BigMap.Key = atomextez.KeyBigMap(key.Key)
bmUpdate.BigMap.Ptr = &bm.Ptr
bmUpdate.Action = updates[0].Action
bmUpdate.Action = updates[i].Action
bmUpdate.Contract = bm.Contract.Address
bmUpdate.Level = updates[0].Level
return t.parseTezosContractUpdate(ctx, bmUpdate)
bmUpdate.Level = updates[i].Level
if err := t.parseTezosContractUpdate(ctx, bmUpdate); err != nil {
return err
}
} else {
var bmUpdate atomexteztoken.BigMap0Update
if err := json.Unmarshal(updates[0].Value, &bmUpdate.BigMap0.Value); err != nil {
if err := json.Unmarshal(updates[i].Value, &bmUpdate.BigMap0.Value); err != nil {
return err
}
bmUpdate.BigMap0.Key = atomexteztoken.Key0(key.Key)
bmUpdate.BigMap0.Ptr = &bm.Ptr
bmUpdate.Action = updates[0].Action
bmUpdate.Action = updates[i].Action
bmUpdate.Contract = bm.Contract.Address
bmUpdate.Level = updates[0].Level
return t.parseTokenContractUpdate(ctx, bmUpdate)
bmUpdate.Level = updates[i].Level
if err := t.parseTokenContractUpdate(ctx, bmUpdate); err != nil {
return err
}
}

default:
return nil
}
return nil
}

func (t *Tezos) parseTezosContractUpdate(ctx context.Context, update atomextez.BigMapUpdate) error {
hashedSecret := chain.NewHexFromBytes(update.BigMap.Key)

switch update.Action {
case BigMapActionAddKey:
event := chain.InitEvent{
HashedSecretHex: chain.Hex(update.BigMap.Key),
HashedSecretHex: hashedSecret,
Chain: chain.ChainTypeTezos,
ContractAddress: update.Contract,
BlockNumber: uint64(update.Level),
Expand Down Expand Up @@ -559,7 +556,7 @@ func (t *Tezos) parseTezosContractUpdate(ctx context.Context, update atomextez.B
}

t.events <- chain.RedeemEvent{
HashedSecretHex: chain.Hex(update.BigMap.Key),
HashedSecretHex: hashedSecret,
Chain: chain.ChainTypeTezos,
ContractAddress: update.Contract,
BlockNumber: update.Level,
Expand All @@ -568,7 +565,7 @@ func (t *Tezos) parseTezosContractUpdate(ctx context.Context, update atomextez.B
return nil
case atomextez.EntrypointRefund:
t.events <- chain.RefundEvent{
HashedSecretHex: chain.Hex(update.BigMap.Key),
HashedSecretHex: hashedSecret,
Chain: chain.ChainTypeTezos,
ContractAddress: update.Contract,
BlockNumber: update.Level,
Expand All @@ -582,10 +579,12 @@ func (t *Tezos) parseTezosContractUpdate(ctx context.Context, update atomextez.B
}

func (t *Tezos) parseTokenContractUpdate(ctx context.Context, update atomexteztoken.BigMap0Update) error {
hashedSecret := chain.NewHexFromBytes(update.BigMap0.Key)

switch update.Action {
case BigMapActionAddKey:
event := chain.InitEvent{
HashedSecretHex: chain.Hex(update.BigMap0.Key),
HashedSecretHex: hashedSecret,
Chain: chain.ChainTypeTezos,
ContractAddress: update.Contract,
BlockNumber: update.Level,
Expand Down Expand Up @@ -628,7 +627,7 @@ func (t *Tezos) parseTokenContractUpdate(ctx context.Context, update atomextezto
}

t.events <- chain.RedeemEvent{
HashedSecretHex: chain.Hex(update.BigMap0.Key),
HashedSecretHex: hashedSecret,
Chain: chain.ChainTypeTezos,
ContractAddress: update.Contract,
BlockNumber: update.Level,
Expand All @@ -637,7 +636,7 @@ func (t *Tezos) parseTokenContractUpdate(ctx context.Context, update atomextezto
return nil
case atomexteztoken.EntrypointRefund:
t.events <- chain.RefundEvent{
HashedSecretHex: chain.Hex(update.BigMap0.Key),
HashedSecretHex: hashedSecret,
Chain: chain.ChainTypeTezos,
ContractAddress: update.Contract,
BlockNumber: update.Level,
Expand All @@ -650,84 +649,17 @@ func (t *Tezos) parseTokenContractUpdate(ctx context.Context, update atomextezto
return nil
}

func (t *Tezos) handleBigMapKey(key api.BigMapKey, contract string) error {
switch contract {
case t.cfg.Contract:
return t.parseContractValueKeys(key, contract)
default:
return t.parseTokensValueKeys(key, contract)
}
}

func (t *Tezos) parseContractValueKeys(key api.BigMapKey, contract string) error {
var value atomextez.ValueBigMap
if err := json.Unmarshal(key.Value, &value); err != nil {
return err
}

event := chain.InitEvent{
HashedSecretHex: chain.Hex(key.Key),
Chain: chain.ChainTypeTezos,
ContractAddress: contract,
BlockNumber: uint64(key.FirstLevel),
Initiator: string(value.Recipients.Initiator),
Participant: string(value.Recipients.Participant),
RefundTime: value.Settings.RefundTime.Value(),
Amount: decimal.NewFromBigInt(value.Settings.Amount.Int, 0),
}

if err := event.SetPayOff(value.Settings.Payoff.Int, t.minPayoff); err != nil {
if errors.Is(err, chain.ErrMinPayoff) {
t.log.Warn().Str("hashed_secret", event.HashedSecretHex.String()).Msg("skip because of small pay off")
return nil
}
return err
}

t.events <- event
return nil
}

func (t *Tezos) parseTokensValueKeys(key api.BigMapKey, contract string) error {
var value atomexteztoken.Value0
if err := json.Unmarshal(key.Value, &value); err != nil {
return err
}

event := chain.InitEvent{
HashedSecretHex: chain.Hex(key.Key),
Chain: chain.ChainTypeTezos,
ContractAddress: contract,
BlockNumber: uint64(key.FirstLevel),
Initiator: string(value.Initiator),
Participant: string(value.Participant),
RefundTime: value.RefundTime.Value(),
Amount: decimal.NewFromBigInt(value.TotalAmount.Int, 0),
}

if err := event.SetPayOff(value.PayoffAmount.Int, t.minPayoff); err != nil {
if errors.Is(err, chain.ErrMinPayoff) {
t.log.Warn().Str("hashed_secret", event.HashedSecretHex.String()).Msg("skip because of small pay off")
return nil
}
return err
}

t.events <- event
return nil
}

func (t *Tezos) sendTransaction(ctx context.Context, transaction node.Transaction) (string, error) {
atomic.AddInt64(&t.counter, 1)
transaction.Counter = fmt.Sprintf("%d", t.counter)

headerCtx, headerCancel := context.WithTimeout(ctx, 10*time.Second)
defer headerCancel()
header, err := t.rpc.Header(fmt.Sprintf("head~%s", t.ttl), node.WithContext(headerCtx))
if err != nil {
return "", err
}

atomic.AddInt64(&t.counter, 1)
transaction.Counter = fmt.Sprintf("%d", t.counter)

encoded, err := forge.OPG(header.Hash, node.Operation{
Body: transaction,
Kind: node.KindTransaction,
Expand All @@ -744,13 +676,8 @@ func (t *Tezos) sendTransaction(ctx context.Context, transaction node.Transactio

injectCtx, injectCancel := context.WithTimeout(ctx, 10*time.Second)
defer injectCancel()
opHash, err := t.rpc.InjectOperaiton(node.InjectOperationRequest{
return t.rpc.InjectOperaiton(node.InjectOperationRequest{
Operation: signature.AppendToHex(msg),
ChainID: header.ChainID,
}, node.WithContext(injectCtx))
if err != nil {
return "", errors.Wrap(err, "InjectOperaiton")
}

return opHash, nil
}
2 changes: 2 additions & 0 deletions internal/chain/tools/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"sync"
"sync/atomic"
"time"

"github.com/atomex-protocol/watch_tower/internal/chain"
"github.com/atomex-protocol/watch_tower/internal/chain/ethereum"
Expand Down Expand Up @@ -178,6 +179,7 @@ func (t *Tracker) onEvent(event chain.Event) {
case chain.RestoredEvent:
t.logger.Info().Str("blockchain", e.Chain.String()).Msg("restored")
atomic.AddInt32(&t.restoreCounter, 1)
time.Sleep(2 * time.Second)

if t.restoreCounter == chainsCount {
for id := range t.swaps {
Expand Down

0 comments on commit ececf15

Please sign in to comment.