Smart Contract
VegeSeller
A.b576a3926d239682.VegeSeller
1import FlowToken from 0x1654653399040a61
2import FungibleToken from 0xf233dcee88fe0abe
3
4access(all) contract VegeSeller {
5
6 access(self) let FlowTokenVault: Capability<&{FungibleToken.Receiver}>
7 access(self) let info: Info
8
9 /* 構造体 */
10 access(all) struct Info {
11
12 access(contract) var data: {String: VegeData}
13
14 /* setter */
15 access(contract) fun set(id: String, add: [String], addAmount: [UInt8], remove: [String]) {
16 if let saved = self.data[id] {
17 for index, name in add {
18 saved.set(category: name, amount: addAmount[index])
19 }
20 for name in remove {
21 saved.unset(category: name)
22 }
23 // Save
24 self.data[id] = saved
25 } else {
26 var data = VegeData()
27 for index, name in add {
28 data.set(category: name, amount: addAmount[index])
29 }
30 for name in remove {
31 data.unset(category: name)
32 }
33 self.data[id] = data
34 }
35 }
36
37 access(contract) fun buy(id: String, buy: [String]) {
38 if let saved = self.data[id] {
39 for name in buy {
40 if (saved.data[name] == nil) {
41 panic("The stock is empty.")
42 }
43 saved.set(category: name, amount: saved.data[name]! - 1)
44 }
45 // Save
46 self.data[id] = saved
47 } else {
48 panic("No data.")
49 }
50 }
51
52 init() {
53 self.data = {}
54 }
55 }
56
57 /* Info内の構造体 */
58 access(all) struct VegeData {
59
60 access(contract) var data: {String: UInt8}
61
62 /* setter */
63 access(contract) fun set(category: String, amount: UInt8) {
64 self.data[category] = amount
65 }
66
67 access(contract) fun unset(category: String) {
68 self.data[category] = nil
69 }
70
71 init() {
72 self.data = {}
73 }
74 }
75
76 // GET
77 access(all) fun getVegeSellerInfo(id: String): VegeData? {
78 return self.info.data[id]
79 }
80 // PUT
81 access(all) fun setVegeSellerInfo(id: String, add: [String], addAmount: [UInt8], remove: [String]) {
82 self.info.set(id: id, add: add, addAmount: addAmount, remove: remove)
83 }
84 // BUY
85 access(all) fun buyVege(id: String, amount: @FlowToken.Vault, buy: [String]) {
86 self.info.buy(id: id, buy: buy)
87 self.FlowTokenVault.borrow()!.deposit(from: <- amount)
88 }
89
90 init() {
91 self.info = Info()
92 self.FlowTokenVault = self.account.capabilities.get<&{FungibleToken.Receiver}>(/public/flowTokenReceiver)
93 }
94}
95