Compilation error following the sample counter contract using forc build?

Lines of codes:

 contract;

storage {
    //declare a storage variable
    counter: u64 = 0,
}

abi Counter {
    //reads and write to storage
    #[storage(read, write)]
    fn increment();

    //reads from storage
    #[storage(read)]
    fn counter() -> u64;
}

impl Counter for Contract {
    #[storage(read)]
    fn counter() -> u64 {
        //using implicit return
        return storage.counter;
        
    }

    #[storage(read, write)]
    fn increment() {
        storage.counter = storage.counter + 1;
    }
}

My toolchain using “$fuelup show”:

Default host: x86_64-unknown-linux-gnu
fuelup home: /home/sheys/.fuelup

installed toolchains

beta-3-x86_64-unknown-linux-gnu
beta-4-x86_64-unknown-linux-gnu (default)
latest-x86_64-unknown-linux-gnu

active toolchain

beta-4-x86_64-unknown-linux-gnu (default)
forc : 0.46.1
- forc-client
- forc-deploy : 0.46.1
- forc-run : 0.46.1
- forc-crypto : not found
- forc-doc : 0.46.1
- forc-explore : 0.28.1
- forc-fmt : 0.46.1
- forc-lsp : 0.46.1
- forc-tx : 0.46.1
- forc-wallet : 0.3.0
fuel-core : 0.20.5
fuel-core-keygen : Error getting version string

fuels versions

forc : 0.45
forc-wallet : 0.45

Now, I could use beta-3 but I also have trouble deploying to beta-3

Hello @Sheys,

I hope you’re doing well. I wanted to address a couple of points:

  1. I’ve attempted to reproduce the issue you mentioned but haven’t been successful so far. To gain a better understanding, could you confirm if the error occurs when you run forc build in the root directory of your Sway project? Additionally, it would be really helpful if you could share the specific error message you’re encountering. This will enable me to diagnose the problem more accurately.

  2. Regarding the beta-3 network, it has been officially deprecated. For more details, you can refer to this link: Sunsetting Beta-3 Testnet. To deploy on the beta-4 network, you can use the following command: forc deploy --testnet.

Let me know if you need more help! :slight_smile:

Hello @calldelegation, thank you.

  1. Here’s the compilation error it shows using forc build in the root directory of my Sway project:
sheys@DESKTOP-MRD72SV:~/fuelvm-projects/counter$ forc build
 Compiling library core (/home/sheys/.forc/git/checkouts/std-9be0d6062747ea7/512a3386f8961185188302f391ccc96553d23a7a/sway-lib-core)
 Compiling library std (git+https://github.com/fuellabs/sway?tag=v0.46.1#512a3386f8961185188302f391ccc96553d23a7a)
 Compiling contract counter (/home/sheys/fuelvm-projects/counter)
warning
 --> /home/sheys/fuelvm-projects/counter/src/main.sw:5:5
  |
3 |
4 |     //declare a storage variable
5 |     counter: u64 = 0,
  |     ------- This storage declaration is never accessed and can be removed.
6 | }
  |
____

error
  --> /home/sheys/fuelvm-projects/counter/src/main.sw:22:16
   |
20 |
21 |         //using implicit return
22 |         return storage.counter;
   |                ^^^^^^^^^^^^^^^ Mismatched types.
expected: u64
found:    StorageKey<u64>.
help: Return statement must return the declared function return type.
23 |
24 |     }
   |
____

error
  --> /home/sheys/fuelvm-projects/counter/src/main.sw:28:9
   |
26 |
27 |     fn increment() {
28 |         storage.counter = storage.counter + 1;
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This expression is not valid on the left hand side of a reassignment.
29 |     }
30 | }
   |
____

error
  --> /home/sheys/fuelvm-projects/counter/src/main.sw:20:5
   |
18 |
19 |       #[storage(read)]
20 |       fn counter() -> u64 {
   |  _____^
21 | |         //using implicit return
22 | |         return storage.counter;
23 | |
24 | |     }
   | |_____^ This path must return a value of type "u64" from function "counter", but it does not.
25 |
26 |       #[storage(read, write)]
   |
____

  Aborting due to 3 errors.
  Error: Failed to compile counter

I recently read that there’s a storage issue with beta-4 testnet here, I don’t think that has been resolved yet… But how can I move forward?

  1. Regarding point 2, thanks for the information :+1: