forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoin.fe
More file actions
145 lines (125 loc) · 3.3 KB
/
Copy pathcoin.fe
File metadata and controls
145 lines (125 loc) · 3.3 KB
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
msg CoinMsg {
#[selector = 0xab7ccc1c]
Credit { user: u256, amount: u256 } -> u256,
#[selector = 0x0cf79e0a]
Transfer { from: u256, amount: u256 } -> u256,
#[selector = 0x6c65aae5]
BalanceOf { user: u256 } -> u256,
#[selector = 0x3940e9ee]
TotalSupply -> u256,
}
struct CoinStore {
alice: u256,
bob: u256,
total_supply: u256,
}
pub contract Coin {
mut store: CoinStore
init() uses (mut store) {
store.alice = 0
store.bob = 0
store.total_supply = 0
}
recv CoinMsg {
Credit { user, amount } -> u256 uses (mut store) {
if user == 0 {
store.alice += amount
store.total_supply += amount
return store.alice
} else {
store.bob += amount
store.total_supply += amount
return store.bob
}
}
Transfer { from, amount } -> u256 uses (mut store) {
if from == 0 {
if store.alice < amount {
return 1
}
store.alice -= amount
store.bob += amount
return 0
} else {
if store.bob < amount {
return 1
}
store.bob -= amount
store.alice += amount
return 0
}
}
BalanceOf { user } -> u256 uses (store) {
if user == 0 {
return store.alice
} else {
return store.bob
}
}
TotalSupply -> u256 uses (store) {
return store.total_supply
}
}
}
#[test]
fn test_coin() uses (evm: mut Evm) {
let contract_addr = evm.create2<Coin>(value: 0, args: (), salt: 0)
assert!(contract_addr.inner != 0)
let alice: u256 = 0
let bob: u256 = 1
// Credit Alice 10
let res: u256 = evm.call(
addr: contract_addr,
gas: 100000,
value: 0,
message: CoinMsg::Credit { user: alice, amount: 10 }
)
assert!(res == 10)
// Credit Bob 5
let res: u256 = evm.call(
addr: contract_addr,
gas: 100000,
value: 0,
message: CoinMsg::Credit { user: bob, amount: 5 }
)
assert!(res == 5)
// Transfer 3 from Alice to Bob
let res: u256 = evm.call(
addr: contract_addr,
gas: 100000,
value: 0,
message: CoinMsg::Transfer { from: alice, amount: 3 }
)
assert!(res == 0)
// Check balances
let bal_alice: u256 = evm.call(
addr: contract_addr,
gas: 100000,
value: 0,
message: CoinMsg::BalanceOf { user: alice }
)
assert!(bal_alice == 7)
let bal_bob: u256 = evm.call(
addr: contract_addr,
gas: 100000,
value: 0,
message: CoinMsg::BalanceOf { user: bob }
)
assert!(bal_bob == 8)
// Transfer too much from Bob
let res: u256 = evm.call(
addr: contract_addr,
gas: 100000,
value: 0,
message: CoinMsg::Transfer { from: bob, amount: 20 }
)
assert!(res == 1)
// Check total supply
let total: u256 = evm.call(
addr: contract_addr,
gas: 100000,
value: 0,
message: CoinMsg::TotalSupply {}
)
assert!(total == 15)
}