Attribute Macro near_sdk::near_bindgen

source ·
#[near_bindgen]
Expand description

This attribute macro is used on a struct and its implementations to generate the necessary code to expose pub methods from the contract as well as generating the glue code to be a valid NEAR contract.

This macro will generate code to load and deserialize state if the self parameter is included as well as saving it back to state if &mut self is used.

For parameter serialization, this macro will generate a struct with all of the parameters as fields and derive deserialization for it. By default this will be JSON deserialized with serde but can be overwritten by using #[serializer(borsh)].

#[near_bindgen] will also handle serializing and setting the return value of the function execution based on what type is returned by the function. By default, this will be done through serde serialized as JSON, but this can be overwritten using #[result_serializer(borsh)].

§Examples

use near_sdk::near_bindgen;

#[near_bindgen]
pub struct Contract {
   data: i8,
}

#[near_bindgen]
impl Contract {
    pub fn some_function(&self) {}
}

Events Standard:

By passing event_json as an argument near_bindgen will generate the relevant code to format events according to NEP-297

For parameter serialization, this macro will generate a wrapper struct to include the NEP-297 standard fields standard and version as well as include serialization reformatting to include the eventanddatafields automatically. Thestandardandversionvalues must be included in the enum and variant declaration (see example below). By default this will be JSON deserialized withserde`

§Examples

use near_sdk::near_bindgen;

#[near_bindgen(event_json(standard = "nepXXX"))]
pub enum MyEvents {
   #[event_version("1.0.0")]
   Swap { token_in: AccountId, token_out: AccountId, amount_in: u128, amount_out: u128 },

   #[event_version("2.0.0")]
   StringEvent(String),

   #[event_version("3.0.0")]
   EmptyEvent
}

#[near_bindgen]
impl Contract {
    pub fn some_function(&self) {
        MyEvents::StringEvent(
            String::from("some_string")
        ).emit();
    }

}

Contract Source Metadata Standard:

By using contract_metadata as an argument near_bindgen will populate the contract metadata according to NEP-330 standard. This still applies even when #[near_bindgen] is used without any arguments.

All fields(version, link, standard) are optional and will be populated with defaults from the Cargo.toml file if not specified.

The contract_source_metadata() view function will be added and can be used to retrieve the source metadata. Also, the source metadata will be stored as a constant, CONTRACT_SOURCE_METADATA, in the contract code.

§Examples

use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::near_bindgen;

#[derive(Default, BorshSerialize, BorshDeserialize)]
#[near_bindgen(contract_metadata(
    version = "39f2d2646f2f60e18ab53337501370dc02a5661c",
    link = "https://github.com/near-examples/nft-tutorial",
    standard(standard = "nep330", version = "1.1.0"),
    standard(standard = "nep171", version = "1.0.0"),
    standard(standard = "nep177", version = "2.0.0"),
))]
struct Contract {}