-
Notifications
You must be signed in to change notification settings - Fork 95
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
multi: Add fee estimate oracle for tatanka #2769
Open
ukane-philemon
wants to merge
3
commits into
decred:master
Choose a base branch
from
ukane-philemon:tatanka-external-wallet-fees
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
package txfee | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"decred.org/dcrdex/dex" | ||
) | ||
|
||
// Oracle provides transaction fees for all configured assets from external | ||
// sources. Fee estimate values are in atoms for dcr, gwei for ethereum, | ||
// satoshis for bitcoin and bitcoin clone blockchains (per byte sat), or the | ||
// lowest non-divisible unit in other non-Bitcoin blockchains. | ||
type Oracle struct { | ||
chainIDs []uint32 | ||
sources []*feeEstimateSource | ||
|
||
feeMtx sync.RWMutex | ||
ukane-philemon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
txFeeEstimates map[uint32]*Estimate | ||
|
||
listener chan<- map[uint32]*Estimate | ||
} | ||
|
||
// NewOracle returns a new instance of *Oracle. | ||
func NewOracle(net dex.Network, cfg Config, chainsIDs []uint32, listener chan<- map[uint32]*Estimate) (*Oracle, error) { | ||
if len(chainsIDs) == 0 { | ||
return nil, errors.New("provide chainIDs to fetch fee estimate for") | ||
} | ||
|
||
if net != dex.Mainnet && net != dex.Testnet { | ||
return nil, errors.New("fee estimate oracle is available for only mainnet and testnet") | ||
} | ||
|
||
o := &Oracle{ | ||
chainIDs: chainsIDs, | ||
sources: feeEstimateSources(net, cfg), | ||
txFeeEstimates: make(map[uint32]*Estimate), | ||
listener: listener, | ||
} | ||
|
||
for _, chainID := range chainsIDs { | ||
if sym := dex.BipIDSymbol(chainID); sym == "" { | ||
return nil, fmt.Errorf("chainID %d is invalid", chainID) | ||
} | ||
|
||
// Init chain. | ||
o.txFeeEstimates[chainID] = new(Estimate) | ||
} | ||
|
||
return o, nil | ||
} | ||
|
||
// FeeEstimates retrieves the current fee estimates. | ||
func (o *Oracle) FeeEstimates() map[uint32]*Estimate { | ||
o.feeMtx.RLock() | ||
defer o.feeMtx.RUnlock() | ||
feeEstimates := make(map[uint32]*Estimate, len(o.txFeeEstimates)) | ||
for chainID, feeEstimate := range o.txFeeEstimates { | ||
if feeEstimate.Value > 0 && time.Since(feeEstimate.LastUpdated) < FeeEstimateExpiry { | ||
fe := *feeEstimate | ||
feeEstimates[chainID] = &fe | ||
} | ||
} | ||
return feeEstimates | ||
} | ||
|
||
// calculateAverage calculates the average fee estimates and distributes the | ||
// result to all listeners. Returns indexes of newly reactivated sources that we | ||
// need to fetch fee estimate from. | ||
func (o *Oracle) calculateAverage() []int { | ||
var reActivatedSourceIndexes []int | ||
totalFeeEstimates := make(map[uint32]*feeSourceCount) | ||
for i := range o.sources { | ||
source := o.sources[i] | ||
if source.isDisabled() { | ||
if source.checkIfSourceCanReactivate() { | ||
reActivatedSourceIndexes = append(reActivatedSourceIndexes, i) | ||
} | ||
continue | ||
} | ||
|
||
source.mtx.Lock() | ||
estimates := source.feeEstimates | ||
source.mtx.Unlock() | ||
|
||
for chainID, feeEstimate := range estimates { | ||
if feeEstimate == 0 { | ||
continue | ||
} | ||
|
||
if _, ok := totalFeeEstimates[chainID]; !ok { | ||
totalFeeEstimates[chainID] = new(feeSourceCount) | ||
} | ||
|
||
totalFeeEstimates[chainID].totalSource++ | ||
totalFeeEstimates[chainID].totalFee += feeEstimate | ||
} | ||
} | ||
|
||
now := time.Now() | ||
o.feeMtx.Lock() | ||
broadCastTxFees := make(map[uint32]*Estimate, len(o.txFeeEstimates)) | ||
for chainID := range o.txFeeEstimates { | ||
if rateInfo := totalFeeEstimates[chainID]; rateInfo != nil { | ||
fee := rateInfo.totalFee / uint64(rateInfo.totalSource) | ||
if fee > 0 { | ||
o.txFeeEstimates[chainID].Value = fee | ||
o.txFeeEstimates[chainID].LastUpdated = now | ||
estimate := *o.txFeeEstimates[chainID] | ||
broadCastTxFees[chainID] = &estimate | ||
} | ||
} | ||
} | ||
o.feeMtx.Unlock() | ||
|
||
// Notify all listeners if we have rates to broadcast. | ||
if len(broadCastTxFees) > 0 { | ||
o.listener <- broadCastTxFees | ||
} | ||
|
||
fmt.Println(broadCastTxFees) | ||
|
||
return reActivatedSourceIndexes | ||
} | ||
|
||
// Run starts the tx fee oracle and blocks until the provided context is | ||
// canceled. | ||
func (o *Oracle) Run(ctx context.Context, log dex.Logger) { | ||
nSuccessfulSources := o.fetchFromAllSource(ctx, log) | ||
if nSuccessfulSources == 0 { | ||
log.Errorf("Failed to retrieve fee estimate, exiting fee estimate oracle...") | ||
return | ||
} | ||
o.calculateAverage() | ||
|
||
ticker := time.NewTicker(defaultFeeRefreshInterval) | ||
defer ticker.Stop() | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
|
||
case <-ticker.C: | ||
o.fetchFromAllSource(ctx, log) | ||
o.calculateAverage() | ||
} | ||
} | ||
} | ||
|
||
// fetchFromAllSource retrieves fee estimates from all fee estimate sources and | ||
// returns the number of sources that successfully returned a fee estimate. | ||
func (o *Oracle) fetchFromAllSource(ctx context.Context, log dex.Logger) int { | ||
var nSuccessfulSources int | ||
for i := range o.sources { | ||
source := o.sources[i] | ||
if source.isDisabled() { | ||
continue | ||
} | ||
|
||
if source.hasFeeEstimates() && source.isExpired() { | ||
source.deactivate(true) | ||
log.Errorf("Fee estimate source %q has been disabled due to lack of fresh data. It will be re-enabled after %0.f hours.", | ||
source.name, reactivationDuration.Hours()) | ||
continue | ||
} | ||
|
||
estimates, err := source.getFeeEstimate(ctx, log, o.chainIDs) | ||
ukane-philemon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
if isAuthError(err) { | ||
source.deactivate(false) | ||
log.Errorf("%s has been deactivated and cannot be auto reactivated due to %v", source.name, err) | ||
} else if isRateLimitError(err) { | ||
source.deactivate(true) | ||
log.Errorf("Fee estimate source %q has been disabled (Reason: %v). It will be re-enabled after %0.f hours.", | ||
source.name, err, reactivationDuration.Hours()) | ||
} else { | ||
log.Errorf("%s.getFeeEstimate error: %v", source.name, err) | ||
} | ||
continue | ||
} | ||
|
||
nSuccessfulSources++ | ||
source.mtx.Lock() | ||
source.feeEstimates = estimates | ||
source.lastRefresh = time.Now() | ||
source.mtx.Unlock() | ||
} | ||
|
||
return nSuccessfulSources | ||
} |
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.
They are all in the DEX
AtomicUnit
of the asset, right?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.
@ukane-philemon I though wei for eth was fine. It looks like it is returning wei.
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.
Not sure what you mean, but they are all in their smallest denom, except eth (gwei). But a review from @JoeGruffins suggests wei so yes.
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.
We use "atomic units" to refer to the integer units used with
asset.Wallet
andasset.Backend
. Howasset.Wallet
andasset.Backend
scale the "atomic units" internally is of no concern outside of the respective wallet packages, really. Well, and here I guess. That said, we already know that there are some problems with this system. Specifically, Polygon and other evm blockchains can have fee rates smaller than 1 gwei per gas. This means that for swaps, we have to lock at least 1 gwei per gas, even if the actual network rate is substantially lower. The rate actually assessed is limited by the block's base fee rate, of course, but fee rates can determine minimum lot sizes and mm bot spreads etc. Luckily, 1 gwei / gas is very, very small even for ethereum, so the effects are not really noticeable. I do think it's worth discussing alternative ways to encode fee rates and maybe values.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.
I think we should stick to gwei for eth for now.