Skip to content

Commit

Permalink
Refactor: PeginQuote class definition
Browse files Browse the repository at this point in the history
		I've defined the PeginQUote class in order to assing all the required
		behavior to each specific quote object
  • Loading branch information
ronaldsg20 committed Oct 11, 2024
1 parent 9dd6969 commit 8fc7027
Show file tree
Hide file tree
Showing 14 changed files with 147 additions and 77 deletions.
19 changes: 4 additions & 15 deletions src/common/services/FlyoverService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '@rsksmart/flyover-sdk';
import * as constants from '@/common/store/constants';
import {
LiquidityProvider2WP, QuotePegIn2WP, QuotePegOut2WP,
LiquidityProvider2WP, PeginQuote, QuotePegOut2WP,
SatoshiBig, WeiBig,
} from '@/common/types';
import { providers } from 'ethers';
Expand Down Expand Up @@ -317,8 +317,8 @@ export default class FlyoverService {
rootstockRecipientAddress: string,
bitcoinRefundAddress: string,
valueToTransfer: SatoshiBig,
):Promise<Array<QuotePegIn2WP>> {
return new Promise<Array<QuotePegIn2WP>>((resolve, reject) => {
):Promise<Array<PeginQuote>> {
return new Promise<Array<PeginQuote>>((resolve, reject) => {
this.flyover?.getQuotes({
rskRefundAddress: rootstockRecipientAddress,
bitcoinRefundAddress,
Expand All @@ -335,18 +335,7 @@ export default class FlyoverService {
bitcoinRefundAddress,
valueToTransfer,
}))
.map(({ quote, quoteHash }: Quote) => ({
quote: {
...quote,
timeForDepositInSeconds: quote.timeForDeposit,
callFee: SatoshiBig.fromWeiBig(new WeiBig(quote.callFee ?? 0, 'wei')),
gasFee: new WeiBig(quote.gasFee ?? 0, 'wei'),
penaltyFee: new WeiBig(quote.penaltyFee ?? 0, 'wei'),
productFeeAmount: SatoshiBig.fromWeiBig(new WeiBig(quote.productFeeAmount ?? 0, 'wei')),
value: SatoshiBig.fromWeiBig(new WeiBig(quote.value ?? 0, 'wei')),
},
quoteHash,
}));
.map((quoteFromServer: Quote) => new PeginQuote(quoteFromServer));
resolve(peginQuotes);
})
.catch((error: Error) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LiquidityProviderBase } from '@rsksmart/flyover-sdk';
import WeiBig from './WeiBig';
import SatoshiBig from './SatoshiBig';
import WeiBig from '../WeiBig';
import SatoshiBig from '../SatoshiBig';

interface PeginProviderDetail {
fee: SatoshiBig;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { FlyoverService } from '../services';
import { LiquidityProvider2WP, QuotePegIn2WP } from './Flyover';
import SatoshiBig from './SatoshiBig';
import WeiBig from './WeiBig';
import { FlyoverService } from '../../services';
import { LiquidityProvider2WP } from './Flyover';
import SatoshiBig from '../SatoshiBig';
import WeiBig from '../WeiBig';
import PeginQuote from './PeginQuote';

export interface FlyoverPeginState {
amountToTransfer: SatoshiBig;
validAmount: boolean;
rootstockRecipientAddress: string;
valueToReceive: WeiBig;
liquidityProviders: LiquidityProvider2WP[];
quotes: Record<number, QuotePegIn2WP[]>;
quotes: Record<number, PeginQuote[]>;
flyoverService: FlyoverService;
txHash?: string;
selectedQuoteHash: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ObjectDifference } from '@/common/types';
import { FlyoverService } from '../services';
import { LiquidityProvider2WP, QuotePegOut2WP } from './Flyover';
import SatoshiBig from './SatoshiBig';
import WeiBig from './WeiBig';
import { LiquidityProvider2WP, ObjectDifference, QuotePegOut2WP } from '@/common/types';
import { FlyoverService } from '../../services';
import SatoshiBig from '../SatoshiBig';
import WeiBig from '../WeiBig';

export interface FlyoverPegoutState {
differences: Array<ObjectDifference>;
Expand Down
34 changes: 34 additions & 0 deletions src/common/types/Flyover/PeginQuote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Quote } from '@rsksmart/flyover-sdk';
import { PeginQuoteDTO2WP, QuotePegIn2WP } from './Flyover';
import SatoshiBig from '../SatoshiBig';
import WeiBig from '../WeiBig';

export default class PeginQuote implements QuotePegIn2WP {
quote: PeginQuoteDTO2WP;

quoteHash: string;

constructor({ quote, quoteHash }: Quote) {
this.quote = {
...quote,
timeForDepositInSeconds: quote.timeForDeposit,
callFee: SatoshiBig.fromWeiBig(new WeiBig(quote.callFee ?? 0, 'wei')),
gasFee: new WeiBig(quote.gasFee ?? 0, 'wei'),
penaltyFee: new WeiBig(quote.penaltyFee ?? 0, 'wei'),
productFeeAmount: SatoshiBig.fromWeiBig(new WeiBig(quote.productFeeAmount ?? 0, 'wei')),
value: SatoshiBig.fromWeiBig(new WeiBig(quote.value ?? 0, 'wei')),
};
this.quoteHash = quoteHash;
}

get totalValueToTransfer(): SatoshiBig {
return this.quote.value
.plus(this.providerFee);
}

get providerFee(): SatoshiBig {
return this.quote.productFeeAmount
.plus(this.quote.callFee)
.plus(SatoshiBig.fromWeiBig(this.quote.gasFee));
}
}
4 changes: 4 additions & 0 deletions src/common/types/Flyover/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './Flyover';
export * from './FlyoverPegout';
export * from './FlyoverPegin';
export { default as PeginQuote } from './PeginQuote';
2 changes: 0 additions & 2 deletions src/common/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,4 @@ export { default as WeiBig } from './WeiBig';
export * from './Feature';
export * from './ethers';
export * from './Flyover';
export * from './FlyoverPegout';
export * from './TxInfo';
export * from './FlyoverPegin';
4 changes: 2 additions & 2 deletions src/common/types/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import SatoshiBig from '@/common/types/SatoshiBig';
import { PegInTxState } from '@/common/types/pegInTx';
import { SessionState } from '@/common/types/session';
import { PegOutTxState } from '@/common/types/pegOutTx';
import { FlyoverPeginState } from '@/common/types/FlyoverPegin';
import { FlyoverPegoutState } from '@/common/types/FlyoverPegout';
import { FlyoverPeginState } from '@/common/types/Flyover/FlyoverPegin';
import { FlyoverPegoutState } from '@/common/types/Flyover/FlyoverPegout';
import { StatusState } from '@/common/types/Status';

export interface RootState {
Expand Down
15 changes: 5 additions & 10 deletions src/pegin/components/create/ConfirmTx.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ import { useState, useGetter, useStateAttribute } from '@/common/store/helper';
import {
LiquidityProvider2WP,
NormalizedSummary,
PegInTxState, QuotePegIn2WP, SatoshiBig, TxStatusType, TxSummaryOrientation,
PeginQuote,
PegInTxState, SatoshiBig, TxStatusType, TxSummaryOrientation,
} from '@/common/types';
import EnvironmentContextProviderService from '@/common/providers/EnvironmentContextProvider';
import { mdiInformation, mdiArrowLeft, mdiArrowRight } from '@mdi/js';
Expand Down Expand Up @@ -83,7 +84,7 @@ export default defineComponent({
const safeFee = useGetter<SatoshiBig>('pegInTx', constants.PEGIN_TX_GET_SAFE_TX_FEE);
const sessionId = useStateAttribute<string>('pegInTx', 'sessionId');
const isHdWallet = useGetter<boolean>('pegInTx', constants.PEGIN_TX_IS_HD_WALLET);
const selectedQuote = useGetter<QuotePegIn2WP>('flyoverPegin', constants.FLYOVER_PEGIN_GET_SELECTED_QUOTE);
const selectedQuote = useGetter<PeginQuote>('flyoverPegin', constants.FLYOVER_PEGIN_GET_SELECTED_QUOTE);
const liquidityProviders = useStateAttribute<LiquidityProvider2WP[]>('flyoverPegin', 'liquidityProviders');
const recipientAddress = useStateAttribute<string>('flyoverPegin', 'rootstockRecipientAddress');
const peginType = useStateAttribute<string>('pegInTx', 'peginType');
Expand Down Expand Up @@ -131,12 +132,6 @@ export default defineComponent({
return provider?.name ?? '';
}
function getProviderFee(): SatoshiBig {
return selectedQuote.value.quote.productFeeAmount
.plus(selectedQuote.value.quote.callFee)
.plus(SatoshiBig.fromWeiBig(selectedQuote.value.quote.gasFee));
}
const txPeginSummary = computed((): NormalizedSummary => ({
amountFromString: pegInTxState.value.amountToTransfer.toBTCTrimmedString(),
amountReceivedString: pegInTxState.value.amountToTransfer.toBTCTrimmedString(),
Expand All @@ -146,13 +141,13 @@ export default defineComponent({
senderAddress: pegInTxState.value.normalizedTx.inputs[0].address,
}));
const flyoverTotalFee = computed(() => getProviderFee()
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),
fee: Number(flyoverTotalFee.value.toBTCTrimmedString()),
total: selectedQuote.value.quote.value.plus(flyoverTotalFee.value).toBTCTrimmedString(),
recipientAddress: recipientAddress.value,
senderAddress: pegInTxState.value.normalizedTx.inputs[0].address,
Expand Down
12 changes: 3 additions & 9 deletions src/pegin/components/create/ConfirmationSteps.vue
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ import { computed, defineComponent } from 'vue';
import SatoshiBig from '@/common/types/SatoshiBig';
import EnvironmentContextProviderService from '@/common/providers/EnvironmentContextProvider';
import { PegInTxState } from '@/common/types/pegInTx';
import { QuotePegIn2WP } from '@/common/types';
import { PeginQuote } from '@/common/types';
import * as constants from '@/common/store/constants';
import { truncateStringToSize, copyToClipboard, getBtcAddressExplorerUrl } from '@/common/utils';
import { useGetter, useState } from '@/common/store/helper';
Expand All @@ -434,18 +434,12 @@ export default defineComponent({
const pegInTxState = useState<PegInTxState>('pegInTx');
const safeFee = useGetter<SatoshiBig>('pegInTx', constants.PEGIN_TX_GET_SAFE_TX_FEE);
const walletName = useGetter<string>('pegInTx', constants.WALLET_NAME);
const selectedQuote = useGetter<QuotePegIn2WP>('flyoverPegin', constants.FLYOVER_PEGIN_GET_SELECTED_QUOTE);
const selectedQuote = useGetter<PeginQuote>('flyoverPegin', constants.FLYOVER_PEGIN_GET_SELECTED_QUOTE);
const flyover = computed(() => pegInTxState.value.peginType === constants.peginType.FLYOVER);
const changeIdx = flyover.value ? 1 : 2;
function getProviderFee(): SatoshiBig {
return selectedQuote.value.quote.productFeeAmount
.plus(selectedQuote.value.quote.callFee)
.plus(SatoshiBig.fromWeiBig(selectedQuote.value.quote.gasFee));
}
const amountToTransfer = computed(() => (flyover.value
? selectedQuote.value.quote.value.plus(getProviderFee())
? selectedQuote.value.quote.value.plus(selectedQuote.value.providerFee)
: pegInTxState.value.amountToTransfer));
const rskFederationAddress = computed(():string => pegInTxState.value
Expand Down
20 changes: 6 additions & 14 deletions src/pegin/components/create/PegInForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ import EnvironmentContextProviderService from '@/common/providers/EnvironmentCon
import { TxStatusType } from '@/common/types/store';
import { TxSummaryOrientation } from '@/common/types/Status';
import {
Feature, FeatureNames, FlyoverPeginState, QuotePegIn2WP, SatoshiBig,
Feature, FeatureNames, FlyoverPeginState, PeginQuote, QuotePegIn2WP, SatoshiBig,
} from '@/common/types';
import {
useAction, useGetter, useState, useStateAttribute,
Expand Down Expand Up @@ -123,7 +123,7 @@ export default defineComponent({
const showOptions = ref(false);
const loadingQuotes = ref(false);
const selected = ref<constants.peginType>();
const selectedQuote = ref<QuotePegIn2WP>();
const selectedQuote = ref<PeginQuote>();
const showErrorDialog = ref(false);
const txError = ref<ServiceError>(new ServiceError('', '', '', ''));
Expand All @@ -144,16 +144,11 @@ export default defineComponent({
const selectedAccountBalance = useGetter<SatoshiBig>('pegInTx', constants.PEGIN_TX_GET_SELECTED_BALANCE);
const enoughAmountFlyover = computed(() => {
const quote = selectedQuote.value?.quote;
if (!quote) {
if (!selectedQuote.value) {
return false;
}
const fullAmount: SatoshiBig = quote?.value
.plus(quote.productFeeAmount)
.plus(quote.callFee)
.plus(SatoshiBig.fromWeiBig(quote.gasFee))
const fullAmount: SatoshiBig = selectedQuote.value.totalValueToTransfer
.plus(selectedFee.value);
return selectedAccountBalance.value?.gte(fullAmount);
});
Expand Down Expand Up @@ -220,10 +215,7 @@ export default defineComponent({
flyoverService.value.acceptPeginQuote(selectedQuoteHash.value)
.then((acceptedQuote) => {
context.emit('createTx', {
amountToTransferInSatoshi: selectedQuote.value?.quote.value
.plus(selectedQuote.value?.quote.productFeeAmount)
.plus(selectedQuote.value?.quote.callFee)
.plus(SatoshiBig.fromWeiBig(selectedQuote.value?.quote.gasFee)),
amountToTransferInSatoshi: selectedQuote.value?.totalValueToTransfer,
refundAddress: '',
recipient: '',
feeLevel: pegInTxState.value.selectedFee,
Expand All @@ -236,7 +228,7 @@ export default defineComponent({
pegInFormState.value.send('fill');
}
async function changeSelectedOption(selectedType: constants.peginType, quote?: QuotePegIn2WP) {
async function changeSelectedOption(selectedType: constants.peginType, quote?: PeginQuote) {
selected.value = selectedType;
await setPeginType(selected.value);
selectedQuote.value = quote;
Expand Down
20 changes: 9 additions & 11 deletions src/pegin/components/create/PeginOptionCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ import { useGetter, useState, useStateAttribute } from '@/common/store/helper';
import { blockConfirmationsToTimeString, truncateString } from '@/common/utils';
import RskAddressInput from '@/pegin/components/create/RskAddressInput.vue';
import EnvironmentContextProviderService from '@/common/providers/EnvironmentContextProvider';
import { PegInTxState, QuotePegIn2WP, SatoshiBig } from '@/common/types';
import { PeginQuote, PegInTxState, SatoshiBig } from '@/common/types';
export default defineComponent({
name: 'PeginOptionCard',
Expand All @@ -87,7 +87,7 @@ export default defineComponent({
default: false,
},
quote: {
type: Object as PropType<QuotePegIn2WP>,
type: Object as PropType<PeginQuote>,
},
},
setup(props, context) {
Expand All @@ -99,13 +99,11 @@ export default defineComponent({
const bitcoinPrice = useStateAttribute<number>('pegInTx', 'bitcoinPrice');
const fixedUSDDecimals = 2;
const quote = computed(() => props.quote?.quote);
const computedQuote = computed(() => props.quote);
const quoteFee = computed(() => {
if (!quote.value) return new SatoshiBig('0', 'btc');
return quote.value.productFeeAmount
.plus(quote.value.callFee)
.plus(SatoshiBig.fromWeiBig(quote.value.gasFee));
if (!computedQuote.value) return new SatoshiBig('0', 'btc');
return computedQuote.value.providerFee;
});
const PeginOptions = {
Expand All @@ -125,11 +123,11 @@ export default defineComponent({
label: 'Powered by PowPeg + Flyover',
subtitleColor: 'orange',
link: 'https://dev.rootstock.io/concepts/rif-suite/#meet-the-suite',
estimatedTime: () => blockConfirmationsToTimeString(quote.value?.confirmations ?? 0, 'btc'),
amountToTransfer: () => quote.value?.value
.plus(quoteFee.value).plus(selectedFee.value) ?? new SatoshiBig('0', 'btc'),
estimatedTime: () => blockConfirmationsToTimeString(computedQuote.value?.quote.confirmations ?? 0, 'btc'),
amountToTransfer: () => computedQuote.value?.totalValueToTransfer
.plus(selectedFee.value) ?? new SatoshiBig('0', 'btc'),
providerFee: () => quoteFee.value,
valueToReceive: () => quote.value?.value ?? new SatoshiBig('0', 'btc'),
valueToReceive: () => computedQuote.value?.quote.value ?? new SatoshiBig('0', 'btc'),
},
};
const option = computed(() => PeginOptions[props.optionType as keyof typeof PeginOptions]);
Expand Down
4 changes: 2 additions & 2 deletions src/pegin/store/FlyoverPegin/mutations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
FlyoverPeginState, LiquidityProvider2WP, QuotePegIn2WP, SatoshiBig,
FlyoverPeginState, LiquidityProvider2WP, PeginQuote, SatoshiBig,
} from '@/common/types';
import { MutationTree } from 'vuex';
import * as constants from '@/common/store/constants';
Expand All @@ -14,7 +14,7 @@ export const mutations: MutationTree<FlyoverPeginState> = {
[constants.FLYOVER_PEGIN_SET_PROVIDERS]: (state, providers: Array<LiquidityProvider2WP>) => {
state.liquidityProviders = providers;
},
[constants.FLYOVER_PEGIN_SET_QUOTES]: (state, quotes: Record<number, Array<QuotePegIn2WP>>) => {
[constants.FLYOVER_PEGIN_SET_QUOTES]: (state, quotes: Record<number, Array<PeginQuote>>) => {
state.quotes = quotes;
},
[constants.FLYOVER_PEGIN_SET_SELECTED_QUOTE]: (state, quoteHash: string) => {
Expand Down
Loading

0 comments on commit 8fc7027

Please sign in to comment.