-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c080f0e
commit 5aac3cd
Showing
6 changed files
with
233 additions
and
3 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
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 { Address, decodeFunctionData, Hex } from 'viem'; | ||
|
||
import { Wallet } from '@/contracts'; | ||
import { erc20ABI } from '@/generated/blockchain/v5'; | ||
|
||
import { BaseContract } from './BaseContract'; | ||
|
||
export class Erc20 extends BaseContract<typeof erc20ABI> { | ||
constructor({ address, walletClient }: { address: Address; walletClient: Wallet }) { | ||
super({ | ||
walletClient, | ||
address, | ||
abi: erc20ABI, | ||
}); | ||
} | ||
|
||
decodeApproveCalldata(calldata: Hex) { | ||
const decoded = decodeFunctionData({ | ||
abi: erc20ABI, | ||
data: calldata, | ||
}); | ||
if (decoded.functionName !== 'approve') { | ||
throw new Error('Wrong calldata'); | ||
} | ||
return decoded.args; | ||
} | ||
} |
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
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
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,37 @@ | ||
import { Execute } from '@reservoir0x/reservoir-sdk'; | ||
|
||
import { GondiPublicClient, Wallet } from '@/contracts'; | ||
|
||
type TransactionStep = Execute['steps'][number]; | ||
type MinimalStep = Pick<TransactionStep, 'kind' | 'id' | 'items'>; | ||
|
||
const isTransaction = (step: MinimalStep) => step.kind === 'transaction'; | ||
|
||
const isSale = (step: MinimalStep) => | ||
step.id === 'sale' && isTransaction(step) && step.items?.length === 1; | ||
|
||
export const isCurrencyApproval = (step: MinimalStep) => | ||
step.id === 'currency-approval' && isTransaction(step) && step.items?.length === 1; | ||
|
||
export const validateSteps = (steps: MinimalStep[]) => { | ||
for (const step of steps) { | ||
if (!isCurrencyApproval(step) && !isSale(step)) { | ||
throw new Error(`Unknown step: ${step.id} - ${step.kind}: ${step}`); | ||
} | ||
} | ||
}; | ||
|
||
export const sendTransaction = async ( | ||
wallet: Wallet, | ||
bcClient: GondiPublicClient, | ||
step: TransactionStep, | ||
) => { | ||
const txHash = await wallet.sendTransaction(step.items?.[0].data); | ||
const receipt = await bcClient.waitForTransactionReceipt({ | ||
hash: txHash, | ||
}); | ||
if (receipt.status !== 'success') { | ||
throw new Error(`Transaction failed: ${txHash}`); | ||
} | ||
return receipt; | ||
}; |
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