For sometime folks have asked for ways to improve the type safety of working with contract storage, with the main complaint being that one can mix up the key type, storage type, and the value type, writing with one type at one location and reading at another.
This issue is a starting point to start collecting storage patterns and solutions that folks are using. Post other solutions and patterns as comments. I will keep the issue description updated with a summary of all.
This issue may not result in a change to the soroban-sdk, but it may if an approach proves to meaningfully improve contract safety and the developer experience. Anything the sdk adopts should be composable and general enough to lean into the 'library' lane rather than lean into the 'framework' lane. That distinction will not always be clear.
Solutions seen in the wild:
For sometime folks have asked for ways to improve the type safety of working with contract storage, with the main complaint being that one can mix up the key type, storage type, and the value type, writing with one type at one location and reading at another.
This issue is a starting point to start collecting storage patterns and solutions that folks are using. Post other solutions and patterns as comments. I will keep the issue description updated with a summary of all.
This issue may not result in a change to the soroban-sdk, but it may if an approach proves to meaningfully improve contract safety and the developer experience. Anything the sdk adopts should be composable and general enough to lean into the 'library' lane rather than lean into the 'framework' lane. That distinction will not always be clear.
Solutions seen in the wild:
soroban-example's
enum DataKeypattern that defines an enum for data keys, so that key reuse is more likely example cc @sisuresh@axelarnetwork's
#[contractstorage]macro that defines a list of storage keys, and associates storage types and value types with the key docs.rs/stellar-axelar-std-derive cc @nbayindirli@BlaineHeffron's
#[contractstorage]on a struct that translates struct fields representing stored values. BlaineHeffron/soroban-sdk-tools cc @willemneal