Why need to 'initialize' contract in AMM github example?

I initially copied this code from the sway examples github:

fn initialize(exchange_bytecode_root: ContractId) {
        require(storage.exchange_bytecode_root.read().is_none(), InitError::BytecodeRootAlreadySet);
        storage.exchange_bytecode_root.write(Option::Some(exchange_bytecode_root.into()));
    }

However i get the error:

“” ^^^^ No method named “read” found for type “Option”.“”"

Could someone explain me this error and maybe even more important. Explain the need to initialise the contract here? I dont see it in other examples?

What version of forc are you using? The error you mentioned seems to be the result of using an older version of Fuel & Sway.

As for why there’s an initializer: Sway doesn’t have constructors like Solidity. If you need to initialize storage slots, there must be an initialize function.

I am using forc 0.37.3 and fuel-core 0.17.11

So basically you are trying to run newer code with an older compiler

Please run

 fuelup toolchain install latest

and then

fuelup default latest

Let me know if it works :wink:

Thanks @sandusky and @david That was it.

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.