/web3js-simple-storage

A repository for compiling, deploying and calling smart contracts using web3.js

Primary LanguageJavaScript

web3js-simple-storage

A repository for compiling, deploying and calling smart contracts using web3.js.

Inspired by Patrick Collins' blockchain course [1], I decided to recreate Python lessons with JavaScript.

This is Lesson 4: Web3 Simple Storage.

Getting started

Run the commands below to clone the repository and install the requirements.

Note that you may need a different version of solc.js.

git clone git@github.com:imagobea/web3js-simple-storage.git
cd web3js-simple-storage.git
npm install

Also needed:

  • Node.js
  • Ganache for spinning a local blockchain
  • An Ethereum blockchain provider, like Infura (plus an Ethereum account e.g. using Metamask, and some test Ether, e.g. from Rinkeby faucet)

About Lesson 4 Simple Storage

I recommend watching the lesson on Youtube, but this is pretty much what covers:

1 Compiling smart contracts

About solc

The solc.js library is used to compile Solidity, hence it has to be compatible with the version of your contract. Update the package.json file as needed before installing the requirements, or update these.

Execution

Place your contract into the contracts folder. Update scripts/compile.js as necessary. Run:

node -r dotenv/config scripts/compile.js

This will create a compiled_code.json file at root. This includes:

  • The contract bytecode, required for deployment
  • The Application Binary Interface (ABI), required for contract interactions

2 Deploying smart contracts

Set up web3.js to use the Ethereum blockchain in JavaScript [2].

3 Calling smart contracts

TBC

Repo room for improvement

  • Network and URL provider environment vars
  • Environment vars management in general
  • Better error handling and logging - a classic!
  • Flexible scripts to allow faster interactions with contracts other than SimpleStorage.sol

Nice to have

  • Explore alternative ways of creating and broadcasting signed transactions (see scripts/deploy_testnet.js, bottom)
  • Improve deployment scripts for publishing verified contracts

Other References

[1] Solidity, Blockchain, and Smart Contract Course - GitHub

[2] JavaScript API Libraries