{"id":6675,"date":"2023-11-13T08:00:00","date_gmt":"2023-11-13T08:00:00","guid":{"rendered":"https:\/\/jamesbachini.com\/?p=6675"},"modified":"2023-11-02T17:49:29","modified_gmt":"2023-11-02T17:49:29","slug":"ethers-rs-tutorial","status":"publish","type":"post","link":"https:\/\/jamesbachini.com\/ethers-rs-tutorial\/","title":{"rendered":"Ethers-rs Tutorial | The Rust Web3 Library"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In this tutorial we will be setting up a Rust script to connect to a smart contract on a blockchain network to display on-chain data using the ethers-rs library.<\/p>\n\n\n\n<div class=\"video-wrapper\" onclick=\"window.open('https:\/\/www.youtube.com\/c\/JamesBachini?sub_confirmation=1');\" >\n\t<div class=\"video-header\">\n\t\t<div class=\"video-channel\">James On YouTube<\/div>\n\t\t<img decoding=\"async\" data-src=\"https:\/\/jamesbachini.com\/wp-content\/uploads\/2020\/04\/youtubeSubscribeButton.png\" class=\"video-button lazyload\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 500px; --smush-placeholder-aspect-ratio: 500\/111;\" \/>\n\t<\/div>\n\t<!-- Copy & Pasted from YouTube -->\n\t<iframe data-src=\"https:\/\/www.youtube.com\/embed\/FA7WFGtyri8?rel=0&amp;hd=1\" allowfullscreen=\"\" width=\"560\" height=\"349\" frameborder=\"0\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" data-load-mode=\"1\"><\/iframe>\n<\/div>\n<div class=\"text-center\">Watch On YouTube: <a href=\"https:\/\/youtu.be\/FA7WFGtyri8\" target=\"_blank\" rel=\"noopener\">https:\/\/youtu.be\/FA7WFGtyri8<\/a> |<a href=\"https:\/\/www.youtube.com\/c\/JamesBachini?sub_confirmation=1\" target=\"_blank\" rel=\"noopener\">Subscribe<\/a> <\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s start by setting up a new Rust project (you&#8217;ll need <a href=\"https:\/\/docs.rs\/\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/docs.rs\/\" rel=\"noreferrer noopener\">rust<\/a> installed on your device and I&#8217;ll be using <a href=\"https:\/\/learn.microsoft.com\/en-us\/windows\/wsl\/install\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/learn.microsoft.com\/en-us\/windows\/wsl\/install\" rel=\"noreferrer noopener\">Windows subsystem for linux<\/a> which is recommended).<\/p>\n\n\n\n<pre class=\"wp-block-code console\"><code>cargo new ethers-test<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following to your dependencies in the <em>Cargo.toml<\/em> file<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;dependencies]\nethers = \"2.0\"\ntokio = { version = \"1\", features = &#91;\"rt-multi-thread\", \"macros\"] }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We will also need an <a href=\"https:\/\/docs.infura.io\/networks\/ethereum\/how-to\/secure-a-project\/project-id\" data-type=\"link\" data-id=\"https:\/\/docs.infura.io\/networks\/ethereum\/how-to\/secure-a-project\/project-id\" target=\"_blank\" rel=\"noreferrer noopener\">Infura API key<\/a>. The infura key enables us to connect to one of their nodes which is connected to the p2p Ethereum network. Let&#8217;s export this to an environmental variable<\/p>\n\n\n\n<pre class=\"wp-block-code console\"><code>export INFURA_API_KEY=your_infura_api_key_here<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We then need a contract address and ABI (application binary interface) for the smart contract we want to interact with. I&#8217;m going to be using this contract on Goerli testnet: 0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can put the contract address in to https:\/\/goerli.etherscan.io and download the contract ABI. <a href=\"https:\/\/goerli.etherscan.io\/address\/0xfba3912ca04dd458c843e2ee08967fc04f3579c2#code\">https:\/\/goerli.etherscan.io\/address\/0xfba3912ca04dd458c843e2ee08967fc04f3579c2#code<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/jamesbachini.com\/wp-content\/uploads\/2023\/11\/image-6.png\"><img decoding=\"async\" width=\"1024\" height=\"223\" data-src=\"https:\/\/jamesbachini.com\/wp-content\/uploads\/2023\/11\/image-6-1024x223.png\" alt=\"Contract ABI\" class=\"wp-image-6676 lazyload\" data-srcset=\"https:\/\/jamesbachini.com\/wp-content\/uploads\/2023\/11\/image-6-1024x223.png 1024w, https:\/\/jamesbachini.com\/wp-content\/uploads\/2023\/11\/image-6-300x65.png 300w, https:\/\/jamesbachini.com\/wp-content\/uploads\/2023\/11\/image-6-768x167.png 768w, https:\/\/jamesbachini.com\/wp-content\/uploads\/2023\/11\/image-6-1536x334.png 1536w, https:\/\/jamesbachini.com\/wp-content\/uploads\/2023\/11\/image-6-2048x446.png 2048w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/223;\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s save the download to the following location <em>src\/abi.json<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s go in and edit the <em>src\/main.rs<\/em> file. The code for this can also be found on Github here: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use ethers::prelude::*;\nuse ethers::providers::{Provider, Http};\nuse std::sync::Arc;\nuse std::env;\n\n#&#91;tokio::main]\nasync fn main() -&gt; Result&lt;(), Box&lt;dyn std::error::Error&gt;&gt; {\n    let contract_address = \"0xfba3912ca04dd458c843e2ee08967fc04f3579c2\".parse::&lt;Address&gt;()?;\n    abigen!(IERC721, \".\/src\/abi.json\");\n    let rpc_url = format!(\"https:\/\/goerli.infura.io\/v3\/{}\", env::var(\"INFURA_API_KEY\")?);\n    let provider = Provider::&lt;Http&gt;::try_from(rpc_url.as_str())?;\n    let provider = Arc::new(provider);\n    let contract = IERC721::new(contract_address, provider.clone());\n\n    let function_name = \"symbol\";\n    let function_params = ();\n    let result: String = contract.method(function_name, function_params)?.call().await?;\n    println!(\"{}\", result);\n    Ok(())\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s build and run the code<\/p>\n\n\n\n<pre class=\"wp-block-code console\"><code>cargo run<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And you should see an output of &#8220;<em>WAGMI<\/em>&#8221; which is the data returned from the smart contract.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/jamesbachini.com\/wp-content\/uploads\/2023\/11\/image-7.png\"><img decoding=\"async\" width=\"1024\" height=\"433\" data-src=\"https:\/\/jamesbachini.com\/wp-content\/uploads\/2023\/11\/image-7-1024x433.png\" alt=\"wagmi with ethers-rs\" class=\"wp-image-6681 lazyload\" data-srcset=\"https:\/\/jamesbachini.com\/wp-content\/uploads\/2023\/11\/image-7-1024x433.png 1024w, https:\/\/jamesbachini.com\/wp-content\/uploads\/2023\/11\/image-7-300x127.png 300w, https:\/\/jamesbachini.com\/wp-content\/uploads\/2023\/11\/image-7-768x325.png 768w, https:\/\/jamesbachini.com\/wp-content\/uploads\/2023\/11\/image-7-1536x650.png 1536w, https:\/\/jamesbachini.com\/wp-content\/uploads\/2023\/11\/image-7.png 1824w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/433;\" \/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial we will be setting up a Rust script to connect to a smart contract on a blockchain network to display on-chain data using the ethers-rs library. James On YouTube Watch On YouTube: https:\/\/youtu.be\/FA7WFGtyri8 |Subscribe Let&#8217;s start by setting up a new Rust project (you&#8217;ll need rust installed on your device and I&#8217;ll [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":6677,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ai_generated_summary":"","wpai_meta_description":"","footnotes":""},"categories":[806,4,811,721,1188,237,1187],"tags":[1348,1451,1450,1102],"class_list":["post-6675","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blockchain","category-code","category-development","category-featured","category-solidity","category-tutorials","category-web3","tag-ethers","tag-ethers-rs","tag-rust","tag-web3"],"_links":{"self":[{"href":"https:\/\/jamesbachini.com\/wp-json\/wp\/v2\/posts\/6675","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jamesbachini.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jamesbachini.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jamesbachini.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jamesbachini.com\/wp-json\/wp\/v2\/comments?post=6675"}],"version-history":[{"count":5,"href":"https:\/\/jamesbachini.com\/wp-json\/wp\/v2\/posts\/6675\/revisions"}],"predecessor-version":[{"id":6683,"href":"https:\/\/jamesbachini.com\/wp-json\/wp\/v2\/posts\/6675\/revisions\/6683"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/jamesbachini.com\/wp-json\/wp\/v2\/media\/6677"}],"wp:attachment":[{"href":"https:\/\/jamesbachini.com\/wp-json\/wp\/v2\/media?parent=6675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jamesbachini.com\/wp-json\/wp\/v2\/categories?post=6675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jamesbachini.com\/wp-json\/wp\/v2\/tags?post=6675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}