Skip to content

Commit

Permalink
reduce diff
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph committed Jun 14, 2024
1 parent 00bfda8 commit 5e5cca4
Showing 1 changed file with 30 additions and 32 deletions.
62 changes: 30 additions & 32 deletions wallet/chain/c/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@
package c

import (
"context"
"errors"
"math/big"
"time"

"github.com/ava-labs/coreth/ethclient"
"github.com/ava-labs/coreth/plugin/evm"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/rpc"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"

ethcommon "github.com/ethereum/go-ethereum/common"
)

var _ Wallet = (*wallet)(nil)
var (
_ Wallet = (*wallet)(nil)

errNotCommitted = errors.New("not committed")
)

type Wallet interface {
// Builder returns the builder that will be used to create the transactions.
Expand Down Expand Up @@ -162,11 +165,31 @@ func (w *wallet) IssueAtomicTx(
return w.Backend.AcceptAtomicTx(ctx, tx)
}

if err := awaitTxAccepted(w.avaxClient, ctx, txID, ops.PollFrequency()); err != nil {
return err
}
pollFrequency := ops.PollFrequency()
ticker := time.NewTicker(pollFrequency)
defer ticker.Stop()

for {
status, err := w.avaxClient.GetAtomicTxStatus(ctx, txID)
if err != nil {
return err
}

return w.Backend.AcceptAtomicTx(ctx, tx)
switch status {
case evm.Accepted:
return w.Backend.AcceptAtomicTx(ctx, tx)
case evm.Dropped, evm.Unknown:
return errNotCommitted
}

// The tx is Processing.

select {
case <-ticker.C:
case <-ctx.Done():
return ctx.Err()
}
}
}

func (w *wallet) baseFee(options []common.Option) (*big.Int, error) {
Expand All @@ -179,28 +202,3 @@ func (w *wallet) baseFee(options []common.Option) (*big.Int, error) {
ctx := ops.Context()
return w.ethClient.EstimateBaseFee(ctx)
}

// TODO: Upstream this function into coreth.
func awaitTxAccepted(
c evm.Client,
ctx context.Context,
txID ids.ID,
freq time.Duration,
options ...rpc.Option,
) error {
ticker := time.NewTicker(freq)
defer ticker.Stop()

for {
status, err := c.GetAtomicTxStatus(ctx, txID, options...)
if err == nil && status == evm.Accepted {
return nil
}

select {
case <-ticker.C:
case <-ctx.Done():
return ctx.Err()
}
}
}

0 comments on commit 5e5cca4

Please sign in to comment.