Skip to content

Commit

Permalink
extract claim action so it can be used outside of raps logic
Browse files Browse the repository at this point in the history
  • Loading branch information
benisgold committed Nov 12, 2024
1 parent f6ce381 commit e1e8513
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/screens/claimables/transaction/claim.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { chainsName } from '@/chains';
import { NewTransaction, TransactionStatus } from '@/entities';
import { TokenColors } from '@/graphql/__generated__/metadata';
import { getProvider } from '@/handlers/web3';
import { RainbowError } from '@/logger';
import { sendTransaction } from '@/model/wallet';
import { addNewTransaction } from '@/state/pendingTransactions';
import { TransactionClaimableTxPayload } from './types';
import { Signer } from '@ethersproject/abstract-signer';
import { ParsedAsset as SwapsParsedAsset } from '@/__swaps__/types/assets';
import { AddysNetworkDetails, ParsedAsset } from '@/resources/assets/types';

export async function executeClaim({
asset,
claimTx,
wallet,
}: {
asset: SwapsParsedAsset | ParsedAsset;
claimTx: TransactionClaimableTxPayload;
wallet: Signer;
}) {
const provider = getProvider({ chainId: claimTx.chainId });

const result = await sendTransaction({ transaction: claimTx, existingWallet: wallet, provider });

if (!result?.result || !!result.error || !result.result.hash) {
throw new RainbowError('[CLAIM-CLAIMABLE]: failed to execute claim transaction');
}

const parsedAsset = {
...asset,
network: chainsName[result.result.chainId],
networks: asset.networks as Record<string, AddysNetworkDetails>,
colors: asset.colors as TokenColors,
} satisfies ParsedAsset;

const transaction = {
amount: '0x0',
gasLimit: result.result.gasLimit,
from: result.result.from ?? null,
to: result.result.to ?? null,
chainId: result.result.chainId,
hash: result.result.hash,
network: chainsName[result.result.chainId],
status: TransactionStatus.pending,
type: 'claim',
nonce: result.result.nonce,
asset: parsedAsset,
} satisfies NewTransaction;

addNewTransaction({
address: claimTx.from,
chainId: claimTx.chainId,
transaction,
});

const tx = await wallet?.provider?.getTransaction(result.result.hash);
const receipt = await tx?.wait();
if (!receipt) {
throw new RainbowError('[CLAIM-CLAIMABLE]: tx not mined');
}

return {
nonce: result.result.nonce,
hash: result.result.hash,
};
}
27 changes: 27 additions & 0 deletions src/screens/claimables/transaction/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { TransactionRequest } from '@ethersproject/providers';

// supports legacy and new gas types
export type TransactionClaimableTxPayload = TransactionRequest &
(
| {
to: string;
from: string;
nonce: number;
gasLimit: string;
maxFeePerGas: string;
maxPriorityFeePerGas: string;
data: string;
value: '0x0';
chainId: number;
}
| {
to: string;
from: string;
nonce: number;
gasLimit: string;
gasPrice: string;
data: string;
value: '0x0';
chainId: number;
}
);

0 comments on commit e1e8513

Please sign in to comment.