How to derive storage keys in rust-sdk?

I need to set initial values for storage at deployment time using the rust sdk.

How can I derive the key for a specific storage slot?

Hey @svimes you can the key for a specific storage slot inside your Sway project under the directory out/debug/my-contract-storage_slots.json after you have built your project.

For example

  1. After writing my contract with some initial storage

storage {
    secret_number: u64 = 777

abi MyContract {
    fn test_function() -> bool;

impl MyContract for Contract {
    fn test_function() -> bool {
  1. Build contract in terminal

forc build

  1. Inside out/debug/my-contract-storage_slots.json you can see something like
    "key": "f383b0ce51358be57daa3b725fe44acdb2d880604e367199080b4379c41bb6ed",
    "value": "0000000000000309000000000000000000000000000000000000000000000000"

Hope this helps! :slight_smile:

Thanks, I was aware of the storage_slots.json.

Unfortunately, I was looking for a way to do it programmatically.

With multiple variables sometimes spanning over multiple slots, doing this manually is messy, it also isn’t the most sustainable.