Repository navigation
Expand file tree
/
Copy pathtoken.cell
More file actions
48 lines (40 loc) · 1.6 KB
/
Copy pathtoken.cell
File metadata and controls
48 lines (40 loc) · 1.6 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
module cellscript::fungible_token
const U64_MAX: u64 = 18446744073709551615
resource Token has store, create, consume, replace, burn, relock {
amount: u64,
symbol: [u8; 8],
}
resource MintAuthority has store, create, replace {
token_symbol: [u8; 8],
max_supply: u64,
minted: u64,
}
action mint_with_authority(auth_before: MintAuthority, to: Address, amount: u64) -> (auth_after: MintAuthority, token: Token) {
transition auth_before -> auth_after
verification
require amount <= U64_MAX - auth_before.minted, "mint overflow"
require auth_before.minted + amount <= auth_before.max_supply, "exceeds max supply"
require auth_after.token_symbol == auth_before.token_symbol
require auth_after.max_supply == auth_before.max_supply
require auth_after.minted == auth_before.minted + amount
create token = Token { amount, symbol: auth_before.token_symbol } with_lock(to)
}
action transfer_token(token: Token, to: Address) -> next_token: Token {
verification
consume token
create next_token = Token { amount: token.amount, symbol: token.symbol } with_lock(to)
}
action burn(token: Token) {
verification
require token.amount > 0, "cannot burn zero"
destroy token
}
action merge(a: Token, b: Token, to: Address) -> merged: Token {
verification
require a.symbol == b.symbol, "symbol mismatch"
require b.amount <= U64_MAX - a.amount, "amount overflow"
let total = a.amount + b.amount
consume a
consume b
create merged = Token { amount: total, symbol: a.symbol } with_lock(to)
}