@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
@@ -0,0 +1,237 @@
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 {
6
+ GenericSigner,
7
+ StellarChangeTrustLinePrerequisite,
8
+ StellarChangeTrustLinePrerequisiteResult,
9
+ StellarTransaction,
10
+ } from 'rango-types';
11
+
12
+ import { STELLAR_CHANGE_TRUSTLINE_TYPE, TransactionType } from 'rango-types';
13
+ import { Err } from 'ts-results';
14
+
15
+ import {
16
+ getCurrentStep,
17
+ getCurrentStepTx,
18
+ handleRejectedSign,
19
+ updateStorageWithPrerequisiteResult,
20
+ } from '../../helpers';
21
+ import { SwapActionTypes } from '../../types';
22
+ import { onNextStateError } from '../common/produceNextStateForTransaction';
23
+ import { requestBlockQueue } from '../common/utils';
24
+
25
+ import {
26
+ checkIfTrustLineIsAlreadyOpened,
27
+ createTrustLineTransaction,
28
+ ensureRequiredStellarWalletIsConnected,
29
+ ensureStellarNamespaceExists,
30
+ getStellarWalletFromSwap,
31
+ } from './utils';
32
+
33
+ export async function checkStellarTrustline(
34
+ actions: ExecuterActions<SwapStorage, SwapActionTypes, SwapQueueContext>
35
+ ): Promise<void> {
36
+ const { failed, context, schedule, getStorage, next } = actions;
37
+ const { meta, getSigners } = context;
38
+
39
+ const onFinish = () => {
40
+ if (actions.context.resetClaimedBy) {
41
+ actions.context.resetClaimedBy();
42
+ }
43
+ };
44
+
45
+ const scheduleCheckPrerequisites = () => {
46
+ schedule(SwapActionTypes.CHECK_PREREQUISITES);
47
+ next();
48
+ onFinish();
49
+ };
50
+
51
+ const handleErr = (err: Err<NextTransactionStateError>) => {
52
+ onNextStateError(actions, err.val);
53
+ failed();
54
+ onFinish();
55
+ };
56
+
57
+ /*
58
+ *
59
+ * 1. Ensure transaction is available.
60
+ *
61
+ */
62
+ const swap = getStorage().swapDetails;
63
+ const currentStep = getCurrentStep(swap)!;
64
+ const currentTransactionFromStorage = getCurrentStepTx(currentStep);
65
+
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
+ );
76
+ return;
77
+ }
78
+
79
+ /*
80
+ *
81
+ * 2. Check if there is a prerequisite with STELLAR_CHANGE_TRUSTLINE_TYPE that does not have any result.
82
+ *
83
+ */
84
+ let unmetStellarChangeTrustLineMeta: {
85
+ prerequisite: StellarChangeTrustLinePrerequisite;
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 === STELLAR_CHANGE_TRUSTLINE_TYPE) {
97
+ const prerequisiteResult = currentStep.prerequisiteResults.find(
98
+ (
99
+ prerequisiteResult
100
+ ): prerequisiteResult is StellarChangeTrustLinePrerequisiteResult =>
101
+ prerequisiteResult.prerequisiteIndex === prerequisiteIndex &&
102
+ prerequisiteResult.prerequisiteType === STELLAR_CHANGE_TRUSTLINE_TYPE
103
+ );
104
+
105
+ if (!prerequisiteResult) {
106
+ unmetStellarChangeTrustLineMeta = {
107
+ prerequisite,
108
+ prerequisiteIndex,
109
+ };
110
+ break;
111
+ }
112
+ }
113
+ }
114
+
115
+ if (!unmetStellarChangeTrustLineMeta) {
116
+ scheduleCheckPrerequisites();
117
+ return;
118
+ }
119
+
120
+ /*
121
+ *
122
+ * 3. Ensure Stellar wallet is connected.
123
+ *
124
+ */
125
+
126
+ const stellarWallet = getStellarWalletFromSwap(swap);
127
+ if (!stellarWallet) {
128
+ handleErr(
129
+ new Err({
130
+ nextStatus: 'failed',
131
+ nextStepStatus: 'failed',
132
+ message: 'Unexpected Error: Stellar wallet was not found in the swap!',
133
+ details: undefined,
134
+ errorCode: 'CLIENT_UNEXPECTED_BEHAVIOUR',
135
+ })
136
+ );
137
+ return;
138
+ }
139
+
140
+ const namespace = await ensureStellarNamespaceExists(
141
+ context,
142
+ stellarWallet.type
143
+ );
144
+ if (namespace.err) {
145
+ handleErr(namespace);
146
+ return;
147
+ }
148
+
149
+ const ensureRequiredStellarWalletIsConnectedResult =
150
+ await ensureRequiredStellarWalletIsConnected(actions, stellarWallet);
151
+
152
+ if (ensureRequiredStellarWalletIsConnectedResult.err) {
153
+ requestBlockQueue(
154
+ actions,
155
+ ensureRequiredStellarWalletIsConnectedResult.val
156
+ );
157
+ return;
158
+ }
159
+
160
+ /*
161
+ *
162
+ * 3. Check if trust line is already opened.
163
+ *
164
+ */
165
+
166
+ const token: TargetToken = {
167
+ code: unmetStellarChangeTrustLineMeta.prerequisite.code,
168
+ issuer: unmetStellarChangeTrustLineMeta.prerequisite.issuer,
169
+ value: unmetStellarChangeTrustLineMeta.prerequisite.value,
170
+ };
171
+
172
+ const trustLineIsAlreadyOpenedResult = await checkIfTrustLineIsAlreadyOpened(
173
+ unmetStellarChangeTrustLineMeta.prerequisite.wallet,
174
+ token,
175
+ {
176
+ namespace: namespace.val,
177
+ }
178
+ );
179
+
180
+ if (trustLineIsAlreadyOpenedResult.err) {
181
+ handleErr(trustLineIsAlreadyOpenedResult);
182
+ return;
183
+ }
184
+
185
+ if (trustLineIsAlreadyOpenedResult.val.trustLineIsAlreadyOpened) {
186
+ const prerequisiteResult: StellarChangeTrustLinePrerequisiteResult = {
187
+ prerequisiteIndex: unmetStellarChangeTrustLineMeta.prerequisiteIndex,
188
+ prerequisiteType: STELLAR_CHANGE_TRUSTLINE_TYPE,
189
+ status: 'skipped',
190
+ data: null,
191
+ };
192
+ updateStorageWithPrerequisiteResult(actions, prerequisiteResult);
193
+ scheduleCheckPrerequisites();
194
+ return;
195
+ }
196
+
197
+ /*
198
+ *
199
+ * 4. Create trust line transaction.
200
+ *
201
+ */
202
+
203
+ try {
204
+ // Create trust line transaction with the required limit
205
+ const trustlineTransaction = await createTrustLineTransaction(
206
+ unmetStellarChangeTrustLineMeta.prerequisite.wallet,
207
+ token
208
+ );
209
+
210
+ const chainId = meta.blockchains['Stellar']?.chainId;
211
+ const walletSigners = await getSigners(stellarWallet.type);
212
+ const signer: GenericSigner<StellarTransaction> = walletSigners.getSigner(
213
+ TransactionType.STELLAR
214
+ );
215
+
216
+ const transactionResult = await signer.signAndSendTx(
217
+ trustlineTransaction,
218
+ stellarWallet.address,
219
+ chainId
220
+ );
221
+
222
+ const prerequisiteResult: StellarChangeTrustLinePrerequisiteResult = {
223
+ prerequisiteIndex: unmetStellarChangeTrustLineMeta.prerequisiteIndex,
224
+ prerequisiteType: STELLAR_CHANGE_TRUSTLINE_TYPE,
225
+ status: 'success',
226
+ data: {
227
+ executedTransactionHash: transactionResult.hash,
228
+ },
229
+ };
230
+
231
+ updateStorageWithPrerequisiteResult(actions, prerequisiteResult);
232
+ scheduleCheckPrerequisites();
233
+ } catch (e) {
234
+ handleRejectedSign(actions)(e);
235
+ onFinish();
236
+ }
237
+ }
@@ -0,0 +1 @@
1
+ export const TRUST_LINE_INFINITE_VALUE = '922337203685.4775807'; // https://stellar-sdk.readthedocs.io/en/stable/_modules/stellar_sdk/operation/change_trust.html
@@ -0,0 +1,6 @@
1
+ export { checkStellarTrustline } from './checkStellarTrustline.js';
2
+ export {
3
+ ensureRequiredStellarWalletIsConnected,
4
+ ensureStellarNamespaceExists,
5
+ getStellarWalletFromSwap,
6
+ } from './utils.js';
@@ -0,0 +1,9 @@
1
+ import type { ProxiedNamespace } from '@rango-dev/wallets-core';
2
+ import type { StellarActions } from '@rango-dev/wallets-core/namespaces/stellar';
3
+
4
+ export type StellarNamespace = ProxiedNamespace<StellarActions>;
5
+ export type TargetToken = {
6
+ code: string;
7
+ issuer: string;
8
+ value: string;
9
+ };
@@ -0,0 +1,200 @@
1
+ import type { StellarNamespace, TargetToken } from './types';
2
+ import type { TargetNamespace } from '../../shared';
3
+ import type { NextTransactionStateError } from '../common/produceNextStateForTransaction';
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, StellarTransaction } from 'rango-types';
10
+ import type { Result } from 'ts-results';
11
+
12
+ import { legacyReadAccountAddress as readAccountAddress } from '@rango-dev/wallets-core/legacy';
13
+ import * as StellarSdk from '@stellar/stellar-sdk';
14
+ import BigNumber from 'bignumber.js';
15
+ import { TransactionType } from 'rango-types';
16
+ import { Err, Ok } from 'ts-results';
17
+
18
+ import {
19
+ ERROR_MESSAGE_WAIT_FOR_WALLET_DESCRIPTION,
20
+ ERROR_MESSAGE_WAIT_FOR_WALLET_DESCRIPTION_WRONG_WALLET,
21
+ } from '../../constants';
22
+ import {
23
+ BlockReason,
24
+ type SwapActionTypes,
25
+ type SwapQueueContext,
26
+ type SwapStorage,
27
+ } from '../../types';
28
+
29
+ import { TRUST_LINE_INFINITE_VALUE } from './constants';
30
+
31
+ export async function ensureStellarNamespaceExists(
32
+ context: SwapQueueContext,
33
+ walletType: string
34
+ ): Promise<Result<StellarNamespace, NextTransactionStateError>> {
35
+ // We need to work with namespace instance
36
+ const provider = context.hubProvider(walletType);
37
+ const stellarNamespace = provider.get('stellar');
38
+ if (!stellarNamespace) {
39
+ return new Err({
40
+ nextStatus: 'failed',
41
+ nextStepStatus: 'failed',
42
+ message: 'Stellar is not available on your wallet.',
43
+ details: undefined,
44
+ errorCode: 'CLIENT_UNEXPECTED_BEHAVIOUR',
45
+ });
46
+ }
47
+ return Ok(stellarNamespace);
48
+ }
49
+
50
+ export async function checkIfTrustLineIsAlreadyOpened(
51
+ account: string,
52
+ token: TargetToken,
53
+ options: { namespace: StellarNamespace }
54
+ ): Promise<
55
+ Result<{ trustLineIsAlreadyOpened: boolean }, NextTransactionStateError>
56
+ > {
57
+ try {
58
+ // Get account lines for the account
59
+ const accountLines = await options.namespace.balanceLines(account);
60
+
61
+ // Find the target account line
62
+ const targetAccountLine = accountLines
63
+ .filter(
64
+ (accountLine) =>
65
+ accountLine.asset_type !== 'native' &&
66
+ accountLine.asset_type !== 'liquidity_pool_shares'
67
+ )
68
+ .find(
69
+ (accountLine) =>
70
+ accountLine.asset_code === token.code &&
71
+ accountLine.asset_issuer === token.issuer
72
+ );
73
+
74
+ // If not account line is open on the target token, return false
75
+ if (!targetAccountLine) {
76
+ return new Ok({
77
+ trustLineIsAlreadyOpened: false,
78
+ });
79
+ }
80
+
81
+ const lineLimit = new BigNumber(targetAccountLine.limit);
82
+ const targetLimit = new BigNumber(TRUST_LINE_INFINITE_VALUE);
83
+
84
+ /*
85
+ * If the account line is open, and contains a suitable limit, return true
86
+ * TODO: we can add some logic to check if the target limit has enough capacity to hold the token amount,
87
+ * but it needs that we always get the correct needed limit value in prerequisites
88
+ */
89
+ return new Ok({
90
+ trustLineIsAlreadyOpened: lineLimit.gte(targetLimit),
91
+ });
92
+ } catch {
93
+ return new Err({
94
+ nextStatus: 'failed',
95
+ nextStepStatus: 'failed',
96
+ message: 'Could not get account lines from Stellar.',
97
+ details: undefined,
98
+ errorCode: 'CLIENT_UNEXPECTED_BEHAVIOUR',
99
+ });
100
+ }
101
+ }
102
+
103
+ export function createTrustLineTransaction(
104
+ _accountId: string,
105
+ token: TargetToken
106
+ ): StellarTransaction {
107
+ const asset = new StellarSdk.Asset(token.code, token.issuer);
108
+ const TRUST_LINE_FEE = '100';
109
+ const TRUST_LINE_TIMEOUT = 30;
110
+ // eslint-disable-next-line @typescript-eslint/no-magic-numbers
111
+ const now = Math.floor(Date.now() / 1000);
112
+
113
+ const operation = StellarSdk.Operation.changeTrust({
114
+ asset,
115
+ limit: TRUST_LINE_INFINITE_VALUE,
116
+ });
117
+
118
+ return {
119
+ type: TransactionType.STELLAR,
120
+ blockChain: 'Stellar',
121
+ prerequisites: [],
122
+ data: {
123
+ baseFee: TRUST_LINE_FEE,
124
+ memoXdrBase64: null,
125
+ preconditions: {
126
+ timeBounds: {
127
+ minTime: 0,
128
+ maxTime: now + TRUST_LINE_TIMEOUT,
129
+ },
130
+ ledgerBounds: { minLedger: 0, maxLedger: 0 },
131
+ minSeqNumber: null,
132
+ minSeqAge: null,
133
+ minSeqLedgerGap: null,
134
+ extraSigners: null,
135
+ },
136
+ operationsXdrBase64: [operation.toXDR('base64')],
137
+ },
138
+ };
139
+ }
140
+
141
+ export function getStellarWalletFromSwap(swap: PendingSwap): {
142
+ type: WalletType;
143
+ namespace: TargetNamespace;
144
+ address: string;
145
+ } | null {
146
+ const stellarWallet = swap.wallets['STELLAR'];
147
+
148
+ if (!stellarWallet) {
149
+ return null;
150
+ }
151
+
152
+ return {
153
+ type: stellarWallet.walletType,
154
+ namespace: {
155
+ namespace: 'Stellar',
156
+ network: 'Stellar',
157
+ },
158
+ address: stellarWallet.address,
159
+ };
160
+ }
161
+
162
+ export async function ensureRequiredStellarWalletIsConnected(
163
+ actions: ExecuterActions<SwapStorage, SwapActionTypes, SwapQueueContext>,
164
+ requiredWallet: {
165
+ type: WalletType;
166
+ namespace: TargetNamespace;
167
+ address: string;
168
+ }
169
+ ): Promise<Result<true, BlockedReason>> {
170
+ const { context } = actions;
171
+
172
+ const walletState = context.state(requiredWallet.type);
173
+ const { accounts, connected } = walletState;
174
+
175
+ if (!connected) {
176
+ return new Err({
177
+ reason: BlockReason.WAIT_FOR_CONNECT_WALLET,
178
+ description: ERROR_MESSAGE_WAIT_FOR_WALLET_DESCRIPTION(
179
+ requiredWallet.type
180
+ ),
181
+ });
182
+ }
183
+
184
+ if (
185
+ !accounts?.find(
186
+ (account) =>
187
+ readAccountAddress(account).address === requiredWallet.address
188
+ )
189
+ ) {
190
+ return new Err({
191
+ reason: BlockReason.WAIT_FOR_CONNECT_WALLET,
192
+ description: ERROR_MESSAGE_WAIT_FOR_WALLET_DESCRIPTION_WRONG_WALLET(
193
+ requiredWallet.type,
194
+ requiredWallet.address
195
+ ),
196
+ });
197
+ }
198
+
199
+ return new Ok(true);
200
+ }