Skip to content

Commit

Permalink
Merge pull request #1904 from multiversx/payable-attr
Browse files Browse the repository at this point in the history
payable attribute - `#[payable]`
  • Loading branch information
andrei-marinica authored Dec 23, 2024
2 parents ebdd8ee + 1e0d401 commit a56917f
Show file tree
Hide file tree
Showing 16 changed files with 33 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ pub trait Contract:
#[init]
fn init(&self) {}

#[payable("*")]
#[payable]
#[endpoint(sellToken)]
fn sell_token_endpoint(&self) {
self.sell_token::<FunctionSelector<Self::Api>>();
}

#[payable("*")]
#[payable]
#[endpoint(buyToken)]
fn buy_token_endpoint(
&self,
Expand All @@ -42,7 +42,7 @@ pub trait Contract:
}

#[endpoint(deposit)]
#[payable("*")]
#[payable]
fn deposit_endpoint(&self, payment_token: OptionalValue<TokenIdentifier>) {
self.deposit::<FunctionSelector<Self::Api>>(payment_token)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub trait Crowdfunding {
}

#[endpoint]
#[payable("*")]
#[payable]
fn fund(&self) {
let (token, _, payment) = self.call_value().egld_or_single_esdt().into_tuple();

Expand Down
4 changes: 2 additions & 2 deletions contracts/examples/digital-cash/src/pay_fee_and_fund.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{constants::*, helpers, storage};
#[multiversx_sc::module]
pub trait PayFeeAndFund: storage::StorageModule + helpers::HelpersModule {
#[endpoint(payFeeAndFundESDT)]
#[payable("*")]
#[payable]
fn pay_fee_and_fund_esdt(&self, address: ManagedAddress, valability: u64) {
let mut payments = self.call_value().all_esdt_transfers().clone();
let fee = EgldOrEsdtTokenPayment::from(payments.get(0).clone());
Expand All @@ -32,7 +32,7 @@ pub trait PayFeeAndFund: storage::StorageModule + helpers::HelpersModule {
}

#[endpoint]
#[payable("*")]
#[payable]
fn fund(&self, address: ManagedAddress, valability: u64) {
require!(!self.deposit(&address).is_empty(), FEES_NOT_COVERED_ERR_MSG);
let deposit_mapper = self.deposit(&address).get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub trait SignatureOperationsModule: storage::StorageModule + helpers::HelpersMo
}

#[endpoint]
#[payable("*")]
#[payable]
fn forward(
&self,
address: ManagedAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub trait EsdtTransferWithFee {
self.tx().to(ToCaller).payment(fees).transfer();
}

#[payable("*")]
#[payable]
#[endpoint]
fn transfer(&self, address: ManagedAddress) {
require!(
Expand Down
4 changes: 2 additions & 2 deletions contracts/examples/fractional-nfts/src/fractional_nfts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub trait FractionalNfts: default_issue_callbacks::DefaultIssueCallbacksModule {
.async_call_and_exit();
}

#[payable("*")]
#[payable]
#[endpoint(fractionalizeNFT)]
fn fractionalize_nft(
&self,
Expand Down Expand Up @@ -97,7 +97,7 @@ pub trait FractionalNfts: default_issue_callbacks::DefaultIssueCallbacksModule {
.transfer();
}

#[payable("*")]
#[payable]
#[endpoint(unFractionalizeNFT)]
fn unfractionalize_nft(&self) {
let fractional_payment = self.call_value().single_esdt();
Expand Down
2 changes: 1 addition & 1 deletion contracts/examples/lottery-esdt/src/lottery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ pub trait Lottery {
}

#[endpoint]
#[payable("*")]
#[payable]
fn buy_ticket(&self, lottery_name: ManagedBuffer) {
let (token_identifier, payment) = self.call_value().egld_or_single_fungible_esdt();

Expand Down
2 changes: 1 addition & 1 deletion contracts/examples/multisig/src/multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub trait Multisig:
}

/// Allows the contract to receive funds even if it is marked as unpayable in the protocol.
#[payable("*")]
#[payable]
#[endpoint]
fn deposit(&self) {}

Expand Down
2 changes: 1 addition & 1 deletion contracts/examples/nft-minter/src/nft_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub trait NftModule {

// endpoints

#[payable("*")]
#[payable]
#[endpoint(buyNft)]
fn buy_nft(&self, nft_nonce: u64) {
let payment = self.call_value().egld_or_single_esdt();
Expand Down
6 changes: 3 additions & 3 deletions contracts/examples/nft-subscription/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub trait NftSubscription:
.transfer();
}

#[payable("*")]
#[payable]
#[endpoint]
fn update_attributes(&self, attributes: ManagedBuffer) {
let payment = self.call_value().single_esdt();
Expand All @@ -65,7 +65,7 @@ pub trait NftSubscription:
.transfer();
}

#[payable("*")]
#[payable]
#[endpoint]
fn renew(&self, duration: u64) {
let payment = self.call_value().single_esdt();
Expand All @@ -84,7 +84,7 @@ pub trait NftSubscription:
.transfer();
}

#[payable("*")]
#[payable]
#[endpoint]
fn cancel(&self) {
let payment = self.call_value().single_esdt();
Expand Down
4 changes: 2 additions & 2 deletions contracts/examples/order-book/pair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub trait Pair:
self.second_token_id().set_if_empty(&second_token_id);
}

#[payable("*")]
#[payable]
#[endpoint(createBuyOrder)]
fn create_buy_order_endpoint(&self, params: OrderInputParams<Self::Api>) {
self.require_global_op_not_ongoing();
Expand All @@ -34,7 +34,7 @@ pub trait Pair:
self.create_order(payment, params, common::OrderType::Buy);
}

#[payable("*")]
#[payable]
#[endpoint(createSellOrder)]
fn create_sell_order_endpoint(&self, params: OrderInputParams<Self::Api>) {
self.require_global_op_not_ongoing();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub trait RewardsDistribution:
self.brackets().set(brackets);
}

#[payable("*")]
#[payable]
#[endpoint(depositRoyalties)]
fn deposit_royalties(&self) {
let payment = self.call_value().egld_or_single_esdt();
Expand Down Expand Up @@ -247,7 +247,7 @@ pub trait RewardsDistribution:
);
}

#[payable("*")]
#[payable]
#[endpoint(claimRewards)]
fn claim_rewards(
&self,
Expand Down
2 changes: 1 addition & 1 deletion contracts/examples/seed-nft-minter/src/nft_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub trait NftModule:

// endpoints

#[payable("*")]
#[payable]
#[endpoint(buyNft)]
fn buy_nft(&self, nft_nonce: u64) {
let payment = self.call_value().egld_or_single_esdt();
Expand Down
9 changes: 5 additions & 4 deletions framework/derive/src/parse/attributes/payable_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::parse::attributes::util::{clean_string, is_first_char_numeric};
use super::attr_names::*;

pub struct PayableAttribute {
pub identifier: Option<String>,
pub identifier: String,
}

impl PayableAttribute {
Expand All @@ -24,10 +24,11 @@ impl PayableAttribute {

/// Current implementation only works with 1 token name.
/// Might be extended in the future.
fn extract_token_identifier(attr: &syn::Attribute) -> Option<String> {
fn extract_token_identifier(attr: &syn::Attribute) -> String {
match attr.meta.clone() {
syn::Meta::Path(_) => {
panic!("attribute needs 1 string argument: Replace with #[payable(\"*\")] or #[payable(\"EGLD\")]")
// #[payable]
"*".to_owned()
},
syn::Meta::List(list) => {
let mut iter = list.tokens.into_iter();
Expand Down Expand Up @@ -60,7 +61,7 @@ fn extract_token_identifier(attr: &syn::Attribute) -> Option<String> {
iter.next().is_none(),
"too many tokens in attribute argument"
);
Some(ticker)
ticker
},
syn::Meta::NameValue(_) => panic!(
"attribute can not be name value. attribute needs 1 string argument: \"*\" or \"EGLD\""
Expand Down
4 changes: 2 additions & 2 deletions framework/derive/src/parse/method_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::{
process_title_attribute, process_upgrade_attribute, process_view_attribute,
};
pub struct MethodAttributesPass1 {
pub method_name: String,
pub _method_name: String,
pub payable: MethodPayableMetadata,
pub only_owner: bool,
pub only_admin: bool,
Expand All @@ -34,7 +34,7 @@ pub fn process_method(m: &syn::TraitItemFn, trait_attributes: &TraitProperties)
};

let mut first_pass_data = MethodAttributesPass1 {
method_name: m.sig.ident.to_string(),
_method_name: m.sig.ident.to_string(),
payable: MethodPayableMetadata::NotPayable,
only_owner: trait_attributes.only_owner,
only_admin: trait_attributes.only_admin,
Expand Down
14 changes: 5 additions & 9 deletions framework/derive/src/parse/payable_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@ pub fn process_payable_attribute(
attr: &syn::Attribute,
pass_1_data: &mut MethodAttributesPass1,
) -> bool {
PayableAttribute::parse(attr).map(|payable_attr| {
if let Some(identifier) = payable_attr.identifier {
pass_1_data.payable = parse_payable_identifier(identifier.as_str());
} else {
panic!(
"Endpoint `payable` attribute requires one argument. Replace with `#[payable(\"*\")]` or `#[payable(\"EGLD\")]`. Method name: {}",
&pass_1_data.method_name);
}
}).is_some()
PayableAttribute::parse(attr)
.map(|payable_attr| {
pass_1_data.payable = parse_payable_identifier(&payable_attr.identifier);
})
.is_some()
}

fn parse_payable_identifier(identifier: &str) -> MethodPayableMetadata {
Expand Down

0 comments on commit a56917f

Please sign in to comment.