@rango-dev/queue-manager-rango-preset 0.61.2-next.4 → 0.61.2-next.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/dist/actions/checkPrerequisites/checkPrerequisites.d.ts +5 -0
  2. package/dist/actions/checkPrerequisites/checkPrerequisites.d.ts.map +1 -0
  3. package/dist/actions/checkPrerequisites/index.d.ts +2 -0
  4. package/dist/actions/checkPrerequisites/index.d.ts.map +1 -0
  5. package/dist/actions/checkPrerequisites/utils.d.ts +21 -0
  6. package/dist/actions/checkPrerequisites/utils.d.ts.map +1 -0
  7. package/dist/actions/checkStellarTrustline/checkStellarTrustline.d.ts +5 -0
  8. package/dist/actions/checkStellarTrustline/checkStellarTrustline.d.ts.map +1 -0
  9. package/dist/actions/checkStellarTrustline/constants.d.ts +2 -0
  10. package/dist/actions/checkStellarTrustline/constants.d.ts.map +1 -0
  11. package/dist/actions/checkStellarTrustline/index.d.ts +3 -0
  12. package/dist/actions/checkStellarTrustline/index.d.ts.map +1 -0
  13. package/dist/actions/checkStellarTrustline/types.d.ts +9 -0
  14. package/dist/actions/checkStellarTrustline/types.d.ts.map +1 -0
  15. package/dist/actions/checkStellarTrustline/utils.d.ts +26 -0
  16. package/dist/actions/checkStellarTrustline/utils.d.ts.map +1 -0
  17. package/dist/actions/checkXrplTrustline/checkXrplTrustline.d.ts.map +1 -1
  18. package/dist/actions/checkXrplTrustline/constants.d.ts +2 -0
  19. package/dist/actions/checkXrplTrustline/constants.d.ts.map +1 -0
  20. package/dist/actions/checkXrplTrustline/index.d.ts +1 -1
  21. package/dist/actions/checkXrplTrustline/index.d.ts.map +1 -1
  22. package/dist/actions/checkXrplTrustline/utils.d.ts +20 -7
  23. package/dist/actions/checkXrplTrustline/utils.d.ts.map +1 -1
  24. package/dist/actions/checkXrplTrustlineTransactionStatus/checkXrplTrustlineTransactionStatus.d.ts +5 -0
  25. package/dist/actions/checkXrplTrustlineTransactionStatus/checkXrplTrustlineTransactionStatus.d.ts.map +1 -0
  26. package/dist/actions/checkXrplTrustlineTransactionStatus/constants.d.ts +3 -0
  27. package/dist/actions/checkXrplTrustlineTransactionStatus/constants.d.ts.map +1 -0
  28. package/dist/actions/checkXrplTrustlineTransactionStatus/index.d.ts +2 -0
  29. package/dist/actions/checkXrplTrustlineTransactionStatus/index.d.ts.map +1 -0
  30. package/dist/actions/executeStellarTransaction/executeStellarTransaction.d.ts +4 -0
  31. package/dist/actions/executeStellarTransaction/executeStellarTransaction.d.ts.map +1 -0
  32. package/dist/actions/executeStellarTransaction/helpers.d.ts +7 -0
  33. package/dist/actions/executeStellarTransaction/helpers.d.ts.map +1 -0
  34. package/dist/actions/executeStellarTransaction/index.d.ts +2 -0
  35. package/dist/actions/executeStellarTransaction/index.d.ts.map +1 -0
  36. package/dist/actions/executeXrplTransaction/executeXrplTransaction.d.ts.map +1 -1
  37. package/dist/actions/executeXrplTransaction/helpers.d.ts +5 -0
  38. package/dist/actions/executeXrplTransaction/helpers.d.ts.map +1 -1
  39. package/dist/actions/scheduleNextStep.d.ts.map +1 -1
  40. package/dist/helpers.d.ts +2 -1
  41. package/dist/helpers.d.ts.map +1 -1
  42. package/dist/index.js +1 -1
  43. package/dist/index.js.map +4 -4
  44. package/dist/queue-manager-rango-preset.build.json +1 -1
  45. package/dist/queueDef.d.ts.map +1 -1
  46. package/dist/types.d.ts +5 -1
  47. package/dist/types.d.ts.map +1 -1
  48. package/package.json +3 -2
  49. package/src/actions/checkPrerequisites/checkPrerequisites.ts +113 -0
  50. package/src/actions/checkPrerequisites/index.ts +1 -0
  51. package/src/actions/checkPrerequisites/utils.ts +124 -0
  52. package/src/actions/checkStellarTrustline/checkStellarTrustline.ts +237 -0
  53. package/src/actions/checkStellarTrustline/constants.ts +1 -0
  54. package/src/actions/checkStellarTrustline/index.ts +6 -0
  55. package/src/actions/checkStellarTrustline/types.ts +9 -0
  56. package/src/actions/checkStellarTrustline/utils.ts +200 -0
  57. package/src/actions/checkXrplTrustline/checkXrplTrustline.ts +147 -97
  58. package/src/actions/checkXrplTrustline/constants.ts +1 -0
  59. package/src/actions/checkXrplTrustline/index.ts +2 -1
  60. package/src/actions/checkXrplTrustline/utils.ts +134 -59
  61. package/src/actions/checkXrplTrustlineTransactionStatus/checkXrplTrustlineTransactionStatus.ts +145 -0
  62. package/src/actions/checkXrplTrustlineTransactionStatus/constants.ts +2 -0
  63. package/src/actions/checkXrplTrustlineTransactionStatus/index.ts +1 -0
  64. package/src/actions/executeStellarTransaction/executeStellarTransaction.ts +168 -0
  65. package/src/actions/executeStellarTransaction/helpers.ts +43 -0
  66. package/src/actions/executeStellarTransaction/index.ts +1 -0
  67. package/src/actions/executeXrplTransaction/executeXrplTransaction.ts +79 -47
  68. package/src/actions/executeXrplTransaction/helpers.ts +46 -0
  69. package/src/actions/scheduleNextStep.ts +2 -6
  70. package/src/helpers.ts +29 -0
  71. package/src/queueDef.ts +10 -1
  72. package/src/types.ts +8 -1
  73. package/dist/actions/executeXrplTransaction/constants.d.ts +0 -2
  74. package/dist/actions/executeXrplTransaction/constants.d.ts.map +0 -1
  75. package/src/actions/executeXrplTransaction/constants.ts +0 -1
@@ -2,63 +2,48 @@ import type { TargetToken } from './types';
2
2
  import type { SwapQueueContext, SwapStorage } from '../../types';
3
3
  import type { NextTransactionStateError } from '../common/produceNextStateForTransaction';
4
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';
5
+ import type {
6
+ GenericSigner,
7
+ XrplChangeTrustLinePrerequisite,
8
+ XrplChangeTrustLinePrerequisiteResult,
9
+ XrplTransaction,
10
+ } from 'rango-types';
11
+
12
+ import { TransactionType, XRPL_CHANGE_TRUSTLINE_TYPE } from 'rango-types';
13
+ import { Err } from 'ts-results';
7
14
 
8
15
  import {
9
16
  getCurrentStep,
10
17
  getCurrentStepTx,
11
18
  handleRejectedSign,
12
- updateStorageOnSuccessfulSign,
19
+ updateStorageWithPrerequisiteResult,
13
20
  } from '../../helpers';
14
- import { getCurrentAddressOf, getRelatedWallet } from '../../shared';
15
21
  import { SwapActionTypes } from '../../types';
16
- import { checkEnvironmentBeforeExecuteTransaction } from '../common/checkEnvironmentBeforeExecuteTransaction';
17
- import {
18
- onNextStateError,
19
- onNextStateOk,
20
- produceNextStateForTransaction,
21
- } from '../common/produceNextStateForTransaction';
22
+ import { onNextStateError } from '../common/produceNextStateForTransaction';
22
23
  import { requestBlockQueue } from '../common/utils';
23
24
 
24
25
  import {
25
- checkTruslineAlreadyOpened,
26
- createTrustlineTransaction,
26
+ checkIfTrustLineIsAlreadyOpened,
27
+ createTrustLineTransaction,
28
+ ensureRequiredXrplWalletIsConnected,
27
29
  ensureXrplNamespaceExists,
28
- ensureXrplTransactionIsValid,
30
+ getXrplWalletFromSwap,
29
31
  } from './utils';
30
32
 
31
33
  export async function checkXrplTrustline(
32
34
  actions: ExecuterActions<SwapStorage, SwapActionTypes, SwapQueueContext>
33
35
  ): 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
36
  const { failed, context, schedule, getStorage, next } = actions;
46
37
  const { meta, getSigners } = context;
47
38
 
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
39
  const onFinish = () => {
56
40
  if (actions.context.resetClaimedBy) {
57
41
  actions.context.resetClaimedBy();
58
42
  }
59
43
  };
60
- const onSuccessfulFinish = () => {
61
- schedule(SwapActionTypes.EXECUTE_XRPL_TRANSACTION);
44
+
45
+ const scheduleCheckPrerequisites = () => {
46
+ schedule(SwapActionTypes.CHECK_PREREQUISITES);
62
47
  next();
63
48
  onFinish();
64
49
  };
@@ -70,110 +55,175 @@ export async function checkXrplTrustline(
70
55
  };
71
56
 
72
57
  /*
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.
58
+ *
59
+ * 1. Ensure transaction is available.
60
+ *
75
61
  */
76
- const nextStateResult = produceNextStateForTransaction(actions);
62
+ const swap = getStorage().swapDetails;
63
+ const currentStep = getCurrentStep(swap)!;
64
+ const currentTransactionFromStorage = getCurrentStepTx(currentStep);
77
65
 
78
- if (nextStateResult.err) {
79
- handleErr(nextStateResult);
66
+ if (!currentTransactionFromStorage) {
67
+ handleErr(
68
+ new Err({
69
+ nextStatus: 'failed',
70
+ nextStepStatus: 'failed',
71
+ message: 'Unexpected Error: tx is null!',
72
+ details: undefined,
73
+ errorCode: 'CLIENT_UNEXPECTED_BEHAVIOUR',
74
+ })
75
+ );
80
76
  return;
81
77
  }
82
78
 
83
- // On success, we should update Swap object and also call notifier
84
- onNextStateOk(actions, nextStateResult.val);
85
-
86
79
  /*
87
80
  *
88
- * 2. Ensure tx is supported, and namespace exists.
81
+ * 2. Check if there is a prerequisite with XRPL_CHANGE_TRUSTLINE_TYPE that does not have any result.
89
82
  *
90
83
  */
91
- const transaction = await ensureXrplTransactionIsValid(
92
- currentTransactionFromStorage
93
- );
94
- if (transaction.err) {
95
- handleErr(transaction);
96
- return;
84
+ let unmetXrplChangeTrustLineMeta: {
85
+ prerequisite: XrplChangeTrustLinePrerequisite;
86
+ prerequisiteIndex: number;
87
+ } | null = null;
88
+
89
+ for (
90
+ let prerequisiteIndex = 0;
91
+ prerequisiteIndex < currentTransactionFromStorage.prerequisites.length;
92
+ prerequisiteIndex++
93
+ ) {
94
+ const prerequisite =
95
+ currentTransactionFromStorage.prerequisites[prerequisiteIndex];
96
+ if (prerequisite.type === XRPL_CHANGE_TRUSTLINE_TYPE) {
97
+ const prerequisiteResult = currentStep.prerequisiteResults?.find(
98
+ (
99
+ prerequisiteResult
100
+ ): prerequisiteResult is XrplChangeTrustLinePrerequisiteResult =>
101
+ prerequisiteResult.prerequisiteIndex === prerequisiteIndex &&
102
+ prerequisiteResult.prerequisiteType === XRPL_CHANGE_TRUSTLINE_TYPE
103
+ );
104
+
105
+ if (!prerequisiteResult) {
106
+ unmetXrplChangeTrustLineMeta = {
107
+ prerequisite,
108
+ prerequisiteIndex,
109
+ };
110
+ }
111
+ }
97
112
  }
98
113
 
99
- const namespace = await ensureXrplNamespaceExists(
100
- context,
101
- sourceWallet.walletType
102
- );
103
- if (namespace.err) {
104
- handleErr(namespace);
114
+ if (!unmetXrplChangeTrustLineMeta) {
115
+ scheduleCheckPrerequisites();
105
116
  return;
106
117
  }
107
118
 
108
119
  /*
109
- * 3. Do we need open a trustline for this transaction?
110
120
  *
111
- * Trust line only should be opened for issued token (not native), server is putting that data as prerequisite for us.
112
- * If there is no need for that, we are skipping this step and consider it as done.
121
+ * 3. Ensure XRPL wallet is connected.
122
+ *
113
123
  */
114
- const trustlinePrerequisite = transaction.val.prerequisites.find(
115
- (item) => item.type === 'XRPL_CHANGE_TRUSTLINE'
116
- );
117
- if (!trustlinePrerequisite) {
118
- onSuccessfulFinish();
124
+
125
+ const xrplWallet = getXrplWalletFromSwap(swap);
126
+ if (!xrplWallet) {
127
+ handleErr(
128
+ new Err({
129
+ nextStatus: 'failed',
130
+ nextStepStatus: 'failed',
131
+ message: 'Unexpected Error: XRPL wallet was not found in the swap!',
132
+ details: undefined,
133
+ errorCode: 'CLIENT_UNEXPECTED_BEHAVIOUR',
134
+ })
135
+ );
136
+ return;
137
+ }
138
+ const namespace = await ensureXrplNamespaceExists(context, xrplWallet.type);
139
+ if (namespace.err) {
140
+ handleErr(namespace);
141
+ return;
142
+ }
143
+
144
+ const ensureRequiredXrplWalletIsConnectedResult =
145
+ await ensureRequiredXrplWalletIsConnected(actions, xrplWallet);
146
+
147
+ if (ensureRequiredXrplWalletIsConnectedResult.err) {
148
+ requestBlockQueue(actions, ensureRequiredXrplWalletIsConnectedResult.val);
119
149
  return;
120
150
  }
121
151
 
122
152
  /*
123
153
  *
124
- * 4. Ensure trusline has been opened, then execute if needed.
154
+ * 3. Check if trust line is already opened.
125
155
  *
126
156
  */
127
- const chainId = meta.blockchains[transaction.val.blockChain]?.chainId;
128
- const walletSigners = await getSigners(sourceWallet.walletType);
129
157
 
130
158
  const token: TargetToken = {
131
- currency: trustlinePrerequisite.currency,
132
- account: trustlinePrerequisite.issuer,
133
- amount: trustlinePrerequisite.value,
159
+ currency: unmetXrplChangeTrustLineMeta.prerequisite.currency,
160
+ account: unmetXrplChangeTrustLineMeta.prerequisite.issuer,
161
+ amount: unmetXrplChangeTrustLineMeta.prerequisite.value,
134
162
  };
135
163
 
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
- trustlinePrerequisite.wallet,
164
+ const trustLineIsAlreadyOpenedResult = await checkIfTrustLineIsAlreadyOpened(
165
+ unmetXrplChangeTrustLineMeta.prerequisite.wallet,
139
166
  token,
140
167
  {
141
168
  namespace: namespace.val,
142
169
  }
143
170
  );
144
171
 
145
- if (!isTruslineAlreadyOpened) {
146
- const trustlineTx = createTrustlineTransaction(
147
- trustlinePrerequisite.wallet,
172
+ if (trustLineIsAlreadyOpenedResult.err) {
173
+ handleErr(trustLineIsAlreadyOpenedResult);
174
+ return;
175
+ }
176
+
177
+ if (trustLineIsAlreadyOpenedResult.val.trustLineIsAlreadyOpened) {
178
+ const prerequisiteResult: XrplChangeTrustLinePrerequisiteResult = {
179
+ prerequisiteIndex: unmetXrplChangeTrustLineMeta.prerequisiteIndex,
180
+ prerequisiteType: XRPL_CHANGE_TRUSTLINE_TYPE,
181
+ status: 'skipped',
182
+ data: null,
183
+ };
184
+ updateStorageWithPrerequisiteResult(actions, prerequisiteResult);
185
+ scheduleCheckPrerequisites();
186
+ return;
187
+ }
188
+
189
+ /*
190
+ *
191
+ * 4. Create trust line transaction.
192
+ *
193
+ */
194
+
195
+ try {
196
+ // Create trust line transaction with the required limit
197
+ const trustlineTransaction = createTrustLineTransaction(
198
+ unmetXrplChangeTrustLineMeta.prerequisite.wallet,
148
199
  token
149
200
  );
150
201
 
202
+ const chainId = meta.blockchains['XRPL']?.chainId;
203
+ const walletSigners = await getSigners(xrplWallet.type);
151
204
  const signer: GenericSigner<XrplTransaction> = walletSigners.getSigner(
152
- transaction.val.type
205
+ TransactionType.XRPL
153
206
  );
154
207
 
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
- );
208
+ const transactionResult = await signer.signAndSendTx(
209
+ trustlineTransaction,
210
+ xrplWallet.address,
211
+ chainId
212
+ );
165
213
 
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;
214
+ const prerequisiteResult: XrplChangeTrustLinePrerequisiteResult = {
215
+ prerequisiteIndex: unmetXrplChangeTrustLineMeta.prerequisiteIndex,
216
+ prerequisiteType: XRPL_CHANGE_TRUSTLINE_TYPE,
217
+ status: 'pending',
218
+ data: {
219
+ executedTransactionHash: transactionResult.hash,
220
+ },
221
+ };
222
+
223
+ updateStorageWithPrerequisiteResult(actions, prerequisiteResult);
224
+ scheduleCheckPrerequisites();
225
+ } catch (e) {
226
+ handleRejectedSign(actions)(e);
227
+ onFinish();
178
228
  }
179
229
  }
@@ -0,0 +1 @@
1
+ export const TRUST_LINE_INFINITE_VALUE = '10000000000000';
@@ -1,5 +1,6 @@
1
1
  export { checkXrplTrustline } from './checkXrplTrustline.js';
2
2
  export {
3
3
  ensureXrplNamespaceExists,
4
- ensureXrplTransactionIsValid,
4
+ ensureRequiredXrplWalletIsConnected,
5
+ getXrplWalletFromSwap,
5
6
  } from './utils.js';
@@ -1,50 +1,31 @@
1
1
  import type { TargetToken, XrplNamespace } from './types';
2
- import type { SwapQueueContext } from '../../types';
2
+ import type { TargetNamespace } from '../../shared';
3
3
  import type { NextTransactionStateError } from '../common/produceNextStateForTransaction';
4
- import type { Transaction } from 'rango-sdk';
5
- import type { XrplTransaction, XrplTrustSetTransactionData } from 'rango-types';
4
+ import type {
5
+ BlockedReason,
6
+ ExecuterActions,
7
+ } from '@rango-dev/queue-manager-core';
8
+ import type { WalletType } from '@rango-dev/wallets-shared';
9
+ import type { PendingSwap, XrplTransaction } from 'rango-types';
6
10
  import type { Result } from 'ts-results';
7
11
 
8
- import { isXrplTransaction } from 'rango-types';
12
+ import { legacyReadAccountAddress as readAccountAddress } from '@rango-dev/wallets-core/legacy';
13
+ import BigNumber from 'bignumber.js';
14
+ import { TransactionType } from 'rango-types';
9
15
  import { Err, Ok } from 'ts-results';
10
16
 
11
- export async function ensureXrplTransactionIsValid(
12
- tx: Transaction | null
13
- ): Promise<Result<XrplTransaction, NextTransactionStateError>> {
14
- if (!tx) {
15
- return new Err({
16
- nextStatus: 'failed',
17
- nextStepStatus: 'failed',
18
- message: 'Unexpected Error: tx is null!',
19
- details: undefined,
20
- errorCode: 'CLIENT_UNEXPECTED_BEHAVIOUR',
21
- });
22
- }
17
+ import {
18
+ ERROR_MESSAGE_WAIT_FOR_WALLET_DESCRIPTION,
19
+ ERROR_MESSAGE_WAIT_FOR_WALLET_DESCRIPTION_WRONG_WALLET,
20
+ } from '../../constants';
21
+ import {
22
+ BlockReason,
23
+ type SwapActionTypes,
24
+ type SwapQueueContext,
25
+ type SwapStorage,
26
+ } from '../../types';
23
27
 
24
- if (!isXrplTransaction(tx)) {
25
- return new Err({
26
- nextStatus: 'failed',
27
- nextStepStatus: 'failed',
28
- message:
29
- "Unexpected Error: Expected XRPL transaction but it doesn't match with the structure.",
30
- details: undefined,
31
- errorCode: 'CLIENT_UNEXPECTED_BEHAVIOUR',
32
- });
33
- }
34
-
35
- if (tx.data.TransactionType !== 'Payment') {
36
- return new Err({
37
- nextStatus: 'failed',
38
- nextStepStatus: 'failed',
39
- message:
40
- 'Unexpected Error: We only support XRPL transactions with payment type',
41
- details: undefined,
42
- errorCode: 'CLIENT_UNEXPECTED_BEHAVIOUR',
43
- });
44
- }
45
-
46
- return Ok(tx);
47
- }
28
+ import { TRUST_LINE_INFINITE_VALUE } from './constants';
48
29
 
49
30
  export async function ensureXrplNamespaceExists(
50
31
  context: SwapQueueContext,
@@ -65,34 +46,128 @@ export async function ensureXrplNamespaceExists(
65
46
  return Ok(xrplNamespace);
66
47
  }
67
48
 
68
- export async function checkTruslineAlreadyOpened(
49
+ export async function checkIfTrustLineIsAlreadyOpened(
69
50
  account: string,
70
51
  token: TargetToken,
71
52
  options: { namespace: XrplNamespace }
72
- ): Promise<boolean> {
73
- const lines = await options.namespace.accountLines(account, {
74
- peer: token.account,
75
- });
76
- return !!lines.some((trustline) => {
77
- return (
78
- trustline.currency === token.currency &&
79
- trustline.account === token.account &&
80
- trustline.limit !== '0'
53
+ ): Promise<
54
+ Result<{ trustLineIsAlreadyOpened: boolean }, NextTransactionStateError>
55
+ > {
56
+ try {
57
+ // Get account lines for the account
58
+ const accountLines = await options.namespace.accountLines(account, {
59
+ peer: token.account,
60
+ });
61
+
62
+ // Find the target account line
63
+ const targetAccountLine = accountLines.find(
64
+ (accountLine) =>
65
+ accountLine.currency === token.currency &&
66
+ accountLine.account === token.account
81
67
  );
82
- });
68
+
69
+ // If not account line is open on the target token, return false
70
+ if (!targetAccountLine) {
71
+ return new Ok({
72
+ trustLineIsAlreadyOpened: false,
73
+ });
74
+ }
75
+
76
+ const lineLimit = new BigNumber(targetAccountLine.limit);
77
+ const targetLimit = new BigNumber(TRUST_LINE_INFINITE_VALUE);
78
+
79
+ // If the account line is open, and contains a suitable limit, return true
80
+ return new Ok({
81
+ trustLineIsAlreadyOpened: lineLimit.gte(targetLimit),
82
+ });
83
+ } catch {
84
+ return new Err({
85
+ nextStatus: 'failed',
86
+ nextStepStatus: 'failed',
87
+ message: 'Could not get account lines from XRPL.',
88
+ details: undefined,
89
+ errorCode: 'CLIENT_UNEXPECTED_BEHAVIOUR',
90
+ });
91
+ }
83
92
  }
84
93
 
85
- export function createTrustlineTransaction(
94
+ export function createTrustLineTransaction(
86
95
  account: string,
87
96
  token: TargetToken
88
- ): XrplTrustSetTransactionData {
97
+ ): XrplTransaction {
89
98
  return {
90
- TransactionType: 'TrustSet',
91
- Account: account,
92
- LimitAmount: {
93
- currency: token.currency,
94
- issuer: token.account,
95
- value: token.amount,
99
+ type: TransactionType.XRPL,
100
+ blockChain: 'XRPL',
101
+ prerequisites: [],
102
+ data: {
103
+ TransactionType: 'TrustSet',
104
+ Account: account,
105
+ LimitAmount: {
106
+ currency: token.currency,
107
+ issuer: token.account,
108
+ value: TRUST_LINE_INFINITE_VALUE,
109
+ },
96
110
  },
97
111
  };
98
112
  }
113
+
114
+ export function getXrplWalletFromSwap(swap: PendingSwap): {
115
+ type: WalletType;
116
+ namespace: TargetNamespace;
117
+ address: string;
118
+ } | null {
119
+ const xrplWallet = swap.wallets['XRPL'];
120
+
121
+ if (!xrplWallet) {
122
+ return null;
123
+ }
124
+
125
+ return {
126
+ type: xrplWallet.walletType,
127
+ namespace: {
128
+ namespace: 'XRPL',
129
+ network: 'XRPL',
130
+ },
131
+ address: xrplWallet.address,
132
+ };
133
+ }
134
+
135
+ export async function ensureRequiredXrplWalletIsConnected(
136
+ actions: ExecuterActions<SwapStorage, SwapActionTypes, SwapQueueContext>,
137
+ requiredWallet: {
138
+ type: WalletType;
139
+ namespace: TargetNamespace;
140
+ address: string;
141
+ }
142
+ ): Promise<Result<true, BlockedReason>> {
143
+ const { context } = actions;
144
+
145
+ const walletState = context.state(requiredWallet.type);
146
+ const { accounts, connected } = walletState;
147
+
148
+ if (!connected) {
149
+ return new Err({
150
+ reason: BlockReason.WAIT_FOR_CONNECT_WALLET,
151
+ description: ERROR_MESSAGE_WAIT_FOR_WALLET_DESCRIPTION(
152
+ requiredWallet.type
153
+ ),
154
+ });
155
+ }
156
+
157
+ if (
158
+ !accounts?.find(
159
+ (account) =>
160
+ readAccountAddress(account).address === requiredWallet.address
161
+ )
162
+ ) {
163
+ return new Err({
164
+ reason: BlockReason.WAIT_FOR_CONNECT_WALLET,
165
+ description: ERROR_MESSAGE_WAIT_FOR_WALLET_DESCRIPTION_WRONG_WALLET(
166
+ requiredWallet.type,
167
+ requiredWallet.address
168
+ ),
169
+ });
170
+ }
171
+
172
+ return new Ok(true);
173
+ }