Skip to content

Commit

Permalink
fix: gracefully wait for coin transfer (#43)
Browse files Browse the repository at this point in the history
* Wait for send coins tx

* Fix waiting
  • Loading branch information
zale144 authored Sep 21, 2024
1 parent f5b9249 commit f513b3f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
45 changes: 43 additions & 2 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"fmt"
"slices"
"strings"
"time"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/google/uuid"
Expand Down Expand Up @@ -209,9 +211,48 @@ func (a *accountService) sendCoins(coins sdk.Coins, toAddrStr string) error {
toAddr,
coins,
)
start := time.Now()

_, err = a.client.BroadcastTx(a.accountName, msg)
return err
rsp, err := a.client.BroadcastTx(a.accountName, msg)
if err != nil {
return fmt.Errorf("failed to broadcast tx: %w", err)
}

if err = a.WaitForTx(rsp.TxHash); err != nil {
return fmt.Errorf("failed to wait for tx: %w", err)
}

a.logger.Debug("coins sent", zap.String("to", toAddrStr), zap.Duration("duration", time.Since(start)))

return nil
}

func (a *accountService) WaitForTx(txHash string) error {
serviceClient := tx.NewServiceClient(a.client.Context())
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
ticker := time.NewTicker(time.Second)

defer func() {
cancel()
ticker.Stop()
}()

for {
select {
case <-ctx.Done():
return fmt.Errorf("timed out waiting for tx %s", txHash)
case <-ticker.C:
resp, err := serviceClient.GetTx(ctx, &tx.GetTxRequest{Hash: txHash})
if err != nil {
continue
}
if resp.TxResponse.Code == 0 {
return nil
} else {
return fmt.Errorf("tx failed with code %d: %s", resp.TxResponse.Code, resp.TxResponse.RawLog)
}
}
}
}

func (a *accountService) balanceOf(denom string) sdk.Int {
Expand Down
3 changes: 0 additions & 3 deletions order_fulfiller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"slices"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dymensionxyz/cosmosclient/cosmosclient"
Expand Down Expand Up @@ -145,8 +144,6 @@ outer:
return nil
}

time.Sleep(7 * time.Second)

ol.logger.Info("fulfilling orders", zap.Int("count", len(ids)))

if err := ol.fulfillDemandOrders(demandOrders...); err != nil {
Expand Down
14 changes: 11 additions & 3 deletions order_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,17 @@ func (or *orderTracker) addFulfilledOrders(ctx context.Context, batch *orderBatc
}

func (or *orderTracker) canFulfillOrder(order *demandOrder) bool {
return !or.isOrderFulfilled(order.id) &&
!or.isOrderCurrent(order.id) &&
or.checkFeePercentage(order)
if or.isOrderFulfilled(order.id) {
return false
}
if or.isOrderCurrent(order.id) {
return false
}
if !or.checkFeePercentage(order) {
return false
}

return true
}

func (or *orderTracker) checkFeePercentage(order *demandOrder) bool {
Expand Down

0 comments on commit f513b3f

Please sign in to comment.