Skip to content

Commit 3a71bc0

Browse files
committed
feat: integrating gemwallet
1 parent a2af722 commit 3a71bc0

File tree

37 files changed

+674
-27
lines changed

37 files changed

+674
-27
lines changed

queue-manager/rango-preset/src/actions/createTransaction.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { SwapQueueContext, SwapStorage } from '../types';
22
import type { ExecuterActions } from '@rango-dev/queue-manager-core';
3-
import type { CreateTransactionRequest } from 'rango-sdk';
3+
4+
import { type CreateTransactionRequest, TransactionType } from 'rango-sdk';
45

56
import { warn } from '@rango-dev/logging-core';
67

@@ -71,7 +72,12 @@ export async function createTransaction(
7172
}
7273

7374
setStorage({ ...getStorage(), swapDetails: swap });
74-
schedule(SwapActionTypes.EXECUTE_TRANSACTION);
75+
76+
if (transaction?.blockChain === TransactionType.XRPL) {
77+
schedule(SwapActionTypes.EXECUTE_XRPL_TRANSACTION);
78+
} else {
79+
schedule(SwapActionTypes.EXECUTE_TRANSACTION);
80+
}
7581
next();
7682
} catch (error: unknown) {
7783
swap.status = 'failed';
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { XrplTransactionDataIssuedCurrencyAmount } from 'rango-types';
2+
3+
export function isIssuedCurrencyAmount(
4+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5+
amount: any
6+
): amount is XrplTransactionDataIssuedCurrencyAmount {
7+
return (
8+
typeof amount === 'object' &&
9+
typeof amount.currency === 'string' &&
10+
typeof amount.issuer === 'string' &&
11+
typeof amount.value === 'string'
12+
);
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { executeXrplTransaction } from './executeXrplTransaction.js';

queue-manager/rango-preset/src/actions/scheduleNextStep.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { SwapQueueContext, SwapStorage } from '../types';
22
import type { ExecuterActions } from '@rango-dev/queue-manager-core';
3-
import type { PendingSwapStep } from 'rango-types';
3+
4+
import { type PendingSwapStep, TransactionType } from 'rango-types';
45

56
import {
67
getCurrentStep,
@@ -37,7 +38,11 @@ export function scheduleNextStep({
3738

3839
if (!!currentStep && !isFailed) {
3940
if (isTxAlreadyCreated(swap, currentStep)) {
40-
schedule(SwapActionTypes.EXECUTE_TRANSACTION);
41+
if (currentStep.fromBlockchain === TransactionType.XRPL) {
42+
schedule(SwapActionTypes.EXECUTE_XRPL_TRANSACTION);
43+
} else {
44+
schedule(SwapActionTypes.EXECUTE_TRANSACTION);
45+
}
4146
return next();
4247
}
4348

queue-manager/rango-preset/src/helpers.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ export const getCurrentStepTx = (
171171
tronTransaction,
172172
tonTransaction,
173173
suiTransaction,
174+
xrplTransaction,
174175
} = currentStep;
175176
return (
176177
evmTransaction ||
@@ -183,7 +184,8 @@ export const getCurrentStepTx = (
183184
tronApprovalTransaction ||
184185
tronTransaction ||
185186
tonTransaction ||
186-
suiTransaction
187+
suiTransaction ||
188+
xrplTransaction
187189
);
188190
};
189191

@@ -207,6 +209,7 @@ export const setCurrentStepTx = (
207209
currentStep.tronTransaction = null;
208210
currentStep.tonTransaction = null;
209211
currentStep.suiTransaction = null;
212+
currentStep.xrplTransaction = null;
210213

211214
const txType = transaction.type;
212215
switch (txType) {
@@ -746,6 +749,7 @@ export const isTxAlreadyCreated = (
746749
swap.wallets[step.solanaTransaction?.blockChain || ''] ||
747750
swap.wallets[step.tonTransaction?.blockChain || ''] ||
748751
swap.wallets[step.suiTransaction?.blockChain || ''] ||
752+
swap.wallets[step.xrplTransaction?.blockChain || ''] ||
749753
step.transferTransaction?.fromWalletAddress ||
750754
null;
751755

queue-manager/rango-preset/src/queueDef.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
import { BlockReason, SwapActionTypes, SwapQueueDef } from './types';
1+
import type { SwapQueueDef } from './types';
2+
23
import { checkStatus } from './actions/checkStatus';
34
import { createTransaction } from './actions/createTransaction';
45
import { executeTransaction } from './actions/executeTransaction';
6+
import { executeXrplTransaction } from './actions/executeXrplTransaction';
57
import { scheduleNextStep } from './actions/scheduleNextStep';
68
import { start } from './actions/start';
79
import {
810
onBlockForChangeNetwork,
911
onBlockForConnectWallet,
1012
onDependsOnOtherQueues,
1113
} from './helpers';
14+
import { BlockReason, SwapActionTypes } from './types';
1215

1316
/**
1417
*
@@ -24,10 +27,12 @@ export const swapQueueDef: SwapQueueDef = {
2427
[SwapActionTypes.SCHEDULE_NEXT_STEP]: scheduleNextStep,
2528
[SwapActionTypes.CREATE_TRANSACTION]: createTransaction,
2629
[SwapActionTypes.EXECUTE_TRANSACTION]: executeTransaction,
30+
[SwapActionTypes.EXECUTE_XRPL_TRANSACTION]: executeXrplTransaction,
2731
[SwapActionTypes.CHECK_TRANSACTION_STATUS]: checkStatus,
2832
},
2933
run: [SwapActionTypes.START],
3034
whenTaskBlocked: (event, meta) => {
35+
console.log({ event });
3136
if (event.reason.reason === BlockReason.WAIT_FOR_CONNECT_WALLET) {
3237
onBlockForConnectWallet(event, meta);
3338
} else if (event.reason.reason === BlockReason.WAIT_FOR_NETWORK_CHANGE) {

queue-manager/rango-preset/src/shared.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export const getCurrentNamespaceOf = (
118118
const solanaNetwork = step.solanaTransaction?.blockChain;
119119
const tonNetwork = step.tonTransaction?.blockChain;
120120
const suiNetwork = step.suiTransaction?.blockChain;
121+
const xrplNetwork = step.xrplTransaction?.blockChain;
121122

122123
if (evmNetwork) {
123124
return {
@@ -154,6 +155,11 @@ export const getCurrentNamespaceOf = (
154155
namespace: 'Sui',
155156
network: suiNetwork,
156157
};
158+
} else if (xrplNetwork) {
159+
return {
160+
namespace: 'XRPL',
161+
network: xrplNetwork,
162+
};
157163
} else if (!!step.transferTransaction) {
158164
const transferAddress = step.transferTransaction.fromWalletAddress;
159165
if (!transferAddress) {
@@ -232,6 +238,7 @@ export const getCurrentWalletTypeAndAddress = (
232238
swap.wallets[step.solanaTransaction?.blockChain || ''] ||
233239
swap.wallets[step.tonTransaction?.blockChain || ''] ||
234240
swap.wallets[step.suiTransaction?.blockChain || ''] ||
241+
swap.wallets[step.xrplTransaction?.blockChain || ''] ||
235242
(step.transferTransaction?.fromWalletAddress
236243
? { address: step.transferTransaction?.fromWalletAddress }
237244
: null) ||
@@ -268,6 +275,14 @@ export function getRelatedWallet(
268275
const wallet = walletKV?.v || null;
269276

270277
const walletType = wallet?.walletType;
278+
279+
console.log({
280+
walletType,
281+
wallet,
282+
blockchain,
283+
walletKV,
284+
walletAddress,
285+
});
271286
if (wallet === null) {
272287
throw PrettyError.AssertionFailed(
273288
`Wallet for source ${blockchain} not passed: walletType: ${walletType}`
@@ -284,8 +299,10 @@ export function getRelatedWalletOrNull(
284299
return null;
285300
}
286301
try {
302+
console.log('checking...');
287303
return getRelatedWallet(swap, currentStep);
288-
} catch {
304+
} catch (e) {
305+
console.log({ e });
289306
return null;
290307
}
291308
}

queue-manager/rango-preset/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export enum SwapActionTypes {
4242
SCHEDULE_NEXT_STEP = 'SCHEDULE_NEXT_STEP',
4343
CREATE_TRANSACTION = 'CREATE_TRANSACTION',
4444
EXECUTE_TRANSACTION = 'EXECUTE_TRANSACTION',
45+
EXECUTE_XRPL_TRANSACTION = 'EXECUTE_XRPL_TRANSACTION',
4546
CHECK_TRANSACTION_STATUS = 'CHECK_TRANSACTION_STATUS',
4647
}
4748

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "@rango-dev/wallets-core/namespaces/xrpl",
3+
"type": "module",
4+
"main": "../../dist/namespaces/xrpl/mod.js",
5+
"module": "../../dist/namespaces/xrpl/mod.js",
6+
"types": "../../dist/namespaces/xrpl/mod.d.ts",
7+
"sideEffects": false
8+
}

0 commit comments

Comments
 (0)