Skip to content

Commit

Permalink
types
Browse files Browse the repository at this point in the history
  • Loading branch information
benisgold committed Sep 23, 2024
1 parent b231ee8 commit 63cfa9e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 58 deletions.
4 changes: 2 additions & 2 deletions src/raps/actions/claimSponsoredClaimable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function claimSponsoredClaimable({ parameters }: ActionPropsV2<'cla
}

return {
nonce: undefined,
hash: undefined,
nonce: null,
hash: null,
};
}
2 changes: 1 addition & 1 deletion src/raps/actions/claimTransactionClaimable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { addNewTransaction } from '@/state/pendingTransactions';
import { NewTransaction } from '@/entities';
import { chainsName } from '@/chains';

export async function claimTransactionClaimable({ parameters, wallet, baseNonce }: ActionPropsV2<'claimTransactionClaimableAction'>) {
export async function claimTransactionClaimable({ parameters, wallet }: ActionPropsV2<'claimTransactionClaimableAction'>) {
const { claimTx } = parameters;

const provider = getProvider({ chainId: claimTx.chainId });
Expand Down
83 changes: 30 additions & 53 deletions src/raps/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
RapActionV2,
RapV2,
RapParameters,
RapActionResponseV2,
RapResponseV2,
} from './references';
import { createUnlockAndCrosschainSwapRap } from './unlockAndCrosschainSwap';
import { createClaimRewardsAndBridgeRap } from './claimRewardsAndBridge';
Expand Down Expand Up @@ -87,8 +89,7 @@ function typeActionV2<T extends RapActionTypesV2>(type: T, props: ActionPropsV2<
case 'claimTransactionClaimableAction':
return () => claimTransactionClaimable(props as ActionPropsV2<'claimTransactionClaimableAction'>);
default:
// eslint-disable-next-line react/display-name
return () => null;
throw new RainbowError(`[raps/execute]: typeActionV2 - unknown type ${type}`);
}
}

Expand Down Expand Up @@ -141,36 +142,30 @@ export async function executeActionV2<T extends RapActionTypesV2>({
action,
wallet,
rap,
index,
baseNonce,
nonceToUse,
rapName,
}: {
action: RapActionV2<T>;
wallet: Signer;
rap: RapV2;
index: number;
baseNonce?: number;
nonceToUse: number | undefined;
rapName: string;
}): Promise<RapActionResponse> {
}): Promise<RapActionResponseV2> {
const { type, parameters } = action;
try {
const actionProps = {
wallet,
currentRap: rap,
index,
parameters,
baseNonce,
nonceToUse,
};
const { nonce, hash } = (await typeActionV2<T>(type, actionProps)()) as RapActionResult;
return { baseNonce: nonce, errorMessage: null, hash };
const { nonce, hash } = await typeActionV2<T>(type, actionProps)();
return { nonce, errorMessage: null, hash };
} catch (error) {
logger.error(new RainbowError(`[raps/execute]: ${rapName} - error execute action`), {
message: (error as Error)?.message,
});
if (index === 0) {
return { baseNonce: null, errorMessage: String(error) };
}
return { baseNonce: null, errorMessage: null };
return { nonce: null, errorMessage: String(error), hash: null };
}
}

Expand Down Expand Up @@ -200,51 +195,33 @@ const waitForNodeAck = async (hash: string, provider: Signer['provider']): Promi
});
};

const executeRap = async (
wallet: Signer,
rap: {
actions: RapActionV2<RapActionTypesV2>[];
},
nonce?: number | undefined
): Promise<RapResponse> => {
let currentNonce = nonce;
const executeRap = async (wallet: Signer, rap: RapV2): Promise<RapResponseV2> => {
const { actions } = rap;
const rapName = getRapFullNameV2(rap.actions);
let errorMessage = null;
if (actions.length) {
const firstAction = actions[0];
const actionParams = {
action: firstAction,
let nonceToUse: number | undefined;

while (actions.length) {
const action = actions.shift();

if (!action) break;

const { nonce, errorMessage, hash } = await executeActionV2({
action,
wallet,
rap,
index: 0,
baseNonce: nonce,
nonceToUse,
rapName,
};
});

const { baseNonce, errorMessage: error, hash } = await executeActionV2(actionParams);
if (errorMessage) return { errorMessage };

if (typeof baseNonce === 'number') {
actions.length > 1 && hash && (await waitForNodeAck(hash, wallet.provider));
for (let index = 1; index < actions.length; index++) {
const action = actions[index];
const actionParams = {
action,
wallet,
rap,
index,
baseNonce,
rapName,
};
const { hash } = await executeActionV2(actionParams);
hash && (await waitForNodeAck(hash, wallet.provider));
}
currentNonce = baseNonce + actions.length - 1;
} else {
errorMessage = error;
if (typeof nonce === 'number') {
actions.length >= 1 && hash && (await waitForNodeAck(hash, wallet.provider));
nonceToUse = nonce + 1;
}
}
return { nonce: currentNonce, errorMessage };

return { errorMessage: null };
};

export const walletExecuteRap = async (
Expand Down Expand Up @@ -311,7 +288,7 @@ export const walletExecuteRap = async (
return { nonce, errorMessage };
};

export async function walletExecuteRapV2(wallet: Signer, rapParameters: RapParameters, nonce?: number | undefined): Promise<RapResponse> {
export async function walletExecuteRapV2(wallet: Signer, rapParameters: RapParameters): Promise<RapResponseV2> {
const rap = await createRap(rapParameters);
return executeRap(wallet, rap, nonce);
return executeRap(wallet, rap);
}
18 changes: 16 additions & 2 deletions src/raps/references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,22 @@ export interface RapActionResponse {
hash?: string | null;
}

export interface RapActionResponseV2 {
nonce: number | null;
errorMessage: string | null;
hash: string | null;
}

export interface RapActionResult {
nonce?: number | undefined;
hash?: string | undefined;
}

export interface RapActionResultV2 {
nonce: number | null;
hash: string | null;
}

export interface ActionProps<T extends RapActionTypes> {
baseNonce?: number;
index: number;
Expand All @@ -229,8 +240,7 @@ export interface ActionProps<T extends RapActionTypes> {
}

export interface ActionPropsV2<T extends RapActionTypesV2> {
baseNonce?: number;
index: number;
nonceToUse?: number;
parameters: RapActionParameterMapV2[T];
wallet: Signer;
currentRap: RapV2;
Expand All @@ -245,3 +255,7 @@ export interface RapResponse {
nonce: number | undefined;
errorMessage: string | null;
}

export interface RapResponseV2 {
errorMessage: string | null;
}

0 comments on commit 63cfa9e

Please sign in to comment.