This is a sample stake contract project
React, Nest JS, Solidity
Commands:
$ npm run setupThis command will install the dependencies for all projects including hardhat project, react-app and nest-app.
$ hardhat compile
Commands:
$ npm start
This command will run react-app and nest-app on port 3000 and 3001 respectively. They'll run in the same terminal
concurrently in development mode.
Hardhat project contains the ProStakers contract. It also has means to deploy the contract on Rinkeby testnet.
Commands:
npx hardhat help
npx hardhat test
GAS_REPORT=true npx hardhat test
npx hardhat node
npx hardhat run scripts/deploy.tsThe React-App is on react-app folder.
React app has an interface to deposit and withdraw from the contract It also fetches the stored events from nest-app.
Notes: The app is pointing to a deployed contract on rinkeby network. To update the contract address you must update the address on
react-app/src/contract/proStakers.json, because I didn't setup any env file yet.
- VITE_CONTRACT_ADDRESS: The contract address.
- VITE_NEST_BASE_URL: The base url of the nest app.
Scripts:
$ npm run devNest.js Backend is located on nest-app folder.
Simple global module to export the ContractWebSocketProvider.
To enable this module just import it on the AppModule like this:
{
imports: [
EthersModule.forRoot({
url: process.env.ALCHEMY_CONTRACT_WS_URL,
})
]
}- ETHERS_WS_CONTRACT_PROVIDER
- ETHERS_WS_PROVIDER
- InjectWebSocketContractProvider
- InjectWebSocketProvider
- CONTRACT_ADDRESS: The contract address.
- ALCHEMY_CONTRACT_WS_URL: The websocket URL from Alchemy
The WebSocket client is a client to listen to the ProStakers contract events. It calls EventsServices when it listens to the events.
- Index
- Endpoint: "/events?address=<ADDRESS_HERE>&type=<EVENT_TYPE>"
- Method: "GET"
- Methods
- Create
- FindEvents
Notes: The nest-app is pointing to a deployed contract on rinkeby network. To update the contract address you must update the address on
nest-app/src/events/socket-client.tsfile.
I think the overall project is ok, but I could make some things better, these are the things I'd like to do next.
- Setup environment variables properly.
- ReactApp:
- Deal with the loading state;
- Validate the form values;
- Create tabs to render Deposit and Withdraw conditionally;
- Refactor the App.tsx file into components;
- Refactor the signing logic into a custom hook;
- Add address and EventType filters to the table .
- Add cool styles with themes.
- NestJS:
- Paginate the getEvents request.
- Refactor ethers.js into a module
Notes: All these done items were completed after 11/07/2022.