Hi @danielbate @Nazeeh21 , I am currently facing an issue with struct storage
Contract :
contract;
pub struct Point {
question_id :u64,
yesnftCount: u64,
nonftCount: u64,
question: str,
starttime : u64,
endtime : u64
}
storage {
counter: u64 = 0,
question_info: StorageMap<(u64,Point)> = StorageMap{},
}
abi MyContract {
#[storage(read, write)]
fn add_question(question_id: b256, question_info: Point);
#[storage(read)]
fn get_question(question_id: str) -> Option<Point>;
}
impl MyContract for Contract {
#[storage(read, write)]
fn add_question(question_id : b256 , question_info : Point){
let incremented : u64 = storage.counter.read() + 1;
storage.counter.write(incremented);
// storage.question_info.write(question_id, _question_info);
}
#[storage(read)]
fn get_question(question_id: str) -> Option<Point> {
storage.question_info.get(question_id)
}
}
Blocker That Iām Facing
I am trying to store Struct details in the struct , how can i store it ?
Fuelup show :
1 Like
p.s
August 1, 2024, 9:30am
2
Hey @mahesh_d_luffy
You can indeed store a Struct in a storage map.
Your declaration of the StorageMap looks a little off though, as it requires a key/value pair like StorageMap<Key, Value>
(see the docs for more information).
The following builds successfully for me on v0.62.0
of forc.
contract;
use std::hash::Hash;
pub struct Point {
question_id :u64,
yesnftCount: u64,
nonftCount: u64,
// question: str,
starttime : u64,
endtime : u64
}
storage {
counter: u64 = 0,
question_info: StorageMap<b256, Point> = StorageMap::<b256, Point>{},
}
abi MyContract {
#[storage(read, write)]
fn add_question(question_id: b256, question_info: Point);
#[storage(read)]
fn get_question(question_id: b256) -> Point;
}
impl MyContract for Contract {
#[storage(read, write)]
fn add_question(question_id: b256, question_info : Point){
let incremented : u64 = storage.counter.read() + 1;
storage.counter.write(incremented);
storage.question_info.insert(question_id, question_info);
}
#[storage(read)]
fn get_question(question_id: b256) -> Point {
storage.question_info.get(question_id).read()
}
}
1 Like
Hi @p.s , I understood , how can we add String type in the struct , I want to store question on it ,
is there any other alternate way to store a question (string) type in it.
Thanks in advance .
p.s
August 1, 2024, 10:11am
4
Hey
I believe you could use a StorageString
for this - read more in our docs .
Hope this helps
1 Like