Skip to content

feat: integrating gemwallet #1096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions queue-manager/rango-preset/src/actions/createTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { SwapQueueContext, SwapStorage } from '../types';
import type { ExecuterActions } from '@rango-dev/queue-manager-core';
import type { CreateTransactionRequest } from 'rango-sdk';

import { type CreateTransactionRequest, TransactionType } from 'rango-sdk';

import { DEFAULT_ERROR_CODE } from '../constants';
import {
Expand Down Expand Up @@ -70,7 +71,12 @@ export async function createTransaction(
}

setStorage({ ...getStorage(), swapDetails: swap });
schedule(SwapActionTypes.EXECUTE_TRANSACTION);

if (transaction?.blockChain === TransactionType.XRPL) {
schedule(SwapActionTypes.EXECUTE_XRPL_TRANSACTION);
} else {
schedule(SwapActionTypes.EXECUTE_TRANSACTION);
}
next();
} catch (error) {
swap.status = 'failed';
Expand Down
24 changes: 17 additions & 7 deletions queue-manager/rango-preset/src/actions/executeTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ import { BlockReason } from '../types';
export async function executeTransaction(
actions: ExecuterActions<SwapStorage, SwapActionTypes, SwapQueueContext>
): Promise<void> {
const checkResult = await checkExecution(actions);

if (checkResult) {
// All the conditions are met. We can safely send the tx to wallet for sign.
await signTransaction(actions);
}
}

export async function checkExecution(
actions: ExecuterActions<SwapStorage, SwapActionTypes, SwapQueueContext>
): Promise<boolean> {
const { getStorage, context } = actions;
const { meta, wallets, providers } = context;
const { claimedBy } = claimQueue();
Expand All @@ -49,7 +60,7 @@ export async function executeTransaction(
};

const swap = getStorage().swapDetails;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion

const currentStep = getCurrentStep(swap)!;

// Resetting network status, so we will set it again during the running of this task.
Expand All @@ -72,7 +83,7 @@ export async function executeTransaction(
description,
};
requestBlock(blockedFor);
return;
return false;
}

/* Wallet should be on correct network */
Expand All @@ -93,7 +104,7 @@ export async function executeTransaction(
details: details,
};
requestBlock(blockedFor);
return;
return false;
} else if (!networkMatched) {
const fromNamespace = getCurrentNamespaceOf(swap, currentStep);
const details = ERROR_MESSAGE_WAIT_FOR_CHANGE_NETWORK(
Expand All @@ -105,7 +116,7 @@ export async function executeTransaction(
details: details,
};
requestBlock(blockedFor);
return;
return false;
}
// Update network to mark it as network changed successfully.
updateNetworkStatus(actions, {
Expand All @@ -127,9 +138,8 @@ export async function executeTransaction(
details: {},
};
requestBlock(blockedFor);
return;
return false;
}

// All the conditions are met. We can safely send the tx to wallet for sign.
await signTransaction(actions);
return true;
}
121 changes: 121 additions & 0 deletions queue-manager/rango-preset/src/actions/executeXrplTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import type { SwapActionTypes, SwapQueueContext, SwapStorage } from '../types';
import type { ExecuterActions } from '@rango-dev/queue-manager-core';
import type {
GenericSigner,
XrplTransaction,
XrplTransactionDataIssuedCurrencyAmount,
XrplTrustSetTransactionData,
} from 'rango-types';

import { isXrplTransaction } from 'rango-types';

import {
checkTranasctionForExecute,
getCurrentStep,
handleSuccessfulSign,
handlRejectedSign,
} from '../helpers';
import { getCurrentAddressOf, getRelatedWallet } from '../shared';

import { checkExecution } from './executeTransaction';

function isIssuedCurrencyAmount(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
amount: any
): amount is XrplTransactionDataIssuedCurrencyAmount {
return (
typeof amount === 'object' &&
typeof amount.currency === 'string' &&
typeof amount.issuer === 'string' &&
typeof amount.value === 'string'
);
}

export async function executeXrplTransaction(
actions: ExecuterActions<SwapStorage, SwapActionTypes, SwapQueueContext>
): Promise<void> {
const checkResult = await checkExecution(actions);

if (checkResult) {
await signTransaction(actions);
}
}

export async function signTransaction(
actions: ExecuterActions<SwapStorage, SwapActionTypes, SwapQueueContext>
): Promise<void> {
const onFinish = () => {
// TODO resetClaimedBy is undefined here
if (actions.context.resetClaimedBy) {
actions.context.resetClaimedBy();
}
};

let tx: XrplTransaction;
let isApproval: boolean;
try {
const result = await checkTranasctionForExecute(actions);
if (isXrplTransaction(result.tx)) {
tx = result.tx;
isApproval = result.isApproval;
} else {
throw new Error('TODO: NEEDS TO FAILED AND THROW A MESSAGE USING HOOKS');
}
} catch {
onFinish();
// Avoid to run the rest of program.
return;
}

const { getStorage, context } = actions;
const { meta, getSigners } = context;
const swap = getStorage().swapDetails;

const currentStep = getCurrentStep(swap)!;

const sourceWallet = getRelatedWallet(swap, currentStep);
const walletAddress = getCurrentAddressOf(swap, currentStep);

const chainId = meta.blockchains?.[tx.blockChain]?.chainId;
const walletSigners = await getSigners(sourceWallet.walletType);

if (tx.data.TransactionType !== 'Payment') {
throw new Error('TODO: SHOULD FAILED Q correctly.');
}

const transactionQueue: XrplTransaction[] = [tx];
if (isIssuedCurrencyAmount(tx.data.Amount)) {
const trustlineTx: XrplTrustSetTransactionData = {
TransactionType: 'TrustSet',
Account: tx.data.Account,
LimitAmount: {
currency: tx.data.Amount.currency,
issuer: tx.data.Amount.issuer,
value: tx.data.Amount.value,
},
};
transactionQueue.unshift({
...tx,
data: trustlineTx,
});
}

const signer: GenericSigner<XrplTransaction> = walletSigners.getSigner(
tx.type
);

// TODO: Check failuire scenario
for (const transaction of transactionQueue) {
await signer
.signAndSendTx(transaction, walletAddress, chainId)
.then(
handleSuccessfulSign(actions, {
isApproval,
}),
handlRejectedSign(actions)
)
.finally(() => {
onFinish();
});
}
}
10 changes: 8 additions & 2 deletions queue-manager/rango-preset/src/actions/scheduleNextStep.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { SwapQueueContext, SwapStorage } from '../types';
import type { ExecuterActions } from '@rango-dev/queue-manager-core';
import type { PendingSwapStep } from 'rango-types';

import { type PendingSwapStep, TransactionType } from 'rango-types';

import {
getCurrentStep,
Expand Down Expand Up @@ -37,7 +38,12 @@ export function scheduleNextStep({

if (!!currentStep && !isFailed) {
if (isTxAlreadyCreated(swap, currentStep)) {
schedule(SwapActionTypes.EXECUTE_TRANSACTION);
console.log('isTxAlreadyCreated', { swap, currentStep });
if (currentStep.fromBlockchain === TransactionType.XRPL) {
schedule(SwapActionTypes.EXECUTE_XRPL_TRANSACTION);
} else {
schedule(SwapActionTypes.EXECUTE_TRANSACTION);
}
return next();
}

Expand Down
Loading