This repository has been archived by the owner on May 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.rs
89 lines (72 loc) · 2.45 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
extern crate ethabi;
#[macro_use]
extern crate ethabi_derive;
#[macro_use]
extern crate ethabi_contract;
extern crate ethereum_types as types;
extern crate rustc_hex;
extern crate solaris;
fn main() {
solaris::main(include_bytes!("../res/BadgeReg_sol_BadgeReg.abi"));
}
use_contract!(badgereg, "BadgeReg", "res/BadgeReg_sol_BadgeReg.abi");
#[cfg(test)]
fn setup() -> (solaris::evm::Evm, badgereg::BadgeReg) {
let contract = badgereg::BadgeReg::default();
let code = include_str!("../res/BadgeReg_sol_BadgeReg.bin");
let mut evm = solaris::evm();
let owner = 3.into();
let _address = evm.with_sender(owner).deploy(&code.from_hex().unwrap());
(evm, contract)
}
#[cfg(test)]
use rustc_hex::FromHex;
#[cfg(test)]
use solaris::unit;
#[cfg(test)]
use solaris::sol;
#[test]
fn badge_reg_test_fee() {
let (mut evm, contract) = setup();
let reg = contract.functions();
// Initial fee is 1 ETH
assert_eq!(unit::convert(reg.fee().call(&mut evm).unwrap()), unit::ether(1));
// The owner should be able to set the fee
reg.set_fee().transact(unit::gwei(10), &mut evm).unwrap();
// Fee should be updated
assert_eq!(unit::convert(reg.fee().call(&mut evm).unwrap()), unit::gwei(10));
// Other address should not be allowed to change the fee
evm.with_sender(10.into());
reg.set_fee().transact(unit::gwei(10), &mut evm).unwrap_err();
}
#[test]
fn anyone_should_be_able_to_register_a_badge() {
let (evm, contract) = setup();
let reg = contract.functions();
evm.run(move |mut evm| {
// Register new entry
reg.register().transact(sol::address(10), sol::bytes32("test"),
evm
.with_value(unit::ether(2))
.with_sender(5.into())
.ensure_funds()
)?;
// TODO [ToDr] The API here is crap, we need to work on sth better.
// Check that the event has been fired.
assert_eq!(
evm.logs(badgereg::events::Registered::default().create_filter(
sol::bytes32("test"),
ethabi::Topic::Any,
)).len(),
1
);
// TODO [ToDr] Perhaps `with_` should not be persistent?
evm.with_value(0.into());
// Test that it was registered correctly
assert_eq!(
reg.from_name().call(sol::bytes32("test"), &mut evm)?,
(sol::raw::uint(0), sol::raw::address(10), sol::raw::address(5), )
);
Ok(())
})
}