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 + "_data") |
---|
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 | | - | |
---|
| 4 | + | let wEUR = base58'6V9wPqB3gMTqa829XVkxjpRnxrMGZMhxR441JC2kmfh3' |
---|
59 | | - | func addItem (title,price,data) = { |
---|
60 | | - | let supplierAddress = toBase58String(i.caller.bytes) |
---|
61 | | - | let item = getKeyItem(supplierAddress, title) |
---|
62 | | - | if ((0 >= price)) |
---|
63 | | - | then throw("purchase amount cannot be less than item price") |
---|
64 | | - | else if ((getValueItemSupplier(item) != NONE)) |
---|
65 | | - | then throw("an item is already exist") |
---|
66 | | - | else [StringEntry(getKeyItemSupplier(item), supplierAddress), IntegerEntry(getKeyItemPrice(item), price), StringEntry(getKeyItemData(item), data)] |
---|
67 | | - | } |
---|
68 | | - | |
---|
69 | | - | |
---|
70 | | - | |
---|
71 | | - | @Callable(i) |
---|
72 | | - | func purchase (item) = { |
---|
| 7 | + | func deposit () = { |
---|
77 | | - | let userAddress = toBase58String(i.caller.bytes) |
---|
78 | | - | let price = getValueItemPrice(item) |
---|
79 | | - | let supplierAddres = getValueItemSupplier(item) |
---|
80 | | - | if ((price > pmt.amount)) |
---|
81 | | - | then throw("purchase amunt cannot be less than item price") |
---|
82 | | - | else if ((pmt.amount > price)) |
---|
83 | | - | then throw("purchase amount cannot be higher than item price") |
---|
84 | | - | else if ((supplierAddres == NONE)) |
---|
85 | | - | then throw("Supplier address doesn't exist") |
---|
86 | | - | else [IntegerEntry(getKeyUserItemCounter(userAddress, item), (getValueUserItemCounter(userAddress, item) + 1)), IntegerEntry(getKeyBalanceSupplier(supplierAddres), (getValueBalanceSupplier(supplierAddres) + pmt.amount))] |
---|
| 12 | + | let currentKey = toBase58String(i.caller.bytes) |
---|
| 13 | + | let currentAmount = match getInteger(this, currentKey) { |
---|
| 14 | + | case a: Int => |
---|
| 15 | + | a |
---|
| 16 | + | case _ => |
---|
| 17 | + | 0 |
---|
| 18 | + | } |
---|
| 19 | + | let newAmount = (currentAmount + pmt.amount) |
---|
| 20 | + | [IntegerEntry(currentKey, newAmount)] |
---|
93 | | - | func withdraw () = { |
---|
94 | | - | let supplierAddress = toBase58String(i.caller.bytes) |
---|
95 | | - | let balance = getValueBalanceSupplier(supplierAddress) |
---|
96 | | - | if ((0 >= balance)) |
---|
97 | | - | then throw("za mało środków") |
---|
98 | | - | else [IntegerEntry(getKeyBalanceSupplier(supplierAddress), 0), ScriptTransfer(addressFromStringValue(supplierAddress), balance, unit)] |
---|
| 27 | + | func witdraw (amount) = { |
---|
| 28 | + | let currentKey = toBase58String(i.caller.bytes) |
---|
| 29 | + | let currentAmount = match getInteger(this, currentKey) { |
---|
| 30 | + | case a: Int => |
---|
| 31 | + | a |
---|
| 32 | + | case _ => |
---|
| 33 | + | 0 |
---|
| 34 | + | } |
---|
| 35 | + | let newAmount = (currentAmount - amount) |
---|
| 36 | + | if ((0 > amount)) |
---|
| 37 | + | then throw("can't withdraw negatives amount") |
---|
| 38 | + | else if ((0 > newAmount)) |
---|
| 39 | + | then throw("Not enough balance") |
---|
| 40 | + | else [IntegerEntry(currentKey, newAmount), ScriptTransfer(i.caller, amount, wEUR)] |
---|