Skip to content

Commit

Permalink
[Issue-66] Fix different fee between fee option and estimate fee
Browse files Browse the repository at this point in the history
  • Loading branch information
S2kael committed May 30, 2024
1 parent 1572cb8 commit 9be9daa
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 47 deletions.
92 changes: 46 additions & 46 deletions packages/extension-base/src/koni/background/handlers/Extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2359,7 +2359,7 @@ export default class KoniExtension {
const substrateApi = this.#koniState.chainService.getSubstrateApi(chain);
let estimatedFee = '0';
let estimatedFeeMax = '0';
let feeOptions: FeeDetail;
let feeOptions: FeeDetail | null = null;
let tip = '0';
let maxTransferable = new BigN(freeBalance.value);

Expand Down Expand Up @@ -2481,7 +2481,28 @@ export default class KoniExtension {
utxos
};

const recipients = to ? [to, address] : [address];
const recipients = transferAll ? [address] : [address, to || address];

const fallbackCalculate = (recipients: string[]) => {
const utxos = filterUneconomicalUtxos({
utxos: determineUtxosArgs.utxos,
feeRate: determineUtxosArgs.feeRate,
recipients,
sender: determineUtxosArgs.sender
});

const { txVBytes: vSize } = getSizeInfo({
inputLength: utxos.length,
sender: address,
recipients
});

return {
vSize,
maxTransferable: utxos.reduce((previous, input) => previous.plus(input.value), new BigN(0)),
estimatedFee: Math.ceil(determineUtxosArgs.feeRate * vSize).toString()
};
};

try {
const { fee: _estimatedFee, inputs } = transferAll ? determineUtxosForSpendAll(determineUtxosArgs) : determineUtxosForSpend(determineUtxosArgs);
Expand All @@ -2494,6 +2515,13 @@ export default class KoniExtension {
recipients
});

estimatedFee = new BigN(_estimatedFee).toFixed(0);
feeOptions = {
..._fee,
estimatedFee,
vSize
};

if (transferAll) {
estimatedFeeMax = new BigN(_estimatedFee).toFixed(0);
} else {
Expand All @@ -2503,57 +2531,29 @@ export default class KoniExtension {
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();
const fb = fallbackCalculate([to || address]);

maxTransferable = fb.maxTransferable;
estimatedFeeMax = fb.estimatedFee;
}
}

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 fb = fallbackCalculate([to || address]);

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();
maxTransferable = fb.maxTransferable;
estimatedFeeMax = fb.estimatedFee;

estimatedFee = _estimatedFee;
estimatedFeeMax = _estimatedFee;
if (!feeOptions) {
const fb = fallbackCalculate([address, to || address]);

console.log('catch', maxTransferable.toString(), estimatedFeeMax.toString());
estimatedFee = fb.estimatedFee;

feeOptions = {
..._fee,
estimatedFee,
vSize
};
feeOptions = {
..._fee,
estimatedFee,
vSize: fb.vSize
};
}
}
} else {
const [mockTx] = await createTransferExtrinsic({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export function determineUtxosForSpend ({ amount,
sizeInfo = getSizeInfo({
inputLength: neededUtxos.length,
sender,
recipients: [recipient, sender]
recipients
});
}

Expand Down

2 comments on commit 9be9daa

@saltict
Copy link

Choose a reason for hiding this comment

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

@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.