diff --git a/src/screens/claimables/transaction/claim.ts b/src/screens/claimables/transaction/claim.ts new file mode 100644 index 00000000000..5c6e057934a --- /dev/null +++ b/src/screens/claimables/transaction/claim.ts @@ -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, + 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, + }; +} diff --git a/src/screens/claimables/transaction/types.ts b/src/screens/claimables/transaction/types.ts new file mode 100644 index 00000000000..d14ca70de84 --- /dev/null +++ b/src/screens/claimables/transaction/types.ts @@ -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; + } + );