Skip to content

Commit

Permalink
fix(workflows): dont directly use msg in handlers for msgs
Browse files Browse the repository at this point in the history
  • Loading branch information
Atticus committed Jun 10, 2024
1 parent ba8d259 commit 3b20e6e
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 206 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ant.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ on: [push, workflow_dispatch]
jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
name: Check out repository code
Expand All @@ -23,7 +22,8 @@ jobs:
run: luarocks install ar-io-ao-0.1-1.rockspec

- name: Run Busted Tests
run: cd ant && busted .
working-directory: ant
run: busted .

- name: Upload coverage reports to Codecov
uses: codecov/[email protected]
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/registry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ jobs:
run: luarocks install ar-io-ao-0.1-1.rockspec

- name: Run Busted Tests
run: cd contract && busted .
working-directory: contract
run: busted .

- name: Upload coverage reports to Codecov
uses: codecov/[email protected]
Expand Down
65 changes: 11 additions & 54 deletions ant/spec/ant_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,15 @@ describe("Arweave Name Token", function()

it("Transfers tokens between accounts", function()
local to = "1111111111111111111111111111111111111111112"
local transferMsg = {
From = fake_address,
Tags = {
Recipient = to,
},
}
balances.transfer(transferMsg) -- happy path
balances.transfer(to) -- happy path

assert.are.same(_G.Balances[fake_address], nil)
assert.are.same(_G.Balances[to], 1)
end)

it("sets a controller", function()
local newController = "1111111111111111111111111111111111111111112"
local controllerMsg = {
From = fake_address,
Tags = {
Controller = newController,
},
}
controllers.setController(controllerMsg) -- happy path
controllers.setController(newController) -- happy path

local hasController = nil
for _, controller in ipairs(_G.Controllers) do
Expand All @@ -89,13 +77,7 @@ describe("Arweave Name Token", function()

it("removes a controller", function()
local controllerToRemove = fake_address
local controllerMsg = {
From = fake_address,
Tags = {
Controller = controllerToRemove,
},
}
controllers.removeController(controllerMsg) -- happy path
controllers.removeController(fake_address) -- happy path

local hasController = nil
for _, controller in ipairs(_G.Controllers) do
Expand All @@ -107,55 +89,30 @@ describe("Arweave Name Token", function()
end)

it("sets a record", function()
-- TODO: not sure why failing,
local recordMsg = {
From = fake_address,
Tags = {
Name = "@",
["Transaction-Id"] = fake_address,
["TTL-Seconds"] = 900,
},
}
records.setRecord(recordMsg) -- happy path
local name, transactionId, ttlSeconds = "@", fake_address, 900
records.setRecord(name, transactionId, ttlSeconds) -- happy path

assert.are.same(_G.Records["@"].transactionId, fake_address)
assert.are.same(_G.Records["@"].ttlSeconds, 900)
end)

it("removes a record", function()
local recordMsg = {
From = fake_address,
Tags = {
Name = "@",
},
}
records.removeRecord(recordMsg) -- happy path

assert.are.same(_G.Records["@"], nil)
local name = "@"
records.removeRecord(name) -- happy path

assert.are.same(_G.Records[name], nil)
end)

it("sets the name", function()
local newName = "New Name"
local nameMsg = {
From = fake_address,
Tags = {
Name = newName,
},
}
balances.setName(nameMsg) -- happy path
balances.setName(newName) -- happy path

assert.are.same(_G.Name, newName)
end)

it("sets the ticker", function()
local newTicker = "NEW"
local tickerMsg = {
From = fake_address,
Tags = {
Ticker = newTicker,
},
}
balances.setTicker(tickerMsg) -- happy path
balances.setTicker(newTicker) -- happy path

assert.are.same(_G.Ticker, newTicker)
end)
Expand Down
43 changes: 17 additions & 26 deletions ant/src/balances.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,45 @@ Balances = Balances or {}

local balances = {}

function balances.info(msg)
function balances.walletHasSufficientBalance(wallet)
return Balances[wallet] ~= nil and Balances[wallet] > 0
end

function balances.info()
utils.reply(json.encode({
Name = Name,
Ticker = Ticker,
TotalSupply = 1,
Logo = Logo,
Denomination = tostring(Denomination),
Denomination = Denomination,
}))
end

function balances.transfer(to, from, qty)
assert(utils.validateArweaveId, to, "Receipent must be a valid arweave address")
assert(utils.validateArweaveId, from, "Sender must be a valid arweave address")
assert(type(qty) == "number and qty > 0 and qty % 0 == 0, "Quantity must be an integer greater than 0")

local fromBalance = Balances[from] or 0
local receipentBalance = Balances[recipient] or 0

if fromBalance < qty then
error("Insufficient balance")
end

Balances[from] = fromBalance - qty
Balances[recipient] = repipentBalance + qty
return
function balances.transfer(to)
Balances = { [to] = 1 }
end

function balances.balance(address)
assert(utils.validateArweaveId(address), "Addreess must be valid Arweave ID")
local balance = Balances[address] or 0
return 0
assert(utils.validateArweaveId(address), "Addreess must be valid Arweave ID")
local balance = Balances[address] or 0
utils.reply(tostring(balance))
end

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

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

function balances.setName(msg)
local name = msg.Tags.Name
function balances.setName(name)
assert(type(name) == "string", "Name must be a string")
Name = name
end

function balances.setTicker(msg)
local ticker = msg.Tags.Ticker
function balances.setTicker(ticker)
assert(type(ticker) == "string", "Ticker must be a string")
Ticker = ticker
end

Expand Down
39 changes: 18 additions & 21 deletions ant/src/controllers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,41 @@ Controllers = Controllers or {}

local controllers = {}

function controllers.setController(msg)
local controller = msg.Tags.Controller

local hasPermission, permissionErr = utils.hasPermission(msg)
if hasPermission == false then
print("permissionErr", permissionErr)
return utils.reply(permissionErr)
end

function controllers.setController(controller)
local controllerValidity, controllerValidityError = utils.validateArweaveId(controller)
if controllerValidity == false then
print("id length" .. #controller)
print("controllerValidityError", controllerValidityError)
return utils.reply(controllerValidityError)
utils.reply(controllerValidityError)
return
end

for _, c in ipairs(Controllers) do
if c == controller then
assert(c ~= controller, "Controller already exists")
end
end

table.insert(Controllers, controller)
end

function controllers.removeController(msg)
local controller = msg.Tags.Controller

local hasPermission, permissionErr = utils.hasPermission(msg)
if not hasPermission then
return utils.reply(permissionErr)
end

local controllerValidity, controllerValidityError = utils.validateArweaveId(controllers)
function controllers.removeController(controller)
local controllerValidity, controllerValidityError = utils.validateArweaveId(controller)
if controllerValidity == false then
return utils.reply(controllerValidityError)
end

local controllerExists = false

for i, v in ipairs(Controllers) do
if v == controller then
table.remove(Controllers, i)
controllerExists = true
break
end
end

if not controllerExists then
assert(controllerExists == true, "Controller does not exist")
end
end

function controllers.getControllers(msg)
Expand Down
14 changes: 14 additions & 0 deletions ant/src/initialize.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local utils = require(".utils")
local initialize = {}

function initialize.initialize(state)
Expand All @@ -7,8 +8,21 @@ function initialize.initialize(state)
assert(type(name) == "string", "name must be a string")
assert(type(ticker) == "string", "ticker must be a string")
assert(type(balances) == "table", "balances must be a table")
for k, v in pairs(balances) do
assert(utils.validateArweaveId(k), "balances keys must be strings")
assert(type(v) == "number", "balances values must be numbers")
end
assert(type(controllers) == "table", "controllers must be a table")
for _, v in ipairs(controllers) do
assert(utils.validateArweaveId(v), "controllers must be a list of arweave id's")
end
assert(type(records) == "table", "records must be a table")
for k, v in pairs(records) do
assert(utils.validateUndername(k), "records keys must be strings")
assert(type(v) == "table", "records values must be tables")
assert(utils.validateArweaveId(v.transactionId), "records transactionId must be a string")
assert(type(v.ttlSeconds) == "number", "records ttlSeconds must be a number")
end

Name = name
Ticker = ticker
Expand Down
Loading

0 comments on commit 3b20e6e

Please sign in to comment.