forked from shentufoundation/testnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.ds
55 lines (42 loc) · 1.52 KB
/
token.ds
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
(* Loosely based on the "sample fixed supply token contract" from https://theethereum.wiki/w/index.php/ERC20_Token_Standard#Sample_Fixed_Supply_Token_Contract *)
external [[except DataTypeOps]] with "Require token.Invariant.\n"
(* Todo: we should support addresses (160-bit integers) as a builtin type. *)
type addr := uint
const _totalSupply = 100000
object signature ERC20Interface = {
initialize : unit -> uint;
const totalSupply : unit -> int;
const balanceOf : addr -> int;
transfer : addr * int -> bool
}
object FixedSupplyToken : ERC20Interface {
let balances : mapping[addr] int := mapping_init
(* This is just a hack, we should fix it so that we can actually compile constructors correctly. *)
let initialize () =
balances[msg_sender] := 100000;
0u0
let totalSupply () =
let bal0 = balances[0u0] in
_totalSupply - bal0
let balanceOf tokenOwner =
let bal = balances[tokenOwner] in
bal
let transfer(toA, tokens) =
let fromA = msg_sender in
let from_bal = balances[fromA] in
let to_bal = balances[toA] in
if (fromA <> toA /\ from_bal >= tokens)
then begin
balances[fromA] := from_bal-tokens;
balances[toA] := to_bal+tokens;
true
end else
false
(* Todo: the approve() and transferFrom() methods. (Requires hashmappings with two keys.) *)
}
layer signature FIXEDSUPPLYTOKENSig = {
fixedSupplyToken : ERC20Interface
}
layer FIXEDSUPPLYTOKEN : [{}] FIXEDSUPPLYTOKENSig = {
fixedSupplyToken = FixedSupplyToken
} assert "Invariant.inv"