Method Types

Payable Methods

  • Methods can be annotated with #[payable] to allow tokens to be transferred with the method invocation.

#[payable]
pub fn my_method(&mut self) {
...
}

Private Methods

  • Some methods need to be exposed to allow the contract to call a method on itself through a promise, but want to disallow any other contract to call it. For this, use the #[private] annotation to panic when this method is called externally.

#[private]
pub fn my_method(&mut self) {
...
}

Difference between pub fn and fn

pub fn will be exported from the WASM so they can be called as part of a transaction. Otherwise it's just a helper function and not an entry point

Difference between private functions and fn

#[private] is a special marker that's part of the #[near_bindgen] macro. You attach it to public functions, and it adds a restriction such that only the contract itself is allowed to call that function as part of a transaction, even though the function is public. #[private] technically creates a public entry point which only the contract itself is allowed to call. Useful for callbacks.

Last updated