Sway has no constructors. Instead, they let you initialize storage slots at deploy time. But the tooling for this is super immature. The way storage works is a bit opaque -- the storage keys of variables don't just start from 0 and work up, each storage variable is found at a slot something like sha("storage-0"). And the way certain types are encoded in storage is also a bit unclear.
// Optional: Configure storage
let key = Bytes32::from([1u8; 32]);
let value = Bytes32::from([2u8; 32]);
let storage_slot = StorageSlot::new(key, value);
let storage_configuration =
StorageConfiguration::default().set_manual_storage(vec![storage_slot]);
But figuring out the right storage keys to use and the correct encoding is pretty unreasonable for us to do ourselves. For example, for this storage block:
It'd be great to not need to put our deployer address as a const within the actual contract and to set the storage slots ourselves, but atm this isn't really reasonable
The desired output here would be to be able to set the owner key for all relevant contracts at deploy time. This could look something like:
Finding or building primitive tooling to help us generate storage slots
Just accepting that the easiest path here is to hardcode our address
Considering finding and replacing the hardcoded owner in these storage slots before deploying
Considering an initialize-style function (knowing that this could probably be frontran...)
Sway has no constructors. Instead, they let you initialize storage slots at deploy time. But the tooling for this is super immature. The way storage works is a bit opaque -- the storage keys of variables don't just start from 0 and work up, each storage variable is found at a slot something like
sha("storage-0")
. And the way certain types are encoded in storage is also a bit unclear.Unfortunately, it doesn't seem like there's nice tooling yet to let you override the storage. The best we can get is by manually specifying the raw storage keys and values like this (from https://rust.fuel.network/v0.38.1/getting-started/contracts.html?highlight=storage#the-deploy-functions):
But figuring out the right storage keys to use and the correct encoding is pretty unreasonable for us to do ourselves. For example, for this storage block:
This is the initial storage that's generated:
It'd be great to not need to put our deployer address as a const within the actual contract and to set the storage slots ourselves, but atm this isn't really reasonable
The desired output here would be to be able to set the owner key for all relevant contracts at deploy time. This could look something like:
initialize
-style function (knowing that this could probably be frontran...)helpful context: Configurable constants exist but they cannot be used for initial storage https://fuellabs.github.io/sway/master/book/basics/variables.html?highlight=configuration#configurable-constants