Skip to content

Commit

Permalink
modify raps to support claim as x
Browse files Browse the repository at this point in the history
  • Loading branch information
benisgold committed Nov 12, 2024
1 parent e1e8513 commit 0ca07c6
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/raps/actions/claimClaimable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ActionProps, RapActionResult } from '../references';
import { executeClaim } from '@/screens/claimables/transaction/claim';

export async function claimClaimable({ wallet, parameters }: ActionProps<'claimClaimable'>): Promise<RapActionResult> {
const { claimTx, asset } = parameters;

return executeClaim({ asset, claimTx, wallet });
}
89 changes: 89 additions & 0 deletions src/raps/claimClaimable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { Address } from 'viem';
import { createNewAction, createNewRap } from './common';
import { RapAction, RapSwapActionParameters } from './references';
import { RainbowError } from '@/logger';
import { CrosschainQuote } from '@rainbow-me/swaps';
import { assetNeedsUnlocking } from './actions';

export async function createClaimClaimableRap(parameters: RapSwapActionParameters<'claimClaimable'>) {
let actions: RapAction<'claimClaimable' | 'crosschainSwap' | 'unlock' | 'swap'>[] = [];

const { sellAmount, assetToBuy, quote, chainId, toChainId, assetToSell, meta, gasFeeParamsBySpeed, gasParams, additionalParams } =
parameters;

if (!additionalParams?.claimTx) throw new RainbowError('[raps/claimClaimable]: claimTx is undefined');

const claim = createNewAction('claimClaimable', {
claimTx: additionalParams.claimTx,
asset: assetToSell,
});
actions = actions.concat(claim);

const {
from: accountAddress,
allowanceTarget,
allowanceNeeded,
} = quote as {
from: Address;
allowanceTarget: Address;
allowanceNeeded: boolean;
};

let swapAssetNeedsUnlocking = false;

if (allowanceNeeded) {
swapAssetNeedsUnlocking = await assetNeedsUnlocking({
owner: accountAddress,
amount: sellAmount,
assetToUnlock: assetToSell,
spender: allowanceTarget,
chainId,
});
}

if (swapAssetNeedsUnlocking) {
if (!quote.to) throw new RainbowError('[raps/claimClaimable]: quote.to is undefined');

const unlock = createNewAction('unlock', {
fromAddress: accountAddress,
assetToUnlock: assetToSell,
chainId,
contractAddress: quote.to as Address,
});
actions = actions.concat(unlock);
}

if (chainId === toChainId) {
// create a swap rap
const swap = createNewAction('swap', {
chainId: chainId,
requiresApprove: swapAssetNeedsUnlocking,
quote: quote as CrosschainQuote,
meta: meta,
assetToSell,
sellAmount,
assetToBuy,
gasParams,
gasFeeParamsBySpeed,
});
actions = actions.concat(swap);
} else {
// create a crosschain swap rap
const crosschainSwap = createNewAction('crosschainSwap', {
chainId: chainId,
requiresApprove: swapAssetNeedsUnlocking,
quote: quote as CrosschainQuote,
meta: meta,
assetToSell,
sellAmount,
assetToBuy,
gasParams,
gasFeeParamsBySpeed,
});
actions = actions.concat(crosschainSwap);
}

// create the overall rap
const newRap = createNewRap(actions);
return newRap;
}
10 changes: 8 additions & 2 deletions src/raps/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { createUnlockAndSwapRap } from './unlockAndSwap';
import { GasFeeParamsBySpeed, LegacyGasFeeParamsBySpeed, LegacyTransactionGasParamAmounts, TransactionGasParamAmounts } from '@/entities';
import { Screens, TimeToSignOperation, performanceTracking } from '@/state/performance/performance';
import { swapsStore } from '@/state/swaps/swapsStore';
import { createClaimClaimableRap } from './claimClaimable';
import { claimClaimable } from './actions/claimClaimable';

export function createSwapRapByType<T extends RapTypes>(
type: T,
Expand All @@ -36,6 +38,8 @@ export function createSwapRapByType<T extends RapTypes>(
return createUnlockAndCrosschainSwapRap(swapParameters as RapSwapActionParameters<'crosschainSwap'>);
case 'swap':
return createUnlockAndSwapRap(swapParameters as RapSwapActionParameters<'swap'>);
case 'claimClaimable':
return createClaimClaimableRap(swapParameters as RapSwapActionParameters<'claimClaimable'>);
default:
return Promise.resolve({ actions: [] });
}
Expand All @@ -53,6 +57,8 @@ function typeAction<T extends RapActionTypes>(type: T, props: ActionProps<T>) {
return () => claimBridge(props as ActionProps<'claimBridge'>);
case 'crosschainSwap':
return () => crosschainSwap(props as ActionProps<'crosschainSwap'>);
case 'claimClaimable':
return () => claimClaimable(props as ActionProps<'claimClaimable'>);
default:
// eslint-disable-next-line react/display-name
return () => null;
Expand Down Expand Up @@ -128,11 +134,11 @@ const waitForNodeAck = async (hash: string, provider: Signer['provider']): Promi
export const walletExecuteRap = async (
wallet: Signer,
type: RapTypes,
parameters: RapSwapActionParameters<'swap' | 'crosschainSwap' | 'claimBridge'>
parameters: RapSwapActionParameters<'swap' | 'crosschainSwap' | 'claimBridge' | 'claimClaimable'>
): Promise<{ nonce: number | undefined; errorMessage: string | null }> => {
// NOTE: We don't care to track claimBridge raps
const rap =
type === 'claimBridge'
type === 'claimBridge' || type == 'claimClaimable'
? await createSwapRapByType(type, parameters)
: await performanceTracking.getState().executeFn({
fn: createSwapRapByType,
Expand Down
24 changes: 20 additions & 4 deletions src/raps/references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Address } from 'viem';
import { ParsedAsset } from '@/__swaps__/types/assets';
import { GasFeeParamsBySpeed, LegacyGasFeeParamsBySpeed, LegacyTransactionGasParamAmounts, TransactionGasParamAmounts } from '@/entities';
import { ChainId } from '@/chains/types';
import { TransactionClaimableTxPayload } from '@/screens/claimables/transaction/types';

export enum SwapModalField {
input = 'inputAmount',
Expand Down Expand Up @@ -39,9 +40,17 @@ export type QuoteTypeMap = {
swap: Quote;
crosschainSwap: CrosschainQuote;
claimBridge: undefined;
claimClaimable: Quote | CrosschainQuote;
};

export interface RapSwapActionParameters<T extends 'swap' | 'crosschainSwap' | 'claimBridge'> {
type AdditionalParamsMap = {
swap: undefined;
crosschainSwap: undefined;
claimBridge: undefined;
claimClaimable: { claimTx: TransactionClaimableTxPayload };
};

export interface RapSwapActionParameters<T extends 'swap' | 'crosschainSwap' | 'claimBridge' | 'claimClaimable'> {
amount?: string | null;
sellAmount: string;
buyAmount?: string;
Expand All @@ -58,6 +67,7 @@ export interface RapSwapActionParameters<T extends 'swap' | 'crosschainSwap' | '
flashbots?: boolean;
quote: QuoteTypeMap[T];
address?: Address;
additionalParams?: AdditionalParamsMap[T];
}

export interface RapUnlockActionParameters {
Expand All @@ -79,11 +89,14 @@ export interface RapClaimActionParameters {
gasParams: TransactionGasParamAmounts | LegacyTransactionGasParamAmounts;
}

export type RapClaimClaimableActionParameters = { claimTx: TransactionClaimableTxPayload; asset: ParsedAsset };

export type RapActionParameters =
| RapSwapActionParameters<'swap'>
| RapSwapActionParameters<'crosschainSwap'>
| RapClaimActionParameters
| RapUnlockActionParameters;
| RapUnlockActionParameters
| RapClaimClaimableActionParameters;

export interface RapActionTransaction {
hash: string | null;
Expand All @@ -95,6 +108,7 @@ export type RapActionParameterMap = {
unlock: RapUnlockActionParameters;
claim: RapClaimActionParameters;
claimBridge: RapClaimActionParameters;
claimClaimable: RapClaimClaimableActionParameters;
};

export interface RapAction<T extends RapActionTypes> {
Expand All @@ -104,7 +118,7 @@ export interface RapAction<T extends RapActionTypes> {
}

export interface Rap {
actions: RapAction<'swap' | 'crosschainSwap' | 'unlock' | 'claim' | 'claimBridge'>[];
actions: RapAction<'swap' | 'crosschainSwap' | 'unlock' | 'claim' | 'claimBridge' | 'claimClaimable'>[];
}

export enum rapActions {
Expand All @@ -113,6 +127,7 @@ export enum rapActions {
unlock = 'unlock',
claim = 'claim',
claimBridge = 'claimBridge',
claimClaimable = 'claimClaimable',
}

export type RapActionTypes = keyof typeof rapActions;
Expand All @@ -121,6 +136,7 @@ export enum rapTypes {
swap = 'swap',
crosschainSwap = 'crosschainSwap',
claimBridge = 'claimBridge',
claimClaimable = 'claimClaimable',
}

export type RapTypes = keyof typeof rapTypes;
Expand All @@ -147,6 +163,6 @@ export interface ActionProps<T extends RapActionTypes> {
}

export interface WalletExecuteRapProps {
rapActionParameters: RapSwapActionParameters<'swap' | 'crosschainSwap' | 'claimBridge'>;
rapActionParameters: RapSwapActionParameters<'swap' | 'crosschainSwap' | 'claimBridge' | 'claimClaimable'>;
type: RapTypes;
}

0 comments on commit 0ca07c6

Please sign in to comment.