Skip to content

Commit

Permalink
Updated chainio and signer READMEs + some code comments
Browse files Browse the repository at this point in the history
added gas oracle

refactored txmgr module

added geometric txmgr

make fmt

log -> logger in simple txmgr

add TODO + rename constructor

update geometric txmgr to use sync api
  • Loading branch information
samlaf committed Jul 13, 2024
1 parent 4b1bb2a commit ce05793
Show file tree
Hide file tree
Showing 12 changed files with 988 additions and 207 deletions.
34 changes: 22 additions & 12 deletions chainio/README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
## ChainIO

This module is used to facilitate reading/writing/subscribing to [eigenlayer core](./clients/elcontracts/) contracts and [avs registry](./clients/avsregistry/) contracts.

To make it easier to understand the different structs in this package, and their hierarchical relationship, we describe each of them below:
- geth's ethClient
- eigensdk [ethClient](./clients/eth/client.go)
- wraps geth's ethClient and adds convenience methods
- [eigenlayerContractBindings](../contracts/bindings/)
### Interacting with a json-rpc node

We have a basic [ethClient](./clients/eth/client.go) which simply wraps geth's ethClient and adds some convenience methods. The Client interface is also implemented by [instrumentedClient](./clients/eth/instrumented_client.go) which adds metrics to the ethClient to conform to the node spec's [rpc metrics](https://docs.eigenlayer.xyz/eigenlayer/avs-guides/spec/metrics/metrics-prom-spec#rpc-metrics) requirements.


### Building Transactions

In order to facilitate reading/writing/subscribing to [eigenlayer core](./clients/elcontracts/) contracts and [avs registry](./clients/avsregistry/) contracts, we use geth's abigen created bindings for low-level interactions, as well as our own high-level clients with higher utility functions:
- [Eigenlayer Contract Bindings](./clients/elcontracts/bindings.go)
- generated by abigen
- low level bindings to eigenlayer core contracts, which wrap our ethClient
- [elContractsClient](./clients/eth/client.go)
- wraps eigenlayerContractBindings and hides a little bit of the underlying complexity, which is not needed in 99% of cases.
- abigen also doesn't create an interface for the bindings it generates, whereas elContractsClient has a well defined interface which we use to generate mocks to help with testing.
- [ELChainReader](./clients/elcontracts/reader.go) / [ELChainWriter](./clients/elcontracts/writer.go) / [ELChainSubscriber](./clients/avsregistry/subscriber.go)
- wraps elContractsClient and adds convenience methods
- hides even more complexity than elContractsClient
- wraps bindings and adds convenience methods
- These structs should be the only ones used by AVS developers, apart from interacting with an ethClient directly to make direct json rpc calls such as waiting for a transaction receipt.

A similar hierarchy applies for the avs registry contracts.
There's a similar setup for the [avs registry](./clients/avsregistry/) contracts.

### Signing, Sending, and Managing Transactions

After building transactions, we need to sign them, send them to the network, and manage the nonce and gas price to ensure they are mined. This functionality is provided by:
- [txmgr](./txmgr/README.md)
- uses a wallet to sign and submit transactions, but then manages them by resubmitting with higher gas prices until they are mined.
- [wallet](./clients/wallet)
- uses a signerv2 to sign transactions, sends them to the network and can query for their receipts
- wallet abstraction is needed because "wallets", such as fireblocks, both sign and send transactions to the network (they don't simply return signed bytes so that we can send them ourselves)
- [signerv2](../signerv2/README.md)
- signs transactions
3 changes: 3 additions & 0 deletions chainio/clients/wallet/READMD.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Wallet

TODO
3 changes: 2 additions & 1 deletion chainio/clients/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (

type TxID = string

// Wallet is an interface for signing and sending transactions to the network
// Wallet is an interface for signing and sending transactions to the txpool.
// For a higher-level interface that includes nonce management and gas bumping, use the TxManager interface.
// This interface is used to abstract the process of sending transactions to the Ethereum network
// For example, for an MPC signer, the transaction would be broadcasted via an external API endpoint
// and the status is tracked via another external endpoint instead of being broadcasted
Expand Down
122 changes: 122 additions & 0 deletions chainio/gasoracle/gasoracle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package gasoracle

import (
"context"
"math/big"

"github.com/Layr-Labs/eigensdk-go/chainio/clients/eth"
"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/Layr-Labs/eigensdk-go/utils"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)

type Params struct {
FallbackGasTipCap uint64
GasMultiplierPercentage uint64
GasTipMultiplierPercentage uint64
}

var DefaultParams = Params{
FallbackGasTipCap: uint64(5_000_000_000), // 5 gwei
GasMultiplierPercentage: uint64(120), // add an extra 20% gas buffer to the gas limit
GasTipMultiplierPercentage: uint64(125), // add an extra 25% to the gas tip
}

type GasOracle struct {
params Params
client eth.Client
logger logging.Logger
}

// params are optional gas parameters any of which will be filled with default values if not provided
func New(client eth.Client, logger logging.Logger, params Params) *GasOracle {
if params.FallbackGasTipCap == 0 {
params.FallbackGasTipCap = DefaultParams.FallbackGasTipCap
}
if params.GasMultiplierPercentage == 0 {
params.GasMultiplierPercentage = DefaultParams.GasMultiplierPercentage
}
if params.GasTipMultiplierPercentage == 0 {
params.GasTipMultiplierPercentage = DefaultParams.GasTipMultiplierPercentage
}
return &GasOracle{
params: params,
client: client,
logger: logger,
}
}

func (o *GasOracle) GetLatestGasCaps(ctx context.Context) (gasTipCap, gasFeeCap *big.Int, err error) {
gasTipCap, err = o.client.SuggestGasTipCap(ctx)
if err != nil {
// If the transaction failed because the backend does not support
// eth_maxPriorityFeePerGas, fallback to using the default constant.
// Currently Alchemy is the only backend provider that exposes this
// method, so in the event their API is unreachable we can fallback to a
// degraded mode of operation. This also applies to our test
// environments, as hardhat doesn't support the query either.
o.logger.Info("eth_maxPriorityFeePerGas is unsupported by current backend, using fallback gasTipCap")
gasTipCap = big.NewInt(0).SetUint64(o.params.FallbackGasTipCap)
}

gasTipCap.Mul(gasTipCap, big.NewInt(int64(o.params.GasTipMultiplierPercentage))).Div(gasTipCap, big.NewInt(100))

header, err := o.client.HeaderByNumber(ctx, nil)
if err != nil {
return nil, nil, err
}
gasFeeCap = getGasFeeCap(gasTipCap, header.BaseFee)
return
}

func (o *GasOracle) UpdateGas(
ctx context.Context,
tx *types.Transaction,
value, gasTipCap, gasFeeCap *big.Int,
from common.Address,
) (*types.Transaction, error) {
gasLimit, err := o.client.EstimateGas(ctx, ethereum.CallMsg{
From: from,
To: tx.To(),
GasTipCap: gasTipCap,
GasFeeCap: gasFeeCap,
Value: value,
Data: tx.Data(),
})
if err != nil {
return nil, utils.WrapError("failed to estimate gas", err)
}

noopSigner := func(addr common.Address, tx *types.Transaction) (*types.Transaction, error) {
return tx, nil
}
opts := &bind.TransactOpts{
From: from,
Signer: noopSigner,
NoSend: true,
}
opts.Context = ctx
opts.Nonce = new(big.Int).SetUint64(tx.Nonce())
opts.GasTipCap = gasTipCap
opts.GasFeeCap = gasFeeCap
opts.GasLimit = o.addGasBuffer(gasLimit)
opts.Value = value

contract := bind.NewBoundContract(*tx.To(), abi.ABI{}, o.client, o.client, o.client)
return contract.RawTransact(opts, tx.Data())
}

func (o *GasOracle) addGasBuffer(gasLimit uint64) uint64 {
return o.params.GasMultiplierPercentage * gasLimit / 100
}

// getGasFeeCap returns the gas fee cap for a transaction, calculated as:
// gasFeeCap = 2 * baseFee + gasTipCap
// Rationale: https://www.blocknative.com/blog/eip-1559-fees
func getGasFeeCap(gasTipCap *big.Int, baseFee *big.Int) *big.Int {
return new(big.Int).Add(new(big.Int).Mul(baseFee, big.NewInt(2)), gasTipCap)
}
16 changes: 12 additions & 4 deletions chainio/txmgr/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
## Transaction Manager

Transaction Manager is responsible for
* Building transactions
* Estimating fees and adding buffer
* Estimating fees and adding gas limit buffer
* Signing transactions
* Sending transactions to the network
* Doing transaction nonce and gas price management to ensure transactions are mined


### Simple Transaction Manager
Here's the flow of the simple transaction manager which is used to send smart contract
transactions to the network.
Here's the flow of the simple transaction manager which is used to send smart contract transactions to the network.
![Simple Transaction Manager](./simple-tx-manager-flow.png)

### Simple Transaction Manager

The simple txmgr simply sends transactions to the network, waits for them to be mined, and returns the receipt. It doesn't do any managing.

### Geometric Transaction Manager

The geometric txmgr is a more advanced version of the simple txmgr. It sends transactions to the network, waits for them to be mined, and if they are not mined within a certain time, it bumps the gas price geometrically and resubmits the transaction. This process is repeated until the transaction is mined.
Loading

0 comments on commit ce05793

Please sign in to comment.