-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server/asset/eth: separate tip/base fee estimates
- Loading branch information
Showing
3 changed files
with
87 additions
and
51 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 |
---|---|---|
|
@@ -22,7 +22,9 @@ import ( | |
"decred.org/dcrdex/server/asset" | ||
"github.com/ethereum/go-ethereum" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/consensus/misc" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/params" | ||
) | ||
|
||
func init() { | ||
|
@@ -86,7 +88,8 @@ type ethFetcher interface { | |
headerByHeight(ctx context.Context, height uint64) (*types.Header, error) | ||
connect(ctx context.Context, ipc string, contractAddr *common.Address) error | ||
shutdown() | ||
suggestGasPrice(ctx context.Context) (*big.Int, error) | ||
suggestGasTipCap(ctx context.Context) (*big.Int, error) // suggested priority fee (tip) per gas | ||
suggestGasPrice(ctx context.Context) (*big.Int, error) // suggested gas price for legacy txns | ||
syncProgress(ctx context.Context) (*ethereum.SyncProgress, error) | ||
swap(ctx context.Context, secretHash [32]byte) (*swapv0.ETHSwapSwap, error) | ||
transaction(ctx context.Context, hash common.Hash) (tx *types.Transaction, isMempool bool, err error) | ||
|
@@ -105,14 +108,15 @@ type hashN struct { | |
type Backend struct { | ||
// A connection-scoped Context is used to cancel active RPCs on | ||
// connection shutdown. | ||
rpcCtx context.Context | ||
cancelRPCs context.CancelFunc | ||
cfg *config | ||
node ethFetcher | ||
rpcCtx context.Context | ||
cancelRPCs context.CancelFunc | ||
cfg *config | ||
node ethFetcher | ||
chainConfig *params.ChainConfig | ||
|
||
// bestHash caches the last know best block hash and height and is used | ||
// to detect reorgs. Only accessed in Connect and poll which is | ||
// syncronous so no locking is needed presently. | ||
// synchronous so no locking is needed presently. | ||
bestHash hashN | ||
|
||
// The backend provides block notification channels through the BlockChannel | ||
|
@@ -145,18 +149,23 @@ func unconnectedETH(logger dex.Logger, cfg *config) *Backend { | |
// possibly a random contract setup, and so this section will need to | ||
// change to support multiple contracts. | ||
var contractAddr common.Address | ||
var chainConfig *params.ChainConfig | ||
switch cfg.network { | ||
case dex.Simnet: | ||
contractAddr = common.HexToAddress(simnetContractAddr) | ||
chainConfig = params.TestChainConfig // if harness running, may set actual config via dexeth.LoadGenesisFile | ||
case dex.Testnet: | ||
contractAddr = common.HexToAddress(testnetContractAddr) | ||
chainConfig = params.GoerliChainConfig | ||
case dex.Mainnet: | ||
contractAddr = common.HexToAddress(mainnetContractAddr) | ||
chainConfig = params.MainnetChainConfig | ||
} | ||
return &Backend{ | ||
rpcCtx: ctx, | ||
cancelRPCs: cancel, | ||
cfg: cfg, | ||
chainConfig: chainConfig, | ||
log: logger, | ||
blockChans: make(map[chan *asset.BlockUpdate]struct{}), | ||
contractAddr: contractAddr, | ||
|
@@ -235,11 +244,16 @@ func (eth *Backend) InitTxSizeBase() uint32 { | |
|
||
// FeeRate returns the current optimal fee rate in gwei / gas. | ||
func (eth *Backend) FeeRate(ctx context.Context) (uint64, error) { | ||
bigGP, err := eth.node.suggestGasPrice(ctx) | ||
tip, err := eth.node.suggestGasTipCap(ctx) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return dexeth.ToGwei(bigGP) | ||
hdr, err := eth.node.bestHeader(ctx) | ||
if err != nil { | ||
return 0, fmt.Errorf("error getting best block header from geth: %w", err) | ||
} | ||
base := misc.CalcBaseFee(eth.chainConfig, hdr) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
return dexeth.ToGwei(tip.Add(tip, base)) | ||
This comment has been minimized.
Sorry, something went wrong.
chappjc
Author
Member
|
||
} | ||
|
||
// BlockChannel creates and returns a new channel on which to receive block | ||
|
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
Arguably it is overkill to use consensus/misc to get the next base fee when we could just use hdr.BaseFee (current base fee) since we're just going to apply some multiplier and add suggested tip. The next base fee could just as easily be less than current as they could be more, so it's not that important since we are not actually using the computed next base fee for consensus, just a max gas fee for a transaction (which BTW is likely to be made many blocks in the future from this block, especially for the taker, but even for maker).