Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impossible to insert into a StorageMap within a struct when using a helper function #6050

Open
bitzoic opened this issue May 23, 2024 · 0 comments
Labels
bug Something isn't working compiler General compiler. Should eventually become more specific as the issue is triaged

Comments

@bitzoic
Copy link
Member

bitzoic commented May 23, 2024

It is currently impossible to insert into a StorageMap that is within a struct if using a helper function.

Within the example contract, you may call storage.my_struct.map_map.insert(0, 0), however if passing the StorageKey<MyStruct>, the StorageKey<StorageMap<u64, u64>> becomes inaccessible.

Example Contract:

contract;

use std::hash::Hash;

pub struct MyStruct {
    my_map: StorageMap<u64, u64>,
}

storage {
    my_struct: MyStruct = MyStruct {
        my_map: StorageMap {},
    },
}

abi MyContract {
    #[storage(read, write)]
    fn write();
}

impl MyContract for Contract {
    #[storage(read, write)]
    fn write() {
        // Possible to insert into map
        storage.my_struct.my_map.insert(1, 1);

        // No longer possible to insert into map
        write(storage.my_struct);
    }
}

// Inserts into a StorageMap
#[storage(read, write)]
pub fn write(struct_key: StorageKey<MyStruct>) {
    let my_struct = struct_key.read();

    // This is now a `StorageMap` and not a `StorageKey<StorageMap>`
    my_struct.my_map.insert(3, 3);
}

@bitzoic bitzoic added bug Something isn't working compiler General compiler. Should eventually become more specific as the issue is triaged labels May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler General compiler. Should eventually become more specific as the issue is triaged
Projects
None yet
Development

No branches or pull requests

1 participant