-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated chainio and signer READMEs + some code comments
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
Showing
12 changed files
with
988 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Wallet | ||
|
||
TODO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Oops, something went wrong.