From 8336758cf1e571a5f3f08fbc695bc8c518288608 Mon Sep 17 00:00:00 2001 From: S2kael Date: Wed, 29 May 2024 19:56:33 +0700 Subject: [PATCH] [Issue-66] Fallback for estimate fee --- .../src/koni/background/handlers/Extension.ts | 101 +++++++++++++----- .../bitcoin/strategy/BlockStream/index.ts | 7 +- 2 files changed, 78 insertions(+), 30 deletions(-) diff --git a/packages/extension-base/src/koni/background/handlers/Extension.ts b/packages/extension-base/src/koni/background/handlers/Extension.ts index 86b09872f6..6f402cfb4b 100644 --- a/packages/extension-base/src/koni/background/handlers/Extension.ts +++ b/packages/extension-base/src/koni/background/handlers/Extension.ts @@ -40,7 +40,7 @@ import { isProposalExpired, isSupportWalletConnectChain, isSupportWalletConnectN import { ResultApproveWalletConnectSession, WalletConnectNotSupportRequest, WalletConnectSessionRequest } from '@subwallet/extension-base/services/wallet-connect-service/types'; import { AccountsStore } from '@subwallet/extension-base/stores'; import { BalanceJson, BitcoinFeeInfo, BitcoinFeeRate, BuyServiceInfo, BuyTokenInfo, DetermineUtxosForSpendArgs, EarningRewardJson, EvmEIP1995FeeOption, EvmFeeInfo, FeeChainType, FeeDetail, FeeInfo, GetFeeFunction, NominationPoolInfo, OptimalYieldPathParams, RequestEarlyValidateYield, RequestGetYieldPoolTargets, RequestStakeCancelWithdrawal, RequestStakeClaimReward, RequestSubmitTransfer, RequestSubscribeTransfer, RequestUnlockDotCheckCanMint, RequestUnlockDotSubscribeMintedData, RequestYieldLeave, RequestYieldStepSubmit, RequestYieldWithdrawal, ResponseGetYieldPoolTargets, ResponseSubscribeTransfer, SubstrateFeeInfo, ValidateYieldProcessParams, YieldPoolType } from '@subwallet/extension-base/types'; -import { combineBitcoinFee, combineEthFee, convertSubjectInfoToAddresses, createTransactionFromRLP, determineUtxosForSpend, determineUtxosForSpendAll, generateAccountProxyId, getSizeInfo, isSameAddress, keyringGetAccounts, reformatAddress, signatureToHex, Transaction as QrTransaction, uniqueStringArray } from '@subwallet/extension-base/utils'; +import { combineBitcoinFee, combineEthFee, convertSubjectInfoToAddresses, createTransactionFromRLP, determineUtxosForSpend, determineUtxosForSpendAll, filterUneconomicalUtxos, generateAccountProxyId, getSizeInfo, isSameAddress, keyringGetAccounts, reformatAddress, signatureToHex, Transaction as QrTransaction, uniqueStringArray } from '@subwallet/extension-base/utils'; import { parseContractInput, parseEvmRlp } from '@subwallet/extension-base/utils/eth/parseTransaction'; import { balanceFormatter, BN_ZERO, formatNumber } from '@subwallet/extension-base/utils/number'; import { MetadataDef } from '@subwallet/extension-inject/types'; @@ -2357,8 +2357,8 @@ export default class KoniExtension { const convertData = async (freeBalance: AmountData, fee: FeeInfo): Promise => { const substrateApi = this.#koniState.chainService.getSubstrateApi(chain); - let estimatedFee: string; - let estimatedFeeMax: string; + let estimatedFee = '0'; + let estimatedFeeMax = '0'; let feeOptions: FeeDetail; let tip = '0'; let maxTransferable = new BigN(freeBalance.value); @@ -2370,8 +2370,6 @@ export default class KoniExtension { if (!destinationTokenInfo) { const _fee = fee as SubstrateFeeInfo; - estimatedFee = '0'; - estimatedFeeMax = '0'; feeOptions = { ..._fee, estimatedFee @@ -2482,29 +2480,81 @@ export default class KoniExtension { sender: address, utxos }; - const { fee: _estimatedFee, inputs, outputs } = transferAll ? determineUtxosForSpendAll(determineUtxosArgs) : determineUtxosForSpend(determineUtxosArgs); - maxTransferable = inputs.reduce((previous, input) => previous.plus(input.value), new BigN(0)); + const recipients = to ? [to, address] : [address]; - const recipients = outputs.filter((o) => o.address).map((o) => o.address || ''); - const { txVBytes: vSize } = getSizeInfo({ - inputLength: inputs.length, - sender: address, - recipients: recipients - }); + try { + const { fee: _estimatedFee, inputs } = transferAll ? determineUtxosForSpendAll(determineUtxosArgs) : determineUtxosForSpend(determineUtxosArgs); - if (transferAll) { - estimatedFeeMax = new BigN(_estimatedFee).toFixed(0); - } else { - estimatedFeeMax = new BigN(determineUtxosForSpendAll(determineUtxosArgs).fee).toFixed(0); - } + maxTransferable = inputs.reduce((previous, input) => previous.plus(input.value), new BigN(0)); - estimatedFee = new BigN(_estimatedFee).toFixed(0); - feeOptions = { - ..._fee, - estimatedFee, - vSize - }; + const { txVBytes: vSize } = getSizeInfo({ + inputLength: inputs.length, + sender: address, + recipients + }); + + if (transferAll) { + estimatedFeeMax = new BigN(_estimatedFee).toFixed(0); + } else { + try { + const { fee: estimateFeeMax, inputs } = determineUtxosForSpendAll(determineUtxosArgs); + + maxTransferable = inputs.reduce((previous, input) => previous.plus(input.value), new BigN(0)); + estimatedFeeMax = new BigN(estimateFeeMax).toFixed(0); + } catch (_e) { + const utxos = filterUneconomicalUtxos({ + utxos: determineUtxosArgs.utxos, + feeRate: determineUtxosArgs.feeRate, + recipients, + sender: determineUtxosArgs.sender + }); + + const { txVBytes: vSize } = getSizeInfo({ + inputLength: utxos.length, + sender: address, + recipients + }); + + maxTransferable = utxos.reduce((previous, input) => previous.plus(input.value), new BigN(0)); + estimatedFeeMax = Math.ceil(determineUtxosArgs.feeRate * vSize).toString(); + } + } + + estimatedFee = new BigN(_estimatedFee).toFixed(0); + feeOptions = { + ..._fee, + estimatedFee, + vSize + }; + } catch (_e) { + const utxos = filterUneconomicalUtxos({ + utxos: determineUtxosArgs.utxos, + feeRate: determineUtxosArgs.feeRate, + recipients, + sender: determineUtxosArgs.sender + }); + + const { txVBytes: vSize } = getSizeInfo({ + inputLength: utxos.length, + sender: address, + recipients + }); + + maxTransferable = utxos.reduce((previous, input) => previous.plus(input.value), new BigN(0)); + const _estimatedFee = Math.ceil(determineUtxosArgs.feeRate * vSize).toString(); + + estimatedFee = _estimatedFee; + estimatedFeeMax = _estimatedFee; + + console.log('catch', maxTransferable.toString(), estimatedFeeMax.toString()); + + feeOptions = { + ..._fee, + estimatedFee, + vSize + }; + } } else { const [mockTx] = await createTransferExtrinsic({ from: address, @@ -2536,9 +2586,6 @@ export default class KoniExtension { } } } catch (e) { - estimatedFee = '0'; - estimatedFeeMax = '0'; - if (fee.type === 'substrate') { feeOptions = { ...fee, diff --git a/packages/extension-base/src/services/chain-service/handler/bitcoin/strategy/BlockStream/index.ts b/packages/extension-base/src/services/chain-service/handler/bitcoin/strategy/BlockStream/index.ts index 61b0c9f2ef..47190b5b8d 100644 --- a/packages/extension-base/src/services/chain-service/handler/bitcoin/strategy/BlockStream/index.ts +++ b/packages/extension-base/src/services/chain-service/handler/bitcoin/strategy/BlockStream/index.ts @@ -49,10 +49,11 @@ export class BlockStreamRequestStrategy extends BaseApiRequestStrategy implement } const blocks = (await rs.json()) as BlockStreamBlock[]; - const lenght = blocks.length; - const time = (blocks[0].timestamp - blocks[lenght - 1].timestamp) * 1000; + const length = blocks.length; + const sortedBlocks = blocks.sort((a, b) => b.timestamp - a.timestamp); + const time = (sortedBlocks[0].timestamp - sortedBlocks[length - 1].timestamp) * 1000; - return time / lenght; + return time / length; }, 0); }