Skip to content

Commit

Permalink
[Issue-66] Fallback for estimate fee
Browse files Browse the repository at this point in the history
  • Loading branch information
S2kael committed May 29, 2024
1 parent 71e779b commit 8336758
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 30 deletions.
101 changes: 74 additions & 27 deletions packages/extension-base/src/koni/background/handlers/Extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -2357,8 +2357,8 @@ export default class KoniExtension {

const convertData = async (freeBalance: AmountData, fee: FeeInfo): Promise<ResponseSubscribeTransfer> => {
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);
Expand All @@ -2370,8 +2370,6 @@ export default class KoniExtension {
if (!destinationTokenInfo) {
const _fee = fee as SubstrateFeeInfo;

estimatedFee = '0';
estimatedFeeMax = '0';
feeOptions = {
..._fee,
estimatedFee
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -2536,9 +2586,6 @@ export default class KoniExtension {
}
}
} catch (e) {
estimatedFee = '0';
estimatedFeeMax = '0';

if (fee.type === 'substrate') {
feeOptions = {
...fee,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

1 comment on commit 8336758

@saltict
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.