Skip to content

Commit

Permalink
Events wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kubaplas committed Nov 14, 2023
1 parent fef0c3b commit 0f37a70
Showing 1 changed file with 97 additions and 4 deletions.
101 changes: 97 additions & 4 deletions examples2/src/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ impl Erc20 {
}
}

pub fn approve(&mut self, to: Address, amount: U256) {
self.env.emit_event(Approval {
owner: self.env.caller(),
spender: to,
value: amount,
});
}

pub fn total_supply(&self) -> U256 {
self.total_supply.get_or_default()
}
Expand Down Expand Up @@ -212,6 +220,16 @@ mod __erc20_wasm_parts {
EntryPointAccess::Public,
EntryPointType::Contract
));
entry_points.add_entry_point(EntryPoint::new(
"approve",
alloc::vec![
Parameter::new("to", Address::cl_type()),
Parameter::new("amount", U256::cl_type()),
],
CLType::Unit,
EntryPointAccess::Public,
EntryPointType::Contract
));
entry_points
}

Expand Down Expand Up @@ -288,6 +306,14 @@ mod __erc20_wasm_parts {
contract.burn_and_get_paid(amount);
}

pub fn execute_approve() {
let to: Address = runtime::get_named_arg("to");
let amount: U256 = runtime::get_named_arg("amount");
let env = WasmContractEnv::new();
let mut contract: Erc20 = Erc20::new(Rc::new(env));
contract.approve(to, amount);
}

#[no_mangle]
fn call() {
execute_call();
Expand Down Expand Up @@ -332,6 +358,11 @@ mod __erc20_wasm_parts {
fn burn_and_get_paid() {
execute_burn_and_get_paid();
}

#[no_mangle]
fn approve() {
execute_approve();
}
}

// #[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -449,6 +480,19 @@ mod __erc20_test_parts {
)
}

pub fn approve(&self, to: Address, amount: U256) {
self.env.call_contract(
&self.address,
CallDef::new(
String::from("approve"),
runtime_args! {
"to" => to,
"amount" => amount
}
)
)
}

pub fn get_event<T: FromBytes + EventInstance>(&self, index: i32) -> Result<T, EventError> {
self.env.get_event(&self.address, index)
}
Expand Down Expand Up @@ -500,6 +544,12 @@ mod __erc20_test_parts {
let result = erc20.burn_and_get_paid(amount);
Bytes::from(result.to_bytes().unwrap())
}
"approve" => {
let to: Address = call_def.get("to").unwrap();
let amount: U256 = call_def.get("amount").unwrap();
let result = erc20.approve(to, amount);
Bytes::from(result.to_bytes().unwrap())
}
_ => panic!("Unknown method")
}
});
Expand Down Expand Up @@ -585,12 +635,55 @@ mod tests {
pobcoin.burn_and_get_paid(100.into());
assert_eq!(env.balance_of(&alice), current_balance + U512::from(100));

env.print_gas_report()
}

#[test]
fn erc20_events_work() {
let env = odra2::test_env();
let alice = env.get_account(0);
let bob = env.get_account(1);
let charlie = env.get_account(2);

// Deploy the contract as Alice.
let erc20 = Erc20Deployer::init(&env, Some(100.into()));

// Emit some events
erc20.transfer(bob, 10.into());
erc20.approve(bob, 10.into());
erc20.transfer(charlie, 20.into());

// Test events
let event: Transfer = erc20.get_event(0).unwrap();
assert_eq!(event.from, Some(alice));
assert_eq!(event.to, Some(bob));
assert_eq!(event.amount, 10.into());
assert_eq!(event, Transfer {
from: Some(alice),
to: Some(bob),
amount: 10.into()
});

env.print_gas_report()
let event: Approval = erc20.get_event(1).unwrap();
assert_eq!(event, Approval {
owner: alice,
spender: bob,
value: 10.into()
});

let event: Transfer = erc20.get_event(2).unwrap();
assert_eq!(event, Transfer {
from: Some(alice),
to: Some(charlie),
amount: 20.into()
});

// Test negative indices
let event: Transfer = erc20.get_event(-1).unwrap();
assert_eq!(event, Transfer {
from: Some(alice),
to: Some(charlie),
amount: 20.into()
});

// Test out of bounds
assert!(erc20.get_event::<Transfer>(3).is_err());
}
}

0 comments on commit 0f37a70

Please sign in to comment.