@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.
- package/dist/actions/checkPrerequisites/checkPrerequisites.d.ts +5 -0
- package/dist/actions/checkPrerequisites/checkPrerequisites.d.ts.map +1 -0
- package/dist/actions/checkPrerequisites/index.d.ts +2 -0
- package/dist/actions/checkPrerequisites/index.d.ts.map +1 -0
- package/dist/actions/checkPrerequisites/utils.d.ts +21 -0
- package/dist/actions/checkPrerequisites/utils.d.ts.map +1 -0
- package/dist/actions/checkStellarTrustline/checkStellarTrustline.d.ts +5 -0
- package/dist/actions/checkStellarTrustline/checkStellarTrustline.d.ts.map +1 -0
- package/dist/actions/checkStellarTrustline/constants.d.ts +2 -0
- package/dist/actions/checkStellarTrustline/constants.d.ts.map +1 -0
- package/dist/actions/checkStellarTrustline/index.d.ts +3 -0
- package/dist/actions/checkStellarTrustline/index.d.ts.map +1 -0
- package/dist/actions/checkStellarTrustline/types.d.ts +9 -0
- package/dist/actions/checkStellarTrustline/types.d.ts.map +1 -0
- package/dist/actions/checkStellarTrustline/utils.d.ts +26 -0
- package/dist/actions/checkStellarTrustline/utils.d.ts.map +1 -0
- package/dist/actions/checkXrplTrustline/checkXrplTrustline.d.ts.map +1 -1
- package/dist/actions/checkXrplTrustline/constants.d.ts +2 -0
- package/dist/actions/checkXrplTrustline/constants.d.ts.map +1 -0
- package/dist/actions/checkXrplTrustline/index.d.ts +1 -1
- package/dist/actions/checkXrplTrustline/index.d.ts.map +1 -1
- package/dist/actions/checkXrplTrustline/utils.d.ts +20 -7
- package/dist/actions/checkXrplTrustline/utils.d.ts.map +1 -1
- package/dist/actions/checkXrplTrustlineTransactionStatus/checkXrplTrustlineTransactionStatus.d.ts +5 -0
- package/dist/actions/checkXrplTrustlineTransactionStatus/checkXrplTrustlineTransactionStatus.d.ts.map +1 -0
- package/dist/actions/checkXrplTrustlineTransactionStatus/constants.d.ts +3 -0
- package/dist/actions/checkXrplTrustlineTransactionStatus/constants.d.ts.map +1 -0
- package/dist/actions/checkXrplTrustlineTransactionStatus/index.d.ts +2 -0
- package/dist/actions/checkXrplTrustlineTransactionStatus/index.d.ts.map +1 -0
- package/dist/actions/executeStellarTransaction/executeStellarTransaction.d.ts +4 -0
- package/dist/actions/executeStellarTransaction/executeStellarTransaction.d.ts.map +1 -0
- package/dist/actions/executeStellarTransaction/helpers.d.ts +7 -0
- package/dist/actions/executeStellarTransaction/helpers.d.ts.map +1 -0
- package/dist/actions/executeStellarTransaction/index.d.ts +2 -0
- package/dist/actions/executeStellarTransaction/index.d.ts.map +1 -0
- package/dist/actions/executeXrplTransaction/executeXrplTransaction.d.ts.map +1 -1
- package/dist/actions/executeXrplTransaction/helpers.d.ts +5 -0
- package/dist/actions/executeXrplTransaction/helpers.d.ts.map +1 -1
- package/dist/actions/scheduleNextStep.d.ts.map +1 -1
- package/dist/helpers.d.ts +2 -1
- package/dist/helpers.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +4 -4
- package/dist/queue-manager-rango-preset.build.json +1 -1
- package/dist/queueDef.d.ts.map +1 -1
- package/dist/types.d.ts +5 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/actions/checkPrerequisites/checkPrerequisites.ts +113 -0
- package/src/actions/checkPrerequisites/index.ts +1 -0
- package/src/actions/checkPrerequisites/utils.ts +124 -0
- package/src/actions/checkStellarTrustline/checkStellarTrustline.ts +237 -0
- package/src/actions/checkStellarTrustline/constants.ts +1 -0
- package/src/actions/checkStellarTrustline/index.ts +6 -0
- package/src/actions/checkStellarTrustline/types.ts +9 -0
- package/src/actions/checkStellarTrustline/utils.ts +200 -0
- package/src/actions/checkXrplTrustline/checkXrplTrustline.ts +147 -97
- package/src/actions/checkXrplTrustline/constants.ts +1 -0
- package/src/actions/checkXrplTrustline/index.ts +2 -1
- package/src/actions/checkXrplTrustline/utils.ts +134 -59
- package/src/actions/checkXrplTrustlineTransactionStatus/checkXrplTrustlineTransactionStatus.ts +145 -0
- package/src/actions/checkXrplTrustlineTransactionStatus/constants.ts +2 -0
- package/src/actions/checkXrplTrustlineTransactionStatus/index.ts +1 -0
- package/src/actions/executeStellarTransaction/executeStellarTransaction.ts +168 -0
- package/src/actions/executeStellarTransaction/helpers.ts +43 -0
- package/src/actions/executeStellarTransaction/index.ts +1 -0
- package/src/actions/executeXrplTransaction/executeXrplTransaction.ts +79 -47
- package/src/actions/executeXrplTransaction/helpers.ts +46 -0
- package/src/actions/scheduleNextStep.ts +2 -6
- package/src/helpers.ts +29 -0
- package/src/queueDef.ts +10 -1
- package/src/types.ts +8 -1
- package/dist/actions/executeXrplTransaction/constants.d.ts +0 -2
- package/dist/actions/executeXrplTransaction/constants.d.ts.map +0 -1
- 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 {
|
|
6
|
-
|
|
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
|
-
|
|
19
|
+
updateStorageWithPrerequisiteResult,
|
|
13
20
|
} from '../../helpers';
|
|
14
|
-
import { getCurrentAddressOf, getRelatedWallet } from '../../shared';
|
|
15
21
|
import { SwapActionTypes } from '../../types';
|
|
16
|
-
import {
|
|
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
|
-
|
|
26
|
-
|
|
26
|
+
checkIfTrustLineIsAlreadyOpened,
|
|
27
|
+
createTrustLineTransaction,
|
|
28
|
+
ensureRequiredXrplWalletIsConnected,
|
|
27
29
|
ensureXrplNamespaceExists,
|
|
28
|
-
|
|
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
|
-
|
|
61
|
-
|
|
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
|
-
*
|
|
74
|
-
*
|
|
58
|
+
*
|
|
59
|
+
* 1. Ensure transaction is available.
|
|
60
|
+
*
|
|
75
61
|
*/
|
|
76
|
-
const
|
|
62
|
+
const swap = getStorage().swapDetails;
|
|
63
|
+
const currentStep = getCurrentStep(swap)!;
|
|
64
|
+
const currentTransactionFromStorage = getCurrentStepTx(currentStep);
|
|
77
65
|
|
|
78
|
-
if (
|
|
79
|
-
handleErr(
|
|
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.
|
|
81
|
+
* 2. Check if there is a prerequisite with XRPL_CHANGE_TRUSTLINE_TYPE that does not have any result.
|
|
89
82
|
*
|
|
90
83
|
*/
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
|
|
100
|
-
|
|
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
|
-
*
|
|
112
|
-
*
|
|
121
|
+
* 3. Ensure XRPL wallet is connected.
|
|
122
|
+
*
|
|
113
123
|
*/
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
)
|
|
117
|
-
|
|
118
|
-
|
|
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
|
-
*
|
|
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:
|
|
132
|
-
account:
|
|
133
|
-
amount:
|
|
159
|
+
currency: unmetXrplChangeTrustLineMeta.prerequisite.currency,
|
|
160
|
+
account: unmetXrplChangeTrustLineMeta.prerequisite.issuer,
|
|
161
|
+
amount: unmetXrplChangeTrustLineMeta.prerequisite.value,
|
|
134
162
|
};
|
|
135
163
|
|
|
136
|
-
|
|
137
|
-
|
|
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 (
|
|
146
|
-
|
|
147
|
-
|
|
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
|
-
|
|
205
|
+
TransactionType.XRPL
|
|
153
206
|
);
|
|
154
207
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
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
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
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,50 +1,31 @@
|
|
|
1
1
|
import type { TargetToken, XrplNamespace } from './types';
|
|
2
|
-
import type {
|
|
2
|
+
import type { TargetNamespace } from '../../shared';
|
|
3
3
|
import type { NextTransactionStateError } from '../common/produceNextStateForTransaction';
|
|
4
|
-
import type {
|
|
5
|
-
|
|
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 {
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
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
|
|
49
|
+
export async function checkIfTrustLineIsAlreadyOpened(
|
|
69
50
|
account: string,
|
|
70
51
|
token: TargetToken,
|
|
71
52
|
options: { namespace: XrplNamespace }
|
|
72
|
-
): Promise<
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
|
94
|
+
export function createTrustLineTransaction(
|
|
86
95
|
account: string,
|
|
87
96
|
token: TargetToken
|
|
88
|
-
):
|
|
97
|
+
): XrplTransaction {
|
|
89
98
|
return {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
+
}
|