Skip to content

Commit

Permalink
chore(ant): reformat to use ao send instead of reply
Browse files Browse the repository at this point in the history
  • Loading branch information
Atticus committed Jun 12, 2024
1 parent 1c3da1e commit af28b7e
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 68 deletions.
12 changes: 9 additions & 3 deletions ant/src/ant-utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@ function utils.validateOwner(caller)
end

function utils.hasPermission(from)
local hasPermission = Controllers[from] or Balances[from]

assert(hasPermission ~= nil, "Only controllers and owners can set controllers and records.")
local isController = false
for _, c in ipairs(Controllers) do
if c == from then
isController = true
break
end
end
local hasPermission = isController or Balances[from] or Owner == from or ao.env.Process.Id == from
assert(not hasPermission, "Only controllers and owners can set controllers and records.")
end

function utils.camelCase(str)
Expand Down
17 changes: 10 additions & 7 deletions ant/src/balances.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,58 @@ function balances.walletHasSufficientBalance(wallet)
end

function balances.info()
utils.reply({
return {
Name = Name,
Ticker = Ticker,
TotalSupply = tostring(TotalSupply),
Logo = Logo,
Denomination = tostring(Denomination),
Owner = Owner,
})
}
end

function balances.transfer(to)
utils.validateArweaveId(to)
Balances = { [to] = 1 }
Owner = to
Controllers = {}
return "Transfer successful"
end

function balances.balance(address)
utils.validateArweaveId(address)
local balance = Balances[address] or 0
utils.reply({
return {
Target = address,
Balance = balance,
Ticker = Ticker,
Account = address,
Data = balance,
})
}
end

function balances.balances()
utils.reply(json.encode(Balances))
return json.encode(Balances)
end

function balances.mint()
utils.reply("Minting not supported")
return "Minting not supported"
end

function balances.burn()
utils.reply("Burning not supported")
return "Burning not supported"
end

function balances.setName(name)
assert(type(name) == "string", "Name must be a string")
Name = name
return "Name set to " .. name
end

function balances.setTicker(ticker)
assert(type(ticker) == "string", "Ticker must be a string")
Ticker = ticker
return "Ticker set to " .. ticker
end

return balances
8 changes: 5 additions & 3 deletions ant/src/controllers.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local json = require(".json")
local utils = require(".ant-utils")

Controllers = Controllers or {}
Controllers = Controllers or { Owner }

local controllers = {}

Expand All @@ -13,6 +13,7 @@ function controllers.setController(controller)
end

table.insert(Controllers, controller)
return "Controller added"
end

function controllers.removeController(controller)
Expand All @@ -28,10 +29,11 @@ function controllers.removeController(controller)
end

assert(controllerExists ~= nil, "Controller does not exist")
return "Controller removed"
end

function controllers.getControllers(msg)
utils.reply(json.encode(Controllers))
function controllers.getControllers()
return json.encode(Controllers)
end

return controllers
2 changes: 2 additions & 0 deletions ant/src/initialize.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ function initialize.initializeANTState(state)
Balances = balances
Controllers = controllers
Records = records

return "State initialized"
end

local function findObject(array, key, value)
Expand Down
88 changes: 48 additions & 40 deletions ant/src/process.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function process.handle(msg, ao)
RemoveController = "Remove-Controller",
SetRecord = "Set-Record",
RemoveRecord = "Remove-Record",
SetName = "Se-Name",
SetName = "Set-Name",
SetTicker = "Set-Ticker",
--- initialization method for bootstrapping the contract from other platforms ---
InitializeState = "Initialize-State",
Expand Down Expand Up @@ -162,10 +162,8 @@ function process.handle(msg, ao)
})
return
elseif not msg.Cast then
local creditNotice = utils.notices.credit(msg)
local debitNotice = utils.notices.debit(msg)

utils.notices.sendNotices({ creditNotice, debitNotice })
ao.send(utils.notices.debit(msg))
ao.send(utils.notices.credit(msg))
end
end
)
Expand All @@ -176,10 +174,19 @@ function process.handle(msg, ao)
function(msg)
local balStatus, balRes = pcall(balances.balance, msg.Tags.Recipient or msg.From)
if not balStatus then
utils.reply({
ao.send({
Target = msg.From,
Tags = { Error = "Balance-Error" },
Error = tostring(balRes),
})
else
ao.send({
Target = msg.From,
Balance = balRes,
Ticker = Ticker,
Account = msg.Tags.Recipient or msg.From,
Data = balRes,
})
end
end
)
Expand All @@ -188,7 +195,10 @@ function process.handle(msg, ao)
camel(TokenSpecActionMap.Balances),
utils.hasMatchingTag("Action", TokenSpecActionMap.Balances),
function(msg)
balances.balances()
ao.send({
Target = msg.From,
Data = balances.balances(),
})
end
)

Expand All @@ -208,15 +218,19 @@ function process.handle(msg, ao)
)

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

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

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

-- ActionMap (ANT Spec)
Expand All @@ -225,9 +239,10 @@ function process.handle(msg, ao)
local hasPermission, permissionErr = pcall(utils.hasPermission, msg.From)
if hasPermission == false then
print("Permission Error", permissionErr)
return utils.reply(permissionErr)
return ao.send({ Target = msg.From, Data = permissionErr, Tags = { Error = "Permission Error" } })
end
controllers.setController(msg.Tags.Controller)
local _, controllerRes = pcall(controllers.setController, msg.Tags.Controller)
ao.send({ Target = msg.From, Data = controllerRes })
end)

Handlers.add(
Expand All @@ -236,70 +251,62 @@ function process.handle(msg, ao)
function(msg)
local hasPermission, permissionErr = pcall(utils.hasPermission, msg.From)
if hasPermission == false then
return utils.reply(permissionErr)
return ao.send({ Target = msg.From, Data = permissionErr, Tags = { Error = "Permission Error" } })
end
local removeControllerValidity, removeControllerStatus =
pcall(controllers.removeController, msg.Tags.Controller)
local _, removeRes = pcall(controllers.removeController, msg.Tags.Controller)

if not removeControllerValidity then
return utils.reply(removeControllerStatus)
end
ao.send({ Target = msg.From, Data = removeRes })
end
)

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

Handlers.add(camel(ActionMap.SetRecord), utils.hasMatchingTag("Action", ActionMap.SetRecord), function(msg)
local hasPermission, permissionErr = pcall(utils.hasPermission, msg.From)
if hasPermission == false then
return utils.reply(permissionErr)
return ao.send({ Target = msg.From, Data = permissionErr, Tags = { Error = "Permission Error" } })
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
return utils.reply(setRecordResult)
end
local _, setRecordResult = pcall(records.setRecord, name, transactionId, ttlSeconds)

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

Handlers.add(camel(ActionMap.RemoveRecord), utils.hasMatchingTag("Action", ActionMap.RemoveRecord), function(msg)
local hasPermission, permissionErr = pcall(utils.hasPermission, msg.From)
if hasPermission == false then
return utils.reply(permissionErr)
return ao.send({ Target = msg.From, Data = permissionErr })
end
records.removeRecord(msg.Tags.Name)
ao.send({ Target = msg.From, Data = records.removeRecord(msg.Tags.Name) })
end)

Handlers.add(camel(ActionMap.GetRecord), utils.hasMatchingTag("Action", ActionMap.GetRecord), function(msg)
local nameValidity, nameValidityError = pcall(records.getRecord, msg.Tags.Name)
if nameValidity == false then
return utils.reply(nameValidityError)
end
local _, nameRes = pcall(records.getRecord, msg.Tags["Sub-Domain"])

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

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

Handlers.add(camel(ActionMap.SetName), utils.hasMatchingTag("Action", ActionMap.SetName), function(msg)
local nameValidity, nameStatus = pcall(balances.setName, msg.Tags.Name)
if not nameValidity then
return utils.reply(nameStatus)
end
local _, nameStatus = pcall(balances.setName, msg.Tags.Name)
ao.send({ Target = msg.From, Data = nameStatus })
end)

Handlers.add(camel(ActionMap.SetTicker), utils.hasMatchingTag("Action", ActionMap.SetTicker), function(msg)
local tickerValidity, tickerStatus = pcall(balances.setTicker, msg.Tags.Ticker)
if not tickerValidity then
return utils.reply(tickerStatus)
end
local _, tickerStatus = pcall(balances.setTicker, msg.Tags.Ticker)

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

Handlers.add(
Expand All @@ -308,7 +315,8 @@ function process.handle(msg, ao)
function(msg)
local status, state = pcall(json.decode(msg.Data))
assert(status, "Invalid state provided")
initialize.initializeANTState(state.Data)
local _, initResult = pcall(initialize.initializeState, state)
ao.send({ Target = msg.From, Data = initResult })
end
)

Expand Down
Binary file modified ant/src/process.wasm
Binary file not shown.
21 changes: 10 additions & 11 deletions ant/src/records.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local utils = require(".ant-utils")
local json = require(".json")
local records = {}

Records = Records or {}
-- defaults to landing page txid
Records = Records or { ["@"] = { transactionId = "UyC5P5qKPZaltMmmZAWdakhlDXsBF6qmyrbWYFchRTk", ttlSeconds = 3600 } }

function records.setRecord(name, transactionId, ttlSeconds)
local nameValidity, nameValidityError = pcall(utils.validateUndername, name)
Expand All @@ -16,28 +16,27 @@ function records.setRecord(name, transactionId, ttlSeconds)
transactionId = transactionId,
ttlSeconds = ttlSeconds,
}

return "Record set"
end

function records.removeRecord(name)
local nameValidity, nameValidityError = pcall(utils.validateUndername, name)
assert(nameValidity ~= false, nameValidityError)
Records[name] = nil

return "Record removed"
end

function records.getRecord(name)
local nameValidity, nameValidityError = pcall(utils.validateUndername, name)
if nameValidity == false then
return utils.reply(nameValidityError)
end

utils.validateUndername(name)
assert(Records[name] ~= nil, "Record does not exist")
local parsedRecord = json.encode(Records[name])
utils.reply(parsedRecord)

return json.encode(Records[name])
end

function records.getRecords()
local parsedRecords = json.encode(Records)
utils.reply(parsedRecords)
return json.encode(Records)
end

return records
8 changes: 4 additions & 4 deletions ant/tools/spawn-ant.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const arweave = Arweave.init({
const ao = connect({
GATEWAY_URL: "https://arweave.net",
})
const moduleId = "I1ecao-MFgB-0Kqhr83uYmQp9wdb5z-A-GsQmsCAkdk"
const moduleId = "-QC1Gcu-WqGPMYVF6ToBoBvnq396vICwH1KyYAiR-f0"
const scheduler = "_GQ33BkPtZrqxA84vM8Zk-N2aO0toNNu_C-l-rawrBA"

async function main() {
Expand All @@ -27,10 +27,10 @@ async function main() {
// scheduler,
// signer
// })
const processId = "0lF7QU7EgNt4t5iiHOu32zEkR4zN1duAKwIzpSJ_bJ0"
const processId = "fF6voaWOrCEnVEkMiCNZ45uv8cR8FnCsnVVT-xPgeKU"
console.log("Process ID:", processId)
console.log("Waiting 20 seconds to ensure process is readied.")
await new Promise((resolve) => setTimeout(resolve, 20_000))
await new Promise((resolve) => setTimeout(resolve, 2_000))
console.log("Continuing...")

const testCases = [
Expand Down Expand Up @@ -60,7 +60,7 @@ async function main() {
signer
})

console.log(result)
console.log(method, result)
}

}
Expand Down

0 comments on commit af28b7e

Please sign in to comment.