Skip to content

Commit

Permalink
add config gasless network
Browse files Browse the repository at this point in the history
  • Loading branch information
daveroga committed Apr 16, 2024
1 parent 9ffae93 commit c3d36f0
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 57 deletions.
1 change: 1 addition & 0 deletions .env-issuer.sample
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ ISSUER_ETHEREUM_CONFIRMATION_BLOCK_COUNT=5
ISSUER_ETHEREUM_RECEIPT_TIMEOUT=600s
ISSUER_ETHEREUM_MIN_GAS_PRICE=0
ISSUER_ETHEREUM_MAX_GAS_PRICE=2500000
ISSUER_ETHEREUM_GASLESS=false
ISSUER_ETHEREUM_RPC_RESPONSE_TIMEOUT=5s
ISSUER_ETHEREUM_WAIT_RECEIPT_CYCLE_TIME=30s
ISSUER_ETHEREUM_WAIT_BLOCK_CYCLE_TIME=30s
Expand Down
1 change: 1 addition & 0 deletions cmd/issuer_initializer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func main() {
ReceiptTimeout: cfg.Ethereum.ReceiptTimeout,
MinGasPrice: big.NewInt(int64(cfg.Ethereum.MinGasPrice)),
MaxGasPrice: big.NewInt(int64(cfg.Ethereum.MaxGasPrice)),
GasLess: cfg.Ethereum.GasLess,
RPCResponseTimeout: cfg.Ethereum.RPCResponseTimeout,
WaitReceiptCycleTime: cfg.Ethereum.WaitReceiptCycleTime,
WaitBlockCycleTime: cfg.Ethereum.WaitBlockCycleTime,
Expand Down
1 change: 1 addition & 0 deletions cmd/notifications/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func newCredentialsService(ctx context.Context, cfg *config.Configuration, stora
ReceiptTimeout: cfg.Ethereum.ReceiptTimeout,
MinGasPrice: big.NewInt(int64(cfg.Ethereum.MinGasPrice)),
MaxGasPrice: big.NewInt(int64(cfg.Ethereum.MaxGasPrice)),
GasLess: cfg.Ethereum.GasLess,
RPCResponseTimeout: cfg.Ethereum.RPCResponseTimeout,
WaitReceiptCycleTime: cfg.Ethereum.WaitReceiptCycleTime,
WaitBlockCycleTime: cfg.Ethereum.WaitBlockCycleTime,
Expand Down
1 change: 1 addition & 0 deletions cmd/pending_publisher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func main() {
ReceiptTimeout: cfg.Ethereum.ReceiptTimeout,
MinGasPrice: big.NewInt(int64(cfg.Ethereum.MinGasPrice)),
MaxGasPrice: big.NewInt(int64(cfg.Ethereum.MaxGasPrice)),
GasLess: cfg.Ethereum.GasLess,
RPCResponseTimeout: cfg.Ethereum.RPCResponseTimeout,
WaitReceiptCycleTime: cfg.Ethereum.WaitReceiptCycleTime,
WaitBlockCycleTime: cfg.Ethereum.WaitBlockCycleTime,
Expand Down
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type Ethereum struct {
ReceiptTimeout time.Duration `tip:"Receipt timeout"`
MinGasPrice int `tip:"Minimum Gas Price"`
MaxGasPrice int `tip:"The Datasource name locator"`
GasLess bool `tip:"Gasless transactions"`
RPCResponseTimeout time.Duration `tip:"RPC Response timeout"`
WaitReceiptCycleTime time.Duration `tip:"Wait Receipt Cycle Time"`
WaitBlockCycleTime time.Duration `tip:"Wait Block Cycle Time"`
Expand Down Expand Up @@ -464,6 +465,7 @@ func bindEnv() {
_ = viper.BindEnv("Ethereum.ReceiptTimeout", "ISSUER_ETHEREUM_RECEIPT_TIMEOUT")
_ = viper.BindEnv("Ethereum.MinGasPrice", "ISSUER_ETHEREUM_MIN_GAS_PRICE")
_ = viper.BindEnv("Ethereum.MaxGasPrice", "ISSUER_ETHEREUM_MAX_GAS_PRICE")
_ = viper.BindEnv("Ethereum.GasLess", "ISSUER_ETHEREUM_GASLESS")
_ = viper.BindEnv("Ethereum.RPCResponseTimeout", "ISSUER_ETHEREUM_RPC_RESPONSE_TIMEOUT")
_ = viper.BindEnv("Ethereum.WaitReceiptCycleTime", "ISSUER_ETHEREUM_WAIT_RECEIPT_CYCLE_TIME")
_ = viper.BindEnv("Ethereum.WaitBlockCycleTime", "ISSUER_ETHEREUM_WAIT_BLOCK_CYCLE_TIME")
Expand Down
1 change: 1 addition & 0 deletions internal/core/services/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func NewAccountService(ethConfig config.Ethereum, keyStore *kms.KMS) *AccountSer
ReceiptTimeout: ethConfig.ReceiptTimeout,
MinGasPrice: big.NewInt(int64(ethConfig.MinGasPrice)),
MaxGasPrice: big.NewInt(int64(ethConfig.MaxGasPrice)),
GasLess: ethConfig.GasLess,
RPCResponseTimeout: ethConfig.RPCResponseTimeout,
WaitReceiptCycleTime: ethConfig.WaitReceiptCycleTime,
WaitBlockCycleTime: ethConfig.WaitBlockCycleTime,
Expand Down
2 changes: 2 additions & 0 deletions internal/providers/blockchain/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func InitEthConnect(cfg config.Ethereum, kms *kms.KMS) (*eth.Client, error) {
ReceiptTimeout: cfg.ReceiptTimeout,
MinGasPrice: big.NewInt(int64(cfg.MinGasPrice)),
MaxGasPrice: big.NewInt(int64(cfg.MaxGasPrice)),
GasLess: cfg.GasLess,
RPCResponseTimeout: cfg.RPCResponseTimeout,
WaitReceiptCycleTime: cfg.WaitReceiptCycleTime,
WaitBlockCycleTime: cfg.WaitBlockCycleTime,
Expand All @@ -65,6 +66,7 @@ func Open(cfg *config.Configuration, kms *kms.KMS) (*eth.Client, error) {
ReceiptTimeout: cfg.Ethereum.ReceiptTimeout,
MinGasPrice: big.NewInt(int64(cfg.Ethereum.MinGasPrice)),
MaxGasPrice: big.NewInt(int64(cfg.Ethereum.MaxGasPrice)),
GasLess: cfg.Ethereum.GasLess,
RPCResponseTimeout: cfg.Ethereum.RPCResponseTimeout,
WaitReceiptCycleTime: cfg.Ethereum.WaitReceiptCycleTime,
WaitBlockCycleTime: cfg.Ethereum.WaitBlockCycleTime,
Expand Down
1 change: 1 addition & 0 deletions k8s/helm/templates/issuer-node-api-configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ data:
ISSUER_ETHEREUM_CONFIRMATION_BLOCK_COUNT: {{ .Values.apiIssuerNode.configMap.issuerEthereumConfirmationBlockCount | quote }}
ISSUER_ETHEREUM_MAX_GAS_PRICE: {{ .Values.apiIssuerNode.configMap.issuerEthereumMaxGasPrice | quote }}
ISSUER_ETHEREUM_MIN_GAS_PRICE: {{ .Values.apiIssuerNode.configMap.issuerEthereumMinGasPrice | quote }}
ISSUER_ETHEREUM_GASLESS: {{ .Values.apiIssuerNode.configMap.issuerEthereumGasLess | quote }}
ISSUER_ETHEREUM_RPC_RESPONSE_TIMEOUT: {{ .Values.apiIssuerNode.configMap.issuerEthereumRpcResponseTimeout }}
ISSUER_ETHEREUM_RECEIPT_TIMEOUT: {{ .Values.apiIssuerNode.configMap.issuerEthereumReceiptTimeout }}
ISSUER_ETHEREUM_WAIT_BLOCK_CYCLE_TIME: {{ .Values.apiIssuerNode.configMap.issuerEthereumWaitBlockCycleTime }}
Expand Down
1 change: 1 addition & 0 deletions k8s/helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ apiIssuerNode:
issuerEthereumDefaultGasLimit: "600000"
issuerEthereumMaxGasPrice: "2500000"
issuerEthereumMinGasPrice: "0"
issuerEthereumGasLess: "false"
issuerEthereumReceiptTimeout: 600s
issuerEthereumResolverPrefixMumbai: polygon:mumbai
issuerEthereumResolverPrefixAmoy: polygon:amoy
Expand Down
131 changes: 74 additions & 57 deletions pkg/blockchain/eth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type ClientConfig struct {
DefaultGasLimit int `json:"default_gas_limit"`
MinGasPrice *big.Int `json:"min_gas_price"`
MaxGasPrice *big.Int `json:"max_gas_price"`
GasLess bool `json:"gas_less"`
RPCResponseTimeout time.Duration `json:"rpc_response_time_out"`
WaitReceiptCycleTime time.Duration `json:"wait_receipt_cycle_time_out"`
WaitBlockCycleTime time.Duration `json:"wait_block_cycle_time_out"`
Expand Down Expand Up @@ -317,18 +318,25 @@ func (c *Client) CreateTxOpts(ctx context.Context, kmsKey kms.KeyID) (*bind.Tran

sigFn := c.signerFnFactory(ctx, kmsKey)

tip, err := c.suggestGasTipCap(ctx)
if err != nil {
return nil, err
}
// tip := big.NewInt(0)
gasLimit := uint64(c.Config.DefaultGasLimit)

opts := &bind.TransactOpts{
From: addr,
Signer: sigFn,
GasTipCap: tip, // The only option we need to set is gasTipCap as some Ethereum nodes don't support eth_maxPriorityFeePerGas
GasLimit: 0, // go-ethereum library will estimate gas limit automatically if it is 0
Context: ctx,
NoSend: false,
From: addr,
Signer: sigFn,
GasLimit: gasLimit, // go-ethereum library will estimate gas limit automatically if it is 0
Context: ctx,
NoSend: false,
}

if !c.Config.GasLess { // Some Ethereum nodes don't support eth_maxPriorityFeePerGas so we set GasLess = true
tip, err := c.suggestGasTipCap(ctx)
if err != nil {
return nil, err
}
gasLimit = uint64(0)
opts.GasLimit = gasLimit
opts.GasTipCap = tip
}

return opts, nil
Expand Down Expand Up @@ -357,63 +365,72 @@ func (c *Client) CreateRawTx(ctx context.Context, txParams TransactionParams) (*
txParams.Nonce = &nonce
}

_ctx2, cancel2 := context.WithTimeout(ctx, c.Config.RPCResponseTimeout)
defer cancel2()
if txParams.Value == nil {
txParams.Value = big.NewInt(0)
}
gasLimit, err := c.client.EstimateGas(_ctx2, ethereum.CallMsg{
From: txParams.FromAddress, // the sender of the 'transaction'

baseTx := &types.DynamicFeeTx{
To: &txParams.ToAddress,
Gas: 0, // wei <-> gas exchange ratio
Value: txParams.Value, // amount of wei sent along with the call
Nonce: *txParams.Nonce,
Gas: uint64(c.Config.DefaultGasLimit),
Value: txParams.Value,
Data: txParams.Payload,
})
if err != nil {
return nil, fmt.Errorf("failed to estimate gas: %v", err)
}

latestBlockHeader, err := c.HeaderByNumber(ctx, nil)
if err != nil {
return nil, err
}
if !c.Config.GasLess {
_ctx2, cancel2 := context.WithTimeout(ctx, c.Config.RPCResponseTimeout)
defer cancel2()
gasLimit, err := c.client.EstimateGas(_ctx2, ethereum.CallMsg{
From: txParams.FromAddress, // the sender of the 'transaction'
To: &txParams.ToAddress,
Gas: 0, // wei <-> gas exchange ratio
Value: txParams.Value, // amount of wei sent along with the call
Data: txParams.Payload,
})

baseTx.Gas = gasLimit

if txParams.BaseFee == nil {
// since ETH and Polygon blockchain already supports London fork.
// no need set special block.
baseFee := eip1559.CalcBaseFee(&params.ChainConfig{LondonBlock: big.NewInt(1)}, latestBlockHeader)

// add 25% to baseFee. baseFee always small value.
// since we use dynamic fee transactions we will get not used gas back.
b := math.Round(float64(baseFee.Int64()) * feeIncrement)
baseFee = big.NewInt(int64(b))
txParams.BaseFee = baseFee
}

if txParams.GasTips == nil {
_ctx3, cancel3 := context.WithTimeout(ctx, c.Config.RPCResponseTimeout)
defer cancel3()
gasTip, err := c.client.SuggestGasTipCap(_ctx3)
// since hardhad doesn't support 'eth_maxPriorityFeePerGas' rpc call.
// we should hardcode 0 as a mainer tips. More information: https://github.com/NomicFoundation/hardhat/issues/1664#issuecomment-1149006010
if err != nil && strings.Contains(err.Error(), "eth_maxPriorityFeePerGas not found") {
log.Error(ctx, "failed get suggest gas tip: %s. use 0 instead", "err", err)
gasTip = big.NewInt(0)
} else if err != nil {
return nil, fmt.Errorf("failed get suggest gas tip: %v", err)
if err != nil {
return nil, fmt.Errorf("failed to estimate gas: %v", err)
}
txParams.GasTips = gasTip
}

maxGasPricePerFee := big.NewInt(0).Add(txParams.BaseFee, txParams.GasTips)
baseTx := &types.DynamicFeeTx{
To: &txParams.ToAddress,
Nonce: *txParams.Nonce,
Gas: gasLimit,
Value: txParams.Value,
Data: txParams.Payload,
GasTipCap: txParams.GasTips,
GasFeeCap: maxGasPricePerFee,
latestBlockHeader, err := c.HeaderByNumber(ctx, nil)
if err != nil {
return nil, err
}

if txParams.BaseFee == nil {
// since ETH and Polygon blockchain already supports London fork.
// no need set special block.
baseFee := eip1559.CalcBaseFee(&params.ChainConfig{LondonBlock: big.NewInt(1)}, latestBlockHeader)

// add 25% to baseFee. baseFee always small value.
// since we use dynamic fee transactions we will get not used gas back.
b := math.Round(float64(baseFee.Int64()) * feeIncrement)
baseFee = big.NewInt(int64(b))
txParams.BaseFee = baseFee
}

if txParams.GasTips == nil {
_ctx3, cancel3 := context.WithTimeout(ctx, c.Config.RPCResponseTimeout)
defer cancel3()
gasTip, err := c.client.SuggestGasTipCap(_ctx3)
// since hardhad doesn't support 'eth_maxPriorityFeePerGas' rpc call.
// we should hardcode 0 as a mainer tips. More information: https://github.com/NomicFoundation/hardhat/issues/1664#issuecomment-1149006010
if err != nil && strings.Contains(err.Error(), "eth_maxPriorityFeePerGas not found") {
log.Error(ctx, "failed get suggest gas tip: %s. use 0 instead", "err", err)
gasTip = big.NewInt(0)
} else if err != nil {
return nil, fmt.Errorf("failed get suggest gas tip: %v", err)
}
txParams.GasTips = gasTip
}

maxGasPricePerFee := big.NewInt(0).Add(txParams.BaseFee, txParams.GasTips)

baseTx.GasTipCap = txParams.GasTips
baseTx.GasFeeCap = maxGasPricePerFee

}

tx := types.NewTx(baseTx)
Expand Down

0 comments on commit c3d36f0

Please sign in to comment.