How to initialize an UnorderedSet that is inside a LookupMap?

Viewed 89

I´m writing a smart contract with rust in near protocol. I have the following struct for the main contract:

#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)] 
pub struct Contract {

    pub total_transactions: u64,

    pub owner_id: AccountId,

    pub transactions_per_account: LookupMap<AccountId, UnorderedSet<TransactionId>>,

    pub transaction_by_id: LookupMap<TransactionId, Transaction>,

    pub transaction_metadata_by_id: UnorderedMap<TransactionId, TransactionMetadata>,
}

I have already written a function to initialize the contract as the following:

#[derive(BorshSerialize)]
pub enum StorageKey {
    TransactionsPerAccount,
    TransactionById,
    TransactionMetadataById,
}

#[near_bindgen]
impl Contract {

    #[init]
    pub fn new(owner_id: AccountId) -> Self {
 
        let this = Self {
            total_transactions: 0, 
            owner_id,
            transactions_per_account: LookupMap::new(StorageKey::TransactionsPerAccount.try_to_vec().unwrap()),
            transaction_by_id: LookupMap::new(StorageKey::TransactionById.try_to_vec().unwrap()),
            transaction_metadata_by_id: UnorderedMap::new(
                StorageKey::TransactionMetadataById.try_to_vec().unwrap(),
            ),
        };
        //return the Contract object
        this
    }
}

But there the UnorderedSet that is inside the LookupMap transactions_per_account is not initialize because it is supposed that there will be an UnorderedSet that corresponds to each user, so it should be created when a user log in for first time and creates an account:

I tried to write that function as the following:

#[payable]
    pub fn create_transaction(       //implement storage management for transactions
        &mut self,
        seller_id: AccountId,
        buyer_id: AccountId,
        price: Price,
        nft_id: TokenId,
        nft_contract_id: AccountId,
        ) -> Transaction {

        let sender = env::predecessor_account_id();

        let transaction = Transaction {
            transaction_id: self.total_transactions,
            creator_id: sender.clone(),
            seller_id: seller_id.clone(),
            buyer_id: buyer_id.clone(), 
            price: price,
            nft_id: nft_id.clone(),
            nft_contract_id: nft_contract_id.clone(),
            amount_in_escrow: false,
            token_in_escrow: false,
            transaction_status: TransactionStatus::Pending,
        };


        //let account_as_key = transaction.creator_id.try_to_vec().unwrap();

        // update number of transactions
        self.total_transactions += 1;

        let unordered_set = self.transactions_per_account.get(&transaction.creator_id); 

        match unordered_set {
            Some(mut set) => {
                set.insert(&transaction.transaction_id);
            },
            None => {
                let mut set = UnorderedSet::new(&*transaction.creator_id.try_to_vec().unwrap());
                set.insert(&transaction.transaction_id);
                self.transactions_per_account.insert(&transaction.creator_id, &set);
            },
        }
        
        self.transaction_by_id.insert(&transaction.transaction_id, &transaction);
       
        transaction
    }

Cargo check let it pass with no error but when trying to compile I got the following error:

error[E0277]: the trait bound `UnorderedSet<u64>: Serialize` is not satisfied
    --> src/lib.rs:133:1
     |
133  | #[near_bindgen]
     | ^^^^^^^^^^^^^^^ the trait `Serialize` is not implemented for `UnorderedSet<u64>`
     |
note: required by a bound in `to_vec`
    --> /home/walquer/.cargo/registry/src/github.com-1ecc6299db9ec823/serde_json-1.0.78/src/ser.rs:2193:17
     |
2193 |     T: ?Sized + Serialize,
     |                 ^^^^^^^^^ required by this bound in `to_vec`
     = note: this error originates in the attribute macro `near_bindgen` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.

I believe the error is caused by a erroneous initialization of the UnorderedSet collection that was defined inside the struct that do have the serialize trait.

Can someone tell me what im missing?

0 Answers
Related