Skip to content

Commit

Permalink
Add network fee on the quote calculation
Browse files Browse the repository at this point in the history
		I've added 2 methods in order to calculate the full tx amounts using the external
		Network fee value
  • Loading branch information
ronaldsg20 committed Oct 21, 2024
1 parent 64e8b69 commit 234fddd
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 13 deletions.
12 changes: 11 additions & 1 deletion src/common/types/Flyover/PeginQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
}
10 changes: 5 additions & 5 deletions src/pegin/components/create/ConfirmTx.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}));
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/pegin/components/create/ConfirmationSteps.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions src/pegin/components/create/PegInForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/pegin/components/create/PeginOptionCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
},
Expand Down
15 changes: 14 additions & 1 deletion tests/unit/common/PeginQuote.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Check warning on line 70 in tests/unit/common/PeginQuote.spec.ts

View workflow job for this annotation

GitHub Actions / checkout-and-build

This line has a length of 108. Maximum allowed is 100
});

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());

Check warning on line 76 in tests/unit/common/PeginQuote.spec.ts

View workflow job for this annotation

GitHub Actions / checkout-and-build

This line has a length of 108. Maximum allowed is 100
});

});

0 comments on commit 234fddd

Please sign in to comment.