Closed SachinNandal closed 3 years ago
There is a lot to unpack here.
Firstly, it seems that contract has no public methods, that is it should be:
#[near_bindgen]
struct BuyOrderDataBTreeMap {
...
}
#[near_bindgen]
impl BuyOrderDataBTreeMap {
// This methods are public with `pub`
The external methods encode the data returned as JSON by default; you can also choose to encode them as borsh (see here), but it is harder to decode as you are trying to do here.
Secondly, if your goal is to have some public methods that update the state and then pull the state via RPC to then decode, or as suggested above return the value encoded as borsh, you will need a schema in JS that matches the schema of the data in the contract. This isn't easy as you must hand write the schema see here.
The other aspect of your code example is that it seems you are trying to store the entire BTree as one entry in storage. This will mean having to read all of it back each time and write it back each time, which as the tree grows will get more and more expensive. So instead I suggest using the provided TreeMap
, which stores each node of the tree separately in storage meaning that each time you make a write it's O(log(n))
of the size and reads are O(1)
.
Thank You So Much for reply @willemneal
Above code that I added here is a part of my solana program. As solana programs are stateless therefore I am using borsh to serialize and deserialize my data so that I can store it on solana blockchain. And whenever I make a call using web3js to my program/smart contract to add/remove anything from my BTreeMap stored on solana blockchain, I have to fetch full BTreeMap. If I use TreeMap instead of BTreeMap, is it possible to add/remove data from TreeMap without loading entire TreeMap data into my solana program?
And after using this TreeMap in rust, is there any typescript schema of TreeMap to decode my uint8array data(which I read from blockchain) to TreeMap<key, BuyOrderDataTreeMap>?
Sorry I'm really not familiar with Solana. There is no schema of TreeMap as its general use case is for view contract calls to access only parts that are needed. TreeMap
is designed for Near anyway as it reads and writes from the storage API.
I'm still not sure what your Solana's external contract calls are. Your best bet is to see the example link and make your own schema or use another binary encoding that is more common in the Solana ecosystem.
As it's very difficult and time consuming to write Typescript schema of rust BTreeMap. I decided to build my rest api to read blockchain data using Rust instead of Typescript.
Thanks for your time @willemneal
Hello, I'm serializing a BTreeMap using BorshSerialize in rust. How to deserialize this uint8array serialized data to vector/array or BTreeMap in Typescript using borsh-js or any other library? Please help
Below is my rust code