Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR HTML

javascript send ethereum

<form>
    <input id="eth" name="eth" type="number" value="0.01">
    <input id="recipient" name="recipient" type="text">
    <button>Send Ethereum</button>
</form>

<!-- Using Ethers.JS -->
<script src="https://unpkg.com/ethers@5.5.2/dist/ethers.umd.min.js"></script>
<script>
    (async function init(){ 

        if ( !window.ethereum ){
            throw new Error("No crypto wallet found!");
        }

        const form = document.querySelector("form");

        form.addEventListener("submit", async (e)=>{

            e.preventDefault();

            try {
                await window.ethereum.send("eth_requestAccounts");
                const provider = new ethers.providers.Web3Provider(window.ethereum);
                const signer = provider.getSigner();
                const recipient = ethers.utils.getAddress(form.recipient.value);
                const tx = await signer.sendTransaction({
                    to: recipient,
                    value: ethers.utils.parseEther(form.eth.value)
                });
                console.log({ 
                    from: tx.from,
                    to: tx.to,
                    hash: tx.hash 
                });

            } catch(e){

                console.error(e.message);

            }
        });

    }());
</script>
Source by www.codegrepper.com #
 
PREVIOUS NEXT
Tagged: #javascript #send #ethereum
ADD COMMENT
Topic
Name
2+5 =