|
| 1 | +import { encodeFunctionData, Hex, parseEther } from 'viem'; |
| 2 | +import { ContractManager } from '../ContractManager'; |
| 3 | +import { printTransactionHash, showError, showText } from '../display'; |
| 4 | +import { actionPresets } from './common'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Sets the mint fee for a collection contract. |
| 8 | + * This feature is only available for contract version >= 1.0.2 |
| 9 | + * @param symbol - The collection symbol |
| 10 | + * @param params - Parameters including the new mint fee in ETH |
| 11 | + */ |
| 12 | +export const setMintFeeAction = async ( |
| 13 | + symbol: string, |
| 14 | + params: { |
| 15 | + mintFee: string; |
| 16 | + }, |
| 17 | +) => { |
| 18 | + try { |
| 19 | + const { cm, config } = await actionPresets(symbol); |
| 20 | + |
| 21 | + const contractAddress = config.deployment?.contract_address as Hex; |
| 22 | + |
| 23 | + if (!contractAddress) { |
| 24 | + throw new Error( |
| 25 | + 'Contract address not found. Please deploy the contract first.', |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + // Get current mint fee |
| 30 | + const currentMintFee = await cm.getMintFee(contractAddress); |
| 31 | + showText( |
| 32 | + `Current mint fee: ${currentMintFee} wei (${Number(currentMintFee) / 1e18} ETH)`, |
| 33 | + ); |
| 34 | + |
| 35 | + // Parse new mint fee |
| 36 | + const newMintFee = parseEther(params.mintFee); |
| 37 | + showText( |
| 38 | + `Setting new mint fee to: ${newMintFee} wei (${params.mintFee} ETH)`, |
| 39 | + ); |
| 40 | + |
| 41 | + await cm.printSignerWithBalance(); |
| 42 | + |
| 43 | + // Send transaction |
| 44 | + const txHash = await sendSetMintFeeTransaction( |
| 45 | + cm, |
| 46 | + contractAddress, |
| 47 | + newMintFee, |
| 48 | + ); |
| 49 | + |
| 50 | + printTransactionHash(txHash, config.chainId); |
| 51 | + showText('Mint fee updated successfully!'); |
| 52 | + } catch (error: any) { |
| 53 | + showError({ text: `Error setting mint fee: ${error.message}` }); |
| 54 | + } |
| 55 | +}; |
| 56 | + |
| 57 | +/** |
| 58 | + * Sends the setMintFee transaction to the contract |
| 59 | + * @param cm - Contract manager instance |
| 60 | + * @param contractAddress - The contract address |
| 61 | + * @param mintFee - The new mint fee in wei |
| 62 | + * @returns The transaction hash |
| 63 | + */ |
| 64 | +const sendSetMintFeeTransaction = async ( |
| 65 | + cm: ContractManager, |
| 66 | + contractAddress: Hex, |
| 67 | + mintFee: bigint, |
| 68 | +): Promise<Hex> => { |
| 69 | + const setMintFeeAbi = { |
| 70 | + inputs: [ |
| 71 | + { |
| 72 | + internalType: 'uint256', |
| 73 | + name: 'mintFee', |
| 74 | + type: 'uint256', |
| 75 | + }, |
| 76 | + ], |
| 77 | + name: 'setMintFee', |
| 78 | + outputs: [], |
| 79 | + stateMutability: 'nonpayable', |
| 80 | + type: 'function', |
| 81 | + } as const; |
| 82 | + |
| 83 | + const data = encodeFunctionData({ |
| 84 | + abi: [setMintFeeAbi], |
| 85 | + functionName: 'setMintFee', |
| 86 | + args: [mintFee], |
| 87 | + }); |
| 88 | + |
| 89 | + const txHash = await cm.sendTransaction({ |
| 90 | + to: contractAddress, |
| 91 | + data, |
| 92 | + }); |
| 93 | + |
| 94 | + const receipt = await cm.waitForTransactionReceipt(txHash); |
| 95 | + if (receipt.status !== 'success') { |
| 96 | + throw new Error('Transaction failed'); |
| 97 | + } |
| 98 | + |
| 99 | + return receipt.transactionHash; |
| 100 | +}; |
| 101 | + |
| 102 | +export default setMintFeeAction; |
| 103 | + |
0 commit comments