|
| 1 | +import type { TargetToken } from './types'; |
| 2 | +import type { SwapQueueContext, SwapStorage } from '../../types'; |
| 3 | +import type { NextTransactionStateError } from '../common/produceNextStateForTransaction'; |
| 4 | +import type { ExecuterActions } from '@rango-dev/queue-manager-core'; |
| 5 | +import type { GenericSigner, XrplTransaction } from 'rango-types'; |
| 6 | +import type { Err } from 'ts-results'; |
| 7 | + |
| 8 | +import { |
| 9 | + getCurrentStep, |
| 10 | + getCurrentStepTx, |
| 11 | + handleRejectedSign, |
| 12 | + updateStorageOnSuccessfulSign, |
| 13 | +} from '../../helpers'; |
| 14 | +import { getCurrentAddressOf, getRelatedWallet } from '../../shared'; |
| 15 | +import { SwapActionTypes } from '../../types'; |
| 16 | +import { checkEnvironmentBeforeExecuteTransaction } from '../common/checkEnvironmentBeforeExecuteTransaction'; |
| 17 | +import { |
| 18 | + onNextStateError, |
| 19 | + onNextStateOk, |
| 20 | + produceNextStateForTransaction, |
| 21 | +} from '../common/produceNextStateForTransaction'; |
| 22 | +import { requestBlockQueue } from '../common/utils'; |
| 23 | + |
| 24 | +import { |
| 25 | + checkTruslineAlreadyOpened, |
| 26 | + createTrustlineTransaction, |
| 27 | + ensureXrplNamespaceExists, |
| 28 | + ensureXrplTransactionIsValid, |
| 29 | +} from './utils'; |
| 30 | + |
| 31 | +export async function checkXrplTrustline( |
| 32 | + actions: ExecuterActions<SwapStorage, SwapActionTypes, SwapQueueContext> |
| 33 | +): Promise<void> { |
| 34 | + /* |
| 35 | + * |
| 36 | + * 1. Ensure wallet is connected with a correct address. |
| 37 | + * |
| 38 | + */ |
| 39 | + const checkResult = await checkEnvironmentBeforeExecuteTransaction(actions); |
| 40 | + if (checkResult.err) { |
| 41 | + requestBlockQueue(actions, checkResult.val); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + const { failed, context, schedule, getStorage, next } = actions; |
| 46 | + const { meta, getSigners } = context; |
| 47 | + |
| 48 | + const swap = getStorage().swapDetails; |
| 49 | + const currentStep = getCurrentStep(swap)!; |
| 50 | + const currentTransactionFromStorage = getCurrentStepTx(currentStep); |
| 51 | + |
| 52 | + const sourceWallet = getRelatedWallet(swap, currentStep); |
| 53 | + const walletAddress = getCurrentAddressOf(swap, currentStep); |
| 54 | + |
| 55 | + const onFinish = () => { |
| 56 | + if (actions.context.resetClaimedBy) { |
| 57 | + actions.context.resetClaimedBy(); |
| 58 | + } |
| 59 | + }; |
| 60 | + const onSuccessfulFinish = () => { |
| 61 | + schedule(SwapActionTypes.EXECUTE_XRPL_TRANSACTION); |
| 62 | + next(); |
| 63 | + onFinish(); |
| 64 | + }; |
| 65 | + |
| 66 | + const handleErr = (err: Err<NextTransactionStateError>) => { |
| 67 | + onNextStateError(actions, err.val); |
| 68 | + failed(); |
| 69 | + onFinish(); |
| 70 | + }; |
| 71 | + |
| 72 | + /* |
| 73 | + * Checking the current transaction state to determine the next step. |
| 74 | + * It will either be Err, indicating process should stop, or Ok, indicating process should continue. |
| 75 | + */ |
| 76 | + const nextStateResult = produceNextStateForTransaction(actions); |
| 77 | + |
| 78 | + if (nextStateResult.err) { |
| 79 | + handleErr(nextStateResult); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + // On success, we should update Swap object and also call notifier |
| 84 | + onNextStateOk(actions, nextStateResult.val); |
| 85 | + |
| 86 | + /* |
| 87 | + * |
| 88 | + * 2. Ensure tx is supported, and namespace exists. |
| 89 | + * |
| 90 | + */ |
| 91 | + const transaction = await ensureXrplTransactionIsValid( |
| 92 | + currentTransactionFromStorage |
| 93 | + ); |
| 94 | + if (transaction.err) { |
| 95 | + handleErr(transaction); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + const namespace = await ensureXrplNamespaceExists( |
| 100 | + context, |
| 101 | + sourceWallet.walletType |
| 102 | + ); |
| 103 | + if (namespace.err) { |
| 104 | + handleErr(namespace); |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + /* |
| 109 | + * 3. Do we need open a trustline for this transaction? |
| 110 | + * |
| 111 | + * Trust line only should be opened for issued token (not native), server is putting that data as precondition for us. |
| 112 | + * If there is no need for that, we are skipping this step and consider it as done. |
| 113 | + */ |
| 114 | + const trustlinePrecondition = transaction.val.preconditions.find( |
| 115 | + (item) => item.type === 'XRPL_CHANGE_TRUSTLINE' |
| 116 | + ); |
| 117 | + if (!trustlinePrecondition) { |
| 118 | + onSuccessfulFinish(); |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + /* |
| 123 | + * |
| 124 | + * 4. Ensure trusline has been opened, then execute if needed. |
| 125 | + * |
| 126 | + */ |
| 127 | + const chainId = meta.blockchains[transaction.val.blockChain]?.chainId; |
| 128 | + const walletSigners = await getSigners(sourceWallet.walletType); |
| 129 | + |
| 130 | + const token: TargetToken = { |
| 131 | + currency: trustlinePrecondition.currency, |
| 132 | + account: trustlinePrecondition.issuer, |
| 133 | + amount: trustlinePrecondition.value, |
| 134 | + }; |
| 135 | + |
| 136 | + // TODO: it's better to add some logic around `balance` to ensure we have enough capacity for the trust line. Now we only check it's already open or not. |
| 137 | + const isTruslineAlreadyOpened = await checkTruslineAlreadyOpened( |
| 138 | + trustlinePrecondition.wallet, |
| 139 | + token, |
| 140 | + { |
| 141 | + namespace: namespace.val, |
| 142 | + } |
| 143 | + ); |
| 144 | + |
| 145 | + if (!isTruslineAlreadyOpened) { |
| 146 | + const trustlineTx = createTrustlineTransaction( |
| 147 | + trustlinePrecondition.wallet, |
| 148 | + token |
| 149 | + ); |
| 150 | + |
| 151 | + const signer: GenericSigner<XrplTransaction> = walletSigners.getSigner( |
| 152 | + transaction.val.type |
| 153 | + ); |
| 154 | + |
| 155 | + try { |
| 156 | + const trustlineTransaction: XrplTransaction = { |
| 157 | + ...transaction.val, |
| 158 | + data: trustlineTx, |
| 159 | + }; |
| 160 | + const result = await signer.signAndSendTx( |
| 161 | + trustlineTransaction, |
| 162 | + walletAddress, |
| 163 | + chainId |
| 164 | + ); |
| 165 | + |
| 166 | + updateStorageOnSuccessfulSign(actions, result, { |
| 167 | + // TODO: approval has different meaning for EVM, we may need to add a third type called trustline for the following function. |
| 168 | + isApproval: true, |
| 169 | + }); |
| 170 | + onSuccessfulFinish(); |
| 171 | + } catch (e) { |
| 172 | + handleRejectedSign(actions)(e); |
| 173 | + onFinish(); |
| 174 | + } |
| 175 | + } else { |
| 176 | + onSuccessfulFinish(); |
| 177 | + return; |
| 178 | + } |
| 179 | +} |
0 commit comments