-
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(connect): separate bitcoin and misc fee levels
- Loading branch information
Showing
8 changed files
with
169 additions
and
58 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
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,118 @@ | ||
// origin: https://github.com/trezor/connect/blob/develop/src/js/core/methods/tx/Fees.js | ||
|
||
// Non-bitcoin fees. Bitcoin fees are handled in bitcoin/BitcoinFees.ts | ||
|
||
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] }); | ||
if (response.eip1559) { | ||
type EipResponse1559Level = 'low' | 'medium' | 'high'; | ||
type Eip1559Level = 'low' | 'normal' | 'high'; | ||
const eip1559ResponseLevelKeys = [ | ||
'low', | ||
'medium', | ||
'high', | ||
] as EipResponse1559Level[]; | ||
|
||
const { eip1559 } = response; | ||
const eip1559Levels = eip1559ResponseLevelKeys.map(levelKey => { | ||
const level = eip1559[levelKey]; | ||
|
||
// We can't pass BaseFeePerGas to firmware, so we calculate the effective gas price here | ||
const calculatedMaxFeePerGas = BigNumber.minimum( | ||
new BigNumber(level?.maxFeePerGas || '0'), | ||
new BigNumber(eip1559.baseFeePerGas || '0').plus( | ||
level?.maxPriorityFeePerGas || '0', | ||
), | ||
).toFixed(); | ||
|
||
const label = | ||
levelKey === 'medium' | ||
? ('normal' as Eip1559Level) | ||
: (levelKey as Eip1559Level); | ||
|
||
return { | ||
label, | ||
maxFeePerGas: level?.maxFeePerGas || '0', | ||
effectiveGasPrice: calculatedMaxFeePerGas, | ||
maxPriorityFeePerGas: level?.maxPriorityFeePerGas || '0', | ||
baseFeePerGas: eip1559.baseFeePerGas, | ||
minWaitTimeEstimate: level?.minWaitTimeEstimate | ||
? level.minWaitTimeEstimate / 1000 | ||
: undefined, // Infura provides wait time in miliseconds | ||
maxWaitTimeEstimate: level?.maxWaitTimeEstimate | ||
? level.maxWaitTimeEstimate / 1000 | ||
: undefined, | ||
feePerUnit: '0', | ||
feeLimit: response.feeLimit, | ||
blocks: -1, | ||
}; | ||
}); | ||
|
||
this.levels = [...eip1559Levels]; | ||
} else { | ||
//misc coins should have only one FeeLevel (normal), for ethereum depends on availability of eip1559 | ||
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, 10)), | ||
).toString(), | ||
}; | ||
} | ||
} catch { | ||
// silent | ||
} | ||
|
||
return this.levels; | ||
} | ||
|
||
updateCustomFee(feePerUnit: string, effectiveGasPrice?: string, maxPriorityFeePerGas?: 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, | ||
maxPriorityFeePerGas, | ||
effectiveGasPrice, | ||
}); | ||
} | ||
} |
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