forked from ashen1dev/SampleSmartContract
-
Notifications
You must be signed in to change notification settings - Fork 0
/
def.lua
100 lines (86 loc) · 2.68 KB
/
def.lua
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
98
99
100
-- function definition
-- simple payment system
local system = require("system");
function genPoint(key, value) --called by smart oracle
system.setItem(key, value)
end
function lookupPoint(msg)
system.print("lookuppoint====")
local res = system.getItem(msg)
return res
end
function bulkgenPoint(msg, point)
for token in string.gmatch(msg, "[^%s]+") do
system.setItem(token, point)
end
end
function registStore(skey, bkey)
local ss = "bankof_" .. skey
system.setItem(ss, bkey)
end
function getBank(skey)
local ss = "bankof_" .. skey
local res = system.getItem(ss)
return res
end
function registBCpoint(bcpoint)
local bc = "BC_POINT"
system.setItem(bc, bcpoint)
end
function getBCpoint()
return system.getItem("BC_POINT")
end
function registTHpoint(thpoint)
local th = "TH_POINT"
system.setItem(th, thpoint)
end
function getTHpoint()
return system.getItem("TH_POINT")
end
function sendPointCustomerTotalFast(ckey, skeylist, cspoint)
for token in string.gmatch(skeylist, "[^%s]+") do
sendPointCustomerTotal(ckey, token, cspoint)
end
end
-- payment function
-- customer -> store
-- if customer has fewer money then cspoint, gets money from a bank
-- if store has enough money then sends money to a bank
function sendPointCustomerTotal(ckey, skey, cspoint)
local cpoint = system.getItem(ckey)
local spoint = system.getItem(skey)
local bcpoint = getBCpoint()
local tpoint = getTHpoint()
if system.getConfirmed() == false then
return;
end
if (cpoint < cspoint) then
local bkey = getBank(skey)
local bpoint = system.getItem(bkey)
cpoint = cpoint + bcpoint
bpoint = bpoint - bcpoint
system.setItem(ckey, cpoint)
system.setItem(bkey, bpoint)
system.print("B to C : " .. bkey .. " to " .. ckey)
end
if (spoint+cspoint > tpoint) then
local bkey = getBank(skey)
local bpoint = system.getItem(bkey)
system.setItem(ckey, cpoint - cspoint)
system.setItem(skey, 0)
system.setItem(bkey, bpoint + spoint + cspoint)
system.print("S to B : " .. skey .. " to " .. bkey)
else
system.setItem(ckey, cpoint - cspoint)
system.setItem(skey, spoint + cspoint)
end
end
function recall(ckey, skey)
local cpoint = system.getItem(ckey)
local spoint = system.getItem(skey)
local bkey = getBank(skey)
local bpoint = system.getItem(bkey)
system.setItem(ckey, 0)
system.setItem(skey, 0)
system.setItem(bkey, bpoint + cpoint + spoint)
end