Skip to content

Commit

Permalink
add gasLimitMultiplier to fireblocks and privatekey wallets
Browse files Browse the repository at this point in the history
  • Loading branch information
samlaf committed Mar 28, 2024
1 parent de51db9 commit 6017c43
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
29 changes: 18 additions & 11 deletions chainio/clients/wallet/fireblocks_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ type fireblocksWallet struct {
// accessed concurrently by SendTransaction and GetTransactionReceipt
mu sync.Mutex

fireblocksClient fireblocks.Client
ethClient eth.Client
vaultAccountName string
logger logging.Logger
chainID *big.Int
fireblocksClient fireblocks.Client
ethClient eth.Client
vaultAccountName string
logger logging.Logger
chainID *big.Int
gasLimitMultiplier float64

// nonceToTx keeps track of the transaction ID for each nonce
// this is used to retrieve the transaction hash for a given nonce
Expand All @@ -53,11 +54,12 @@ func NewFireblocksWallet(fireblocksClient fireblocks.Client, ethClient eth.Clien
}
logger.Debug("Creating new Fireblocks wallet for chain", "chainID", chainID)
return &fireblocksWallet{
fireblocksClient: fireblocksClient,
ethClient: ethClient,
vaultAccountName: vaultAccountName,
logger: logger,
chainID: chainID,
fireblocksClient: fireblocksClient,
ethClient: ethClient,
vaultAccountName: vaultAccountName,
logger: logger,
chainID: chainID,
gasLimitMultiplier: FallbackGasLimitMultiplier,

nonceToTxID: make(map[uint64]TxID),
txIDToNonce: make(map[TxID]uint64),
Expand All @@ -68,6 +70,11 @@ func NewFireblocksWallet(fireblocksClient fireblocks.Client, ethClient eth.Clien
}, nil
}

func (t *fireblocksWallet) WithGasLimitMultiplier(multiplier float64) *fireblocksWallet {
t.gasLimitMultiplier = multiplier
return t
}

func (t *fireblocksWallet) getAccount(ctx context.Context) (*fireblocks.VaultAccount, error) {
if t.account == nil {
accounts, err := t.fireblocksClient.ListVaultAccounts(ctx)
Expand Down Expand Up @@ -158,7 +165,7 @@ func (t *fireblocksWallet) SendTransaction(ctx context.Context, tx *types.Transa

gasLimit := ""
if tx.Gas() > 0 {
gasLimit = strconv.FormatUint(tx.Gas(), 10)
gasLimit = strconv.FormatUint(uint64(float64(tx.Gas())*t.gasLimitMultiplier), 10)
}

// if the gas fees are specified in the transaction, use them.
Expand Down
30 changes: 19 additions & 11 deletions chainio/clients/wallet/privatekey_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,39 @@ import (
)

var (
FallbackGasTipCap = big.NewInt(15_000_000_000)
FallbackGasTipCap = big.NewInt(15_000_000_000)
FallbackGasLimitMultiplier = 1.1
)

var _ Wallet = (*privateKeyWallet)(nil)

type privateKeyWallet struct {
ethClient eth.Client
address common.Address
signerFn signerv2.SignerFn
logger logging.Logger
ethClient eth.Client
address common.Address
signerFn signerv2.SignerFn
logger logging.Logger
gasLimitMultiplier float64

// cache
contracts map[common.Address]*bind.BoundContract
}

func NewPrivateKeyWallet(ethClient eth.Client, signer signerv2.SignerFn, signerAddress common.Address, logger logging.Logger) (Wallet, error) {
return &privateKeyWallet{
ethClient: ethClient,
address: signerAddress,
signerFn: signer,
logger: logger,
contracts: make(map[common.Address]*bind.BoundContract, 0),
ethClient: ethClient,
address: signerAddress,
signerFn: signer,
logger: logger,
gasLimitMultiplier: FallbackGasLimitMultiplier,
contracts: make(map[common.Address]*bind.BoundContract, 0),
}, nil
}

func (t *privateKeyWallet) WithGasLimitMultiplier(multiplier float64) *privateKeyWallet {
t.gasLimitMultiplier = multiplier
return t
}

func (t *privateKeyWallet) SendTransaction(ctx context.Context, tx *types.Transaction) (TxID, error) {
// Estimate gas and nonce
// can't print tx hash in logs because the tx changes below when we complete and sign it
Expand Down Expand Up @@ -134,7 +142,7 @@ func (t *privateKeyWallet) estimateGasAndNonce(ctx context.Context, tx *types.Tr
GasFeeCap: gasFeeCap,
Data: tx.Data(),
Value: tx.Value(),
Gas: gasLimit, // TODO(add buffer)
Gas: uint64(float64(gasLimit) * t.gasLimitMultiplier),
Nonce: tx.Nonce(), // We are not doing any nonce management for now but we probably should later for more robustness
}

Expand Down

0 comments on commit 6017c43

Please sign in to comment.