Skip to content

Commit 33da16f

Browse files
committed
feat: integrating gemwallet
1 parent 5b00b86 commit 33da16f

File tree

36 files changed

+823
-120
lines changed

36 files changed

+823
-120
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 { DEFAULT_ERROR_CODE } from '../constants';
67
import {
@@ -70,7 +71,12 @@ export async function createTransaction(
7071
}
7172

7273
setStorage({ ...getStorage(), swapDetails: swap });
73-
schedule(SwapActionTypes.EXECUTE_TRANSACTION);
74+
75+
if (transaction?.blockChain === TransactionType.XRPL) {
76+
schedule(SwapActionTypes.EXECUTE_XRPL_TRANSACTION);
77+
} else {
78+
schedule(SwapActionTypes.EXECUTE_TRANSACTION);
79+
}
7480
next();
7581
} catch (error) {
7682
swap.status = 'failed';

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ import { BlockReason } from '../types';
3636
export async function executeTransaction(
3737
actions: ExecuterActions<SwapStorage, SwapActionTypes, SwapQueueContext>
3838
): Promise<void> {
39+
const checkResult = await checkExecution(actions);
40+
41+
if (checkResult) {
42+
// All the conditions are met. We can safely send the tx to wallet for sign.
43+
await signTransaction(actions);
44+
}
45+
}
46+
47+
export async function checkExecution(
48+
actions: ExecuterActions<SwapStorage, SwapActionTypes, SwapQueueContext>
49+
): Promise<boolean> {
3950
const { getStorage, context } = actions;
4051
const { meta, wallets, providers } = context;
4152
const { claimedBy } = claimQueue();
@@ -49,7 +60,7 @@ export async function executeTransaction(
4960
};
5061

5162
const swap = getStorage().swapDetails;
52-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
63+
5364
const currentStep = getCurrentStep(swap)!;
5465

5566
// Resetting network status, so we will set it again during the running of this task.
@@ -72,7 +83,7 @@ export async function executeTransaction(
7283
description,
7384
};
7485
requestBlock(blockedFor);
75-
return;
86+
return false;
7687
}
7788

7889
/* Wallet should be on correct network */
@@ -93,7 +104,7 @@ export async function executeTransaction(
93104
details: details,
94105
};
95106
requestBlock(blockedFor);
96-
return;
107+
return false;
97108
} else if (!networkMatched) {
98109
const fromNamespace = getCurrentNamespaceOf(swap, currentStep);
99110
const details = ERROR_MESSAGE_WAIT_FOR_CHANGE_NETWORK(
@@ -105,7 +116,7 @@ export async function executeTransaction(
105116
details: details,
106117
};
107118
requestBlock(blockedFor);
108-
return;
119+
return false;
109120
}
110121
// Update network to mark it as network changed successfully.
111122
updateNetworkStatus(actions, {
@@ -127,9 +138,8 @@ export async function executeTransaction(
127138
details: {},
128139
};
129140
requestBlock(blockedFor);
130-
return;
141+
return false;
131142
}
132143

133-
// All the conditions are met. We can safely send the tx to wallet for sign.
134-
await signTransaction(actions);
144+
return true;
135145
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import type { SwapActionTypes, SwapQueueContext, SwapStorage } from '../types';
2+
import type { ExecuterActions } from '@rango-dev/queue-manager-core';
3+
import type {
4+
GenericSigner,
5+
XrplTransaction,
6+
XrplTransactionDataIssuedCurrencyAmount,
7+
XrplTrustSetTransactionData,
8+
} from 'rango-types';
9+
10+
import { isXrplTransaction } from 'rango-types';
11+
12+
import {
13+
checkTranasctionForExecute,
14+
getCurrentStep,
15+
handleSuccessfulSign,
16+
handlRejectedSign,
17+
} from '../helpers';
18+
import { getCurrentAddressOf, getRelatedWallet } from '../shared';
19+
20+
import { checkExecution } from './executeTransaction';
21+
22+
function isIssuedCurrencyAmount(
23+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
24+
amount: any
25+
): amount is XrplTransactionDataIssuedCurrencyAmount {
26+
return (
27+
typeof amount === 'object' &&
28+
typeof amount.currency === 'string' &&
29+
typeof amount.issuer === 'string' &&
30+
typeof amount.value === 'string'
31+
);
32+
}
33+
34+
export async function executeXrplTransaction(
35+
actions: ExecuterActions<SwapStorage, SwapActionTypes, SwapQueueContext>
36+
): Promise<void> {
37+
const checkResult = await checkExecution(actions);
38+
39+
if (checkResult) {
40+
await signTransaction(actions);
41+
}
42+
}
43+
44+
export async function signTransaction(
45+
actions: ExecuterActions<SwapStorage, SwapActionTypes, SwapQueueContext>
46+
): Promise<void> {
47+
const onFinish = () => {
48+
// TODO resetClaimedBy is undefined here
49+
if (actions.context.resetClaimedBy) {
50+
actions.context.resetClaimedBy();
51+
}
52+
};
53+
54+
let tx: XrplTransaction;
55+
let isApproval: boolean;
56+
try {
57+
const result = await checkTranasctionForExecute(actions);
58+
if (isXrplTransaction(result.tx)) {
59+
tx = result.tx;
60+
isApproval = result.isApproval;
61+
} else {
62+
throw new Error('TODO: NEEDS TO FAILED AND THROW A MESSAGE USING HOOKS');
63+
}
64+
} catch {
65+
onFinish();
66+
// Avoid to run the rest of program.
67+
return;
68+
}
69+
70+
const { getStorage, context } = actions;
71+
const { meta, getSigners } = context;
72+
const swap = getStorage().swapDetails;
73+
74+
const currentStep = getCurrentStep(swap)!;
75+
76+
const sourceWallet = getRelatedWallet(swap, currentStep);
77+
const walletAddress = getCurrentAddressOf(swap, currentStep);
78+
79+
const chainId = meta.blockchains?.[tx.blockChain]?.chainId;
80+
const walletSigners = await getSigners(sourceWallet.walletType);
81+
82+
if (tx.data.TransactionType !== 'Payment') {
83+
throw new Error('TODO: SHOULD FAILED Q correctly.');
84+
}
85+
86+
const transactionQueue: XrplTransaction[] = [tx];
87+
if (isIssuedCurrencyAmount(tx.data.Amount)) {
88+
const trustlineTx: XrplTrustSetTransactionData = {
89+
TransactionType: 'TrustSet',
90+
Account: tx.data.Account,
91+
LimitAmount: {
92+
currency: tx.data.Amount.currency,
93+
issuer: tx.data.Amount.issuer,
94+
value: tx.data.Amount.value,
95+
},
96+
};
97+
transactionQueue.unshift({
98+
...tx,
99+
data: trustlineTx,
100+
});
101+
}
102+
103+
const signer: GenericSigner<XrplTransaction> = walletSigners.getSigner(
104+
tx.type
105+
);
106+
107+
// TODO: Check failuire scenario
108+
for (const transaction of transactionQueue) {
109+
await signer
110+
.signAndSendTx(transaction, walletAddress, chainId)
111+
.then(
112+
handleSuccessfulSign(actions, {
113+
isApproval,
114+
}),
115+
handlRejectedSign(actions)
116+
)
117+
.finally(() => {
118+
onFinish();
119+
});
120+
}
121+
}

queue-manager/rango-preset/src/actions/scheduleNextStep.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 { PendingSwapStep } from 'rango-types';
3+
4+
import { type PendingSwapStep, TransactionType } from 'rango-types';
45

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

3839
if (!!currentStep && !isFailed) {
3940
if (isTxAlreadyCreated(swap, currentStep)) {
40-
schedule(SwapActionTypes.EXECUTE_TRANSACTION);
41+
console.log('isTxAlreadyCreated', { swap, currentStep });
42+
if (currentStep.fromBlockchain === TransactionType.XRPL) {
43+
schedule(SwapActionTypes.EXECUTE_XRPL_TRANSACTION);
44+
} else {
45+
schedule(SwapActionTypes.EXECUTE_TRANSACTION);
46+
}
4147
return next();
4248
}
4349

0 commit comments

Comments
 (0)