What does payable decorator in near-sdk-rs do?

Viewed 308

The docs says:

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 the 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.

https://github.com/near/near-sdk-rs

Does the token here mean only native near tokens?

Whey this function have to be payable? To pay near tokens for storage?

/// Transfer `amount` of tokens from the caller of the contract (`predecessor_id`) to
/// `new_owner_id`.
/// Act the same was as `transfer_from` with `owner_id` equal to the caller of the contract
/// (`predecessor_id`).
/// Requirements:
/// * Caller of the method has to attach deposit enough to cover storage difference at the
///   fixed storage price defined in the contract.
#[payable]
pub fn transfer(&mut self, new_owner_id: AccountId, amount: U128) {
    // NOTE: New owner's Account ID checked in transfer_from.
    // Storage fees are also refunded in transfer_from.
    self.transfer_from(env::predecessor_account_id(), new_owner_id, amount);
}

https://github.com/near-examples/FT/blob/master/contracts/rust/src/lib.rs#L211

1 Answers

Does the token here mean only native near tokens?

Yes

Whey this function have to be payable? To pay near tokens for storage?

If you want this function to be able to accept deposit. How the deposit is going to be used is up to the contract to decide.

Related