From 1e0d401281a34464b2b86e82786f3c3cf629f0f7 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Sun, 22 Dec 2024 18:10:09 +0200 Subject: [PATCH] payable attribute - refactoring --- .../derive/src/parse/attributes/payable_attr.rs | 8 ++++---- framework/derive/src/parse/method_parse.rs | 4 ++-- framework/derive/src/parse/payable_parse.rs | 14 +++++--------- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/framework/derive/src/parse/attributes/payable_attr.rs b/framework/derive/src/parse/attributes/payable_attr.rs index bdc38d8800..6c0241848f 100644 --- a/framework/derive/src/parse/attributes/payable_attr.rs +++ b/framework/derive/src/parse/attributes/payable_attr.rs @@ -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, + pub identifier: String, } impl PayableAttribute { @@ -24,11 +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 { +fn extract_token_identifier(attr: &syn::Attribute) -> String { match attr.meta.clone() { syn::Meta::Path(_) => { // #[payable] - Some("*".to_owned()) + "*".to_owned() }, syn::Meta::List(list) => { let mut iter = list.tokens.into_iter(); @@ -61,7 +61,7 @@ fn extract_token_identifier(attr: &syn::Attribute) -> Option { 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\"" diff --git a/framework/derive/src/parse/method_parse.rs b/framework/derive/src/parse/method_parse.rs index e299912cdd..b5fa90b54d 100644 --- a/framework/derive/src/parse/method_parse.rs +++ b/framework/derive/src/parse/method_parse.rs @@ -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, @@ -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, diff --git a/framework/derive/src/parse/payable_parse.rs b/framework/derive/src/parse/payable_parse.rs index 91deba4509..99e6c8e17b 100644 --- a/framework/derive/src/parse/payable_parse.rs +++ b/framework/derive/src/parse/payable_parse.rs @@ -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 {