adding a stake form and approvals in both stake and swap form
This commit is contained in:
@@ -357,4 +357,109 @@ export function useSwap() {
|
||||
gasEstimate,
|
||||
isEstimatingGas,
|
||||
};
|
||||
}
|
||||
|
||||
export function useSwapMint() {
|
||||
const publicClient = usePublicClient();
|
||||
const { data: walletClient } = useWalletClient();
|
||||
const [isSwapMinting, setIsSwapMinting] = useState(false);
|
||||
const [swapMintHash, setSwapMintHash] = useState<`0x${string}` | null>(null);
|
||||
const [swapMintError, setSwapMintError] = useState<string | null>(null);
|
||||
|
||||
const executeSwapMint = async (
|
||||
poolAddress: `0x${string}`,
|
||||
inputTokenAddress: `0x${string}`,
|
||||
inputTokenIndex: number,
|
||||
maxAmountIn: bigint
|
||||
) => {
|
||||
if (!walletClient || !publicClient) {
|
||||
setSwapMintError('Wallet not connected');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
setIsSwapMinting(true);
|
||||
setSwapMintError(null);
|
||||
setSwapMintHash(null);
|
||||
|
||||
const userAddress = walletClient.account.address;
|
||||
|
||||
// STEP 1: Approve the pool to spend the input token
|
||||
console.log('🔐 Approving token spend for swap mint...');
|
||||
console.log('Token to approve:', inputTokenAddress);
|
||||
console.log('Spender (pool):', poolAddress);
|
||||
console.log('Amount:', maxAmountIn.toString());
|
||||
|
||||
const approvalHash = await walletClient.writeContract({
|
||||
address: inputTokenAddress,
|
||||
abi: [
|
||||
{
|
||||
name: 'approve',
|
||||
type: 'function',
|
||||
stateMutability: 'nonpayable',
|
||||
inputs: [
|
||||
{ name: 'spender', type: 'address' },
|
||||
{ name: 'amount', type: 'uint256' }
|
||||
],
|
||||
outputs: [{ name: '', type: 'bool' }]
|
||||
}
|
||||
],
|
||||
functionName: 'approve',
|
||||
args: [poolAddress, maxAmountIn],
|
||||
});
|
||||
|
||||
console.log('✅ Approval transaction submitted:', approvalHash);
|
||||
await publicClient.waitForTransactionReceipt({ hash: approvalHash });
|
||||
console.log('✅ Approval confirmed');
|
||||
|
||||
// STEP 2: Calculate deadline (5 minutes from now)
|
||||
const deadline = BigInt(Math.floor(Date.now() / 1000) + 300); // 5 minutes = 300 seconds
|
||||
|
||||
console.log('🚀 Executing swapMint with params:', {
|
||||
pool: poolAddress,
|
||||
payer: userAddress,
|
||||
receiver: userAddress,
|
||||
inputTokenIndex,
|
||||
maxAmountIn: maxAmountIn.toString(),
|
||||
deadline: deadline.toString(),
|
||||
});
|
||||
|
||||
// STEP 3: Execute the swapMint transaction
|
||||
const hash = await walletClient.writeContract({
|
||||
address: poolAddress,
|
||||
abi: IPartyPoolABI,
|
||||
functionName: 'swapMint',
|
||||
args: [
|
||||
userAddress, // payer
|
||||
userAddress, // receiver
|
||||
BigInt(inputTokenIndex),
|
||||
maxAmountIn,
|
||||
deadline,
|
||||
],
|
||||
});
|
||||
|
||||
setSwapMintHash(hash);
|
||||
console.log('✅ SwapMint transaction submitted:', hash);
|
||||
|
||||
// Wait for transaction confirmation
|
||||
const receipt = await publicClient.waitForTransactionReceipt({ hash });
|
||||
console.log('✅ SwapMint transaction confirmed:', receipt);
|
||||
|
||||
return receipt;
|
||||
} catch (err) {
|
||||
const errorMessage = err instanceof Error ? err.message : 'SwapMint failed';
|
||||
setSwapMintError(errorMessage);
|
||||
console.error('❌ SwapMint error:', err);
|
||||
throw err;
|
||||
} finally {
|
||||
setIsSwapMinting(false);
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
executeSwapMint,
|
||||
isSwapMinting,
|
||||
swapMintHash,
|
||||
swapMintError,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user