Skip to content

Commit

Permalink
Merge branch 'PE-7013-remove-record-fixes' into PE-7090
Browse files Browse the repository at this point in the history
  • Loading branch information
atticusofsparta committed Nov 13, 2024
2 parents 16220a9 + e9c8571 commit bae6207
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 296 deletions.
13 changes: 13 additions & 0 deletions spec/ant_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe("Arweave Name Token", function()
_G.Description = "ANT's description"
_G.Keywords = { "KEYWORD-1", "KEYWORD-2", "KEYWORD-3" }
_G.Denomination = 1
_G.Logo = ""
end)

it("Initializes the state of the process", function()
Expand Down Expand Up @@ -182,4 +183,16 @@ describe("Arweave Name Token", function()
balances.setKeywords(notAnArray)
end, "Keywords must be an array")
end)

it("should set the logo", function()
local logo = string.rep("1", 43)
balances.setLogo(logo)
assert.are.same(logo, _G.Logo)
end)
it("should not set the logo with invalid id", function()
local logo = string.rep("1", 42)
assert.has_error(function()
balances.setLogo(logo)
end)
end)
end)
40 changes: 17 additions & 23 deletions src/common/balances.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,82 @@
-- @module balances

local utils = require(".common.utils")
local json = require(".common.json")

local balances = {}

--- Checks if a wallet has sufficient balance.
---@param wallet string - The wallet address to check.
---@return boolean - Returns true if the wallet has a balance greater than 0, otherwise false.
function balances.walletHasSufficientBalance(wallet)
return Balances[wallet] ~= nil and Balances[wallet] > 0
end

--- Transfers the ANT to a specified wallet.
---@param to string - The wallet address to transfer the balance to.
---@return string - Returns the encoded JSON representation of the transferred balance.
---@return table<string, integer>
function balances.transfer(to)
utils.validateArweaveId(to)
Balances = { [to] = 1 }
--luacheck: ignore Owner Controllers
Owner = to
Controllers = {}
return json.encode({ [to] = 1 })

return { [to] = 1 }
end

--- Retrieves the balance of a specified wallet.
---@param address string - The wallet address to retrieve the balance from.
---@return number - Returns the balance of the specified wallet.
---@return integer - Returns the balance of the specified wallet.
function balances.balance(address)
utils.validateArweaveId(address)
local balance = Balances[address] or 0
return balance
end

--- Retrieves all balances.
---@return string - Returns the encoded JSON representation of all balances.
---@return table<string, integer> - Returns the encoded JSON representation of all balances.
function balances.balances()
return json.encode(Balances)
return Balances
end

--- Sets the name of the ANT.
---@param name string - The name to set.
---@return string - Returns the encoded JSON representation of the updated name.
---@return table<string, string>
function balances.setName(name)
assert(type(name) == "string", "Name must be a string")
Name = name
return json.encode({ name = Name })
return { Name = Name }
end

--- Sets the ticker of the ANT.
---@param ticker string - The ticker to set.
---@return string - Returns the encoded JSON representation of the updated ticker.
---@return table<string, string>
function balances.setTicker(ticker)
assert(type(ticker) == "string", "Ticker must be a string")
Ticker = ticker
return json.encode({ ticker = Ticker })
return { Ticker = Ticker }
end

--- Sets the description of the ANT.
---@param description string - The description to set.
---@return string - Returns the encoded JSON representation of the updated description.
---@return table<string, string>
function balances.setDescription(description)
assert(type(description) == "string", "Description must be a string")
assert(#description <= 512, "Description must not be longer than 512 characters")
Description = description
return json.encode({ description = Description })
return { Description = Description }
end

--- Sets the keywords of the ANT.
---@param keywords table - The keywords to set.
---@return string - Returns the encoded JSON representation of the updated keywords.
---@return table<string, string>
function balances.setKeywords(keywords)
utils.validateKeywords(keywords)

Keywords = keywords
return json.encode({ keywords = Keywords })
return { Keywords = Keywords }
end

--- Sets the logo of the ANT.
---@param logo string - The Arweave transaction ID that represents the logo.
---@return string - Returns the encoded JSON representation of the updated logo.
---@return table<string, string>
function balances.setLogo(logo)
utils.validateArweaveId(logo)
Logo = logo
return json.encode({ logo = Logo })
return { Logo = Logo }
end

return balances
13 changes: 6 additions & 7 deletions src/common/controllers.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
local json = require(".common.json")
local utils = require(".common.utils")

local controllers = {}

--- Set a controller.
---@param controller string The controller to set.
---@return string The encoded JSON representation of the updated controllers.
---@return string[]
function controllers.setController(controller)
utils.validateArweaveId(controller)

Expand All @@ -14,12 +13,12 @@ function controllers.setController(controller)
end

table.insert(Controllers, controller)
return json.encode(Controllers)
return Controllers
end

--- Remove a controller.
---@param controller string The controller to remove.
---@return string The encoded JSON representation of the updated controllers.
---@return string[]
function controllers.removeController(controller)
utils.validateArweaveId(controller)
local controllerExists = false
Expand All @@ -33,13 +32,13 @@ function controllers.removeController(controller)
end

assert(controllerExists ~= nil, "Controller does not exist")
return json.encode(Controllers)
return Controllers
end

--- Get all controllers.
---@return string The encoded JSON representation of the controllers.
---@return string[]
function controllers.getControllers()
return json.encode(Controllers)
return Controllers
end

return controllers
10 changes: 5 additions & 5 deletions src/common/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ function ant.init()
-- utils
local json = require(".common.json")
local utils = require(".common.utils")
local notices = require(".common.notices")
local camel = utils.camelCase
local createActionHandler = utils.createActionHandler

Expand Down Expand Up @@ -36,7 +37,7 @@ function ant.init()
---@alias Description string
---@description A brief description of this ANT up to 255 characters
Description = Description or "A brief description of this ANT."
---@alias Keywords table<string>
---@alias Keywords string[]
---@description A list of keywords that describe this ANT. Each keyword must be a string, unique, and less than 32 characters. There can be up to 16 keywords
Keywords = Keywords or {}
---@alias Denomination integer
Expand Down Expand Up @@ -96,8 +97,8 @@ function ant.init()
utils.validateOwner(msg.From)
balances.transfer(recipient)
if not msg.Cast then
ao.send(utils.notices.debit(msg))
ao.send(utils.notices.credit(msg))
ao.send(notices.debit(msg))
ao.send(notices.credit(msg))
end
end)

Expand Down Expand Up @@ -213,7 +214,6 @@ function ant.init()

createActionHandler(ActionMap.SetLogo, function(msg)
utils.assertHasPermission(msg.From)
utils.validateArweaveId(msg.Logo)
return balances.setLogo(msg.Logo)
end)

Expand All @@ -222,7 +222,7 @@ function ant.init()
end)

createActionHandler(ActionMap.State, function(msg)
utils.notices.notifyState(msg, msg.From)
notices.notifyState(msg, msg.From)
end)

-- IO Network Contract Handlers
Expand Down
83 changes: 83 additions & 0 deletions src/common/notices.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
local json = require("json")
local notices = {}

--- @param oldMsg AoMessage
--- @param newMsg AoMessage
--- @description Add forwarded tags to the new message
--- @return AoMessage
function notices.addForwardedTags(oldMsg, newMsg)
for tagName, tagValue in pairs(oldMsg) do
-- Tags beginning with "X-" are forwarded
if string.sub(tagName, 1, 2) == "X-" then
newMsg[tagName] = tagValue
end
end
return newMsg
end

--- @param msg AoMessage
--- @description Create a credit notice message
--- @return AoMessage
function notices.credit(msg)
return notices.addForwardedTags(msg, {
Target = msg.Recipient,
Action = "Credit-Notice",
Sender = msg.From,
Quantity = tostring(1),
})
end

--- @param msg AoMessage
--- @description Create a debit notice message
--- @return AoMessage
function notices.debit(msg)
return notices.addForwardedTags(msg, {
Target = msg.From,
Action = "Debit-Notice",
Recipient = msg.Recipient,
Quantity = tostring(1),
})
end

--- @param noticesToSend table<AoMessage>
function notices.sendNotices(noticesToSend)
for _, notice in ipairs(noticesToSend) do
ao.send(notice)
end
end

--- @param msg AoMessage
--- @param target string
--- @description Notify the target of the current state
--- @return nil
function notices.notifyState(msg, target)
if not target then
print("No target specified for state notice")
return
end

---@type AntState
local state = {
Records = Records,
Controllers = Controllers,
Balances = Balances,
Owner = Owner,
Name = Name,
Ticker = Ticker,
Logo = Logo,
Description = Description,
Keywords = Keywords,
Denomination = Denomination,
TotalSupply = TotalSupply,
Initialized = Initialized,
["Source-Code-TX-ID"] = SourceCodeTxId,
}

ao.send(notices.addForwardedTags(msg, {
Target = target,
Action = "State-Notice",
Data = json.encode(state),
}))
end

return notices
15 changes: 7 additions & 8 deletions src/common/records.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local utils = require(".common.utils")
local json = require(".common.json")
local records = {}
-- defaults to landing page txid
Records = Records or { ["@"] = { transactionId = "-k7t8xMoB8hW482609Z9F4bTFMC3MnuW8bTvTyT8pFI", ttlSeconds = 3600 } }
Expand All @@ -8,7 +7,7 @@ Records = Records or { ["@"] = { transactionId = "-k7t8xMoB8hW482609Z9F4bTFMC3Mn
---@param name string The name of the record.
---@param transactionId string The transaction ID of the record.
---@param ttlSeconds number The time-to-live in seconds for the record.
---@return string The encoded JSON representation of the record.
---@return Record
function records.setRecord(name, transactionId, ttlSeconds)
utils.validateUndername(name)
utils.validateArweaveId(transactionId)
Expand All @@ -25,29 +24,29 @@ function records.setRecord(name, transactionId, ttlSeconds)
ttlSeconds = ttlSeconds,
}

return json.encode({
return {
transactionId = transactionId,
ttlSeconds = ttlSeconds,
})
}
end

--- Remove a record from the ANT.
---@param name string The name of the record to remove.
---@return string The encoded JSON representation of the deletion message.
---@return table<string, Record> Returns the records of the ANT
function records.removeRecord(name)
utils.validateUndername(name)
Records[name] = nil
return json.encode({ message = "Record deleted" })
return Records
end

--- Get a record from the ANT.
---@param name string The name of the record to retrieve.
---@return string The encoded JSON representation of the record.
---@return Record
function records.getRecord(name)
utils.validateUndername(name)
assert(Records[name] ~= nil, "Record does not exist")

return json.encode(Records[name])
return Records[name]
end

--- Get all records from the ANT
Expand Down
4 changes: 2 additions & 2 deletions src/common/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
--- Name: string,
--- Ticker: string,
--- Description: string,
--- Keywords: string,
--- Keywords: table<string>,
--- Logo: string,
--- Balances: table<string, integer>,
--- Owner: string,
--- Controllers: table<string>,
--- Controllers: string[],
--- Denomination: integer,
--- TotalSupply: integer,
--- Initialized: boolean,
Expand Down
Loading

0 comments on commit bae6207

Please sign in to comment.