Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: gracefully wait for coin transfer #43

Merged
merged 2 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading