Skip to content

Commit

Permalink
Wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kubaplas committed Jan 17, 2024
1 parent ccaa3b5 commit 1dd45bf
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 18 deletions.
5 changes: 5 additions & 0 deletions core/src/host_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ impl HostEnv {
deployed_contract
}

pub fn register_contract(&self, address: Address) {
self.deployed_contracts.borrow_mut().push(address);
self.events_count.borrow_mut().insert(address, 0);
}

pub fn call_contract<T: FromBytes + CLTyped>(
&self,
address: Address,
Expand Down
5 changes: 3 additions & 2 deletions examples/bin/erc20_on_livenet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ fn main() {
let address = Address::from_str(address).unwrap();
let mut token = Erc20HostRef::new(address, env.clone());
env.set_gas(1_000_000_000u64);
// token.approve(owner, U256::from(1000));
token.approve(owner, U256::from(1000));
let name = token.name();

println!("Token name: {}", token.symbol());
// println!("Token name: {}", token.symbol());

// env.set_gas(3_000_000_000u64);
// token.transfer(recipient, U256::from(1000));
Expand Down
4 changes: 4 additions & 0 deletions odra-casper/casper-client/src/casper_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ impl CasperClient {
value
}

pub fn call_without_deploy(&self, address: Address, call_def: CallDef) -> Result<Bytes, OdraError> {
todo!("call_without_deploy");
}

/// Deploy the entrypoint call.
pub fn deploy_entrypoint_call(
&self,
Expand Down
3 changes: 3 additions & 0 deletions odra-casper/livenet-env/src/livenet_host_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ impl HostContext for LivenetEnv {
call_def: CallDef,
use_proxy: bool
) -> Result<Bytes, OdraError> {
if !call_def.is_mut() {
return self.casper_client.borrow_mut().call_without_deploy(*address, call_def);
}
match use_proxy {
true => Ok(self.casper_client.borrow_mut().deploy_entrypoint_call_with_proxy(*address, call_def)),
false => {
Expand Down
32 changes: 25 additions & 7 deletions odra-macros/src/ast/deployer_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use derive_try_from::TryFromRef;

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

use super::deployer_utils::{
DeployerInitSignature, EntrypointCallerExpr, HostRefInstanceExpr, NewContractExpr
};
use super::deployer_utils::{CallEpcExpr, EpcSignature, DeployerInitSignature, EntrypointCallerExpr, HostRefInstanceExpr, NewContractExpr};

#[derive(syn_derive::ToTokens)]
struct DeployStructItem {
Expand Down Expand Up @@ -34,6 +32,8 @@ struct DeployImplItem {
#[syn(braced)]
brace_token: syn::token::Brace,
#[syn(in = brace_token)]
epc_fn: ContractEpcFn,
#[syn(in = brace_token)]
init_fn: ContractInitFn
}

Expand All @@ -45,11 +45,25 @@ impl TryFrom<&'_ ModuleImplIR> for DeployImplItem {
impl_token: Default::default(),
ident: module.deployer_ident()?,
brace_token: Default::default(),
epc_fn: module.try_into()?,
init_fn: module.try_into()?
})
}
}

#[derive(syn_derive::ToTokens, TryFromRef)]
#[source(ModuleImplIR)]
pub struct ContractEpcFn {
#[expr(utils::syn::visibility_pub())]
vis: syn::Visibility,
sig: EpcSignature,
#[syn(braced)]
#[default]
braces: syn::token::Brace,
#[syn(in = braces)]
caller: EntrypointCallerExpr,
}

#[derive(syn_derive::ToTokens, TryFromRef)]
#[source(ModuleImplIR)]
struct ContractInitFn {
Expand All @@ -60,7 +74,7 @@ struct ContractInitFn {
#[default]
braces: syn::token::Brace,
#[syn(in = braces)]
caller: EntrypointCallerExpr,
caller: CallEpcExpr,
#[syn(in = braces)]
new_contract: NewContractExpr,
#[syn(in = braces)]
Expand All @@ -87,8 +101,8 @@ mod deployer_impl {
pub struct Erc20Deployer;

impl Erc20Deployer {
pub fn init(env: &odra::HostEnv, total_supply: Option<U256>) -> Erc20HostRef {
let caller = odra::EntryPointsCaller::new(env.clone(), |contract_env, call_def| {
pub fn epc(env: &odra::HostEnv) -> odra::EntryPointsCaller {
odra::EntryPointsCaller::new(env.clone(), |contract_env, call_def| {
match call_def.method() {
"init" => {
let result = __erc20_exec_parts::execute_init(contract_env);
Expand All @@ -114,7 +128,11 @@ mod deployer_impl {
odra::VmError::NoSuchMethod(odra::prelude::String::from(name)),
))
}
});
})
}

pub fn init(env: &odra::HostEnv, total_supply: Option<U256>) -> Erc20HostRef {
let caller = Self::epc(env);

let address = env.new_contract(
"Erc20",
Expand Down
74 changes: 66 additions & 8 deletions odra-macros/src/ast/deployer_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,48 @@ impl TryFrom<&'_ ModuleImplIR> for DeployerInitSignature {
}
}

#[derive(syn_derive::ToTokens)]
pub struct EpcSignature {
fn_token: syn::token::Fn,
epc_token: syn::Ident,
#[syn(parenthesized)]
paren_token: syn::token::Paren,
#[syn(in = paren_token)]
input: syn::FnArg,
output: syn::ReturnType
}

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

fn try_from(module: &'_ ModuleImplIR) -> Result<Self, Self::Error> {
let epc_ident = utils::ty::entry_points_caller();
let ty_host_env = utils::ty::host_env();
let env = utils::ident::env();

let input = parse_quote!(#env: &#ty_host_env);

Ok(Self {
fn_token: Default::default(),
epc_token: utils::ident::epc(),
paren_token: Default::default(),
input,
output: utils::misc::ret_ty(&epc_ident)
})
}
}

#[derive(syn_derive::ToTokens)]
pub struct EntrypointCallerExpr {
let_token: syn::token::Let,
ident: syn::Ident,
assign_token: syn::token::Eq,
caller_expr: syn::Expr,
semi_token: syn::token::Semi
}

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

fn try_from(module: &'_ ModuleImplIR) -> Result<Self, Self::Error> {
Ok(Self {
let_token: Default::default(),
ident: utils::ident::caller(),
assign_token: Default::default(),
caller_expr: Self::entrypoint_caller(module)?,
semi_token: Default::default()
})
}
}
Expand Down Expand Up @@ -88,6 +111,41 @@ impl EntrypointCallerExpr {
}
}

#[derive(syn_derive::ToTokens)]
pub struct CallEpcExpr {
let_token: syn::token::Let,
ident: syn::Ident,
assign_token: syn::token::Eq,
epc_expression: syn::Expr,
semi_token: syn::token::Semi
}

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

fn try_from(module: &'_ ModuleImplIR) -> Result<Self, Self::Error> {
let epc_ident = utils::ident::epc();
let env_ident = utils::ident::env();
let args = module
.constructor()
.map(|f| fn_utils::runtime_args_block(&f, ref_utils::insert_arg_stmt))
.map(utils::expr::some)
.unwrap_or_else(utils::expr::none);


Ok(Self {
let_token: Default::default(),
ident: utils::ident::caller(),
assign_token: Default::default(),
epc_expression: parse_quote!(
Self::epc(env)
),
semi_token: Default::default()
})
}
}


#[derive(syn_derive::ToTokens)]
pub struct NewContractExpr {
let_token: syn::token::Let,
Expand Down
1 change: 1 addition & 0 deletions odra-macros/src/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ impl ModuleImplIR {
module_ident.span()
))
}

pub fn contract_ref_ident(&self) -> Result<Ident, syn::Error> {
let module_ident = self.module_ident()?;
Ok(Ident::new(
Expand Down
4 changes: 4 additions & 0 deletions odra-macros/src/utils/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ pub fn init() -> syn::Ident {
format_ident!("init")
}

pub fn epc() -> syn::Ident {
format_ident!("epc")
}

pub fn address() -> syn::Ident {
format_ident!("address")
}
Expand Down
2 changes: 1 addition & 1 deletion odra-vm/src/odra_vm_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl HostContext for OdraVmHost {
if let Some(init_args) = init_args {
let _ = self.call_contract(
&address,
CallDef::new(String::from("init"), init_args),
CallDef::new(String::from("init"), true, init_args),
false
);
}
Expand Down

0 comments on commit 1dd45bf

Please sign in to comment.