Payable Methods
We can allow methods to accept token transfer together with the function call. This is done so that contracts can define a fee in tokens that needs to be payed when they are used. By default the methods are not payable and they will panic if someone will attempt to transfer tokens to them during the invocation. This is done for safety reason, in case someone accidentally transfers tokens during the function call.
To declare a method as payable, use the #[payable]
annotation within the near_bindgen
macro as follows:
This will allow the my_method
function to be called and transfer balance to the contract.
Example:
is equivalent to: