To split our project's contract from frontend code, let's initialize our frontend project: assuming that your terminal is open at your contract's folder /home/user/path/to/counter-contract let's go back up one directory.
The fuels package includes all the main tools you need to interact with your Sway programs and the Fuel network.
The @fuel-wallet packages include everything you need to interact with user wallets.
The fuels init command generates a fuels.config.ts file that is used by the SDK to generate contract types.
Use the contracts flag to define where your contract folder is located, and the output flag to define where you want the generated files to be created.
Run the command below in your frontend folder to generate the config file:
Now that you have a fuels.config.ts file, you can use the fuels build command to rebuild your contract and generate types.
Running this command will interpret the output ABI JSON from your contract and generate the correct TypeScript definitions.
If you see the folder fuel-project/counter-contract/out you will be able to see the ABI JSON there.
Inside the fuel-project/frontend directory run:
npx fuels build
A successful process should print and output like the following:
If you're having any issues with this part, try adding useBuiltinForc: false, to your fuels.config.ts config file to make sure it's using the same version of forc as your default toolchain.
Now you should be able to find a new folder fuel-project/frontend/src/sway-api.
Compiled successfully!You can now view frontend in the browser. Local: http://localhost:3000 On Your Network: http://192.168.4.48:3000Note that the development build is not optimized.To create a production build, use npm run build.
Click the "Connect" button and select "Fuel Wallet Development" to connect your wallet.
Once connected, you should see the counter value and increment button:
If you run into any problems, a good first step is to compare your code to this repo and resolve any differences.
Tweet us @fuel_network Icon Link letting us know you just built a dapp on Fuel, you might get invited to a private group of builders, be invited to the next Fuel dinner, get alpha on the project, or something š.
To develop and test faster, we recommend using the fuels dev command to start a local node and automatically redeploy and generate types for your contract on each change.
Once you're ready to redeploy your contract to the testnet, here are the steps you should take to get your frontend and contract back in sync:
In your frontend directory, re-run this command: npx fuels build.
In your contract directory, redeploy the contract by running this command and following the same steps as above to sign the transaction with your wallet: forc deploy --testnet.
In your frontend directory, update the contract ID in your App.tsx file.