Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non payable transferred_value upgrade #268

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions detectors/non-payable-transferred-value/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ impl<'tcx> Visitor<'tcx> for TransferredValueSearcher {
impl EarlyLintPass for NonPayableTransferredValue {
fn check_impl_item(&mut self, cx: &EarlyContext<'_>, item: &AssocItem) {
if let AssocItemKind::Fn(it) = &item.kind
&& !any_attr_is_payable(&item.attrs)
&& !attr_is_present(&item.attrs, "payable")
&& attr_is_present(&item.attrs, "message")
&& it.body.is_some()
{
let mut visitor = TransferredValueSearcher::default();
Expand Down Expand Up @@ -86,25 +87,25 @@ fn is_self_env_transferred_value(met: &MethodCall) -> bool {
false
}

fn any_attr_is_payable(attrs: &[Attribute]) -> bool {
fn attr_is_present(attrs: &[Attribute], find: &str) -> bool {
for attr in attrs {
if let AttrKind::Normal(nattr) = &attr.kind
&& nattr.item.path.segments.len() == 1
&& nattr.item.path.segments[0].ident.name.to_string() == "ink"
&& let AttrArgs::Delimited(DelimArgs { tokens, .. }) = &nattr.item.args
&& is_payable_token_present(tokens)
&& is_token_present(tokens, find)
{
return true;
}
}
false
}

fn is_payable_token_present(token_stream: &TokenStream) -> bool {
fn is_token_present(token_stream: &TokenStream, find: &str) -> bool {
token_stream.trees().any(|tree| match tree {
TokenTree::Token(token, _) => token
.ident()
.map_or(false, |ident| ident.0.name.to_string() == "payable"),
TokenTree::Delimited(_, _, _, token_stream) => is_payable_token_present(token_stream),
.map_or(false, |ident| ident.0.name.to_string() == find),
TokenTree::Delimited(_, _, _, token_stream) => is_token_present(token_stream, find),
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@
mod non_pble_transferred_value {

#[ink(storage)]
pub struct NonPbleTransferredValue {
value: u32,
}
pub struct NonPbleTransferredValue {}

impl NonPbleTransferredValue {
#[ink(constructor)]
pub fn new(value: u32) -> Self {
Self { value }
pub fn new() -> Self {
Self {}
}

#[ink(message)]
pub fn something(&self) -> bool {
if self.env().transferred_value() > 0 {
return true;
}
false
self.env().transferred_value() > 0
}

#[ink(message)]
pub fn something_in_other_function(&self) -> bool {
self.the_other_function()
}

pub fn the_other_function(&self) -> bool {
self.env().transferred_value() > 0
}
}
}
Loading