-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.fe
More file actions
200 lines (176 loc) · 5.96 KB
/
Copy pathlib.fe
File metadata and controls
200 lines (176 loc) · 5.96 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/// ERC-20 token in Fe.
///
/// One file, no inheritance, exhaustive msg dispatch. Effects declare
/// exactly what each handler can access.
msg Erc20 {
#[selector = sol("totalSupply()")]
TotalSupply -> u256,
#[selector = sol("balanceOf(address)")]
BalanceOf { account: Address } -> u256,
#[selector = sol("allowance(address,address)")]
Allowance { owner: Address, spender: Address } -> u256,
#[selector = sol("transfer(address,uint256)")]
Transfer { to: Address, amount: u256 } -> bool,
#[selector = sol("approve(address,uint256)")]
Approve { spender: Address, amount: u256 } -> bool,
#[selector = sol("transferFrom(address,address,uint256)")]
TransferFrom { from: Address, to: Address, amount: u256 } -> bool,
}
msg Mintable {
#[selector = sol("mint(address,uint256)")]
Mint { to: Address, amount: u256 },
}
struct TokenStore {
total_supply: u256,
balances: StorageMap<Address, u256>,
allowances: StorageMap<(Address, Address), u256>,
}
pub contract Token uses (ctx: Ctx) {
mut store: TokenStore,
init(initial_supply: u256, owner: Address)
uses (mut store)
{
store.total_supply = initial_supply
store.balances.set(key: owner, value: initial_supply)
}
recv Erc20 {
TotalSupply -> u256 uses (store) {
store.total_supply
}
BalanceOf { account } -> u256 uses (store) {
store.balances.get(key: account)
}
Allowance { owner, spender } -> u256 uses (store) {
store.allowances.get(key: (owner, spender))
}
Transfer { to, amount } -> bool uses (mut store, ctx) {
let from = ctx.caller()
let bal = store.balances.get(key: from)
if bal < amount {
return false
}
store.balances.set(key: from, value: bal - amount)
store.balances.set(key: to, value: store.balances.get(key: to) + amount)
true
}
Approve { spender, amount } -> bool uses (mut store, ctx) {
store.allowances.set(key: (ctx.caller(), spender), value: amount)
true
}
TransferFrom { from, to, amount } -> bool
uses (mut store, ctx)
{
let spender = ctx.caller()
let allowed = store.allowances.get(key: (from, spender))
if allowed < amount {
return false
}
let bal = store.balances.get(key: from)
if bal < amount {
return false
}
store.allowances.set(key: (from, spender), value: allowed - amount)
store.balances.set(key: from, value: bal - amount)
store.balances.set(key: to, value: store.balances.get(key: to) + amount)
true
}
}
recv Mintable {
Mint { to, amount } uses (mut store) {
store.total_supply += amount
store.balances.set(key: to, value: store.balances.get(key: to) + amount)
}
}
}
/// A vault that holds tokens on behalf of depositors.
/// Demonstrates cross-contract calls via typed messages.
///
/// In Solidity, calling another contract requires either an interface
/// declaration or raw abi.encodeWithSelector. In Fe, the msg type IS
/// the interface — the same Erc20 msg used by Token is used by Vault
/// to call it. No separate interface file, no ABI mismatch risk.
msg VaultMsg {
#[selector = sol("deposit(address,uint256)")]
Deposit { token: Address, amount: u256 },
#[selector = sol("balance(address)")]
Balance { token: Address } -> u256,
}
struct VaultStore { holdings: StorageMap<Address, u256> }
pub contract Vault uses (ctx: Ctx, call: mut Call) {
mut store: VaultStore,
init() {}
recv VaultMsg {
// Pull tokens from caller into the vault via transferFrom.
// Caller must have approved the vault first.
Deposit { token, amount } uses (mut store, ctx, mut call) {
let ok: bool = call
.call(
addr: token,
gas: 100000,
value: 0,
message: Erc20::TransferFrom { from: ctx.caller(), to: ctx.address(), amount },
)
if ok {
store.holdings.set(key: token, value: store.holdings.get(key: token) + amount)
}
}
// Query how many of a given token the vault holds.
Balance { token } -> u256 uses (store) {
store.holdings.get(key: token)
}
}
}
#[test]
fn test_deploy_and_check_supply() uses (evm: mut Evm) {
let owner = Address { inner: 1 }
let token = evm.create2<Token>(value: 0, args: (1000, owner), salt: 0)
assert!(token.inner != 0)
let supply: u256 = evm.call(
addr: token,
gas: 100000,
value: 0,
message: Erc20::TotalSupply {},
)
assert!(supply == 1000)
let bal: u256 = evm
.call(
addr: token,
gas: 100000,
value: 0,
message: Erc20::BalanceOf { account: owner },
)
assert!(bal == 1000)
}
#[test]
fn test_transfer() uses (evm: mut Evm) {
let alice = Address { inner: 0xA11CE }
let token = evm.create2<Token>(value: 0, args: (0, alice), salt: 1)
evm.call(
addr: token,
gas: 100000,
value: 0,
message: Mintable::Mint { to: alice, amount: 500 },
)
let bal: u256 = evm
.call(
addr: token,
gas: 100000,
value: 0,
message: Erc20::BalanceOf { account: alice },
)
assert!(bal == 500)
}
#[test]
fn test_allowance() uses (evm: mut Evm) {
let owner = Address { inner: 1 }
let spender = Address { inner: 2 }
let token = evm.create2<Token>(value: 0, args: (1000, owner), salt: 2)
let a: u256 = evm
.call(
addr: token,
gas: 100000,
value: 0,
message: Erc20::Allowance { owner, spender },
)
assert!(a == 0)
}