{"id":7205,"date":"2024-02-13T11:46:27","date_gmt":"2024-02-13T11:46:27","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=7205"},"modified":"2024-02-13T11:46:30","modified_gmt":"2024-02-13T11:46:30","slug":"send-eth-to-solidity-payable-function-using-etherjs","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/send-eth-to-solidity-payable-function-using-etherjs\/","title":{"rendered":"How to Send ETH to a Solidity Payable Function Using Ether.js?"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_56_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/metaschool.so\/articles\/send-eth-to-solidity-payable-function-using-etherjs\/#What_is_the_Solidity_payable_function\" title=\"What is the\u00a0Solidity payable function?\">What is the\u00a0Solidity payable function?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/metaschool.so\/articles\/send-eth-to-solidity-payable-function-using-etherjs\/#Send_ETH_to_Solidity_payable_function_coding_example\" title=\"Send ETH to Solidity payable function coding example\">Send ETH to Solidity payable function coding example<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/metaschool.so\/articles\/send-eth-to-solidity-payable-function-using-etherjs\/#Explanation\" title=\"Explanation\">Explanation<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/metaschool.so\/articles\/send-eth-to-solidity-payable-function-using-etherjs\/#Conclusion\" title=\"Conclusion\">Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-solidity-payable-function\"><span class=\"ez-toc-section\" id=\"What_is_the_Solidity_payable_function\"><\/span>What is the\u00a0Solidity payable function?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>A Solidity payable function is a type of function that allows you to receive\u00a0ether\u00a0(ETH &#8211; Ethereum token) as an input parameter. When a contract calls the payable function, this feature allows the caller to input ether. After the transaction is complete, the ether becomes a part of the contract&#8217;s balance. Exciting, right?<\/p>\n\n\n\n<p>So, how do you send ether to your <a href=\"https:\/\/metaschool.so\/articles\/guide-solidity-contracts\/\" target=\"_blank\" rel=\"noreferrer noopener\">Solidity<\/a> payable function? We can use the ether.js to perform this task with ease. Here\u2019s the coding example for you.<\/p>\n\n\n\n<p><strong>\ud83d\udd25 Check this out:<\/strong>\u00a0<a href=\"https:\/\/metaschool.so\/build-in-public\" target=\"_blank\" rel=\"noreferrer noopener\">Build in Public<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"send-eth-to-solidity-payable-function-coding-example\"><span class=\"ez-toc-section\" id=\"Send_ETH_to_Solidity_payable_function_coding_example\"><\/span>Send ETH to Solidity payable function <strong>coding example<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This code snippet demonstrates how to send ether to a payable function in a <a href=\"https:\/\/metaschool.so\/articles\/structure-of-a-smart-contract\/\" target=\"_blank\" rel=\"noreferrer noopener\">smart contract<\/a> using ethers.js. It covers the initialization of a provider, creating a contract instance, specifying the amount of ether to send, and executing a transaction.<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-text-dark-bg-background-color has-background\"><code>\/\/Initial Setup\n\nconst amountToSend = ethers.utils.parseUnits(\"0.1\", 18); \/\/ 0.1 ETH in wei\nconst options = { value: amountToSend };\n\nconst transaction = await contractInstance.myPayableFunction(options);\nawait transaction.wait();\n\nconsole.log(\"Transaction complete:\", transaction.hash);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"explanation\"><span class=\"ez-toc-section\" id=\"Explanation\"><\/span><strong>Explanation<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li>The amount of ether to send (0.1 ETH) is converted to Wei (Wei is a smaller unit of ETH, which is equivalent to 10^-18 ETH.) using\u00a0<code>ethers.utils.parseUnits<\/code>.<\/li><li>We create an options object specifying the ether value to send.<\/li><li>The payable function (<code>myPayableFunction<\/code>) is called with the specified options, initiating the transaction.<\/li><li>We wait for the transaction to be processed and confirmed on the blockchain using the <code>await<\/code> keyword.<\/li><li>We log the transaction hash once the transaction is complete.<\/li><\/ol>\n\n\n\n<p>\ud83d\udd25\u00a0<strong>Check this course out:<\/strong>\u00a0<a href=\"https:\/\/metaschool.so\/courses\/how-to-write-a-smart-contract-and-mint-elon-musk-nft-on-opensea\" target=\"_blank\" rel=\"noreferrer noopener\">Write an\u00a0Elon Musk NFT Smart Contract on OpenSea<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span><strong>Conclusion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This code provides a practical example of interacting with a payable function in a smart contract, demonstrating the essential steps using ethers.js.<\/p>\n\n\n\n<p><strong>Try it out, ask us questions, and let us know how it went by tagging Metaschool on Social Media.<\/strong><\/p>\n\n\n\n<p><strong>Follow us on<\/strong>&nbsp;\u2013<\/p>\n\n\n\n<p>\ud83d\udd2eTwitter \u2013\u00a0<a href=\"https:\/\/twitter.com\/0xmetaschool\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/twitter.com\/0xmetaschool<\/a><\/p>\n\n\n\n<p>\ud83d\udd17LinkedIn \u2013\u00a0<a href=\"https:\/\/www.linkedin.com\/company\/0xmetaschool\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.linkedin.com\/company\/0xmetaschool\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":15,"featured_media":7206,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[292],"tags":[51,7,31,303,46],"class_list":["post-7205","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-answers","tag-blockchain","tag-ethereum","tag-ethereum-blockchain","tag-ethers-js","tag-solidity"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/comments?post=7205"}],"version-history":[{"count":3,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7205\/revisions"}],"predecessor-version":[{"id":7209,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7205\/revisions\/7209"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/7206"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=7205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=7205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=7205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}