Repository navigation
Expand file tree
/
Copy pathSpecSyntax.lean
More file actions
82 lines (61 loc) · 2.97 KB
/
Copy pathSpecSyntax.lean
File metadata and controls
82 lines (61 loc) · 2.97 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
import Examples.ERC20.Spec
import Stele.Notation
/-!
# ERC20 — the Solidity source expressed in Stele
The constructor and mutating functions follow `ERC20.sol` in declaration and statement order.
The public storage getters are written explicitly at the end, using the existing frontend.
Non-payable guards are implicit. Explicit `as uint256` checks appear only where the preceding
guards do not already establish the result's range.
Lean keywords `from` and `to` need guillemet escapes. Event declarations and literal require
messages are accepted, but logs and revert payloads are outside the refinement relation.
The checked-in creation wrapper stores the initial balances without emitting a log.
-/
open Stele Stele.Notation
namespace ERC20.Syntax
def contractSyntax : ContractDecl := solidity% contract ERC20 {
mapping(address => uint256) balanceOf; -- slot 0
mapping(address => mapping(address => uint256)) allowance; -- slot 1
uint256 totalSupply; -- slot 2
event Transfer(address indexed «from», address indexed «to», uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
constructor(uint256 initialSupply) {
balanceOf[msg.sender] = initialSupply;
totalSupply = initialSupply;
emit Transfer(address(0), msg.sender, initialSupply);
}
function transfer(address «to», uint256 value) external returns (bool) {
require(balanceOf[msg.sender] >= value, "ERC20: insufficient balance");
balanceOf[msg.sender] -= value;
balanceOf[«to»] = (balanceOf[«to»] + value) as uint256;
emit Transfer(msg.sender, «to», value);
return true;
}
function approve(address spender, uint256 value) external returns (bool) {
allowance[msg.sender][spender] = value;
emit Approval(msg.sender, spender, value);
return true;
}
function transferFrom(address «from», address «to», uint256 value) external returns (bool) {
uint256 currentAllowance = allowance[«from»][msg.sender];
require(currentAllowance >= value, "ERC20: insufficient allowance");
require(balanceOf[«from»] >= value, "ERC20: insufficient balance");
allowance[«from»][msg.sender] = currentAllowance - value;
balanceOf[«from»] = (balanceOf[«from»] - value) as uint256;
balanceOf[«to»] = (balanceOf[«to»] + value) as uint256;
emit Transfer(«from», «to», value);
return true;
}
-- Getters generated by Solidity for the three public storage declarations.
function balanceOf(address owner) external view returns (uint256) {
return balanceOf[owner];
}
function allowance(address owner, address spender) external view returns (uint256) {
return allowance[owner][spender];
}
function totalSupply() external view returns (uint256) {
return totalSupply;
}
}
/-- The macro-generated contract is *definitionally* the hand-written one. -/
theorem contractSyntax_eq : contractSyntax = ERC20.erc20Contract := by rfl
end ERC20.Syntax