How to set default storage values during deployment?

Hello, I have a question
Is it possible to set some default storage values during deployment?
like a constructor in solidity

2 Likes

Initial storage values can be specified in the storage block itself as in:

storage {
    x: u64 = 42,
    s: MyStruct = MyStruct {
        a: 6,
        b: 8,
    }
}

When deploying the contract via forc deploy, then initial values are set as raw initial storage values with whatever keys the compiler chose for the variables above.
The raw initial slots are also emitted to a JSON file: out/debug/<project_name>-storage_slots.json which can be used by the SDK.

3 Likes

For example, I want to declare the name of the token when I deploy the dapp to avoid changing the token contract every time before deployment. How I can do it?

2 Likes

configuration time constants may be what you are looking for

3 Likes

Yeah as @SwayStar123 said, you need configuration-time constants here. See an example here, and the documentation in Variables - The Sway Programming Language

4 Likes

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