1

I've been trying to write to a contract on a local blockchain (Anvil from Foundry) but running the write() function (running it after submitting a form write?.()) does not work.

Tried to disable the submit button in the form by disabled={!write} and waited for a few minutes, it still does not work. Double checked the ABI and Token Address too, they seem to be correct.

here's the code for the page

function TokenForm() {
    const [formData, setFormData] = useState({
        tokenName: '',
        tokenSymbol: '',
        initialMintAmount: '',
    })

    const debouncedFormData = useDebounce(formData, 500)

    const { config } = usePrepareContractWrite({
        address: tokenFactoryAddress,
        contractInterface: abi,
        functionName: 'createToken',
        args: [
            debouncedFormData.tokenName,
            debouncedFormData.tokenSymbol,
            (
                BigInt(debouncedFormData.initialMintAmount) * BigInt(10 ** 18)
            ).toString(),
        ],
    })

    const { isSuccess, write } = useContractWrite(config)

    function handleChange(event) {
        const { name, value } = event.target
        setFormData((prevFormData) => {
            return {
                ...prevFormData,
                [name]: value,
            }
        })
    }

    function handleSubmit(event) {
        event.preventDefault()
        write?.()
        console.log(isSuccess)
    }

    return (
        <div className={styles.container}>
            <form onSubmit={handleSubmit} className={styles.form}>
                <input
                    type="text"
                    placeholder="your token's name"
                    onChange={handleChange}
                    name="tokenName"
                    value={formData.tokenName}
                    className={styles.inputBox}
                />
                <input
                    type="text"
                    placeholder="your token's symbol"
                    onChange={handleChange}
                    name="tokenSymbol"
                    value={formData.tokenSymbol}
                    className={styles.inputBox}
                />

                <input
                    type="number"
                    placeholder="initial tokens to mint"
                    onChange={handleChange}
                    name="initialMintAmount"
                    value={formData.initialMintAmount}
                    className={styles.inputBox}
                />
                {/* disabled={!write} */}
                <button disabled={!write}>Submit</button>
            </form>
        </div>
    )
}

For more context the function createToken() takes in 3 arguments - a string name, another string symbol and a uint256 initialMintAmount

Thank you!

1 Answer 1

3

In usePrepareContractWrite() I have written contractInterface: abi it should be abi: abi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.