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 external_contract proc macro #292

Merged
merged 3 commits into from
Dec 13, 2023
Merged
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
7 changes: 7 additions & 0 deletions core/src/contract_def.rs
Original file line number Diff line number Diff line change
@@ -70,6 +70,11 @@ pub trait HasEntrypoints {
/// A trait that should be implemented by each smart contract to allow the backend.
pub trait HasEvents {
fn events() -> Vec<Event>;

#[cfg(target_arch = "wasm32")]
fn event_schemas() -> crate::prelude::BTreeMap<String, casper_event_standard::Schema> {
crate::prelude::BTreeMap::new()
}
}

#[derive(Debug, Clone)]
@@ -95,6 +100,8 @@ pub trait IntoEvent {

impl<T: EventInstance> IntoEvent for T {
fn into_event() -> Event {
let mut schemas = casper_event_standard::Schemas::new();
schemas.add::<T>();
let ident = <T as EventInstance>::name();
let schema = <T as EventInstance>::schema();
let args = schema
7 changes: 6 additions & 1 deletion examples2/src/erc20.rs
Original file line number Diff line number Diff line change
@@ -31,6 +31,11 @@ pub enum Erc20Error {
InsufficientAllowance = 2
}

#[odra::external_contract]
trait TotalSupply {
fn total_supply(&self) -> U256;
}

#[odra::module(events = [OnTransfer, OnCrossTransfer, OnApprove])]
pub struct Erc20 {
total_supply: Variable<U256>,
@@ -80,7 +85,7 @@ impl Erc20 {
}

pub fn cross_total(&self, other: &Address) -> U256 {
let other_erc20 = Erc20ContractRef {
let other_erc20 = TotalSupplyContractRef {
address: *other,
env: self.env()
};
10 changes: 5 additions & 5 deletions odra-macros/src/ast/blueprint.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use derive_try_from::TryFromRef;
use syn::parse_quote;

use crate::{ir::ModuleIR, utils};
use crate::{ir::ModuleImplIR, utils};

use super::parts_utils::UseSuperItem;

#[derive(syn_derive::ToTokens, TryFromRef)]
#[source(ModuleIR)]
#[source(ModuleImplIR)]
pub struct BlueprintItem {
#[expr(utils::attr::odra_module(&item.module_str()?))]
attr: syn::Attribute,
mod_item: BlueprintModItem
}

#[derive(syn_derive::ToTokens, TryFromRef)]
#[source(ModuleIR)]
#[source(ModuleImplIR)]
struct BlueprintModItem {
#[default]
mod_token: syn::token::Mod,
@@ -40,10 +40,10 @@ struct SchemaFnItem {
expr: syn::Expr
}

impl TryFrom<&'_ ModuleIR> for SchemaFnItem {
impl TryFrom<&'_ ModuleImplIR> for SchemaFnItem {
type Error = syn::Error;

fn try_from(module: &'_ ModuleIR) -> Result<Self, Self::Error> {
fn try_from(module: &'_ ModuleImplIR) -> Result<Self, Self::Error> {
let ty_blueprint = utils::ty::contract_blueprint();
let ident_module_schema = utils::ident::module_schema();

14 changes: 7 additions & 7 deletions odra-macros/src/ast/deployer_item.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use derive_try_from::TryFromRef;

use crate::{ir::ModuleIR, utils};
use crate::{ir::ModuleImplIR, utils};

use super::deployer_utils::{
DeployerInitSignature, EntrypointCallerExpr, HostRefInstanceExpr, NewContractExpr
@@ -14,10 +14,10 @@ struct DeployStructItem {
semi_token: syn::token::Semi
}

impl TryFrom<&'_ ModuleIR> for DeployStructItem {
impl TryFrom<&'_ ModuleImplIR> for DeployStructItem {
type Error = syn::Error;

fn try_from(module: &'_ ModuleIR) -> Result<Self, Self::Error> {
fn try_from(module: &'_ ModuleImplIR) -> Result<Self, Self::Error> {
Ok(Self {
vis: utils::syn::visibility_pub(),
struct_token: Default::default(),
@@ -37,10 +37,10 @@ struct DeployImplItem {
init_fn: ContractInitFn
}

impl TryFrom<&'_ ModuleIR> for DeployImplItem {
impl TryFrom<&'_ ModuleImplIR> for DeployImplItem {
type Error = syn::Error;

fn try_from(module: &'_ ModuleIR) -> Result<Self, Self::Error> {
fn try_from(module: &'_ ModuleImplIR) -> Result<Self, Self::Error> {
Ok(Self {
impl_token: Default::default(),
ident: module.deployer_ident()?,
@@ -51,7 +51,7 @@ impl TryFrom<&'_ ModuleIR> for DeployImplItem {
}

#[derive(syn_derive::ToTokens, TryFromRef)]
#[source(ModuleIR)]
#[source(ModuleImplIR)]
struct ContractInitFn {
#[expr(utils::syn::visibility_pub())]
vis: syn::Visibility,
@@ -68,7 +68,7 @@ struct ContractInitFn {
}

#[derive(syn_derive::ToTokens, TryFromRef)]
#[source(ModuleIR)]
#[source(ModuleImplIR)]
pub struct DeployerItem {
struct_item: DeployStructItem,
impl_item: DeployImplItem
20 changes: 10 additions & 10 deletions odra-macros/src/ast/deployer_utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{fn_utils, ref_utils};
use crate::utils::misc::AsType;
use crate::{
ir::{FnIR, ModuleIR},
ir::{FnIR, ModuleImplIR},
utils
};
use proc_macro2::TokenStream;
@@ -19,10 +19,10 @@ pub struct DeployerInitSignature {
output: syn::ReturnType
}

impl TryFrom<&'_ ModuleIR> for DeployerInitSignature {
impl TryFrom<&'_ ModuleImplIR> for DeployerInitSignature {
type Error = syn::Error;

fn try_from(module: &'_ ModuleIR) -> Result<Self, Self::Error> {
fn try_from(module: &'_ ModuleImplIR) -> Result<Self, Self::Error> {
let host_ref_ident = module.host_ref_ident()?.as_type();
let ty_host_env = utils::ty::host_env();
let env = utils::ident::env();
@@ -49,10 +49,10 @@ pub struct EntrypointCallerExpr {
semi_token: syn::token::Semi
}

impl TryFrom<&'_ ModuleIR> for EntrypointCallerExpr {
impl TryFrom<&'_ ModuleImplIR> for EntrypointCallerExpr {
type Error = syn::Error;

fn try_from(module: &'_ ModuleIR) -> Result<Self, Self::Error> {
fn try_from(module: &'_ ModuleImplIR) -> Result<Self, Self::Error> {
Ok(Self {
let_token: Default::default(),
ident: utils::ident::caller(),
@@ -64,7 +64,7 @@ impl TryFrom<&'_ ModuleIR> for EntrypointCallerExpr {
}

impl EntrypointCallerExpr {
fn entrypoint_caller(module: &ModuleIR) -> Result<syn::Expr, syn::Error> {
fn entrypoint_caller(module: &ModuleImplIR) -> Result<syn::Expr, syn::Error> {
let env_ident = utils::ident::env();
let contract_env_ident = utils::ident::contract_env();
let call_def_ident = utils::ident::call_def();
@@ -96,10 +96,10 @@ pub struct NewContractExpr {
semi_token: syn::token::Semi
}

impl TryFrom<&'_ ModuleIR> for NewContractExpr {
impl TryFrom<&'_ ModuleImplIR> for NewContractExpr {
type Error = syn::Error;

fn try_from(module: &'_ ModuleIR) -> Result<Self, Self::Error> {
fn try_from(module: &'_ ModuleImplIR) -> Result<Self, Self::Error> {
let module_str = module.module_str()?;
let caller_expr = utils::expr::some(utils::ident::caller());
let env_ident = utils::ident::env();
@@ -136,10 +136,10 @@ pub struct HostRefInstanceExpr {
fields: syn::punctuated::Punctuated<syn::FieldValue, syn::Token![,]>
}

impl TryFrom<&'_ ModuleIR> for HostRefInstanceExpr {
impl TryFrom<&'_ ModuleImplIR> for HostRefInstanceExpr {
type Error = syn::Error;

fn try_from(module: &'_ ModuleIR) -> Result<Self, Self::Error> {
fn try_from(module: &'_ ModuleImplIR) -> Result<Self, Self::Error> {
let address_ident = utils::ident::address();
let env_ident = utils::ident::env();
let attached_value_ident = utils::ident::attached_value();
12 changes: 6 additions & 6 deletions odra-macros/src/ast/entrypoints_item.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use syn::parse_quote;

use crate::ir::FnIR;
use crate::{ir::ModuleIR, utils};
use crate::{ir::ModuleImplIR, utils};

#[derive(syn_derive::ToTokens)]
pub struct HasEntrypointsImplItem {
@@ -15,10 +15,10 @@ pub struct HasEntrypointsImplItem {
events_fn: EntrypointsFnItem
}

impl TryFrom<&'_ ModuleIR> for HasEntrypointsImplItem {
impl TryFrom<&'_ ModuleImplIR> for HasEntrypointsImplItem {
type Error = syn::Error;

fn try_from(struct_ir: &'_ ModuleIR) -> Result<Self, Self::Error> {
fn try_from(struct_ir: &'_ ModuleImplIR) -> Result<Self, Self::Error> {
Ok(Self {
impl_token: Default::default(),
has_ident_ty: utils::ty::has_entrypoints(),
@@ -39,10 +39,10 @@ pub struct EntrypointsFnItem {
expr: syn::Expr
}

impl TryFrom<&'_ ModuleIR> for EntrypointsFnItem {
impl TryFrom<&'_ ModuleImplIR> for EntrypointsFnItem {
type Error = syn::Error;

fn try_from(struct_ir: &'_ ModuleIR) -> Result<Self, Self::Error> {
fn try_from(struct_ir: &'_ ModuleImplIR) -> Result<Self, Self::Error> {
let ident_entrypoints = utils::ident::entrypoints();
let entrypoint_ty = utils::ty::entry_point_def();
let expr = struct_entrypoints_expr(struct_ir)?;
@@ -55,7 +55,7 @@ impl TryFrom<&'_ ModuleIR> for EntrypointsFnItem {
}
}

fn struct_entrypoints_expr(ir: &ModuleIR) -> Result<syn::Expr, syn::Error> {
fn struct_entrypoints_expr(ir: &ModuleImplIR) -> Result<syn::Expr, syn::Error> {
let struct_entrypoints = ir
.functions()
.iter()
22 changes: 11 additions & 11 deletions odra-macros/src/ast/events_item.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ use crate::ast::fn_utils::FnItem;
use crate::ast::utils::ImplItem;
use crate::ir::TypeIR;
use crate::utils::misc::AsBlock;
use crate::{ir::StructIR, utils};
use crate::{ir::ModuleStructIR, utils};

#[derive(syn_derive::ToTokens)]
pub struct HasEventsImplItem {
@@ -15,10 +15,10 @@ pub struct HasEventsImplItem {
events_fn: EventsFnsItem
}

impl TryFrom<&'_ StructIR> for HasEventsImplItem {
impl TryFrom<&'_ ModuleStructIR> for HasEventsImplItem {
type Error = syn::Error;

fn try_from(ir: &'_ StructIR) -> Result<Self, Self::Error> {
fn try_from(ir: &'_ ModuleStructIR) -> Result<Self, Self::Error> {
Ok(Self {
impl_item: ImplItem::has_events(ir)?,
brace_token: Default::default(),
@@ -78,7 +78,7 @@ impl EventsFnsItem {
utils::misc::ret_ty(&btree)
}

fn events_fn(ir: &StructIR) -> Result<FnItem, syn::Error> {
fn events_fn(ir: &ModuleStructIR) -> Result<FnItem, syn::Error> {
let ident_events = utils::ident::events();
let struct_events_stmt = struct_events_stmt(ir);
let chain_events_expr = chain_events_expr(ir)?;
@@ -93,7 +93,7 @@ impl EventsFnsItem {
))
}

fn event_schemas_fn(ir: &StructIR) -> Result<FnItem, syn::Error> {
fn event_schemas_fn(ir: &ModuleStructIR) -> Result<FnItem, syn::Error> {
let ident_events = utils::ident::event_schemas();
let struct_events_stmt = struct_event_schemas_stmt(ir);
let chain_events_expr = chain_event_schemas_expr(ir)?;
@@ -109,10 +109,10 @@ impl EventsFnsItem {
}
}

impl TryFrom<&'_ StructIR> for EventsFnsItem {
impl TryFrom<&'_ ModuleStructIR> for EventsFnsItem {
type Error = syn::Error;

fn try_from(ir: &'_ StructIR) -> Result<Self, Self::Error> {
fn try_from(ir: &'_ ModuleStructIR) -> Result<Self, Self::Error> {
Ok(Self {
events_fn: Self::events_fn(ir)?,
wasm_attr: utils::attr::wasm32(),
@@ -121,7 +121,7 @@ impl TryFrom<&'_ StructIR> for EventsFnsItem {
}
}

fn struct_events_stmt(ir: &StructIR) -> syn::Stmt {
fn struct_events_stmt(ir: &ModuleStructIR) -> syn::Stmt {
let events_ident = utils::ident::events();

let struct_events = ir
@@ -133,7 +133,7 @@ fn struct_events_stmt(ir: &StructIR) -> syn::Stmt {
parse_quote!(let #events_ident = #vec;)
}

fn chain_events_expr(ir: &StructIR) -> Result<syn::Expr, syn::Error> {
fn chain_events_expr(ir: &ModuleStructIR) -> Result<syn::Expr, syn::Error> {
let ev_ty = utils::ty::event();
let events_ident = utils::ident::events();
let fields_events = ir
@@ -152,7 +152,7 @@ fn chain_events_expr(ir: &StructIR) -> Result<syn::Expr, syn::Error> {
))
}

fn struct_event_schemas_stmt(ir: &StructIR) -> syn::Stmt {
fn struct_event_schemas_stmt(ir: &ModuleStructIR) -> syn::Stmt {
let result_ident = utils::ident::result();

let events = ir
@@ -169,7 +169,7 @@ fn struct_event_schemas_stmt(ir: &StructIR) -> syn::Stmt {
parse_quote!(let #result_ident = #new_btree_map;)
}

fn chain_event_schemas_expr(ir: &StructIR) -> Result<syn::Expr, syn::Error> {
fn chain_event_schemas_expr(ir: &ModuleStructIR) -> Result<syn::Expr, syn::Error> {
let result_ident = utils::ident::result();
let fields_events = ir
.unique_fields_ty()?
16 changes: 8 additions & 8 deletions odra-macros/src/ast/exec_parts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::parts_utils::{UsePreludeItem, UseSuperItem};
use crate::{
ir::{FnIR, ModuleIR},
ir::{FnIR, ModuleImplIR},
utils
};
use derive_try_from::TryFromRef;
@@ -12,10 +12,10 @@ pub struct ExecPartsReexportItem {
reexport_stmt: syn::Stmt
}

impl TryFrom<&'_ ModuleIR> for ExecPartsReexportItem {
impl TryFrom<&'_ ModuleImplIR> for ExecPartsReexportItem {
type Error = syn::Error;

fn try_from(module: &'_ ModuleIR) -> Result<Self, Self::Error> {
fn try_from(module: &'_ ModuleImplIR) -> Result<Self, Self::Error> {
let test_parts_ident = module.exec_parts_mod_ident()?;
Ok(Self {
reexport_stmt: parse_quote!(pub use #test_parts_ident::*;)
@@ -37,10 +37,10 @@ pub struct ExecPartsItem {
exec_functions: Vec<ExecFunctionItem>
}

impl TryFrom<&'_ ModuleIR> for ExecPartsItem {
impl TryFrom<&'_ ModuleImplIR> for ExecPartsItem {
type Error = syn::Error;

fn try_from(module: &'_ ModuleIR) -> Result<Self, Self::Error> {
fn try_from(module: &'_ ModuleImplIR) -> Result<Self, Self::Error> {
Ok(Self {
parts_module: module.try_into()?,
brace_token: Default::default(),
@@ -85,10 +85,10 @@ struct ExecFunctionItem {
return_stmt: syn::Stmt
}

impl TryFrom<(&'_ ModuleIR, &'_ FnIR)> for ExecFunctionItem {
impl TryFrom<(&'_ ModuleImplIR, &'_ FnIR)> for ExecFunctionItem {
type Error = syn::Error;

fn try_from(value: (&'_ ModuleIR, &'_ FnIR)) -> Result<Self, Self::Error> {
fn try_from(value: (&'_ ModuleImplIR, &'_ FnIR)) -> Result<Self, Self::Error> {
let (module, func) = value;
let fn_ident = func.name();
let result_ident = utils::ident::result();
@@ -173,7 +173,7 @@ impl TryFrom<&'_ FnIR> for ExecFnSignature {
}

#[derive(syn_derive::ToTokens, TryFromRef)]
#[source(ModuleIR)]
#[source(ModuleImplIR)]
struct ExecPartsModuleItem {
#[default]
mod_token: syn::token::Mod,
Loading