Skip to content

Commit

Permalink
chore(ant): add bundling and aos vs custom module builds
Browse files Browse the repository at this point in the history
  • Loading branch information
Atticus committed Jun 13, 2024
1 parent 7892043 commit 2bab59d
Show file tree
Hide file tree
Showing 29 changed files with 496 additions and 450 deletions.
3 changes: 2 additions & 1 deletion ant/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"build": "cd src && ao build && cd .. && yarn load-process",
"deploy": "cd src && ao publish process.wasm -w ../tools/key.json --tag=\"Memory-Limit\" --value=\"1-gb\" --tag=\"Compute-Limit\" --value=\"9000000000000\" && cd ..",
"load-process": "node tools/load-process.cjs",
"spawn": "node tools/spawn-ant.cjs"
"spawn": "node tools/spawn-ant.cjs",
"publish:aos-ant": "node tools/bundle-aos-lua.cjs"
}
}
298 changes: 298 additions & 0 deletions ant/src/aos-ant.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,298 @@
-- main.lua
-- utils
local json = require("json")
local utils = require(".common.utils")
local camel = utils.camelCase
-- spec modules
local balances = require(".common.balances")
local initialize = require(".common.initialize")
local records = require(".common.records")
local controllers = require(".common.controllers")

Owner = Owner or ao.env.Process.Owner
Balances = Balances or { [Owner] = 1 }
Controllers = Controllers or { Owner }

Name = Name or "Arweave Name Token"
Ticker = Ticker or "ANT"
Logo = Logo or "Sie_26dvgyok0PZD_-iQAFOhOd5YxDTkczOLoqTTL_A"
Denomination = Denomination or 0
TotalSupply = TotalSupply or 1
Initialized = Initialized or false

local ActionMap = {
-- write
SetController = "Set-Controller",
RemoveController = "Remove-Controller",
SetRecord = "Set-Record",
RemoveRecord = "Remove-Record",
SetName = "Set-Name",
SetTicker = "Set-Ticker",
--- initialization method for bootstrapping the contract from other platforms ---
InitializeState = "Initialize-State",
-- read
GetControllers = "Get-Controllers",
GetRecord = "Get-Record",
GetRecords = "Get-Records",
}

local TokenSpecActionMap = {
Info = "Info",
Balances = "Balances",
Balance = "Balance",
Transfer = "Transfer",
TotalSupply = "Total-Supply",
CreditNotice = "Credit-Notice",
-- not implemented
Mint = "Mint",
Burn = "Burn",
}

Handlers.add(
camel(TokenSpecActionMap.Transfer),
utils.hasMatchingTag("Action", TokenSpecActionMap.Transfer),
function(msg)
local recipient = msg.Tags.Recipient
local function checkAssertions()
utils.validateArweaveId(recipient)
utils.validateOwner(msg.From)
end

local inputStatus, inputResult = pcall(checkAssertions)

if not inputStatus then
ao.send({
Target = msg.From,
Tags = { Error = "Transfer-Error" },
Error = tostring(inputResult),
["Message-Id"] = msg.Id,
Data = inputResult,
})
return
end
local transferStatus, transferResult = pcall(balances.transfer, recipient)

if not transferStatus then
ao.send({
Target = msg.From,
Action = "Transfer-Error",
Error = tostring(transferResult),
["Message-Id"] = msg.Id,
Data = transferResult,
})
return
elseif not msg.Cast then
ao.send(utils.notices.debit(msg))
ao.send(utils.notices.credit(msg))
end
end
)

Handlers.add(
camel(TokenSpecActionMap.Balance),
utils.hasMatchingTag("Action", TokenSpecActionMap.Balance),
function(msg)
local balStatus, balRes = pcall(balances.balance, msg.Tags.Recipient or msg.From)
if not balStatus then
ao.send({
Target = msg.From,
Tags = { Error = "Balance-Error" },
Error = tostring(balRes),
["Message-Id"] = msg.Id,
Data = balRes,
})
else
ao.send({
Target = msg.From,
Balance = balRes,
Ticker = Ticker,
Account = msg.Tags.Recipient or msg.From,
Data = balRes,
})
end
end
)

Handlers.add(
camel(TokenSpecActionMap.Balances),
utils.hasMatchingTag("Action", TokenSpecActionMap.Balances),
function(msg)
ao.send({
Target = msg.From,
Data = balances.balances(),
})
end
)

Handlers.add(
camel(TokenSpecActionMap.TotalSupply),
utils.hasMatchingTag("Action", TokenSpecActionMap.TotalSupply),
function(msg)
assert(msg.From ~= ao.id, "Cannot call Total-Supply from the same process!")

ao.send({
Target = msg.From,
Action = "Total-Supply",
Data = TotalSupply,
Ticker = Ticker,
})
end
)

Handlers.add(camel(TokenSpecActionMap.Info), utils.hasMatchingTag("Action", TokenSpecActionMap.Info), function(msg)
local info = balances.info()
ao.send({
Target = msg.From,
Tags = info,
Data = json.encode(info),
})
end)

Handlers.add(camel(TokenSpecActionMap.Mint), utils.hasMatchingTag("Action", TokenSpecActionMap.Mint), function(msg)
ao.send({ Target = msg.From, Data = balances.mint() })
end)

Handlers.add(camel(TokenSpecActionMap.Burn), utils.hasMatchingTag("Action", TokenSpecActionMap.Burn), function(msg)
ao.send({ Target = msg.From, Data = balances.burn() })
end)

-- ActionMap (ANT Spec)

Handlers.add(camel(ActionMap.SetController), utils.hasMatchingTag("Action", ActionMap.SetController), function(msg)
local assertHasPermission, permissionErr = pcall(utils.assertHasPermission, msg.From)
if assertHasPermission == false then
return ao.send({
Target = msg.From,
Error = "Set-Controller-Error",
["Message-Id"] = msg.Id,
Data = permissionErr,
})
end
local controllerStatus, controllerRes = pcall(controllers.setController, msg.Tags.Controller)
if not controllerStatus then
ao.send({
Target = msg.From,
Error = "Set-Controller-Error",
["Message-Id"] = msg.Id,
Data = controllerRes,
})
return
end
ao.send({ Target = msg.From, Data = controllerRes })
end)

Handlers.add(
camel(ActionMap.RemoveController),
utils.hasMatchingTag("Action", ActionMap.RemoveController),
function(msg)
local assertHasPermission, permissionErr = pcall(utils.assertHasPermission, msg.From)
if assertHasPermission == false then
return ao.send({
Target = msg.From,
Data = permissionErr,
Error = "Remove-Controller-Error",
["Message-Id"] = msg.Id,
})
end
local removeStatus, removeRes = pcall(controllers.removeController, msg.Tags.Controller)
if not removeStatus then
ao.send({
Target = msg.From,
Data = removeRes,
Error = "Remove-Controller-Error",
["Message-Id"] = msg.Id,
})
return
end

ao.send({ Target = msg.From, Data = removeRes })
end
)

Handlers.add(camel(ActionMap.GetControllers), utils.hasMatchingTag("Action", ActionMap.GetControllers), function(msg)
ao.send({ Target = msg.From, Data = controllers.getControllers() })
end)

Handlers.add(camel(ActionMap.SetRecord), utils.hasMatchingTag("Action", ActionMap.SetRecord), function(msg)
local assertHasPermission, permissionErr = pcall(utils.assertHasPermission, msg.From)
if assertHasPermission == false then
return ao.send({
Target = msg.From,
Data = permissionErr,
Error = "Set-Record-Error",
["Message-Id"] = msg.Id,
})
end
local tags = msg.Tags
local name, transactionId, ttlSeconds = tags["Sub-Domain"], tags["Transaction-Id"], tonumber(tags["TTL-Seconds"])

local setRecordStatus, setRecordResult = pcall(records.setRecord, name, transactionId, ttlSeconds)
if not setRecordStatus then
ao.send({ Target = msg.From, Data = setRecordResult, Error = "Set-Record-Error", ["Message-Id"] = msg.Id })
return
end

ao.send({ Target = msg.From, Data = setRecordResult })
end)

Handlers.add(camel(ActionMap.RemoveRecord), utils.hasMatchingTag("Action", ActionMap.RemoveRecord), function(msg)
local assertHasPermission, permissionErr = pcall(utils.assertHasPermission, msg.From)
if assertHasPermission == false then
return ao.send({ Target = msg.From, Data = permissionErr })
end
local removeRecordStatus, removeRecordResult = pcall(records.removeRecord, msg.Tags["Sub-Domain"])
if not removeRecordStatus then
ao.send({
Target = msg.From,
Data = removeRecordResult,
Error = "Remove-Record-Error",
["Message-Id"] = msg.Id,
})
else
ao.send({ Target = msg.From, Data = removeRecordResult })
end
end)

Handlers.add(camel(ActionMap.GetRecord), utils.hasMatchingTag("Action", ActionMap.GetRecord), function(msg)
local nameStatus, nameRes = pcall(records.getRecord, msg.Tags["Sub-Domain"])
if not nameStatus then
ao.send({ Target = msg.From, Data = nameRes, Error = "Get-Record-Error", ["Message-Id"] = msg.Id })
return
end

ao.send({ Target = msg.From, Data = nameRes })
end)

Handlers.add(camel(ActionMap.GetRecords), utils.hasMatchingTag("Action", ActionMap.GetRecords), function(msg)
ao.send({ Target = msg.From, Data = records.getRecords() })
end)

Handlers.add(camel(ActionMap.SetName), utils.hasMatchingTag("Action", ActionMap.SetName), function(msg)
local nameStatus, nameRes = pcall(balances.setName, msg.Tags.Name)
if not nameStatus then
ao.send({ Target = msg.From, Data = nameRes, Error = "Set-Name-Error", ["Message-Id"] = msg.Id })
return
end
ao.send({ Target = msg.From, Data = nameRes })
end)

Handlers.add(camel(ActionMap.SetTicker), utils.hasMatchingTag("Action", ActionMap.SetTicker), function(msg)
local tickerStatus, tickerRes = pcall(balances.setTicker, msg.Tags.Ticker)
if not tickerStatus then
ao.send({ Target = msg.From, Data = tickerRes, Error = "Set-Ticker-Error", ["Message-Id"] = msg.Id })
return
end

ao.send({ Target = msg.From, Data = tickerRes })
end)

Handlers.add(camel(ActionMap.InitializeState), utils.hasMatchingTag("Action", ActionMap.InitializeState), function(msg)
local initStatus, result = pcall(initialize.initializeANTState, msg.Data)

if not initStatus then
ao.send({ Target = msg.From, Data = result, Error = "Initialize-State-Error", ["Message-Id"] = msg.Id })
return
else
ao.send({ Target = msg.From, Data = json.encode(result) })
end
end)
4 changes: 2 additions & 2 deletions ant/src/balances.lua → ant/src/common/balances.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local utils = require(".utils")
local json = require(".json")
local utils = require(".common.utils")
local json = require(".common.json")

local balances = {}

Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions ant/src/controllers.lua → ant/src/common/controllers.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local json = require(".json")
local utils = require(".utils")
local json = require(".common.json")
local utils = require(".common.utils")

local controllers = {}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local _utils = { _version = "0.0.1" }

local _ = require(".utils")
local _ = require(".common.utils")
local ao = ao or require("ao")

function _utils.hasMatchingTag(name, value)
Expand Down
2 changes: 1 addition & 1 deletion ant/src/handlers.lua → ant/src/common/handlers.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local handlers = { _version = "0.0.3" }

handlers.utils = require(".handlers-utils")
handlers.utils = require(".common.handlers-utils")
handlers.list = {}

local function findIndexByProp(array, prop, value)
Expand Down
4 changes: 2 additions & 2 deletions ant/src/initialize.lua → ant/src/common/initialize.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local utils = require(".utils")
local json = require("json")
local utils = require(".common.utils")
local json = require(".common.json")
local initialize = {}

function initialize.initializeANTState(state)
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions ant/src/records.lua → ant/src/common/records.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local utils = require(".utils")
local json = require(".json")
local utils = require(".common.utils")
local json = require(".common.json")
local records = {}
-- defaults to landing page txid
Records = Records or { ["@"] = { transactionId = "UyC5P5qKPZaltMmmZAWdakhlDXsBF6qmyrbWYFchRTk", ttlSeconds = 3600 } }
Expand Down
2 changes: 1 addition & 1 deletion ant/src/utils.lua → ant/src/common/utils.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local constants = require(".constants")
local constants = require(".common.constants")
local utils = { _version = "0.0.1" }

local function isArray(table)
Expand Down
Loading

0 comments on commit 2bab59d

Please sign in to comment.