2 | | - | {-# SCRIPT_TYPE ACCOUNT #-} |
---|
3 | | - | {-# CONTENT_TYPE DAPP #-} |
---|
4 | | - | let NONE = "none" |
---|
5 | | - | |
---|
6 | | - | func getNumberByKey (key) = { |
---|
7 | | - | let num = match getInteger(this, key) { |
---|
8 | | - | case a: Int => |
---|
9 | | - | a |
---|
10 | | - | case _ => |
---|
11 | | - | 0 |
---|
12 | | - | } |
---|
13 | | - | num |
---|
14 | | - | } |
---|
15 | | - | |
---|
16 | | - | |
---|
17 | | - | func getStrByKey (key) = { |
---|
18 | | - | let str = match getString(this, key) { |
---|
19 | | - | case a: String => |
---|
20 | | - | a |
---|
21 | | - | case _ => |
---|
22 | | - | NONE |
---|
23 | | - | } |
---|
24 | | - | str |
---|
25 | | - | } |
---|
26 | | - | |
---|
27 | | - | |
---|
28 | | - | func getKeyItemPrice (item) = (item + "_price") |
---|
29 | | - | |
---|
30 | | - | |
---|
31 | | - | func getValueItemPrice (item) = getNumberByKey(getKeyItemPrice(item)) |
---|
32 | | - | |
---|
33 | | - | |
---|
34 | | - | func getKeyUserItemCounter (user,item) = (((item + "_") + user) + "_cnt") |
---|
35 | | - | |
---|
36 | | - | |
---|
37 | | - | func getValueUserItemCounter (user,item) = getNumberByKey(getKeyUserItemCounter(user, item)) |
---|
38 | | - | |
---|
39 | | - | |
---|
40 | | - | func getKeyItem (supplier,title) = ("item_" + toBase58String(sha256(toBytes((supplier + title))))) |
---|
41 | | - | |
---|
42 | | - | |
---|
43 | | - | func getKeyItemData (item) = (item + "_data") |
---|
44 | | - | |
---|
45 | | - | |
---|
46 | | - | func getKeyItemSupplier (item) = (item + "_owner") |
---|
47 | | - | |
---|
48 | | - | |
---|
49 | | - | func getValueItemSupplier (item) = getStrByKey(getKeyItemSupplier(item)) |
---|
50 | | - | |
---|
51 | | - | |
---|
52 | | - | func getKeyBalanceSupplier (account) = (account + "_balance") |
---|
53 | | - | |
---|
54 | | - | |
---|
55 | | - | func getValueBalanceSupplier (account) = getNumberByKey(getKeyBalanceSupplier(account)) |
---|
56 | | - | |
---|
57 | | - | |
---|
58 | | - | @Callable(i) |
---|
59 | | - | func purchase (B) = { |
---|
60 | | - | let pmt = extract(i.payment) |
---|
61 | | - | if (isDefined(pmt.assetId)) |
---|
62 | | - | then throw("can use WAVES only at the moment") |
---|
63 | | - | else { |
---|
64 | | - | let customerAddress = toBase58String(i.caller.bytes) |
---|
65 | | - | let price = match getInteger(this, (("item_" + B) + "_coupon_price")) { |
---|
66 | | - | case a: Int => |
---|
67 | | - | a |
---|
68 | | - | case _ => |
---|
69 | | - | 0 |
---|
70 | | - | } |
---|
71 | | - | if ((price > pmt.amount)) |
---|
72 | | - | then throw("purchase amount cannot be less than item price ") |
---|
73 | | - | else if ((pmt.amount > price)) |
---|
74 | | - | then throw("purchase amount cannot be higher than item price") |
---|
75 | | - | else WriteSet([DataEntry(((("status:purchase_item_" + B) + "_customer_") + customerAddress), "confirmed"), DataEntry(((("price:purchase_item_" + B) + "_customer_") + customerAddress), price)]) |
---|
76 | | - | } |
---|
77 | | - | } |
---|
78 | | - | |
---|
79 | | - | |
---|
80 | | - | |
---|
81 | | - | @Callable(i) |
---|
82 | | - | func addItem (title,price,data) = { |
---|
83 | | - | let supplierAddress = toBase58String(i.caller.bytes) |
---|
84 | | - | let item = getKeyItem(supplierAddress, title) |
---|
85 | | - | if ((0 >= price)) |
---|
86 | | - | then throw("purchase amount cannot be less then item price") |
---|
87 | | - | else if ((getValueItemSupplier(item) != NONE)) |
---|
88 | | - | then throw("an item is already exist") |
---|
89 | | - | else WriteSet([DataEntry(getKeyItemSupplier(item), supplierAddress), DataEntry(getKeyItemPrice(item), price), DataEntry(getKeyItemData(item), data)]) |
---|
90 | | - | } |
---|
91 | | - | |
---|
92 | | - | |
---|
| 2 | + | {-# CONTENT_TYPE EXPRESSION #-} |
---|
| 3 | + | true |
---|