-
-
Notifications
You must be signed in to change notification settings - Fork 278
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
Separate bitcoin and misc fee levels #17156
Open
enjojoy
wants to merge
1
commit into
develop
Choose a base branch
from
refactor-fee-levels
base: develop
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.
+229
−60
Open
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// origin: https://github.com/trezor/connect/blob/develop/src/js/core/methods/tx/Fees.js | ||
|
||
import { BigNumber } from '@trezor/utils/src/bigNumber'; | ||
|
||
import { Blockchain } from '../../backend/BlockchainLink'; | ||
import type { CoinInfo, FeeLevel } from '../../types'; | ||
|
||
type Blocks = Array<string | undefined>; | ||
|
||
export const findBlocksForFee = (feePerUnit: string, blocks: Blocks) => { | ||
const bn = new BigNumber(feePerUnit); | ||
// find first occurrence of value lower or equal than requested | ||
const lower = blocks.find(b => typeof b === 'string' && bn.gte(b)); | ||
if (!lower) return -1; | ||
|
||
// if not found get latest know value | ||
return blocks.indexOf(lower); | ||
}; | ||
|
||
export class MiscFeeLevels { | ||
coinInfo: CoinInfo; | ||
|
||
levels: FeeLevel[]; | ||
longTermFeeRate?: string; // long term fee rate is used by @trezor/utxo-lib composeTx module | ||
|
||
blocks: Blocks = []; | ||
|
||
constructor(coinInfo: CoinInfo) { | ||
this.coinInfo = coinInfo; | ||
this.levels = coinInfo.defaultFees; | ||
} | ||
|
||
async load(blockchain: Blockchain) { | ||
try { | ||
const [response] = await blockchain.estimateFee({ blocks: [1] }); | ||
|
||
//misc coins should have only one FeeLevel (normal) | ||
this.levels[0] = { | ||
...this.levels[0], | ||
...response, | ||
// validate `feePerUnit` from the backend | ||
// should be lower than `coinInfo.maxFee` and higher than `coinInfo.minFee` | ||
// xrp sends values from 1 to very high number occasionally | ||
// see: https://github.com/trezor/trezor-suite/blob/develop/packages/blockchain-link/src/workers/ripple/index.ts#L316 | ||
feePerUnit: Math.min( | ||
this.coinInfo.maxFee, | ||
Math.max(this.coinInfo.minFee, parseInt(response.feePerUnit ?? '0', 10)), | ||
).toString(), | ||
}; | ||
} catch { | ||
// silent | ||
} | ||
|
||
return this.levels; | ||
} | ||
|
||
updateCustomFee(feePerUnit: string) { | ||
// remove "custom" level from list | ||
this.levels = this.levels.filter(l => l.label !== 'custom'); | ||
// recreate "custom" level | ||
const blocks = findBlocksForFee(feePerUnit, this.blocks); | ||
this.levels.push({ | ||
label: 'custom', | ||
feePerUnit, | ||
blocks, | ||
}); | ||
} | ||
} |
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.
This misc looks a lot ethereum specific.
Maybe there should be some generic implementation and other should inherit from it or override it?
MiscFeeLevels (the generic one or even one more), BitcoinFeeLevels, EthereumFeeLevels
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 thought about this too for the future though, but I agree that it already looks too Ethereum specific that it makes sense to separate it even now.
I'll do it then
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.
Yeah, we've discussed this and you're right, but this was ok for #16342 and also it somehow keep the previous naming (
load
vsloadMisc
) 🤷