Skip to content

kenfj/solidity-todo-example

Repository files navigation

Solidity Todo DApp Example 2021

This is simple Todo DApp using Solidity Truffle and React TypeScript

truffle version
# Truffle v5.3.9 (core: 5.3.9)
# Solidity - 0.8.3 (solc-js)
# Node v14.16.1
# Web3.js v1.3.6

Setup

# setup private network
truffle develop
truffle(develop)> compile
truffle(develop)> migrate
truffle(develop)> test

# start web app
cd app
BROWSER=none npm start
open http://127.0.0.1:3000/

Initial Setup Log

truffle init

# update compilers.solc.version: "0.8.3" in truffle-config.js

truffle create contract ToDo

# Open ToDo.sol in VSCode and right click
# Solidity Change workspace compiler version to 0.8.3
#
# This will add solidity version in .vscode/settings.json
# "solidity.compileUsingRemoteVersion": "v0.8.3+xxx"

Deploy and Test

# create migration script
truffle create migration ToDo

# start local blockchain at http://127.0.0.1:9545/
truffle develop
# import 1st account to MetaMask

compile

migrate
# or
migrate --reset
# create several tasks for testing
let toDo = await ToDo.deployed()
toDo.createTask("My task")

# check the created task
toDo.getTask(1)
(await toDo.getTaskIds()).toString()
# run test
truffle create test ToDo
truffle test

Web Interface

npx create-react-app app --template typescript --use-npm

cd app
BROWSER=none npm start
open http://127.0.0.1:3000/

# run test
npm test

npm install --save-dev web3 typechain @typechain/web3-v1

# https://material-ui.com/components/snackbars/#complementary-projects
npm install notistack

# Note: during development, sometimes you may need to close
# browser tab and re-open it (instead of just reload the page)

Types

  • generate type definition by typechain
# add contracts_build_directory in truffle-config.js
truffle compile
# this will output json to app/src/contracts/

cd app
# https://tech.mobilefactory.jp/entry/2019/12/04/163000
npx typechain --target=web3-v1 'src/contracts/**/*.json'
# this will output .d.ts to types/web3-v1-contracts/

# or one command
npm run typechain
# one shot command to truffle compile and typechain in app
npm run build

Using Web3Modal

npm install web3modal
npm install @walletconnect/web3-provider

Deploy to Ropsten test network

How to use MetaMask with web3

Troubleshooting

Reference