Awesome
NextJS - Starknet.js - Cairo • Workshop
StarknetCC - Full dApp workshop by Alpha Road team
You'll find the PDF presentation here
Introduction
This workshop will show you how to dev a full Starknet dApp with NextJS & Starknet.js, including multi-wallets support(Argent-x | Braavos) and multi-call transactions.
This repo contains the front-end part, you'll find the contracts part here (access controller)
You'll also use an ERC20 contract, with a 'freeMint' function added allowing you to mint up to 1k tokens per call(see ABI).
This workshop currently run on the Starknet Goerli Testnet
Use case
Explore the contract interactions in NextJS by using an access controller contract (whitelist) & ERC20 contracts deployed on Starknet.
- Contract interactions
- Start with existing contracts
- Deploy & connect your own
- Access controller
- freeSlotsCount
- isAllowed
- register
- Minting tokens (multicalls)
The Goal
Prerequisite
To follow this workshop you should at least:
- Have a Starknet compatible wallet installed (with some Starknet Goerli ETH in wallet to do TX's)
- Have basic knowledge in ReactJS / NextJS framework
- Have basic knowledge in Cairo smart contracts development
- Have basic knowledge on how Starknet works(BC in general, Account abstraction, ...)
About the tooling:
- IDE
- Yarn or NPM
- Nile (smart contracts part)
Clone the repository
git clone git@github.com:419Labs/workshop-arf-dapp.git
cd workshop-arf-dapp
Install Dependencies
yarn | npm install
Run in dev
yarn dev | npm run dev
File tree
Here are the interesting files tree you'll have to update/use during this workshop
- src
- components
- wallet
- Block.tsx
- MultiMint.tsx
- RegisterWhitelist.tsx
- Transactions.tsx
- WalletConnect.tsx
- wallet
- pages
- _app.tsx
- index.tsx
- context
- BlockProvider
- ContractProvider
- StarknetProvider
- TransactionsProvider
- contracts
- abis
- ARF_ERC20.json
- AccessController.json
- addresses.ts
- abis
- components
TODO
- Familiarize a little with the project
- See the file tree
- Contract addresses are located in contract constants
- Use of React Context to isolate code complexity
- (use of ChakraUI components library)
- Uncomment part by part the workshop in index.ts
- Fetching block infos
- Update the StarknetProvider to connect to the default Starknet provider by using Starknet.js
- Update the BlockProvider, use the current provider to fetch current block infos
- (The UI part is already done here)
- Wallet connect
- Look at the WalletConnect button component
- Update the "connectBrowserWallet" StarknetProvider to ask user wallet connection by using get-starknet library
- Update the default provider by the user provider
- You should be able to connect with Braavos or ArgentX, and see your current address on the UI
- Your first transaction
- Start by taking a look at the Contract Provider model
- You can now update the RegisterWhitelist component to
- Fetch the total free slots currently available
- Fetch the current whitelisted status of the connected account
- Register to the whitelist if there is free slots & the connected account is not currently whitelisted
- Multi-call
- Look at the MultiMint component
- Here you'll have to
- Fetch balances of arfBTC & arfETH when needed
- Mint both arfBTC & arfETH in 1 TX using the multicall
- (You can also make a 'register to whitelist + mint token' in 1 TX as well)
- Deploy your own Access Controller
- You can put aside IDE for the time & open your terminal
- clone the access-controller repo
> git clone git@github.com:419Labs/access-controller-contracts.git
- Use nile to compile & deploy the contracts
> nile compile
> nile deploy AccessController --alias my_access_controller [...args] --network=goerli
- Wait until the contract is deployed
- You can check the status of the TX by calling get_transaction on the starknet cli
- Once it's ok, you can test it by calling freeSlotsCount on the deployed contract
- With Nile CLI
- With the voyager
- You can now replace the ACCESS_CONTROLLER_CONTRACT_ADDRESS in the contract constants to use your own
- Add your feature
- In this part you'll add your own feature from A to Z
- Start by choosing the feature you want to implement
- Remove from whitelist, Get a list of all whitelisted addresses, ...
- Implement the feature on the AccessController_base contract & Add the corresponding @view or @external interface on the AccessController contract
- Compile & Deploy your new contract
- Get the new abi.json & update the current one
- Implement the new feature on the UI
- By create a new component in /src/components/wallet & add it in the index
Contract addresses
Access Controller
0x027b91a18ed8c3ea77617c1818fb43ab0397144b956506181bf88ab9885a1338
arfETH ERC20
0x3dd7b0db7cca8e8468d06d27b40ca9368754c30d76900fcd19a65736fab9084
name: Ethereum Goerli
symbol: arfETH
supply: Infinite
arfBTC ERC20
0x72df4dc5b6c4df72e4288857317caf2ce9da166ab8719ab8306516a2fddfff7
name: Bitcoin Goerli
symbol: arfBTC
supply: Infinite
Go further
There some improvements you can make to improve this workshop, here is a non-exhaustive list:
- Save transactions in local storage
- Display current transaction information on the UI (toast, history panel, ...)
- Auto reload of: whitelisted after register, balance after mint
- Implement a button to be able to add a specific token to your wallet
- ...
Docs
Here is all the docs you'll need to complete the workshop
https://github.com/starknet-community-libs/get-starknet
https://github.com/OpenZeppelin/nile
https://github.com/OpenZeppelin/cairo-contracts
Helps
Work with big numbers in JavaScript could be a mess. As you'll probably use the 18 decimals standard for the token balances, you'll not be able to store it as a Number but as a string representation. To abstract the complexity, you can use the well known Ether.js library (especially the formatUnits & parseUnits) in completion of the Starknet.js helpers(toFelt, bnToUint256, ...)
Thanks
Forked from Cairopal
License
This workshop is released under the AGPL-3.0-only.