This smart contract programming tutorial will teach you how to incorporate existing smart contract functionality into your dapps. Thanks to wagmi and Moralis โ the tools we use in this tutorial โ your dapp will be able to trigger โreadโ and โwriteโ Web3 contract methods or functions. The following snippets of code will do all the heavy lifting:
- To run โreadโ smart contract functions:
const response = await Moralis.EvmApi.utils.runContractFunction({ abi, functionName, address, chain, });
- To run โwriteโ smart contract functions:
const { config } = usePrepareContractWrite({})
const { write } = useContractWrite()
Along the way, youโll also have a chance to learn how to get a walletโs ERC20 token balance. This is where Moralisโ Ethereum API for tokens will do the trick via the following snippet of code:
const response = await Moralis.EvmApi.token.getWalletTokenBalances({ address, chain, });
If youโre already a proficient developer familiar with Moralis, go ahead and implement the above code snippets right away! If not, make sure to complete todayโs smart contract programming tutorial and level up your game. Just create your free Moralis account and follow our lead!

Overview
The core of todayโs article will be our smart contract programming tutorial. The latter will consist of two sub-tutorials โ one teaching you to run โreadโ smart contract functions and the other to run โwriteโ functions. If youโd like to begin the tutorial right away, click here.
For todayโs tutorial, your JavaScript (NodeJS and ReactJS) proficiency will get you to the finish line. Keep in mind that weโll provide you with the primary lines of code herein and links to the complete code that awaits you on GitHub. Consequently, weโve ensured that there are no obstacles in your way and that you can easily complete this smart contract programming tutorial.

After youโve rolled up your sleeves and successfully completed the tutorial, weโre going to provide you with some basics related to smart contract programming. As such, weโll explain what smart contracts are and what smart contract programming is. Weโll also list the leading smart contract programming languages in case some of you want to start creating your own smart contracts.
If youโd like to expand your skills even further after today, make sure to check out Moralis Academy and enroll in the Ethereum Smart Contract Programming 101 course!
Smart Contract Programming Tutorial
As you proceed, youโll have an opportunity to complete two sub-tutorials. One will show you how to run smart contract functions from your dapp, and the other how to write them. The first sub-tutorial will focus on completing the task within your terminal, without actually creating a frontend dapp. However, the second smart contract programming tutorial will also show you how to create a simple frontend dapp. Hereโs the gist of that dapp:
The above screenshot shows that the dapp youโll have a chance to build in the second sub-tutorial enables users to connect their wallets. Once users connect their wallets, they get to send their Chainlink (LINK) tokens by entering the amount and pasting a recipient address:
In order to incorporate existing smart contracts in dapps, you also want to learn how to explore their details. This is where blockchain explorers enter the picture. Since weโll be focusing on the Ethereum and Polygon (Mumbai) chains, Etherscan and PolygonScan will do the trick. Fortunately, both of these explorers are quite similar. Essentially, you need to enter a smart contractโs address or projectโs/tokenโs name. Then, you scroll down where you see a menu bar and click on the โContractโ option. By doing so, youโll get to explore a smart contractโs code, including the ABI (via โCodeโ), and the contractโs โreadโ and โwriteโ functions (via โRead Contractโ and โWrite Contractโ):
You must also have your Moralis Web3 API key to complete the upcoming challenges. To get it, make sure to create your free Moralis account. Then, you can access your admin area and obtain your API key:
Run Smart Contract Functions
One of the ways to incorporate smart contracts into dapps is to run smart contract functions. Thanks to Moralisโ โrunContractFunctionโ endpoint, it becomes incredibly straightforward. You just need to create a NodeJS dapp and incorporate the correct endpoint.
Start by creating a โContractFunctionsโ folder and open it in Visual Studio Code (VSC). Next, use your terminal to initialize NodeJS by running the following command:
npm init -y
The above command will result in a โpackage.jsonโ file in your file tree:
Then, install the required dependencies with this command:
npm i moralis dotenv
Also, create your โ.envโ file, in which you need to paste your above-obtained Web3 API key:
Moving forward, create an โindex.jsโ file. The latter will contain the logic required to complete this smart contract programming tutorial. Open this file, and at the top, import Moralis and require โ.envโ:
const Moralis = require(โmoralisโ).default; require(โdotenvโ).config();
Then you need to define the ABI of the smart contract that you want to focus on. Weโll use the โCool Catsโ contract on the Ethereum chain. By using the tips provided in the previous section, you already know how to get any contractโs ABI:
With the ABI copied, return to VSC and create a new โabi.jsonโ file. Open it and paste the contractโs ABI. Next, use โShift+Option+Fโ on Mac (or Windows/Linux equivalent) to properly rearrange the content. This contract has several โreadโ functions; however, we will focus on running โgetPriceโ with the โrunContractFunctionโ endpoint.
Utilizing the โrunContractFunctionโ Method
Reopen your โindex.jsโ file and require the above-created โabi.jsonโ file:
const ABI = require(โ./abi.jsonโ);
You can then initialize Moralis by utilizing your API key and implementing the โMoralis.EvmApi.utils.runContractFunctionโ method. You also need to use the smart contractโs details as parameters. When focusing on โCool Catsโ, these are the lines of code that do the trick:
Moralis.start({ apiKey: process.env.MORALIS_KEY }).then(async()=>{ const response = await Moralis.EvmApi.utils.runContractFunction({ address: โ0x1A92f7381B9F03921564a437210bB9396471050Cโ, functionName: โgetPriceโ abi: ABI }); console.log(response.raw) })
Finally, you can run your โindex.jsโ script with the following command:
node index.js
If you have followed our lead so far, your terminal should return the following results:
Note: The prices in ETH use 18 decimal places. Hence, the above result indicates that the initial price for the โCool Catsโ NFT was 0.02 ETH.
You can find the video version of this sub-tutorial below. This is also where you can practice running smart contract functions on another โreadโ function (โtokenURIโ). In addition, you can use the video below to learn how to combine the results of running the โgetPriceโ function with Moralisโ โgetNFTLowestPriceโ NFT API endpoint.
Write Smart Contract Functions
When running โreadโ smart contract functions, you do not change the state of the blockchain (you do not execute on-chain transactions). However, when you write smart contract functions, you execute blockchain transactions. Thus, you need to have your MetaMask wallet ready for this smart contract programming tutorial. Moving forward, weโll focus on an example smart contract dapp that runs on the Polygon testnet (Mumbai). Weโll walk you through the core components of the backend and frontend scripts of our example โSend ChainLinkโ dapp.
Note: You can find the complete code behind our example dapp on GitHub. There youโll see the โwriteโ (frontend) and โbackendโ folders.
With our code cloned, make sure you have the content of the โwriteโ folder in your โfrontendโ folder. Then, open that folder in VSC and install the required dependencies with this command:
npm i
Inside the โindex.jsโ frontend script, you can see all required functionalities imported at the top, including wagmi:
import React from 'react'; import ReactDOM from 'react-dom/client'; import './index.css'; import App from './App'; import { configureChains, mainnet, WagmiConfig, createClient } from 'wagmi' import { publicProvider } from 'wagmi/providers/public' import { polygonMumbai } from '@wagmi/chains';
Next, this script enables the Mumbai chain:
const { provider, webSocketProvider } = configureChains( [mainnet, polygonMumbai], [publicProvider()], )
This is where we create a wagmi client:
const client = createClient({ autoConnect: true, provider, webSocketProvider, })
Finally, this script wraps the entire app in โwagmiConfigโ:
const root = ReactDOM.createRoot(document.getElementById('root')); root.render( <WagmiConfig client={client}> <App /> </WagmiConfig> );
Implementing the MetaMask Wallet Connector
With the lines of code above, youโve set up your frontend. Next, you can use wagmi to connect the MetaMask wallet to your dapp inside the โApp.jsโ script. For a detailed code walkthrough, use the video below, starting at 5:03. However, as far as connecting wallet functionality goes, the following โimportโ lines are a prerequisite:
import { useConnect, useAccount, usePrepareContractWrite, useContractWrite } from "wagmi"; import { MetaMaskConnector } from "wagmi/connectors/metaMask"; import { useEffect, useState } from "react";
The following lines of code inside the โApp()โ function finalizes the process:
const { address, isConnected } = useAccount(); const { connect } = useConnect({ connector: new MetaMaskConnector(), });
Getting the Wallet Balance
One of the code snippets from this articleโs introduction focuses on getting a wallet balance. To implement this feature, you must use the โbackendโ folder. In it, you can find the backend โindex.jsโ script (08:15). This script only contains one endpoint: โ/getBalanceโ. At the core of this endpoint is the โMoralis.EvmApi.token.getWalletTokenBalancesโ method. The latter fetches the connected wallet address via โquery.addressโ for the LINK token (via โtokenAddressesโ) on the Mumbai chain (via chain ID). Here are the lines of code for this:
const response = await Moralis.EvmApi.token.getWalletTokenBalances({ address: query.address, chain: "80001", tokenAddresses: ["0x326C977E6efc84E512bB9C30f76E30c160eD06FB"] })
Just like you did in the above โrunโ smart contract programming tutorial, you must initialize Moralis with your API key:
Moralis.start({ apiKey: process.env.MORALIS_KEY, }).then(() => { app.listen(port, () => { console.log(`Listening for reqs`); }); });
Note: Again, make sure to store your key inside your โ.envโ file.
Do not forget to โcdโ into your โbackendโ folder and install all required dependencies with the โnpm iโ command. Then, youโll be able to run your backend dapp with the following command:
node index.js
With your backend running, your frontend (App.js) gets to display the balance with the following โasyncโ function:
async function getBalance() { const response = await axios.get("http://localhost:3000/getBalance", { params: { address: address, }, }); setUserBal(response.data.balance); }
The script utilizes โuseEffectโ to call the above โgetBalanceโ function when wallets connect to our dapp. Make sure to open a new terminal for your frontend because you need to keep your backend running. Then, โcdโ into your โfrontendโ folder and run the following command:
npm run start
Note: Use the video below (14:10) to explore how your dappโs input fields work.
The โwriteโ Function
With the above functionalities in place, weโve reached the core aspect of this smart contract programming tutorial. This is where weโll take a closer look at the lines of code that enable your dapp to trigger a smart contractโs โwriteโ functions.
As you might have noticed earlier, the โApp.jsโ script imports โusePrepareContractWriteโ and โuseContractWriteโ. This enables your code to prepare the data and actually trigger the โwriteโ contract functions. Using the video below, starting at 16:00, you can see how we use PolygonScan to get the โChainLink Tokenโ smart contract details. This includes inspecting all โwriteโ functions this smart contract includes and copying its ABI. You already have the latter ready in the โabi.jsonโ file. Now, since our dapp aims to transfer LINK tokens, we want to focus on the โtransferโ function of the โChainLink Tokenโ contract.
Note: You can learn the purpose of the โuseDebounce.jsโ script in the video below (17:27).
Ultimately, the โApp.jsโ script first prepares the details of the contract we are focusing on by utilizing โusePrepareContractWriteโ. Then, it uses โuseContractWriteโ based on those details. These are the lines of code that enable our example dapp to trigger a โwriteโ smart contract function:
const { config } = usePrepareContractWrite({ address: '0x326C977E6efc84E512bB9C30f76E30c160eD06FB', abi: ABI, chainId: 80001, functionName: 'transfer(address,uint256)', args: [debouncedReceiver, debouncedSendAmount], enabled: Boolean(debouncedSendAmount) }) const { write } = useContractWrite(config)
The above โwriteโ function is triggered when users hit the โSendโ button:
<button disabled={!write} onClick={()=>write?.()}>Send</button>
Finally, hereโs the video that weโve been referencing in this second sub-tutorial:
What are Smart Contracts?
Smart contracts are on-chain programs โ pieces of software that run on development blockchains (e.g., Ethereum). As such, smart contracts automate and guide on-chain processes. They do so by executing specific predefined actions whenever certain predefined conditions are met. Itโs thanks to this automation that these on-chain programs are โsmartโ. Furthermore, smart contracts are cost-effective, autonomous, trustless, and secure. Plus, the code behind every smart contract is fully transparent โ everyone can view it with blockchain explorers.
Thanks to these properties, Web3 contracts have enormous potential to radically change all industries in the upcoming years. They have the power to eliminate intermediaries and make the world more efficient and fair. Of course, thatโs the long-term goal. However, Web3 is still in its early stage; therefore, nowโs the best time to learn Web3 programming. What better way to do that than to take on a smart contract programming tutorial?
What is Smart Contract Programming?
So, what is smart contract programming? At its core, smart contract programming is the process of writing, compiling, deploying, and verifying smart contracts. However, since the first part โ writing a smart contract โ is the trickiest, smart contract programming primarily focuses on that task. After all, you can compile, deploy, and verify Web3 contracts with a couple of clicks when using the right tools. On the other hand, writing a proper smart contract from scratch is a rather advanced feat, especially if you aim to implement some original functionalities.
To create a smart contract, you need to be proficient in one of the programming languages for smart contracts. Moreover, there isnโt one universal smart contract language that targets all popular blockchains, at least not yet. Instead, you need to decide which blockchain you want to focus on and then choose among the supported languages for that chain. All in all, if you want to master smart contract programming, you need to be prepared to put in quite some time and effort.
However, to start working with smart contracts is rather simple. For instance, if you want to deploy your smart contract on Ethereum that deals with already known tasks, you can use one of many verified smart contract templates and apply minor tweaks. Then, you can compile, deploy, and verify it with the Remix IDE and MetaMask. Furthermore, you can start incorporating smart contract functionalities into decentralized applications (dapps). A great example of that would be to call a smart contract function from JavaScript. You can use a Web3 JS call contract function or listen to smart contract events using ethers.js.

Languages for Smart Contract Programming
Below, you can see the list of the six most popular smart contract coding languages and the leading chains they focus on:
- Solidity for Ethereum and other EVM-compatible chains
- Vyper for Ethereum and other EVM-compatible chains
- Yul (and Yul+) as an intermediate language for the Solidity compiler
- Rust for Solana, Polkadot, NEAR, and several other chains
- C++ for EOS
- JavaScript (NodeJS) for Hyperledger Fabric and NEAR
Other note-worthy programming languages that you can use to write smart contracts include Clarity (Bitcoin), Golang/Go (Ethereum), Marlowe (Cardano), Cadence (Flow), LIGO (Tezos), Move (Aptos), TEAL (Algorand), and Python (Hyperledger Fabric).
Note: If you want to learn more details about Solidity, Vyper, and Yul, make sure to use the โprogramming languages for smart contractsโ link above.
When it comes to incorporating smart contract functionalities into dapps, you can use any of your favorite programming languages thanks to Moralisโ cross-platform interoperability.
Smart Contract Programming Tutorial for Blockchain Developers โ Summary
In todayโs smart contract programming tutorial, we covered quite some ground. You had a chance to follow our lead and complete todayโs smart contract programming tutorial. First, you learned to create a NodeJS dapp that runs โreadโ smart contract functions using the Moralis โrunContractFunctionโ endpoint. And in the second tutorial, you had an opportunity to clone our dapp and use it to trigger โwriteโ smart contract functions. Once we finished the tutorial, we addressed some of the basics. As such, you had a chance to refresh your understanding of smart contracts and smart contract programming. You also learned what the leading smart contract coding languages are.
If you enjoyed todayโs tutorial, make sure to dive into Moralisโ docs and tackle the โTutorialsโ section. Donโt forget to reinforce your blockchain development knowledge by visiting the Moralis YouTube channel and the Moralis blog. These two outlets cover a wide range of topics. For instance, some of the latest articles will help you understand blockchain-based data storage, how to get contract logs, how to get Ethereum transaction details, and much more. If you want to take on the smart contract programming course mentioned in the โOverviewโ section, make sure to enroll in Moralis Academy.
Read More: moralis.io