Skip to content

ZuperHunt/Fuel-Writing-a-Sway-Smart-Contract

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 

Repository files navigation

Author: Naufal

Note

WHAT IS FUEL?
Fuel is an operating system purpose-built for Ethereum rollups, designed to help developers build decentralized economies at scale.

Writing a Sway Smart Contract

Tip

Use Gitpod, see acknowledgements for the guide, basically you just need an Gitpod account.

Warning

DO NOT SKIP EVERY STEPS! FOLLOW CLOSELY!

1. Dependencies

1.1 Install rustup

Proceed with standard installation (default - just press enter).

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Configure your PATH environment variable.

. "/workspace/.cargo/env"

1.2 Install fuelup

This will install forc, forc-client, forc-fmt, forc-lsp, forc-wallet as well as fuel-core.

curl https://install.fuel.network | sh

Configure your PATH environment variable.

export PATH="${HOME}/.fuelup/bin:${PATH}"

2. Building Smart Contract

2.1 Create a new Fuel project

Run the command below to generate a counter contract in Sway.

forc new zuperfuel
cd zuperfuel

2.2 Set up smart contract

Edit src/main.sw file by run the command below, clear/delete everything inside.

nano src/main.sw

Copy and paste the code below into your src/main.sw file, and save the file by press ctrl + x on your keyboard, then press y, and press enter.

contract;
 
storage {
    counter: u64 = 0,
}
 
abi Counter {
    #[storage(read, write)]
    fn increment();
 
    #[storage(read)]
    fn count() -> u64;
}
 
impl Counter for Contract {
    #[storage(read)]
    fn count() -> u64 {
        storage.counter.read()
    }
 
    #[storage(read, write)]
    fn increment() {
        let incremented = storage.counter.read() + 1;
        storage.counter.write(incremented);
    }
}

Build the smart contract by run the command below.

forc build

3. Deploying Smart Contract

3.1 Set up wallet

Import your wallet, input your mnemonic phrase, and random password.

forc wallet import

Next, create a new wallet account. Don't worry, this step will create index 0 account, which means the same address you uses daily.
Just run the command below, you'll be asked to input the password created earlier.

forc wallet account new

3.2 Deploooyy!

Steady lads.. deploying smart contract 🤓

forc deploy --testnet

You'll be asked to input the password created earlier.
You'll be asked to provide the index of account to use for signing, just input the number 0, then input y, and press enter.

Finally, you will get back the network endpoint where the contract was deployed, a Contract ID and the block where the transaction was signed.

Help

If you have any questions, find us on:
ZuperHunt's Discord server
ZuperHunt's X(Twitter)

Acknowledgements