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

the method may_load exists for struct cw_storage_plus::Map<'static, (std::string::String, Uint256), Uint256>, but its trait bounds were not satisfied the following trait bounds were not satisfied: (std::string::String, Uint256): PrimaryKey #663

Closed
Flydexo opened this issue Feb 18, 2022 · 11 comments

Comments

@Flydexo
Copy link

Flydexo commented Feb 18, 2022

Hello I have this error when I try to call may_load on a Map.
my code
let c = CARDS.may_load(deps.storage, (from, token_id))?; Ok(match c { Some(v) => { if v < value { return Err(ContractError::NoOwnCard) } }, None => return Err(ContractError::Unkown) });

@Gdowski
Copy link

Gdowski commented Feb 18, 2022

Because as the error suggests, there's no implementation of a certain trait.
In this case, part of your tuple doesn't have PrimaryKey trait implemented.
Looking into documentation,
https://github.com/CosmWasm/cw-plus/blob/main/packages/storage-plus/src/keys.rs
you can see that there is an implementation for String, but not for Uint256,
as close to your desire as possible is u64.
So, either changing to u64 or implementing PrimaryKey trait for your type, is what you need :)

@maurolacy
Copy link
Contributor

maurolacy commented Feb 19, 2022

you can see that there is an implementation for String, but not for Uint256,
as close to your desire as possible is u64.
So, either changing to u64 or implementing PrimaryKey trait for your type, is what you need :)

That's correct. We purposely refrained for implementing PrimaryKey over u128 (and Uint256), as that would double (quadruple) the required memory layout to handle int keys (check the Key enum, in case you are interested).

You should be good to go with u64... why would you need a unique key that large? Are you planning to index the atoms in the known universe? You would still need a handful more bits for that by the way. Not to mention space requirements.

@Flydexo
Copy link
Author

Flydexo commented Feb 19, 2022

No I was using u256 because I was using solidity before and it was recommended to use u256. That's why. But yes u64 is enough.

@Flydexo Flydexo closed this as completed Feb 19, 2022
@Flydexo
Copy link
Author

Flydexo commented Feb 19, 2022

With u64 I have the same error

@maurolacy
Copy link
Contributor

Feel free to re-open and post a snippet of the failing code, and we'll take a look.

@Flydexo
Copy link
Author

Flydexo commented Feb 19, 2022

pub const CARDS: Map<(String, u64), u64> = Map::new("cards");
let c = CARDS.may_load(deps.storage,(from, token_id))?;

@Flydexo Flydexo reopened this Feb 19, 2022
@maurolacy
Copy link
Contributor

Works for me. What version of cw-plus are you using?

@Flydexo
Copy link
Author

Flydexo commented Feb 19, 2022

cw-storage-plus = "0.8.0"

@maurolacy
Copy link
Contributor

maurolacy commented Feb 19, 2022

cw-plus is now in version 0.12.1. I think you should try at least with version 0.10.x or so for this new format to work (check CHANGELOG.md, for details about IntKey deprecation).

For 0.8, using IntKey<u64> / U64Key for the integer field should work, and there should be some examples in the code base. But I seriously recommend you to update to at least v0.10.x. Or better, to the latest version.

@Flydexo
Copy link
Author

Flydexo commented Feb 19, 2022

Sorry guys but now when I try to update I recieve this error

expected a FnOnce<(std::option::Option<u64>,)> closure, found u64
the trait FnOnce<(std::option::Option<u64>,)> is not implemented for u64

With this code
let new_from_balance = CARDS.update(deps.storage, (from, token_id), q)?;

@maurolacy
Copy link
Contributor

maurolacy commented Feb 19, 2022

update requires a closure. You should learn to use the storage / storage-plus API.

Closing this, as this is not relevant to the original issue, nor an error or limitation in the API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants