Cover photo

Deploy your First Smart Contract on Beta-5!

Hey there fam! As you know a while ago Beta-5 testnet was launched and at first, I decided not to do the guide about Smart Contract Deployment as the process is pretty much as an old one, but after reading 10+ messages from you asking about doing it I decided - why not :)


As you already guessed this guide will be about contract creation on Beta-5, but I decided to add at least some colors, so I divided this guide into 2 parts:

  • Contract Creation in 30 sec with Sway Playground on Beta-5

  • Full Steps Guide for contract creation via terminal on Beta-5

It is up to you which option to choose, important to mention that the output will be the same - you’ll deploy a smart contract! But from my side, I really recommend to try both as it will show you why I love Fuel so much!)

So no more words, let’s do it!

Contract Creation in 30 sec with Sway Playground on Beta-5

1.1 Install Fuel Wallet And Faucet Funds

https://wallet.fuel.network/

Go to the Fuel Wallet page and install it (the process is pretty much the same as MM so I don’t think I should explain the steps)

Then copy your wallet address and faucet funds here

post image

Funds are arriving in just seconds so after that you are good to move to the next step.

https://sway-playground.org/

post image

Copy the contract code and paste it in the Playground:

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);
    }
}

Press Compile and wait for couple of seconds - you should see an output like that:

Compiled with forc 0.49.3

After that press Deploy

post image

Confirm tx in Wallet

post image

And congratulations you’ve deployed your smart contract!

(Optional) Now let’s interact with it:

This is a simple counter smart contract so the logic is simple": Every time we are calling Incerement function counter value is being incremented.

So you see we have a value 0 of couner
So you see we have a value 0 of couner
post image

After calling a count function one more time we can see the counter value has been incremented by 1, this means that we’ve done everything successfully and out smart contract is fully functional!

That’s all!You’ve now should feel pretty comfortable wth Sway Playground which will be pretty handy if you’d like to build on Fuel!

Full Steps Guide for contract creation via terminal on Beta-5

Open your Terminal in Ubuntu or Mac (Windows is no officially supported for now but you can try with WSL I believe)

1.1 We need to update and then install git

sudo apt update && sudo apt upgrade -y

Enter your password

sudo apt-get install git-all

Press Y

Now lets install curl

sudo apt-get install curl

1.2 Installing Rust Toolchain

As Sway language is based on Rust therefore we also need to install Rust toolchain

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

Press 1 to process default installation

source "$HOME/.cargo/env"

After that, we need to remove old files

cd $home && rm -rf .fuel && rm -rf .forc && rm -rf .fuelup && rm -rf fuel-project

And then,install Fuel Toolchain

curl --proto '=https' --tlsv1.2 -sSf https://install.fuel.network/fuelup-init.sh | sh

Press Y

After that close and open your terminal again,so the toolchain will be available in your shell

1.3 Let’s update and set beta-5 as a default.

Just paste this commands 1 by 1.

fuelup self update
post image
post image
fuelup default beta-5
Expected output
Expected output

You can check your fuelup version by just typing

fuelup --version

1.4 Creating your Smart-Contract

Create folder

mkdir fuel-project

Then move into it

cd fuel-project

Then we create a contract project using forc:

forc new counter-contract
post image

Then we need to edit out main.sw file (we will make it simple with just nano,you can use VS code if you like)

nano counter-contract/src/main.sw

So now delete everything that is inside the file

And replace with this:

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);
    }
}

Every Sway file must start with a declaration of what type of program the file contains; here, we've declared that this file is a contract.In our case as you can see this is contract;

post image

Once you’ve done it:

Press Ctrl + X

Then Y

Then Enter

1.5 Build your contract

Move to the folder of your created project

cd counter-contract

Now it’s time to build your contract

post image

I’ll just leave you a link to the official guide - where you can check out how to test your contract and how to check out thee view of your project

1.6 Setup or Import your wallet

  • If you already have a fuel-wallet(like in my case - btw you can set up it here):

Just write that command

forc-wallet import
post image

You’ll be asked to write 12 words - your seed and create a password to protect your wallet.

Then write

forc wallet account new

And one more time you’ll be asked to write a password that you’ve created.

  • If you don’t have an account:

You can create a new wallet with the command below:

forc wallet new

Create a password to protect your wallet and make sure to save your seed.

Next, create a new wallet account with:

forc wallet account new

By that command you can check list of your wallets:

forc wallet accounts

As you can see I only have 1 account with that address

1.7 Faucet some funds

To deploy your contract on the network - you need some Test Eth.You can faucet it here.

1.8 Deploy Contract

And now it’s finally time to deploy your contract on the Fuel Network

forc deploy --testnet
post image

You’ll be asked to enter your password again

Once you’ve entered a password,you’ll see your accounts(in my case there is only 1 account - its number is 0 )

Select number of your account and then type y

post image

And congratulations!!!

You can see your Contract ID - which you need to save here as we will need it in the next steps!But I want to congratulate you - you’ve deployed your first contract on Beta-5.

1.9 Check our contract in the block explorer

You can see your contract in the block explorer here.

post image
post image
post image

Anyway don’t forget to tweet that you’ve built a smart-contract on Fuel,tagging @fuel_network

post image

That is it guys! Thanks everyone for reading and I really hope that you’ve made it!

Join the Fuel community