Skip to content

Commit

Permalink
payable attribute - refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Dec 23, 2024
1 parent 0fad48a commit 1e0d401
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
8 changes: 4 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,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<String> {
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();
Expand Down Expand Up @@ -61,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 1e0d401

Please sign in to comment.