Skip to content

Commit 1c5d276

Browse files
authored
Merge pull request #447 from permaweb/feat/m3
Feat/m3
2 parents 5ca2f04 + 0014dda commit 1c5d276

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+10376
-1141
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ aos.json
88
.prettierignore
99

1010
.vscode
11-
.DS_Store
11+
.DS_Store
12+
bun.lock

.npmignore

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ node_modules
33
wallet.json
44
backup
55
echo
6-
potato
6+
hyper
77
modules
88
logos
9+
extensions
10+
process/test
11+
process/crypto
12+
.gitpod
13+
.husky
14+
.github
915
aos.json
10-
*.tgz
16+
*.tgz

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ aos [name/process-id]
6666
| `--cron [Interval]` | The cron flag can only be used when spawning a process, it instructs the Process to generate messages every `Interval`. An Interval can be defined with a [n]-(period) for example: 1-minute, 10-minutes, 5-hours, 10-blocks, etc. | 0-1 |
6767
| `--get-blueprints [dir]` | This command will grab blueprints from the `/blueprints` folder or a folder specified by the user. These blueprints are small lua files that can be applied to your process to automatically give it some functionality. | 0-1 |
6868
| `--tag-name [name]` and `--tag-value [value]` | These flags are also only when aos is spawning a Process. You may add many of these combinations as you would like and they will appear on the Process tags object | 0-n |
69+
| `--authority [authority]` | The authority flag will allow you to set your authority when spawning a Process. Can be a single address or a comma-separated list. | 0-1 |
6970
| `--load [luaFile]` | The load command allows you to load lua source files from your local directory. | 0-n |
7071
| `--gateway-url [url]` | The gateway-url flag allows you to specify a custom Gateway to connect to. | 0-1 |
7172
| `--cu-url [url]` | The cu-url flag allows you to specify a custom Computer Unit to connect to. | 0-1 |

blueprints/htoken.lua

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
local bint = require('.bint')(256)
2+
--[[
3+
This module implements the ao Standard Token Specification.
4+
5+
Terms:
6+
Sender: the wallet or Process that sent the Message
7+
8+
It will first initialize the internal state, and then attach handlers,
9+
according to the ao Standard Token Spec API:
10+
11+
- Info(): return the token parameters, like Name, Ticker, Logo, and Denomination
12+
13+
- Balance(Target?: string): return the token balance of the Target. If Target is not provided, the Sender
14+
is assumed to be the Target
15+
16+
- Balances(): return the token balance of all participants
17+
18+
- Transfer(Target: string, Quantity: number): if the Sender has a sufficient balance, send the specified Quantity
19+
to the Target. It will also issue a Credit-Notice to the Target and a Debit-Notice to the Sender
20+
21+
- Mint(Quantity: number): if the Sender matches the Process Owner, then mint the desired Quantity of tokens, adding
22+
them the Processes' balance
23+
]]
24+
--
25+
local json = require('json')
26+
27+
--[[
28+
utils helper functions to remove the bint complexity.
29+
]]
30+
--
31+
32+
33+
local utils = {
34+
add = function(a, b)
35+
return tostring(bint(a) + bint(b))
36+
end,
37+
subtract = function(a, b)
38+
return tostring(bint(a) - bint(b))
39+
end,
40+
toBalanceValue = function(a)
41+
return tostring(bint(a))
42+
end,
43+
toNumber = function(a)
44+
return bint.tonumber(a)
45+
end
46+
}
47+
48+
49+
--[[
50+
Initialize State
51+
52+
ao.id is equal to the Process.Id
53+
]]
54+
--
55+
Variant = "0.0.4"
56+
57+
-- token should be idempotent and not change previous state updates
58+
Denomination = Denomination or 12
59+
Balances = Balances or { [id] = utils.toBalanceValue(10000 * 10 ^ Denomination) }
60+
TotalSupply = TotalSupply or utils.toBalanceValue(10000 * 10 ^ Denomination)
61+
Name = Name or 'Hyper Test Coin'
62+
Ticker = Ticker or 'HYPER'
63+
Logo = Logo or 'eskTHZQzCLOFrpzkr7zeaf4RxmkaNVYvHuPLh4CLpX4'
64+
65+
--[[
66+
Add handlers for each incoming Action defined by the ao Standard Token Specification
67+
]]
68+
--
69+
70+
--[[
71+
Info
72+
]]
73+
--
74+
Handlers.add('info', function(msg)
75+
send({
76+
Target = msg.from,
77+
Name = Name,
78+
Ticker = Ticker,
79+
Logo = Logo,
80+
Denomination = tostring(Denomination)
81+
})
82+
end)
83+
84+
--[[
85+
Balance
86+
]]
87+
--
88+
Handlers.add('balance', { action = "balance"}, function(msg)
89+
local bal = '0'
90+
print('balance called')
91+
print('from ' .. msg.from)
92+
93+
-- If not Recipient is provided, then return the Senders balance
94+
if (msg.recipient) then
95+
if (Balances[msg.recipient]) then
96+
bal = Balances[msg.recipient]
97+
end
98+
elseif msg.target and Balances[msg.target] then
99+
bal = Balances[msg.target]
100+
elseif Balances[msg.from] then
101+
bal = Balances[msg.from]
102+
end
103+
send({
104+
target = msg.from,
105+
balance = bal,
106+
ticker = Ticker,
107+
account = msg.recipient or msg.from,
108+
data = bal
109+
})
110+
end)
111+
112+
--[[
113+
Balances
114+
]]
115+
--
116+
Handlers.add('balances',
117+
function(msg)
118+
send({target = msg.from, data = json.encode(Balances) })
119+
end)
120+
121+
--[[
122+
Transfer
123+
]]
124+
--
125+
Handlers.add('transfer', function(msg)
126+
assert(type(msg.recipient) == 'string', 'Recipient is required!')
127+
assert(type(msg.quantity) == 'string', 'Quantity is required!')
128+
assert(bint.__lt(0, bint(msg.quantity)), 'Quantity must be greater than 0')
129+
130+
if not Balances[msg.from] then Balances[msg.from] = "0" end
131+
if not Balances[msg.recipient] then Balances[msg.recipient] = "0" end
132+
133+
if bint(msg.quantity) <= bint(Balances[msg.from]) then
134+
Balances[msg.from] = utils.subtract(Balances[msg.from], msg.quantity)
135+
Balances[msg.recipient] = utils.add(Balances[msg.recipient], msg.quantity)
136+
137+
--[[
138+
Only send the notifications to the Sender and Recipient
139+
if the Cast tag is not set on the Transfer message
140+
]]
141+
--
142+
if not msg.cast then
143+
-- Debit-Notice message template, that is sent to the Sender of the transfer
144+
local debitNotice = {
145+
action = 'Debit-Notice',
146+
recipient = msg.recipient,
147+
quantity = msg.quantity,
148+
data = colors.gray ..
149+
"You transferred " ..
150+
colors.blue .. msg.quantity .. colors.gray .. " to " .. colors.green .. msg.recipient .. colors.reset
151+
}
152+
-- Credit-Notice message template, that is sent to the Recipient of the transfer
153+
local creditNotice = {
154+
target = msg.recipient,
155+
action = 'Credit-Notice',
156+
sender = msg.from,
157+
quantity = msg.quantity,
158+
data = colors.gray ..
159+
"You received " ..
160+
colors.blue .. msg.quantity .. colors.gray .. " from " .. colors.green .. msg.from .. colors.reset
161+
}
162+
163+
-- Add forwarded tags to the credit and debit notice messages
164+
for tagName, tagValue in pairs(msg) do
165+
-- Tags beginning with "X-" are forwarded
166+
if string.sub(tagName, 1, 2) == "x-" then
167+
debitNotice[tagName] = tagValue
168+
creditNotice[tagName] = tagValue
169+
end
170+
end
171+
172+
-- Send Debit-Notice and Credit-Notice
173+
debitNotice.Target = msg.from
174+
send(debitNotice)
175+
send(creditNotice)
176+
end
177+
else
178+
send({
179+
target = msg.from,
180+
action = 'Transfer-Error',
181+
['Message-Id'] = msg.id,
182+
error = 'Insufficient Balance!'
183+
})
184+
end
185+
end)
186+
187+
--[[
188+
Mint
189+
]]
190+
--
191+
Handlers.add('mint', function(msg)
192+
assert(type(msg.quantity) == 'string', 'Quantity is required!')
193+
assert(bint(0) < bint(msg.quantity), 'Quantity must be greater than zero!')
194+
195+
if not Balances[id] then Balances[id] = "0" end
196+
197+
if msg.from == id then
198+
-- Add tokens to the token pool, according to Quantity
199+
Balances[msg.from] = utils.add(Balances[msg.from], msg.quantity)
200+
TotalSupply = utils.add(TotalSupply, msg.quantity)
201+
send({
202+
target = msg.from,
203+
data = colors.gray .. "Successfully minted " .. colors.blue .. msg.quantity .. colors.reset
204+
})
205+
else
206+
send({
207+
target = msg.from,
208+
action = 'Mint-Error',
209+
['Message-Id'] = msg.id,
210+
error = 'Only the Process Id can mint new ' .. Ticker .. ' tokens!'
211+
})
212+
end
213+
end)
214+
215+
--[[
216+
Total Supply
217+
]]
218+
--
219+
Handlers.add('totalSupply', function(msg)
220+
assert(msg.From ~= id, 'Cannot call Total-Supply from the same process!')
221+
send({
222+
Target = msg.From,
223+
Action = 'Total-Supply',
224+
Data = TotalSupply,
225+
Ticker = Ticker
226+
})
227+
end)
228+
229+

hyper/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
test:
2+
eval $(luarocks path)
3+
busted
4+
5+
download:
6+
wget https://www.lua.org/ftp/lua-5.3.6.tar.gz
7+
tar -xzf lua-5.3.6.tar.gz
8+
wget https://luarocks.org/releases/luarocks-3.9.2.tar.gz
9+
tar -xzf luarocks-3.9.2.tar.gz

hyper/README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# hyper AOS
2+
3+
hyper AOS is a hyperBEAM based implementation of AOS, focused to deliver lighting fast performance to the AO network.
4+
5+
6+
## Developer Setup
7+
8+
9+
* Install lua
10+
11+
```sh
12+
wget https://www.lua.org/ftp/lua-5.3.6.tar.gz
13+
tar -xzf lua-5.3.6.tar.gz
14+
cd lua-5.3.6
15+
```
16+
17+
For linux
18+
19+
```sh
20+
make linux
21+
```
22+
23+
For mac
24+
25+
```sh
26+
make macosx
27+
```
28+
29+
```sh
30+
sudo make install
31+
```
32+
33+
* Install luarocks
34+
35+
```sh
36+
wget https://luarocks.org/releases/luarocks-3.9.2.tar.gz
37+
tar -xzf luarocks-3.9.2.tar.gz
38+
cd luarocks-3.9.2
39+
```
40+
41+
```sh
42+
./configure --lua-version=5.3 --with-lua=/usr/local
43+
make
44+
sudo make install
45+
```
46+
47+
* Init lua env
48+
49+
```sh
50+
luarocks init
51+
```
52+
53+
* Install busted testing library
54+
55+
```sh
56+
luarocks install busted
57+
```
58+
59+
* Setup lua env
60+
61+
```sh
62+
eval $(luarocks path)
63+
```
64+
65+
## Tests
66+
67+
* Running Tests
68+
69+
```sh
70+
busted
71+
```
72+
73+
* Writing Tests
74+
75+
Add your test in the `spec` folder and name the test ending with `_spec.lua`
76+
77+

0 commit comments

Comments
 (0)