|
| 1 | +import type { |
| 2 | + SwapActionTypes, |
| 3 | + SwapQueueContext, |
| 4 | + SwapStorage, |
| 5 | +} from '../../types'; |
| 6 | +import type { ExecuterActions } from '@rango-dev/queue-manager-core'; |
| 7 | + |
| 8 | +import { |
| 9 | + type GenericSigner, |
| 10 | + isXrplTransaction, |
| 11 | + type XrplTransaction, |
| 12 | + type XrplTrustSetTransactionData, |
| 13 | +} from 'rango-types'; |
| 14 | + |
| 15 | +import { |
| 16 | + getCurrentStep, |
| 17 | + getCurrentStepTx, |
| 18 | + handleSuccessfulSign, |
| 19 | + handlRejectedSign, |
| 20 | + isApprovalCurrentStepTx, |
| 21 | +} from '../../helpers'; |
| 22 | +import { getCurrentAddressOf, getRelatedWallet } from '../../shared'; |
| 23 | +import { checkEnvironmentBeforeExecuteTransaction } from '../common/checkEnvironmentBeforeExecuteTransaction'; |
| 24 | +import { |
| 25 | + onNextStateError, |
| 26 | + onNextStateOk, |
| 27 | + produceNextStateForTransaction, |
| 28 | +} from '../common/produceNextStateForTransaction'; |
| 29 | +import { requestBlockQueue } from '../common/utils'; |
| 30 | + |
| 31 | +import { isIssuedCurrencyAmount } from './helpers'; |
| 32 | + |
| 33 | +export async function executeXrplTransaction( |
| 34 | + actions: ExecuterActions<SwapStorage, SwapActionTypes, SwapQueueContext> |
| 35 | +): Promise<void> { |
| 36 | + const checkResult = await checkEnvironmentBeforeExecuteTransaction(actions); |
| 37 | + if (checkResult.err) { |
| 38 | + requestBlockQueue(actions, checkResult.val); |
| 39 | + |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + const { failed, getStorage, context } = actions; |
| 44 | + const { meta, getSigners } = context; |
| 45 | + |
| 46 | + const swap = getStorage().swapDetails; |
| 47 | + const currentStep = getCurrentStep(swap)!; |
| 48 | + const isApproval = isApprovalCurrentStepTx(currentStep); |
| 49 | + const onFinish = () => { |
| 50 | + // TODO resetClaimedBy is undefined here |
| 51 | + if (actions.context.resetClaimedBy) { |
| 52 | + actions.context.resetClaimedBy(); |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + /* |
| 57 | + * Checking the current transaction state to determine the next step. |
| 58 | + * It will either be Err, indicating process should stop, or Ok, indicating process should continu. |
| 59 | + */ |
| 60 | + const nextStateResult = produceNextStateForTransaction(actions); |
| 61 | + |
| 62 | + if (nextStateResult.err) { |
| 63 | + onNextStateError(actions, nextStateResult.val); |
| 64 | + failed(); |
| 65 | + onFinish(); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + // On sucess, we should update Swap object and also call notifier |
| 70 | + onNextStateOk(actions, nextStateResult.val); |
| 71 | + |
| 72 | + // when we are producing next step, it will check to tx shouldn't be null. So ! here is safe. |
| 73 | + const tx = getCurrentStepTx(currentStep)!; |
| 74 | + |
| 75 | + if (!isXrplTransaction(tx)) { |
| 76 | + throw new Error('TODO: NEEDS TO FAILED AND THROW A MESSAGE USING HOOKS'); |
| 77 | + } |
| 78 | + |
| 79 | + const sourceWallet = getRelatedWallet(swap, currentStep); |
| 80 | + const walletAddress = getCurrentAddressOf(swap, currentStep); |
| 81 | + |
| 82 | + const chainId = meta.blockchains?.[tx.blockChain]?.chainId; |
| 83 | + const walletSigners = await getSigners(sourceWallet.walletType); |
| 84 | + |
| 85 | + if (tx.data.TransactionType !== 'Payment') { |
| 86 | + throw new Error('TODO: SHOULD FAILED Q correctly.'); |
| 87 | + } |
| 88 | + |
| 89 | + const transactionQueue: XrplTransaction[] = [tx]; |
| 90 | + if (isIssuedCurrencyAmount(tx.data.Amount)) { |
| 91 | + const trustlineTx: XrplTrustSetTransactionData = { |
| 92 | + TransactionType: 'TrustSet', |
| 93 | + Account: tx.data.Account, |
| 94 | + LimitAmount: { |
| 95 | + currency: tx.data.Amount.currency, |
| 96 | + issuer: tx.data.Amount.issuer, |
| 97 | + value: tx.data.Amount.value, |
| 98 | + }, |
| 99 | + }; |
| 100 | + transactionQueue.unshift({ |
| 101 | + ...tx, |
| 102 | + data: trustlineTx, |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + const signer: GenericSigner<XrplTransaction> = walletSigners.getSigner( |
| 107 | + tx.type |
| 108 | + ); |
| 109 | + |
| 110 | + // TODO: Check failuire scenario |
| 111 | + for (const transaction of transactionQueue) { |
| 112 | + await signer |
| 113 | + .signAndSendTx(transaction, walletAddress, chainId) |
| 114 | + .then( |
| 115 | + handleSuccessfulSign(actions, { |
| 116 | + isApproval, |
| 117 | + }), |
| 118 | + handlRejectedSign(actions) |
| 119 | + ) |
| 120 | + .finally(() => { |
| 121 | + onFinish(); |
| 122 | + }); |
| 123 | + } |
| 124 | +} |
0 commit comments