Registry code working well enough to demo (various checks not performed in either...
[robmyers:artworld-ethereum.git] / contract / registry.lll
1 {
2  (def 'next-record 0x10)
3  (def 'RECORD-SIZE 64)
4  ;; Next record position
5  ;; This starts one cell above the maximum value of RipeMD
6  [[next-record]] 0x10000000000000000000000000000000000000000
7
8  (return
9    0x0
10    (lll
11      {
12       ;; Action
13       ;; 0 - first cell in message
14       [action] (calldataload 0)
15       (when (= @action "register")
16         {
17          ;;TODO: Check correct message length
18          ;;TODO: Check digest in range
19          ;; Artwork digest
20          [digest] (calldataload 32)
21          ;; If already registered, don't continue
22          (when @@ @digest
23            (return "Arwork already registered."))
24          ;; Get storage for new record
25          [storage] @@next-record
26          ;; Store digest
27          [[@storage]] @digest
28          ;; Artist account
29          [storage] (+ @storage 1)
30          [[@storage]] (caller)
31          ;; Artist resale percentage
32          [storage] (+ @storage 1)
33          [[@storage]] (calldataload 64)
34          ;; Artist is the current owner
35          [storage] (+ @storage 1)
36          [[@storage]] (caller)
37          ;; Skip purchaser and price
38          [storage] (+ @storage 3)
39          ;; Copy over the url and description
40          ;; 96 is 32 x 3 = 3rd cell in message
41          [source] 96
42          (for [i] 6    (< @i 64)    [i] (+ @i 1)
43               {
44                [[@storage]] (calldataload @source)
45                [storage] (+ @storage 1)
46                [source] (+ @source 32)
47                })
48          ;; Store digest-to-record link
49          [[@digest]] @@next-record
50          ;; Increment next record position
51          [[next-record]] (+ @@next-record RECORD-SIZE)
52          })
53       (when (= @action "offer")
54         {
55          ;;TODO: Check correct message length
56          ;;TODO: Check digest in range
57          ;; Get artwork record storage for digest or stop
58          ;; 32 = second cell in message
59          [storage] @@(calldataload 32)
60          (when (not @storage)
61            (return "Artwork not registered."))
62          ;; If the caller is the owner
63          (when (= @@ (+ @storage 3) (caller))
64            {
65             ;; Offer subject
66             [[(+ @storage 4)]] (calldataload 64)
67             ;; Offer price
68             [[(+ @storage 5)]] (calldataload 96)
69             })
70          })
71       (when (= @action "accept")
72         {
73          ;;TODO: Check correct message length
74          ;;TODO: Check digest in range
75          ;;TODO: Error messages for bad price or buyer
76          ;; Get artwork record storage for digest or stop
77          ;; 32 = second cell in message
78          [storage] @@(calldataload 32)
79          (when (not @storage)
80            (return "Artwork not registered."))
81          [buyer] @@(+ @storage 4)
82          [price] @@(+ @storage 5)
83          ;; If the caller is the buyer and it's the correct payment
84          ;; Or there's no buyer and it's the correct nonzero payment
85          (when (|| (&& (= @buyer (caller))
86                        (= @price (callvalue)))
87                    (&& (= @buyer 0)
88                        (> @price 0)))
89            {
90             ;; For payment
91             ;; Ethereum doesn't allow fractional amounts
92             ;; Warn users about making prices divisible
93             [hundredth] (/ @price 100)
94             [arr] @@(+ @storage 2)
95             ;; Pay artist
96             (call (- (gas) 250) @@(+ @storage 1) (* @hundredth @arr) 0 0 0 0)
97             ;; Pay owner
98             (call (- (gas) 250) @@(+ @storage 3) (* @hundredth (- 100 @arr)) 0 0 0 0)
99             ;; Transfer ownership
100             [[(+ @storage 3)]] (caller)
101             ;; Clear offer subject and price
102             [[(+ @storage 4)]] 0
103             [[(+ @storage 5)]] 0
104             })
105          })
106       }
107      0x0))}