This repository was archived by the owner on Feb 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathlib.rs
154 lines (136 loc) · 5.79 KB
/
lib.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#![no_std]
#![allow(non_snake_case)]
#![feature(proc_macro_hygiene)]
extern crate pwasm_std;
extern crate pwasm_ethereum;
extern crate pwasm_abi;
extern crate pwasm_abi_derive;
pub mod token {
use pwasm_ethereum;
use pwasm_abi::types::*;
// eth_abi is a procedural macros https://doc.rust-lang.org/book/first-edition/procedural-macros.html
use pwasm_abi_derive::eth_abi;
lazy_static::lazy_static! {
static ref TOTAL_SUPPLY_KEY: H256 =
H256::from(
[2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
);
static ref OWNER_KEY: H256 =
H256::from(
[3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
);
}
#[eth_abi(TokenEndpoint, TokenClient)]
pub trait TokenInterface {
/// The constructor
fn constructor(&mut self, _total_supply: U256);
/// Total amount of tokens
#[constant]
fn totalSupply(&mut self) -> U256;
/// What is the balance of a particular account?
#[constant]
fn balanceOf(&mut self, _owner: Address) -> U256;
/// Transfer the balance from owner's account to another account
fn transfer(&mut self, _to: Address, _amount: U256) -> bool;
/// Event declaration
#[event]
fn Transfer(&mut self, indexed_from: Address, indexed_to: Address, _value: U256);
}
pub struct TokenContract;
impl TokenInterface for TokenContract {
fn constructor(&mut self, total_supply: U256) {
let sender = pwasm_ethereum::sender();
// Set up the total supply for the token
pwasm_ethereum::write(&TOTAL_SUPPLY_KEY, &total_supply.into());
// Give all tokens to the contract owner
pwasm_ethereum::write(&balance_key(&sender), &total_supply.into());
// Set the contract owner
pwasm_ethereum::write(&OWNER_KEY, &H256::from(sender).into());
}
fn totalSupply(&mut self) -> U256 {
U256::from_big_endian(&pwasm_ethereum::read(&TOTAL_SUPPLY_KEY))
}
fn balanceOf(&mut self, owner: Address) -> U256 {
read_balance_of(&owner)
}
fn transfer(&mut self, to: Address, amount: U256) -> bool {
let sender = pwasm_ethereum::sender();
let senderBalance = read_balance_of(&sender);
let recipientBalance = read_balance_of(&to);
if amount == 0.into() || senderBalance < amount || to == sender {
false
} else {
let new_sender_balance = senderBalance - amount;
let new_recipient_balance = recipientBalance + amount;
pwasm_ethereum::write(&balance_key(&sender), &new_sender_balance.into());
pwasm_ethereum::write(&balance_key(&to), &new_recipient_balance.into());
self.Transfer(sender, to, amount);
true
}
}
}
// Reads balance by address
fn read_balance_of(owner: &Address) -> U256 {
U256::from_big_endian(&pwasm_ethereum::read(&balance_key(owner)))
}
// Generates a balance key for some address.
// Used to map balances with their owners.
fn balance_key(address: &Address) -> H256 {
let mut key = H256::from(*address);
key.as_bytes_mut()[0] = 1; // just a naive "namespace";
key
}
}
// Declares the dispatch and dispatch_ctor methods
use pwasm_abi::eth::EndpointInterface;
#[no_mangle]
pub fn call() {
let mut endpoint = token::TokenEndpoint::new(token::TokenContract{});
// Read http://solidity.readthedocs.io/en/develop/abi-spec.html#formal-specification-of-the-encoding for details
pwasm_ethereum::ret(&endpoint.dispatch(&pwasm_ethereum::input()));
}
#[no_mangle]
pub fn deploy() {
let mut endpoint = token::TokenEndpoint::new(token::TokenContract{});
endpoint.dispatch_ctor(&pwasm_ethereum::input());
}
#[cfg(test)]
#[allow(non_snake_case)]
mod tests {
extern crate pwasm_test;
extern crate std;
use super::*;
use core::str::FromStr;
use pwasm_abi::types::*;
use self::pwasm_test::{ext_reset, ext_get};
use token::TokenInterface;
#[test]
fn should_succeed_transfering_1000_from_owner_to_another_address() {
let mut contract = token::TokenContract{};
let owner_address = Address::from_str("ea674fdde714fd979de3edf0f56aa9716b898ec8").unwrap();
let sam_address = Address::from_str("db6fd484cfa46eeeb73c71edee823e4812f9e2e1").unwrap();
// Here we're creating an External context using ExternalBuilder and set the `sender` to the `owner_address`
// so `pwasm_ethereum::sender()` in TokenInterface::constructor() will return that `owner_address`
ext_reset(|e| e.sender(owner_address.clone()));
let total_supply = 10000.into();
contract.constructor(total_supply);
assert_eq!(contract.balanceOf(owner_address), total_supply);
assert_eq!(contract.transfer(sam_address, 1000.into()), true);
assert_eq!(contract.balanceOf(owner_address), 9000.into());
assert_eq!(contract.balanceOf(sam_address), 1000.into());
// 1 log entry should be created
assert_eq!(ext_get().logs().len(), 1);
}
#[test]
fn should_not_transfer_to_self() {
let mut contract = token::TokenContract{};
let owner_address = Address::from_str("ea674fdde714fd979de3edf0f56aa9716b898ec8").unwrap();
ext_reset(|e| e.sender(owner_address.clone()));
let total_supply = 10000.into();
contract.constructor(total_supply);
assert_eq!(contract.balanceOf(owner_address), total_supply);
assert_eq!(contract.transfer(owner_address, 1000.into()), false);
assert_eq!(contract.balanceOf(owner_address), 10000.into());
assert_eq!(ext_get().logs().len(), 0);
}
}