wao 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,238 +0,0 @@
1
- local bint = require('.bint')(256)
2
- local json = require('json')
3
- local base64 = require(".base64")
4
- local ao = require("ao")
5
-
6
- if Name ~= '<NAME>' then Name = '<NAME>' end
7
- if Creator ~= '<CREATOR>' then Creator = '<CREATOR>' end
8
- if Ticker ~= '<TICKER>' then Ticker = '<TICKER>' end
9
- if Denomination ~= '<DENOMINATION>' then Denomination = '<DENOMINATION>' end
10
- if not Balances then Balances = { ['<CREATOR>'] = '<BALANCE>' } end
11
-
12
- Transferable = true
13
-
14
- local function checkValidAddress(address)
15
- if not address or type(address) ~= 'string' then
16
- return false
17
- end
18
-
19
- return string.match(address, "^[%w%-_]+$") ~= nil and #address == 43
20
- end
21
-
22
- local function checkValidAmount(data)
23
- return (math.type(tonumber(data)) == 'integer' or math.type(tonumber(data)) == 'float') and bint(data) > 0
24
- end
25
-
26
- local function decodeMessageData(data)
27
- local status, decodedData = pcall(json.decode, data)
28
-
29
- if not status or type(decodedData) ~= 'table' then
30
- return false, nil
31
- end
32
-
33
- return true, decodedData
34
- end
35
-
36
- -- Read process state
37
- Handlers.add('Info', Handlers.utils.hasMatchingTag('Action', 'Info'), function(msg)
38
- ao.send({
39
- Target = msg.From,
40
- Action = 'Read-Success',
41
- Data = json.encode({
42
- Name = Name,
43
- Ticker = Ticker,
44
- Denomination = Denomination,
45
- Balances = Balances,
46
- Transferable = Transferable,
47
- })
48
- })
49
- end)
50
-
51
- -- Transfer balance to recipient (Data - { Recipient, Quantity })
52
- Handlers.add('Transfer', Handlers.utils.hasMatchingTag('Action', 'Transfer'), function(msg)
53
- if not Transferable then
54
- ao.send({ Target = msg.From, Action = 'Validation-Error', Tags = { Status = 'Error', Message = 'Transfers are not allowed' } })
55
- return
56
- end
57
-
58
- local data = {
59
- Recipient = msg.Tags.Recipient,
60
- Quantity = msg.Tags.Quantity
61
- }
62
-
63
- if checkValidAddress(data.Recipient) and checkValidAmount(data.Quantity) then
64
- -- Transfer is valid, calculate balances
65
- if not Balances[data.Recipient] then
66
- Balances[data.Recipient] = '0'
67
- end
68
-
69
- Balances[msg.From] = tostring(bint(Balances[msg.From]) - bint(data.Quantity))
70
- Balances[data.Recipient] = tostring(bint(Balances[data.Recipient]) + bint(data.Quantity))
71
-
72
- -- If new balance zeroes out then remove it from the table
73
- if bint(Balances[msg.From]) <= 0 then
74
- Balances[msg.From] = nil
75
- end
76
- if bint(Balances[data.Recipient]) <= 0 then
77
- Balances[data.Recipient] = nil
78
- end
79
-
80
- local debitNoticeTags = {
81
- Status = 'Success',
82
- Message = 'Balance transferred, debit notice issued',
83
- Recipient = msg.Tags.Recipient,
84
- Quantity = msg.Tags.Quantity,
85
- }
86
-
87
- local creditNoticeTags = {
88
- Status = 'Success',
89
- Message = 'Balance transferred, credit notice issued',
90
- Sender = msg.From,
91
- Quantity = msg.Tags.Quantity,
92
- }
93
-
94
- for tagName, tagValue in pairs(msg) do
95
- if string.sub(tagName, 1, 2) == 'X-' then
96
- debitNoticeTags[tagName] = tagValue
97
- creditNoticeTags[tagName] = tagValue
98
- end
99
- end
100
-
101
- -- Send a debit notice to the sender
102
- ao.send({
103
- Target = msg.From,
104
- Action = 'Debit-Notice',
105
- Tags = debitNoticeTags,
106
- Data = json.encode({
107
- Recipient = data.Recipient,
108
- Quantity = tostring(data.Quantity)
109
- })
110
- })
111
-
112
- -- Send a credit notice to the recipient
113
- ao.send({
114
- Target = data.Recipient,
115
- Action = 'Credit-Notice',
116
- Tags = creditNoticeTags,
117
- Data = json.encode({
118
- Sender = msg.From,
119
- Quantity = tostring(data.Quantity)
120
- })
121
- })
122
- end
123
- end)
124
-
125
- -- Mint new tokens (Data - { Quantity })
126
- Handlers.add('Mint', Handlers.utils.hasMatchingTag('Action', 'Mint'), function(msg)
127
- local decodeCheck, data = decodeMessageData(msg.Data)
128
-
129
- if decodeCheck and data then
130
- -- Check if quantity is present
131
- if not data.Quantity then
132
- ao.send({ Target = msg.From, Action = 'Input-Error', Tags = { Status = 'Error', Message = 'Invalid arguments, required { Quantity }' } })
133
- return
134
- end
135
-
136
- -- Check if quantity is a valid integer greater than zero
137
- if not checkValidAmount(data.Quantity) then
138
- ao.send({ Target = msg.From, Action = 'Validation-Error', Tags = { Status = 'Error', Message = 'Quantity must be an integer greater than zero' } })
139
- return
140
- end
141
-
142
- -- Check if owner is sender
143
- if msg.From ~= Owner then
144
- ao.send({ Target = msg.From, Action = 'Validation-Error', Tags = { Status = 'Error', Message = 'Only the process owner can mint new tokens' } })
145
- return
146
- end
147
-
148
- -- Mint request is valid, add tokens to the pool
149
- if not Balances[Owner] then
150
- Balances[Owner] = '0'
151
- end
152
-
153
- Balances[Owner] = tostring(bint(Balances[Owner]) + bint(data.Quantity))
154
-
155
- ao.send({ Target = msg.From, Action = 'Mint-Success', Tags = { Status = 'Success', Message = 'Tokens minted' } })
156
- else
157
- ao.send({
158
- Target = msg.From,
159
- Action = 'Input-Error',
160
- Tags = {
161
- Status = 'Error',
162
- Message = string.format('Failed to parse data, received: %s. %s', msg.Data,
163
- 'Data must be an object - { Quantity }')
164
- }
165
- })
166
- end
167
- end)
168
-
169
- -- Read balance (Data - { Target })
170
- Handlers.add('Balance', Handlers.utils.hasMatchingTag('Action', 'Balance'), function(msg)
171
- local decodeCheck, data = decodeMessageData(msg.Data)
172
-
173
- if decodeCheck and data then
174
- -- Check if target is present
175
- if not data.Target then
176
- ao.send({ Target = msg.From, Action = 'Input-Error', Tags = { Status = 'Error', Message = 'Invalid arguments, required { Target }' } })
177
- return
178
- end
179
-
180
- -- Check if target is a valid address
181
- if not checkValidAddress(data.Target) then
182
- ao.send({ Target = msg.From, Action = 'Validation-Error', Tags = { Status = 'Error', Message = 'Target is not a valid address' } })
183
- return
184
- end
185
-
186
- -- Check if target has a balance
187
- if not Balances[data.Target] then
188
- ao.send({ Target = msg.From, Action = 'Read-Error', Tags = { Status = 'Error', Message = 'Target does not have a balance' } })
189
- return
190
- end
191
-
192
- ao.send({
193
- Target = msg.From,
194
- Action = 'Read-Success',
195
- Tags = { Status = 'Success', Message = 'Balance received' },
196
- Data =
197
- Balances[data.Target]
198
- })
199
- else
200
- ao.send({
201
- Target = msg.From,
202
- Action = 'Input-Error',
203
- Tags = {
204
- Status = 'Error',
205
- Message = string.format('Failed to parse data, received: %s. %s', msg.Data,
206
- 'Data must be an object - { Target }')
207
- }
208
- })
209
- end
210
- end)
211
-
212
- -- Read balances
213
- Handlers.add('Balances', Handlers.utils.hasMatchingTag('Action', 'Balances'),
214
- function(msg) ao.send({ Target = msg.From, Action = 'Read-Success', Data = json.encode(Balances) }) end)
215
-
216
- -- Initialize a request to add the uploaded asset to a profile
217
- Handlers.add('Add-Asset-To-Profile', Handlers.utils.hasMatchingTag('Action', 'Add-Asset-To-Profile'), function(msg)
218
- if checkValidAddress(msg.Tags.ProfileProcess) then
219
- -- ao.assign({ Processes = { msg.Tags.ProfileProcess }, Message = ao.id })
220
- ao.send({
221
- Target = msg.Tags.ProfileProcess,
222
- Action = 'Add-Uploaded-Asset',
223
- Data = json.encode({
224
- Id = ao.id,
225
- Quantity = msg.Tags.Quantity or '0'
226
- })
227
- })
228
- else
229
- ao.send({
230
- Target = msg.From,
231
- Action = 'Input-Error',
232
- Tags = {
233
- Status = 'Error',
234
- Message = 'ProfileProcess tag not specified or not a valid Process ID'
235
- }
236
- })
237
- end
238
- end)