Skip to content

Commit

Permalink
Accepting and broadcasting pegin flyover
Browse files Browse the repository at this point in the history
		I've updated the peginForm in order to handle both pegin types
		and accepting the peginQuote before the tx are built by the WalletService.
  • Loading branch information
ronaldsg20 authored and lserra-iov committed Jul 1, 2024
1 parent 9da8386 commit 4797161
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/common/store/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const PEGIN_TX_STOP_ASKING_FOR_BALANCE = 'PEGIN_TX_STOP_ASKING_FOR_BALANC
export const PEGIN_TX_ADD_STATUS_SAFE_FEE = 'PEGIN_TX_ADD_STATUS_SAFE_FEE';
export const PEGIN_TX_ADD_STATUS_REFUND_ADDRESS = 'PEGIN_TX_ADD_STATUS_REFUND_ADDRESS';
export const PEGIN_TX_ADD_STATUS_TX_ID = 'PEGIN_TX_ADD_STATUS_TX_ID';
export const PEGIN_TX_ADD_PEGIN_TYPE = 'PEGIN_TX_ADD_PEGIN_TYPE';

// PegOut actions
export const PEGOUT_TX_INIT = 'PEGOUT_TX_INIT';
Expand Down Expand Up @@ -89,6 +90,7 @@ export const FLYOVER_PEGIN_ADD_AMOUNT = 'FLYOVER_PEGIN_ADD_AMOUNT';
export const FLYOVER_PEGIN_ADD_ROOTSTOCK_ADDRESS = 'FLYOVER_PEGIN_ADD_ROOTSTOCK_ADDRESS';
export const FLYOVER_PEGIN_USE_LIQUIDITY_PROVIDER = 'FLYOVER_PEGIN_USE_LIQUIDITY_PROVIDER';
export const FLYOVER_PEGIN_GET_QUOTES = 'FLYOVER_PEGIN_GET_QUOTES';
export const FLYOVER_PEGIN_ADD_SELECTED_QUOTE = 'FLYOVER_PEGIN_ADD_SELECTED_QUOTE';

// Session actions
export const WEB3_SESSION_GET_ACCOUNT = 'WEB3_SESSION_GET_ACCOUNT';
Expand Down Expand Up @@ -129,6 +131,7 @@ export const PEGIN_TX_WALLET_SERVICE_SUBSCRIBE = 'PEGIN_TX_WALLET_SERVICE_SUBSCR
export const PEGIN_TX_SET_STATUS_SAFE_FEE = 'PEGIN_TX_SET_STATUS_SAFE_FEE';
export const PEGIN_TX_SET_STATUS_REFUND_ADDRESS = 'PEGIN_TX_SET_STATUS_REFUND_ADDRESS';
export const PEGIN_TX_SET_STATUS_TX_ID = 'PEGIN_TX_SET_STATUS_TX_ID';
export const PEGIN_TX_SET_PEGIN_TYPE = 'PEGIN_TX_SET_PEGIN_TYPE';

// PegOut mutations
export const PEGOUT_TX_SET_SELECTED_FEE_LEVEL = 'PEGOUT_TX_SET_SELECTED_FEE_LEVEL';
Expand Down Expand Up @@ -162,6 +165,7 @@ export const FLYOVER_PEGIN_SET_PROVIDERS = 'FLYOVER_PEGIN_SET_PROVIDERS';
export const FLYOVER_PEGIN_SET_AMOUNT = 'FLYOVER_PEGIN_SET_AMOUNT';
export const FLYOVER_PEGIN_SET_ROOTSTOCK_ADDRESS = 'FLYOVER_PEGIN_SET_ROOTSTOCK_ADDRESS';
export const FLYOVER_PEGIN_SET_QUOTES = 'FLYOVER_PEGIN_SET_QUOTES';
export const FLYOVER_PEGIN_SET_SELECTED_QUOTE = 'FLYOVER_PEGIN_SET_SELECTED_QUOTE';

// Session mutations
export const SESSION_SET_ACCOUNT = 'SESSION_SET_ACCOUNT';
Expand Down
2 changes: 2 additions & 0 deletions src/common/types/pegInTx.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AccountBalance, FeeAmountData, NormalizedTx } from '@/common/types/Common';
import SatoshiBig from '@/common/types/SatoshiBig';
import { WalletService } from '@/common/services';
import * as constants from '@/common/store/constants';

export type BtcAccount = 'BITCOIN_LEGACY_ADDRESS' |
'BITCOIN_SEGWIT_ADDRESS' |
Expand Down Expand Up @@ -72,6 +73,7 @@ export interface PegInTxState {
refundAddress: string;
safeFee: SatoshiBig;
}
peginType: constants.peginType;
}

export interface UnusedWalletAddress {
Expand Down
2 changes: 2 additions & 0 deletions src/common/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '@/common/types';
import { FlyoverService } from '@/common/services';
import { markRaw } from 'vue';
import * as constants from '@/common/store/constants';

export const getChunkedValue = (value: string, maxLength: number) => (value.length < maxLength ? value : `${value.substr(0, maxLength / 2)}...${value.substr(value.length - maxLength / 2, value.length)}`);

Expand Down Expand Up @@ -65,6 +66,7 @@ export const getClearPeginTxState = (): PegInTxState => ({
refundAddress: '',
safeFee: new SatoshiBig(0, 'btc'),
},
peginType: constants.peginType.POWPEG,
});

export const getClearPegoutTxState = (): PegOutTxState => ({
Expand Down
14 changes: 11 additions & 3 deletions src/pegin/components/create/PegInForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import {
import AddressWarningDialog from '@/common/components/exchange/AddressWarningDialog.vue';
import BtcFeeSelect from '@/pegin/components/create/BtcFeeSelect.vue';
import { BridgeService } from '@/common/services/BridgeService';
import { FlyoverService } from '@/common/services';
export default defineComponent({
name: 'PegInForm',
Expand All @@ -107,7 +108,7 @@ export default defineComponent({
const flyoverEnabled = ref(true);
const showOptions = ref(false);
const loadingQuotes = ref(false);
const selected = ref();
const selected = ref<constants.peginType>();
const selectedQuote = ref<QuotePegIn2WP>();
const account = useStateAttribute<string>('web3Session', 'account');
Expand All @@ -116,8 +117,12 @@ export default defineComponent({
const refundAddress = useGetter<string>('pegInTx', constants.PEGIN_TX_GET_REFUND_ADDRESS);
const enoughBalanceSelectedFee = useGetter<boolean>('pegInTx', constants.PEGIN_TX_GET_ENOUGH_FEE_VALUE);
const getPeginQuotes = useAction('flyoverPegin', constants.FLYOVER_PEGIN_GET_QUOTES);
const setSelectedQuote = useAction('flyoverPegin', constants.FLYOVER_PEGIN_ADD_SELECTED_QUOTE);
const session = useState<SessionState>('web3Session');
const quotes = useStateAttribute<Record<number, QuotePegIn2WP[]>>('flyoverPegin', 'quotes');
const setPeginType = useAction('pegInTx', constants.PEGIN_TX_ADD_PEGIN_TYPE);
const selectedQuoteHash = useStateAttribute<string>('flyoverPegin', 'selectedQuoteHash');
const flyoverService = useStateAttribute<FlyoverService>('flyoverPegin', 'flyoverService');
const isReadyToCreate = computed((): boolean => pegInTxState.value.isValidAmountToTransfer
&& !pegInTxState.value.loadingFee
Expand Down Expand Up @@ -158,6 +163,7 @@ export default defineComponent({
peginType: constants.peginType.POWPEG,
});
} else {
const acceptedQuote = await flyoverService.value.acceptPeginQuote(selectedQuoteHash.value);
context.emit('createTx', {
amountToTransferInSatoshi: selectedQuote.value?.quote.value
.plus(selectedQuote.value?.quote.productFeeAmount)
Expand All @@ -167,15 +173,17 @@ export default defineComponent({
recipient: '',
feeLevel: pegInTxState.value.selectedFee,
accountType: pegInTxState.value.selectedAccount,
btcRecipient: selectedQuote.value?.quote.lpBTCAddr,
btcRecipient: acceptedQuote.bitcoinDepositAddressHash,
peginType: constants.peginType.FLYOVER,
});
}
}
function changeSelectedOption(selectedType: constants.peginType, quote?: QuotePegIn2WP) {
async function changeSelectedOption(selectedType: constants.peginType, quote?: QuotePegIn2WP) {
selected.value = selectedType;
await setPeginType(selected.value);
selectedQuote.value = quote;
setSelectedQuote(quote?.quoteHash);
}
async function getQuotes() {
Expand Down
2 changes: 1 addition & 1 deletion src/pegin/components/create/SendBitcoin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import ConnectDevice from '@/common/components/exchange/ConnectDevice.vue';
import TxErrorDialog from '@/common/components/exchange/TxErrorDialog.vue';
import { TrezorError } from '@/common/types/exception/TrezorError';
import LeatherTxBuilder from '@/pegin/middleware/TxBuilder/LeatherTxBuilder';
import PeginTxService from '../../services/PeginTxService';
import PeginTxService from '@/pegin/services/PeginTxService';
export default defineComponent({
name: 'SendBitcoin',
Expand Down
3 changes: 3 additions & 0 deletions src/pegin/store/FlyoverPegin/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,7 @@ export const actions: ActionTree<FlyoverPeginState, RootState> = {
.then(() => resolve(commit(constants.FLYOVER_PEGIN_SET_QUOTES, quotesByProvider)))
.catch(reject);
}),
[constants.FLYOVER_PEGIN_ADD_SELECTED_QUOTE]: ({ commit }, quoteHash: string) => {
commit(constants.FLYOVER_PEGIN_SET_SELECTED_QUOTE, quoteHash);
},
};
3 changes: 3 additions & 0 deletions src/pegin/store/FlyoverPegin/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ export const mutations: MutationTree<FlyoverPeginState> = {
[constants.FLYOVER_PEGIN_SET_QUOTES]: (state, quotes: Record<number, Array<QuotePegIn2WP>>) => {
state.quotes = quotes;
},
[constants.FLYOVER_PEGIN_SET_SELECTED_QUOTE]: (state, quoteHash: string) => {
state.selectedQuoteHash = quoteHash;
},
};
3 changes: 3 additions & 0 deletions src/pegin/store/PeginTx/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,7 @@ export const actions: ActionTree<PegInTxState, RootState> = {
[constants.PEGIN_TX_ADD_STATUS_TX_ID]: ({ commit }, txId: string): void => {
commit(constants.PEGIN_TX_SET_STATUS_TX_ID, txId);
},
[constants.PEGIN_TX_ADD_PEGIN_TYPE]: ({ commit }, peginType: constants.peginType): void => {
commit(constants.PEGIN_TX_SET_PEGIN_TYPE, peginType);
},
};
3 changes: 3 additions & 0 deletions src/pegin/store/PeginTx/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,7 @@ export const mutations: MutationTree<PegInTxState> = {
[constants.PEGIN_TX_SET_STATUS_TX_ID]: (state, txId: string) => {
state.statusInfo.txId = txId;
},
[constants.PEGIN_TX_SET_PEGIN_TYPE]: (state, peginType: constants.peginType) => {
state.peginType = peginType;
},
};

0 comments on commit 4797161

Please sign in to comment.