forked from ethereum-optimism/op-geth
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
eth_estimateGas
CIP-64 and CIP-66 compatibility
#91
Merged
+405
−108
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
82af4a2
rpc: include feeCurrency in transaction-args
ezdac c5ac47e
Rename celoapi file
ezdac 8252069
Add Backend wrapper for Celo functionality
ezdac 3fc30ba
Make transaction-args CIP-64/66 compatible
ezdac 947fc1c
Make eth_estimateGas CIP64 and CIP66 compatible
ezdac d63a96d
Move error message inside function
ezdac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
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,97 @@ | ||
package celoapi | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/common/exchange" | ||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/ethereum/go-ethereum/contracts" | ||
"github.com/ethereum/go-ethereum/core" | ||
"github.com/ethereum/go-ethereum/internal/ethapi" | ||
) | ||
|
||
type Ethereum interface { | ||
BlockChain() *core.BlockChain | ||
} | ||
|
||
type CeloAPI struct { | ||
ethAPI *ethapi.EthereumAPI | ||
eth Ethereum | ||
} | ||
|
||
func NewCeloAPI(e Ethereum, b ethapi.CeloBackend) *CeloAPI { | ||
return &CeloAPI{ | ||
ethAPI: ethapi.NewEthereumAPI(b), | ||
eth: e, | ||
} | ||
} | ||
|
||
func (c *CeloAPI) convertedCurrencyValue(v *hexutil.Big, feeCurrency *common.Address) (*hexutil.Big, error) { | ||
if feeCurrency != nil { | ||
convertedTipCap, err := c.convertGoldToCurrency(v.ToInt(), feeCurrency) | ||
if err != nil { | ||
return nil, fmt.Errorf("convert to feeCurrency: %w", err) | ||
} | ||
v = (*hexutil.Big)(convertedTipCap) | ||
} | ||
return v, nil | ||
} | ||
|
||
func (c *CeloAPI) celoBackendCurrentState() (*contracts.CeloBackend, error) { | ||
state, err := c.eth.BlockChain().State() | ||
if err != nil { | ||
return nil, fmt.Errorf("retrieve HEAD blockchain state': %w", err) | ||
} | ||
|
||
cb := &contracts.CeloBackend{ | ||
ChainConfig: c.eth.BlockChain().Config(), | ||
State: state, | ||
} | ||
return cb, nil | ||
} | ||
|
||
func (c *CeloAPI) convertGoldToCurrency(nativePrice *big.Int, feeCurrency *common.Address) (*big.Int, error) { | ||
cb, err := c.celoBackendCurrentState() | ||
if err != nil { | ||
return nil, err | ||
} | ||
er, err := contracts.GetExchangeRates(cb) | ||
if err != nil { | ||
return nil, fmt.Errorf("retrieve exchange rates from current state: %w", err) | ||
} | ||
return exchange.ConvertGoldToCurrency(er, feeCurrency, nativePrice) | ||
} | ||
|
||
// GasPrice wraps the original JSON RPC `eth_gasPrice` and adds an additional | ||
// optional parameter `feeCurrency` for fee-currency conversion. | ||
// When `feeCurrency` is not given, then the original JSON RPC method is called without conversion. | ||
func (c *CeloAPI) GasPrice(ctx context.Context, feeCurrency *common.Address) (*hexutil.Big, error) { | ||
tipcap, err := c.ethAPI.GasPrice(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Between the call to `ethapi.GasPrice` and the call to fetch and convert the rates, | ||
// there is a chance of a state-change. This means that gas-price suggestion is calculated | ||
// based on state of block x, while the currency conversion could be calculated based on block | ||
// x+1. | ||
// However, a similar race condition is present in the `ethapi.GasPrice` method itself. | ||
return c.convertedCurrencyValue(tipcap, feeCurrency) | ||
} | ||
|
||
// MaxPriorityFeePerGas wraps the original JSON RPC `eth_maxPriorityFeePerGas` and adds an additional | ||
// optional parameter `feeCurrency` for fee-currency conversion. | ||
// When `feeCurrency` is not given, then the original JSON RPC method is called without conversion. | ||
func (c *CeloAPI) MaxPriorityFeePerGas(ctx context.Context, feeCurrency *common.Address) (*hexutil.Big, error) { | ||
tipcap, err := c.ethAPI.MaxPriorityFeePerGas(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Between the call to `ethapi.MaxPriorityFeePerGas` and the call to fetch and convert the rates, | ||
// there is a chance of a state-change. This means that gas-price suggestion is calculated | ||
// based on state of block x, while the currency conversion could be calculated based on block | ||
// x+1. | ||
return c.convertedCurrencyValue(tipcap, feeCurrency) | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The diff here looks a bit confusing - basically I just moved the
CeloAPI
from theceloapi/backend.go
to theceloapi/api.go
file, and then added the newCeloBackend
implementation in theceloapi/backend.go
file.