-
Notifications
You must be signed in to change notification settings - Fork 634
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract claim action so it can be used outside of raps logic
- Loading branch information
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
); |