-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathERC20.hs
More file actions
97 lines (76 loc) · 3.29 KB
/
ERC20.hs
File metadata and controls
97 lines (76 loc) · 3.29 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
module ERC20 where
import Control.LinearlyVersionedMonad.LVM qualified as LVM
import Prelude.YulDSL
-- | Ethereum contract is a Yul Object in Yolc.
object = mkYulObject "ERC20" yulNoop
[ staticFn "balanceOf" balanceOf
, staticFn "allowance" allowance
, omniFn "transfer" transfer
, omniFn "mint" mint
]
-- | Storage map of account balances
balances :: SMap (ADDR -> U256)
balances = makeSMap "Yolc.Demo.ERC20.Storage.AccountBalances"
-- | ERC20 balance of the account.
balanceOf :: StaticFn (ADDR -> U256)
balanceOf = $lfn $ ylvm'pv \owner -> sgetM $ balances #-> owner
-- | Storage map of allowances
allowances :: SMap (ADDR {- Owner -} -> ADDR {- spender -} -> U256)
allowances = makeSMap "Yolc.Demo.ERC20.Storage.Allowances"
-- | ERC20 allowance function.
allowance :: StaticFn (ADDR -> ADDR -> U256)
allowance = $lfn $ ylvm'pv
\owner spender -> sgetM $ allowances #-> (owner, spender)
-- | ERC20 transfer function.
transfer :: OmniFn (ADDR -> U256 -> BOOL)
transfer = $lfn $ ylvm'pv
\to amount -> LVM.do
Ur from <- ycaller
-- ✅ CORRECT CODE:
Ur senderBalance <- ycall balanceOf (ver from)
Ur newSenderBalance <- (ver amount, senderBalance) `yrpurelamN_1`
\amount' senderBalance' ->
if senderBalance' >= amount' then senderBalance' - amount'
else yulRevert
balances #-> from <<:= newSenderBalance
Ur receiverBalance <- ycall balanceOf (ver to)
Ur newReceiverBalance <- yrpurelamN_1 (ver amount, receiverBalance) \x y -> x + y
balances #-> to <<:= newReceiverBalance
-- ⛔ INCORRECT CODE, CANNOT PASS COMPILATION:
-- Ur senderBalance <- ycall balanceOf (ver from)
-- Ur receiverBalance <- ycall balanceOf (ver to)
-- -- calculate new balances
-- Ur (newSenderBalance, newReceiverBalance) <- yrpurelamN
-- (ver amount, senderBalance, receiverBalance)
-- \amount' senderBalance' receiverBalance' ->
-- be (senderBalance' - amount', receiverBalance' + amount')
-- -- WARNING: THIS IS WRONG
-- -- Have you found the issue?
-- balances #-> from <<:= newSenderBalance
-- balances #-> to <<:= newReceiverBalance
-- always return true as a silly urban-legendary ERC20 convention
yembed true
-- | Mint new tokens
mint :: OmniFn (ADDR -> U256 -> ())
mint = $lfn $ ylvm'pv
-- [solidity] function mint(address to, uint256 amount) returns ()
\to amount -> LVM.do
-- [solidity] uint256 balanceBefore = balanceOf(to)
Ur balanceBefore <- ycall balanceOf (ver to)
-- calculate new balance
-- [solidity] uint256 newAmount = balanceBefore + amount
Ur newAmount <- (balanceBefore, ver amount) `yrpurelamN_1` \x y -> x + y
--
-- ⚠️ NOTE: swap the following code blocks will not compile, because there can be reentrance!
-- update balance
-- [solidity] balances[to] = newAmount
balances #-> to <<:= (newAmount :: _) :: _
-- call **untrusted** external contract onTokenMinted
-- [solidity] TokenMintHook(to).onTokenMinted(to, amount)
ycall (to @-> onTokenMinted) (ver to) (ver amount)
-- return () always, for demo purpose
yembed ()
--
-- TODO: this should/could be generated from a solidity interface definition file:
-- | A hook to the token minted event for the mint receiver.
onTokenMinted = externalOmniFn @(ADDR -> U256 -> ()) "onTokenMinted"