Methods can be called externally by using the
pub identifier within the
#[near_bindgen] macro which will expose the method in the compiled WASM bytecode.
It is important to only mark methods that should be called externally as public. If you need a contract to call itself, you can mark the function as public but add the
#[private] annotation so that it will panic if called from anything but the contract itself.
A basic usage of this would look like the following:
Where this would expose
some_method from the WASM binary and allow it to be called externally.
Expand to see generated code
Functions can also be exposed through trait implementations. This can be useful if implementing a shared interface or standard for a contract. This code generation is handled very similarly to basic
pub functions, but the
#[near_bindgen] macro only needs to be attached to the trait implementation, not the trait itself:
In this example, the generated code will be the same as the previous example, except with a different method name.