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

Add missing odra::prelude path in generated code #436

Merged
merged 2 commits into from
May 16, 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
24 changes: 12 additions & 12 deletions odra-macros/src/ast/contract_ref_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ mod ref_item_tests {
self.env.call_contract(
self.address,
odra::CallDef::new(
String::from("init"),
odra::prelude::string::String::from("init"),
true,
{
let mut named_args = odra::casper_types::RuntimeArgs::new();
Expand All @@ -211,7 +211,7 @@ mod ref_item_tests {
self.env.call_contract(
self.address,
odra::CallDef::new(
String::from("total_supply"),
odra::prelude::string::String::from("total_supply"),
false,
{
let mut named_args = odra::casper_types::RuntimeArgs::new();
Expand All @@ -227,7 +227,7 @@ mod ref_item_tests {
.call_contract(
self.address,
odra::CallDef::new(
String::from("pay_to_mint"),
odra::prelude::string::String::from("pay_to_mint"),
true,
{
let mut named_args = odra::casper_types::RuntimeArgs::new();
Expand All @@ -243,7 +243,7 @@ mod ref_item_tests {
.call_contract(
self.address,
odra::CallDef::new(
String::from("approve"),
odra::prelude::string::String::from("approve"),
true,
{
let mut named_args = odra::casper_types::RuntimeArgs::new();
Expand All @@ -262,7 +262,7 @@ mod ref_item_tests {
.call_contract(
self.address,
odra::CallDef::new(
String::from("airdrop"),
odra::prelude::string::String::from("airdrop"),
false,
{
let mut named_args = odra::casper_types::RuntimeArgs::new();
Expand Down Expand Up @@ -304,7 +304,7 @@ mod ref_item_tests {
self.env.call_contract(
self.address,
odra::CallDef::new(
String::from("total_supply"),
odra::prelude::string::String::from("total_supply"),
false,
{
let mut named_args = odra::casper_types::RuntimeArgs::new();
Expand All @@ -319,7 +319,7 @@ mod ref_item_tests {
.call_contract(
self.address,
odra::CallDef::new(
String::from("pay_to_mint"),
odra::prelude::string::String::from("pay_to_mint"),
true,
{
let mut named_args = odra::casper_types::RuntimeArgs::new();
Expand Down Expand Up @@ -360,7 +360,7 @@ mod ref_item_tests {
self.env.call_contract(
self.address,
odra::CallDef::new(
String::from("total_supply"),
odra::prelude::string::String::from("total_supply"),
false,
{
let mut named_args = odra::casper_types::RuntimeArgs::new();
Expand All @@ -376,7 +376,7 @@ mod ref_item_tests {
.call_contract(
self.address,
odra::CallDef::new(
String::from("get_owner"),
odra::prelude::string::String::from("get_owner"),
false,
{
let mut named_args = odra::casper_types::RuntimeArgs::new();
Expand All @@ -392,7 +392,7 @@ mod ref_item_tests {
.call_contract(
self.address,
odra::CallDef::new(
String::from("set_owner"),
odra::prelude::string::String::from("set_owner"),
true,
{
let mut named_args = odra::casper_types::RuntimeArgs::new();
Expand All @@ -409,7 +409,7 @@ mod ref_item_tests {
.call_contract(
self.address,
odra::CallDef::new(
String::from("name"),
odra::prelude::string::String::from("name"),
false,
{
let mut named_args = odra::casper_types::RuntimeArgs::new();
Expand All @@ -425,7 +425,7 @@ mod ref_item_tests {
.call_contract(
self.address,
odra::CallDef::new(
String::from("symbol"),
odra::prelude::string::String::from("symbol"),
false,
{
let mut named_args = odra::casper_types::RuntimeArgs::new();
Expand Down
102 changes: 53 additions & 49 deletions odra-macros/src/ast/entrypoints_item.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use syn::parse_quote;
use syn::punctuated::Punctuated;

use crate::ir::FnIR;
use crate::{ir::ModuleImplIR, utils};
Expand Down Expand Up @@ -46,9 +47,10 @@ impl TryFrom<&'_ ModuleImplIR> for EntrypointsFnItem {
let ident_entrypoints = utils::ident::entrypoints();
let entrypoint_ty = utils::ty::entry_point_def();
let expr = struct_entrypoints_expr(struct_ir)?;
let vec_ty = utils::ty::vec_of(&entrypoint_ty);

Ok(Self {
sig: parse_quote!(fn #ident_entrypoints() -> Vec<#entrypoint_ty>),
sig: parse_quote!(fn #ident_entrypoints() -> #vec_ty),
brace_token: Default::default(),
expr
})
Expand Down Expand Up @@ -78,21 +80,23 @@ fn struct_entrypoints_expr(ir: &ModuleImplIR) -> syn::Result<syn::Expr> {
let attributes = vec![is_payable_attr, is_non_reentrant]
.into_iter()
.flatten()
.collect::<Vec<_>>();
.collect::<Punctuated<_, syn::token::Comma>>();
let attributes = utils::expr::vec(attributes);
let ty_entrypoint = utils::ty::entry_point_def();
let name = utils::expr::string_from(ident);

let expr: syn::Expr = parse_quote!(#ty_entrypoint {
name: String::from(#ident),
name: #name,
args: #args,
is_mutable: #is_mut,
return_ty: #ret,
ty: #ty,
attributes: vec![#(#attributes),*]
attributes: #attributes
});
Ok(expr)
})
.collect::<syn::Result<syn::punctuated::Punctuated<syn::Expr, syn::token::Comma>>>()?;
Ok(parse_quote!(vec![#struct_entrypoints]))
Ok(utils::expr::vec(struct_entrypoints))
}

fn entrypoint_args(f: &FnIR) -> syn::Result<syn::Expr> {
Expand All @@ -104,8 +108,8 @@ fn entrypoint_args(f: &FnIR) -> syn::Result<syn::Expr> {
let ty = utils::ty::unreferenced_ty(&arg.ty()?);
Ok(utils::expr::into_arg(ty, ident))
})
.collect::<syn::Result<Vec<syn::Expr>>>()?;
Ok(parse_quote!(vec![#(#args),*]))
.collect::<syn::Result<Punctuated<syn::Expr, syn::token::Comma>>>()?;
Ok(utils::expr::vec(args))
}

#[cfg(test)]
Expand All @@ -120,56 +124,56 @@ mod test {
let module = test_utils::mock::module_impl();
let expected = quote!(
impl odra::contract_def::HasEntrypoints for Erc20 {
fn entrypoints() -> Vec<odra::contract_def::Entrypoint> {
vec![
fn entrypoints() -> odra::prelude::vec::Vec<odra::contract_def::Entrypoint> {
odra::prelude::vec![
odra::contract_def::Entrypoint {
name: String::from("init"),
args: vec![
name: odra::prelude::string::String::from("init"),
args: odra::prelude::vec![
odra::args::odra_argument::<Option<U256> >("total_supply")
],
is_mutable: true,
return_ty: <() as odra::casper_types::CLTyped>::cl_type(),
ty: odra::contract_def::EntrypointType::Constructor,
attributes: vec![]
attributes: odra::prelude::vec![]
},
odra::contract_def::Entrypoint {
name: String::from("total_supply"),
args: vec![],
name: odra::prelude::string::String::from("total_supply"),
args: odra::prelude::vec![],
is_mutable: false,
return_ty: <U256 as odra::casper_types::CLTyped>::cl_type(),
ty: odra::contract_def::EntrypointType::Public,
attributes: vec![]
attributes: odra::prelude::vec![]
},
odra::contract_def::Entrypoint {
name: String::from("pay_to_mint"),
args: vec![],
name: odra::prelude::string::String::from("pay_to_mint"),
args: odra::prelude::vec![],
is_mutable: true,
return_ty: <() as odra::casper_types::CLTyped>::cl_type(),
ty: odra::contract_def::EntrypointType::Public,
attributes: vec![odra::contract_def::EntrypointAttribute::Payable]
attributes: odra::prelude::vec![odra::contract_def::EntrypointAttribute::Payable]
},
odra::contract_def::Entrypoint {
name: String::from("approve"),
args: vec![
name: odra::prelude::string::String::from("approve"),
args: odra::prelude::vec![
odra::args::odra_argument::<Address>("to"),
odra::args::odra_argument::<U256>("amount"),
odra::args::odra_argument::<Maybe<String> >("msg")
],
is_mutable: true,
return_ty: <() as odra::casper_types::CLTyped>::cl_type(),
ty: odra::contract_def::EntrypointType::Public,
attributes: vec![odra::contract_def::EntrypointAttribute::NonReentrant]
attributes: odra::prelude::vec![odra::contract_def::EntrypointAttribute::NonReentrant]
},
odra::contract_def::Entrypoint {
name: String::from("airdrop"),
args: vec![
name: odra::prelude::string::String::from("airdrop"),
args: odra::prelude::vec![
odra::args::odra_argument::<odra::prelude::vec::Vec<Address> >("to"),
odra::args::odra_argument::<U256>("amount")
],
is_mutable: false,
return_ty: <() as odra::casper_types::CLTyped>::cl_type(),
ty: odra::contract_def::EntrypointType::Public,
attributes: vec![]
attributes: odra::prelude::vec![]
}
]
}
Expand All @@ -184,23 +188,23 @@ mod test {
let module = test_utils::mock::module_trait_impl();
let expected = quote!(
impl odra::contract_def::HasEntrypoints for Erc20 {
fn entrypoints() -> Vec<odra::contract_def::Entrypoint> {
vec![
fn entrypoints() -> odra::prelude::vec::Vec<odra::contract_def::Entrypoint> {
odra::prelude::vec![
odra::contract_def::Entrypoint {
name: String::from("total_supply"),
args: vec![],
name: odra::prelude::string::String::from("total_supply"),
args: odra::prelude::vec![],
is_mutable: false,
return_ty: <U256 as odra::casper_types::CLTyped>::cl_type(),
ty: odra::contract_def::EntrypointType::Public,
attributes: vec![]
attributes: odra::prelude::vec![]
},
odra::contract_def::Entrypoint {
name: String::from("pay_to_mint"),
args: vec![],
name: odra::prelude::string::String::from("pay_to_mint"),
args: odra::prelude::vec![],
is_mutable: true,
return_ty: <() as odra::casper_types::CLTyped>::cl_type(),
ty: odra::contract_def::EntrypointType::Public,
attributes: vec![odra::contract_def::EntrypointAttribute::Payable]
attributes: odra::prelude::vec![odra::contract_def::EntrypointAttribute::Payable]
}
]
}
Expand All @@ -215,49 +219,49 @@ mod test {
let module = test_utils::mock::module_delegation();
let expected = quote!(
impl odra::contract_def::HasEntrypoints for Erc20 {
fn entrypoints() -> Vec<odra::contract_def::Entrypoint> {
vec![
fn entrypoints() -> odra::prelude::vec::Vec<odra::contract_def::Entrypoint> {
odra::prelude::vec![
odra::contract_def::Entrypoint {
name: String::from("total_supply"),
args: vec![],
name: odra::prelude::string::String::from("total_supply"),
args: odra::prelude::vec![],
is_mutable: false,
return_ty: <U256 as odra::casper_types::CLTyped>::cl_type(),
ty: odra::contract_def::EntrypointType::Public,
attributes: vec![]
attributes: odra::prelude::vec![]
},
odra::contract_def::Entrypoint {
name: String::from("get_owner"),
args: vec![],
name: odra::prelude::string::String::from("get_owner"),
args: odra::prelude::vec![],
is_mutable: false,
return_ty: <Address as odra::casper_types::CLTyped>::cl_type(),
ty: odra::contract_def::EntrypointType::Public,
attributes : vec![]
attributes : odra::prelude::vec![]
},
odra::contract_def::Entrypoint {
name: String::from("set_owner"),
args: vec![
name: odra::prelude::string::String::from("set_owner"),
args: odra::prelude::vec![
odra::args::odra_argument::<Address>("new_owner")
],
is_mutable: true,
return_ty: <() as odra::casper_types::CLTyped>::cl_type(),
ty: odra::contract_def::EntrypointType::Public,
attributes: vec![]
attributes: odra::prelude::vec![]
},
odra::contract_def::Entrypoint {
name: String::from("name"),
args: vec![],
name: odra::prelude::string::String::from("name"),
args: odra::prelude::vec![],
is_mutable: false,
return_ty: <String as odra::casper_types::CLTyped >::cl_type(),
ty: odra::contract_def::EntrypointType::Public,
attributes : vec![]
attributes : odra::prelude::vec![]
},
odra::contract_def::Entrypoint {
name: String::from("symbol"),
args: vec![],
name: odra::prelude::string::String::from("symbol"),
args: odra::prelude::vec![],
is_mutable: false,
return_ty: <String as odra::casper_types::CLTyped>::cl_type(),
ty: odra::contract_def::EntrypointType::Public,
attributes :vec![]
attributes: odra::prelude::vec![]
}
]
}
Expand Down
4 changes: 2 additions & 2 deletions odra-macros/src/ast/external_contract_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ mod test {
self.env.call_contract(
self.address,
odra::CallDef::new(
String::from("balance_of"),
odra::prelude::string::String::from("balance_of"),
false,
{
let mut named_args = odra::casper_types::RuntimeArgs::new();
Expand Down Expand Up @@ -137,7 +137,7 @@ mod test {
self.env.call_contract(
self.address,
odra::CallDef::new(
String::from("balance_of"),
odra::prelude::string::String::from("balance_of"),
false,
{
let mut named_args = odra::casper_types::RuntimeArgs::new();
Expand Down
Loading
Loading