From 234fddddfd2dccc917ae69780432a39549deefca Mon Sep 17 00:00:00 2001 From: ronaldsg Date: Wed, 16 Oct 2024 10:38:23 -0500 Subject: [PATCH] Add network fee on the quote calculation I've added 2 methods in order to calculate the full tx amounts using the external Network fee value --- src/common/types/Flyover/PeginQuote.ts | 12 +++++++++++- src/pegin/components/create/ConfirmTx.vue | 10 +++++----- src/pegin/components/create/ConfirmationSteps.vue | 2 +- src/pegin/components/create/PegInForm.vue | 5 ++--- src/pegin/components/create/PeginOptionCard.vue | 4 ++-- tests/unit/common/PeginQuote.spec.ts | 15 ++++++++++++++- 6 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/common/types/Flyover/PeginQuote.ts b/src/common/types/Flyover/PeginQuote.ts index ca93c5ea..5ef819c7 100644 --- a/src/common/types/Flyover/PeginQuote.ts +++ b/src/common/types/Flyover/PeginQuote.ts @@ -21,7 +21,7 @@ export default class PeginQuote implements QuotePegIn2WP { this.quoteHash = quoteHash; } - get totalValueToTransfer(): SatoshiBig { + get valueToTransfer(): SatoshiBig { return this.quote.value .plus(this.providerFee); } @@ -31,4 +31,14 @@ export default class PeginQuote implements QuotePegIn2WP { .plus(this.quote.callFee) .plus(SatoshiBig.fromWeiBig(this.quote.gasFee)); } + + getTotalQuoteFee(btcNetworkFee: SatoshiBig): SatoshiBig { + return this.providerFee + .plus(btcNetworkFee); + } + + getTotalTxAmount(btcNetworkFee: SatoshiBig): SatoshiBig { + return this.valueToTransfer + .plus(btcNetworkFee); + } } diff --git a/src/pegin/components/create/ConfirmTx.vue b/src/pegin/components/create/ConfirmTx.vue index cb6bbd62..a4f68b1d 100644 --- a/src/pegin/components/create/ConfirmTx.vue +++ b/src/pegin/components/create/ConfirmTx.vue @@ -141,14 +141,14 @@ export default defineComponent({ senderAddress: pegInTxState.value.normalizedTx.inputs[0].address, })); - const flyoverTotalFee = computed(() => selectedQuote.value.providerFee - .plus(safeFee.value)); + // const flyoverTotalFee = computed(() => selectedQuote.value.providerFee + // .plus(safeFee.value)); const flyoverPeginSummary = computed((): NormalizedSummary => ({ amountFromString: selectedQuote.value.quote.value.toBTCTrimmedString(), amountReceivedString: selectedQuote.value.quote.value.toBTCTrimmedString(), - fee: Number(flyoverTotalFee.value.toBTCTrimmedString()), - total: selectedQuote.value.quote.value.plus(flyoverTotalFee.value).toBTCTrimmedString(), + fee: Number(selectedQuote.value.getTotalQuoteFee(safeFee.value).toBTCTrimmedString()), + total: selectedQuote.value.getTotalTxAmount(safeFee.value).toBTCTrimmedString(), recipientAddress: recipientAddress.value, senderAddress: pegInTxState.value.normalizedTx.inputs[0].address, })); @@ -159,7 +159,7 @@ export default defineComponent({ const flyoverProps = computed(() => ({ value: Number(selectedQuote.value.quote.value.toBTCTrimmedString()), - fee: Number(flyoverTotalFee.value.toBTCTrimmedString()), + fee: Number(selectedQuote.value.getTotalQuoteFee(safeFee.value).toBTCTrimmedString()), provider: getLPName(), details: { senderAddress: pegInTxState.value.normalizedTx.inputs[0].address, diff --git a/src/pegin/components/create/ConfirmationSteps.vue b/src/pegin/components/create/ConfirmationSteps.vue index f7bd837f..76294b80 100644 --- a/src/pegin/components/create/ConfirmationSteps.vue +++ b/src/pegin/components/create/ConfirmationSteps.vue @@ -439,7 +439,7 @@ export default defineComponent({ const changeIdx = flyover.value ? 1 : 2; const amountToTransfer = computed(() => (flyover.value - ? selectedQuote.value.quote.value.plus(selectedQuote.value.providerFee) + ? selectedQuote.value.valueToTransfer : pegInTxState.value.amountToTransfer)); const rskFederationAddress = computed(():string => pegInTxState.value diff --git a/src/pegin/components/create/PegInForm.vue b/src/pegin/components/create/PegInForm.vue index 047006ba..d48f534e 100644 --- a/src/pegin/components/create/PegInForm.vue +++ b/src/pegin/components/create/PegInForm.vue @@ -147,8 +147,7 @@ export default defineComponent({ if (!selectedQuote.value) { return false; } - const fullAmount: SatoshiBig = selectedQuote.value.totalValueToTransfer - .plus(selectedFee.value); + const fullAmount: SatoshiBig = selectedQuote.value.getTotalTxAmount(selectedFee.value); return selectedAccountBalance.value?.gte(fullAmount); }); @@ -215,7 +214,7 @@ export default defineComponent({ flyoverService.value.acceptPeginQuote(selectedQuoteHash.value) .then((acceptedQuote) => { context.emit('createTx', { - amountToTransferInSatoshi: selectedQuote.value?.totalValueToTransfer, + amountToTransferInSatoshi: selectedQuote.value?.valueToTransfer, refundAddress: '', recipient: '', feeLevel: pegInTxState.value.selectedFee, diff --git a/src/pegin/components/create/PeginOptionCard.vue b/src/pegin/components/create/PeginOptionCard.vue index 7cfcd7c3..ea5580a1 100644 --- a/src/pegin/components/create/PeginOptionCard.vue +++ b/src/pegin/components/create/PeginOptionCard.vue @@ -124,8 +124,8 @@ export default defineComponent({ subtitleColor: 'orange', link: 'https://dev.rootstock.io/concepts/rif-suite/#meet-the-suite', estimatedTime: () => blockConfirmationsToTimeString(computedQuote.value?.quote.confirmations ?? 0, 'btc'), - amountToTransfer: () => computedQuote.value?.totalValueToTransfer - .plus(selectedFee.value) ?? new SatoshiBig('0', 'btc'), + amountToTransfer: () => computedQuote.value?.getTotalTxAmount(selectedFee.value) + ?? new SatoshiBig('0', 'btc'), providerFee: () => quoteFee.value, valueToReceive: () => computedQuote.value?.quote.value ?? new SatoshiBig('0', 'btc'), }, diff --git a/tests/unit/common/PeginQuote.spec.ts b/tests/unit/common/PeginQuote.spec.ts index c8a88f2e..105276a2 100644 --- a/tests/unit/common/PeginQuote.spec.ts +++ b/tests/unit/common/PeginQuote.spec.ts @@ -61,6 +61,19 @@ describe('PeginQuote', () => { it('should calculate totalValueToTransfer correctly', () => { const expectedTotalValueToTransfer = SatoshiBig.fromWeiBig(new WeiBig(quoteValues.value, 'wei')) .plus(peginQuote.providerFee); - expect(peginQuote.totalValueToTransfer.toString()).toEqual(expectedTotalValueToTransfer.toString()); + expect(peginQuote.valueToTransfer.toString()).toEqual(expectedTotalValueToTransfer.toString()); }); + + it('should calculate totalQuoteFee correctly', () => { + const btcNetworkFee = new SatoshiBig(100000000, 'satoshi'); + const expectedTotalQuoteFee = peginQuote.providerFee.plus(btcNetworkFee); + expect(peginQuote.getTotalQuoteFee(btcNetworkFee).toString()).toEqual(expectedTotalQuoteFee.toString()); + }); + + it('should calculate totalTxAmount correctly', () => { + const btcNetworkFee = new SatoshiBig(100000000, 'satoshi'); + const expectedTotalTxAmount = peginQuote.valueToTransfer.plus(btcNetworkFee); + expect(peginQuote.getTotalTxAmount(btcNetworkFee).toString()).toEqual(expectedTotalTxAmount.toString()); + }); + });