Is is possible to access public structure fileds in NEAR smart-contract without a function?

117 Views Asked by At

Let's say I have the following smart contract (in Rust):

#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
pub struct Number {
    pub val: u128,
    pub decimals: u8
}

Can I access decimals or val without defining a pub fn for it?

1

There are 1 best solutions below

0
On BEST ANSWER

TL;DR: No, you cannot access specific public fields of a state structure without a getter function.

Long answer: the state structure is stored in Borsh-serialized format when you use near-sdk-rs, but it is just a high-level wrapper defined on near-sdk-rs helper; NEAR protocol does not put any constraints on the stored values (the name, the format, or anything at all), and it only provides key-value storage for the smart contract runtimes. If you want to dig deeper, you may view the raw state of your contract via RPC, and observe that there is a key "STATE" created for you by near-sdk-rs, and you may just deserialize it with Borsh on your side.