Error: The transaction reverted with an unknown reason: 0

Hi @p.s @Nazeeh21 Facing this blocker while calling the function on testnet

contract;

use std::hash::Hash;

pub struct Point {
    question_id :u64,
    yesnftcount: u64,
    nonftcount: u64,
    question: u64,
    starttime : u64,
    endtime : u64
}

storage {
    counter: u64 = 0,
    question_info: StorageMap<b256, Point> = StorageMap::<b256, Point>{},
    used_id: StorageMap<b256, bool> = StorageMap {},
}

abi MyContract {
    #[storage(read, write)]
    fn add_question(question_id: b256, question_info: Point);

    #[storage(read)]
    fn get_question(question_id: b256) -> Point;

    #[storage(read)]
    fn used_id(question_id: b256) -> bool;

    #[storage(read)]
    fn question_counter() -> u64;

    #[storage(read, write)]
    fn set_question_time(question_id: b256, start_time: u64, end_time: u64);

    #[storage(read, write)]
    fn set_question_name(question_id: b256, question: u64);


    #[storage(read, write)]
    fn set_question_count(question_id: b256, yes_count: u64, no_count: u64);

}

impl MyContract for Contract {

    #[storage(read)]
    fn question_counter() -> u64 {
        storage.counter.read()
    }

    #[storage(read)]
    fn get_question(question_id: b256) -> Point {
        storage.question_info.get(question_id).read()
    }

    #[storage(read)]
    fn used_id(question_id: b256) -> bool {
        storage.used_id.get(question_id).read()
    }

    #[storage(read, write)]
    fn add_question(question_id: b256, question_info : Point){
        require(storage.used_id.get(question_id).read() != true, "Cryptosage: Used question ID");
        let incremented : u64 = storage.counter.read() + 1;
        storage.counter.write(incremented);
        storage.question_info.insert(question_id, question_info);
        storage.question_info.get(question_id).write(question_info);
        storage.used_id.get(question_id).write(true);
    }

    #[storage(read, write)]
    fn set_question_time(question_id: b256, start_time: u64, end_time: u64) {
        require(storage.used_id.get(question_id).read() != false, "Cryptosage: Non existing question");
        let mut question_info = storage.question_info.get(question_id).read();
        question_info.starttime = start_time;
        question_info.endtime = end_time;
    }

    #[storage(read, write)]
    fn set_question_name(question_id: b256, question: u64) {
        require(storage.used_id.get(question_id).read() != false, "Cryptosage: Non existing question");
        let mut question_info = storage.question_info.get(question_id).read();
        question_info.question = question;
    }

    #[storage(read, write)]
    fn set_question_count(question_id: b256, yes_count: u64, no_count: u64) {
        require(storage.used_id.get(question_id).read() != false, "Cryptosage: Non existing question");
        let mut question_info = storage.question_info.get(question_id).read();
        question_info.yesnftcount = yes_count;
        question_info.nonftcount = no_count;
    }
}

Can you help me to fix this issue ?

Thanks in advance :slight_smile:

@rishabhkeshan will be helping you out here

1 Like

Hey @mahesh_d_luffy could you give me more details on the function you were trying to call?
Also is this working well on your local environment?
Could you also link me to the deployed contract.

Hi @rishabhkeshan ,

I am trying to call add_question in which there just storing the details in struct.

yes its working locally fine ,

deployed contract address : 0x63be5b13548aa6de0e6e0081c0e4a4341b53ab2a204c96d6149fcf4a1b466d01

Understood let me check this and get back to you at the earliest.

okay thanks in advance for your support.

Hey @mahesh_d_luffy , we are working on the issue. We hope to have a solution for this soon. Appreciate your patience.

Hey @mahesh_d_luffy, here are the errors in the above code and a solution for the same:

The write method is not available directly on the StorageMap . You should use insert to update the value. The StorageMap methods like get and insert need to be used correctly.

Here is the updated code, let me know if this works for you:

contract;

use std::hash::Hash;

pub struct Point {
    question_id: u64,
    yesnftcount: u64,
    nonftcount: u64,
    question: u64,
    starttime: u64,
    endtime: u64,
}

storage {
    counter: u64 = 0,
    question_info: StorageMap<b256, Point> = StorageMap {},
    used_id: StorageMap<b256, bool> = StorageMap {},
}

abi MyContract {
    #[storage(read, write)]
    fn add_question(question_id: b256, question_info: Point);

    #[storage(read)]
    fn get_question(question_id: b256) -> Option<Point>;

    #[storage(read)]
    fn used_id(question_id: b256) -> bool;

    #[storage(read)]
    fn question_counter() -> u64;

    #[storage(read, write)]
    fn set_question_time(question_id: b256, start_time: u64, end_time: u64);

    #[storage(read, write)]
    fn set_question_name(question_id: b256, question: u64);

    #[storage(read, write)]
    fn set_question_count(question_id: b256, yes_count: u64, no_count: u64);
}

impl MyContract for Contract {
    #[storage(read)]
    fn question_counter() -> u64 {
        storage.counter.read()
    }

    #[storage(read)]
    fn get_question(question_id: b256) -> Option<Point> {
        storage.question_info.get(question_id).try_read()
    }

    #[storage(read)]
    fn used_id(question_id: b256) -> bool {
        storage.used_id.get(question_id).try_read().unwrap_or(false)
    }

    #[storage(read, write)]
    fn add_question(question_id: b256, question_info: Point) {
        require(
            storage.used_id.get(question_id).try_read().unwrap_or(false) != true,
            "Cryptosage: Used question ID",
        );
        let incremented: u64 = storage.counter.read() + 1;
        storage.counter.write(incremented);
        storage.question_info.insert(question_id, question_info);
        storage.used_id.insert(question_id, true);
    }

    #[storage(read, write)]
    fn set_question_time(question_id: b256, start_time: u64, end_time: u64) {
        require(
            storage.used_id.get(question_id).try_read().unwrap_or(false) != false,
            "Cryptosage: Non existing question",
        );
        if let Some(question_info) = storage.question_info.get(question_id).try_read() {
            let mut question_info = question_info;
            question_info.starttime = start_time;
            question_info.endtime = end_time;
            storage.question_info.insert(question_id, question_info);
        }
    }

    #[storage(read, write)]
    fn set_question_name(question_id: b256, question: u64) {
        require(
            storage.used_id.get(question_id).try_read().unwrap_or(false) != false,
            "Cryptosage: Non existing question",
        );
        if let Some(question_info) = storage.question_info.get(question_id).try_read() {
            let mut question_info = question_info;
            question_info.question = question;
            storage.question_info.insert(question_id, question_info);
        }
    }

    #[storage(read, write)]
    fn set_question_count(question_id: b256, yes_count: u64, no_count: u64) {
        require(
            storage.used_id.get(question_id).try_read().unwrap_or(false) != false,
            "Cryptosage: Non existing question",
        );
        if let Some(question_info) = storage.question_info.get(question_id).try_read() {
            let mut question_info = question_info;
            question_info.yesnftcount = yes_count;
            question_info.nonftcount = no_count;
            storage.question_info.insert(question_id, question_info);
        }
    }
}

@rishabhkeshan Thanks let me try out this and will get back to you

Hi @rishabhkeshan

contract address : 0x90c1fdda1a985fc5639bf633a0cbec96530018570a9a5c4be5ac2df1023f20cd

function invoked : add_question()

parameters :
question id : 0xa84c1ae796254d21b7b51cb74862a7fabe20f609fdd344c6e0718f7f31e51004
“yesnftcount”: 1,
“nonftcount”: 1,
“question”: 1,
“starttime”: 1,
“endtime”: 1

  1. List item

I tested above code that you have sent , getting undefined while function calling. Facing the ‘undefined’ error please check it and revert back.

Hey Mahesh, can you try this out:

contract;

use std::identity::Identity;
use std::address::Address;
use std::hash::Hash;
use std::hash::sha256;

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

impl StorageMapDemo for Contract {
    #[storage(read, write)]
    fn demo() {
        let map_01 = StorageKey::<StorageMap<Identity, bool>>::new(b256::zero(), 0, b256::zero());
        let map_02 = StorageKey::<StorageMap<Identity, u64>>::new(b256::zero(), 0, sha256(b256::zero()));

        let identity = Identity::Address(Address::zero());

        map_01.insert(identity, true);
        map_02.insert(identity, 112233);

        let bool_val = map_01.get(identity).read();
        assert_eq(bool_val, true);

        let u64_val = map_02.get(identity).read();
        assert_eq(u64_val, 112233);
    }
}

#[test]
fn test() {
    let caller = abi(StorageMapDemo, CONTRACT_ID);
    caller.demo();
}