web3.js
web3.js is a Javascript library for building on EVM-compatible networks.
It allows developers to interact with smart contracts, send transactions, and retrieve data from the network.
Installation​
This guide assumes you have the latest version of Node.js installed.
To install web3
, run the following command:
_10npm install web3
Connecting to Flow​
To use web3
in your project, start by imporing the module and initializing your Web3 provider the desired Flow RPC endpoint.
_10const { Web3 } = require('web3')_10const web3 = new Web3('https://previewnet.evm.nodes.onflow.org')
Currently, only Previewnet is available. More networks are coming soon, see here for more info.
Interacting With Smart Contracts​
web3.js
allows developers to interact with smart contracts via the web3.eth.Contract
API.
For this example we will use the following contract, deployed on the Flow Previewnet to the address 0x1234
. However, if you wish to deploy your own contract, see the how to do so using Hardhat or Remix.
_14// SPDX-License-Identifier: MIT_14pragma solidity ^0.8.0;_14_14contract Storage {_14 uint256 public storedData;_14_14 function store(uint256 x) public {_14 storedData = x;_14 }_14_14 function retrieve() public view returns (uint256) {_14 return storedData;_14 }_14}
The ABI for this contract can be generated using the solc
compiler, or by using a tool like Hardhat or Remix.
_33const abi = [_33 {_33 "inputs": [],_33 "stateMutability": "nonpayable",_33 "type": "constructor"_33 },_33 {_33 "inputs": [_33 {_33 "internalType": "uint256",_33 "name": "x",_33 "type": "uint256"_33 }_33 ],_33 "name": "store",_33 "outputs": [],_33 "stateMutability": "nonpayable",_33 "type": "function"_33 },_33 {_33 "inputs": [],_33 "name": "retrieve",_33 "outputs": [_33 {_33 "internalType": "uint256",_33 "name": "",_33 "type": "uint256"_33 }_33 ],_33 "stateMutability": "view",_33 "type": "function"_33 }_33]
A Contract
object may be instantiated using the ABI and the address of the deployed contract.
_10const abi = [_10 ... // ABI of deployed contract_10]_10_10const contractAddress = "0x1234" // replace with the address of the deployed contract_10const contract = new web3.eth.Contract(abi, contractAddress)
Now that we have a Contract
object, we can interact with the contract by calling its methods.
Querying State​
We can query data from the contract by using the call
function on one of the contract's methods. This will not mutate the state and will not send a transaction.
_10const result = contract.methods.retrieve().call()_10_10console.log(result) // "0" (if the contract has not been interacted with yet)
Mutating State​
We can mutate the state of the contract by sending a transaction to the contract.
First, we need to sign the transaction using a user's account. To do this, we can use the privateKeyToAccount
function to create an account object from a private key. You can generate a private key using a tool like Metamask or Hardhat.
_10const userAccount = web3.eth.accounts.privateKeyToAccount('0x1234') // replace with the private key of the user's account
Then, we can sign a transaction using the user's account and send it to the network.
_13const newValue = 1337 // replace with the new value to store_13const sender = "0x1234" // replace with the address this transaction will be sent from_13_13let signed = await userAccount.signTransaction({_13 from: userAccount.address,_13 to: address,_13 data: contract.methods.store(newValue).encodeABI(),_13})_13_13// send signed transaction that stores a new value_13result = await web3.eth.sendSignedTransaction(signed.rawTransaction)_13_13console.log(result) // { status: true, transactionHash: '0x1234' }
For more information about using smart contracts in web3.js, see the official documentation.