Skip to content

Commit

Permalink
chore(test): standardize PROCESS_OWNER and PROCESS_ID
Browse files Browse the repository at this point in the history
This is so we can discern where tokens go when auctions (and other events) happen.
  • Loading branch information
dtfiedler committed Oct 9, 2024
1 parent 3ba2bce commit 80b9792
Show file tree
Hide file tree
Showing 9 changed files with 716 additions and 493 deletions.
4 changes: 2 additions & 2 deletions spec/arns_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ describe("arns", function()
local auctionIntervalMs = 1000 * 60 * 2 -- ~2 min per price interval
local bidTimestamp = startTimestamp + auctionIntervalMs - 1 -- 1 ms before the next price interval
local auction = arns.createAuction("test-name", "permabuy", startTimestamp, "test-initiator")
local wonAuctionRecord = arns.submitAuctionBid(
local result = arns.submitAuctionBid(
"test-name",
auction.startPrice,
testAddressArweave,
Expand All @@ -665,7 +665,7 @@ describe("arns", function()
assert.are.equal(balances[_G.ao.id], expectedPrice * 0.5)
assert.are.equal(NameRegistry.auctions["test-name"], nil)
assert.same(expectedRecord, NameRegistry.records["test-name"])
assert.same(expectedRecord, wonAuctionRecord)
assert.same(expectedRecord, result.record)
end
)

Expand Down
10 changes: 9 additions & 1 deletion src/arns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ function arns.submitAuctionBid(name, bidAmount, bidder, timestamp, processId)
local record = {
processId = processId,
startTimestamp = timestamp,
endTimestamp = auction.type == "lease" and timestamp + constants.oneYearMs * auction.years or nil,
type = auction.type,
undernameLimit = constants.DEFAULT_UNDERNAME_COUNT,
purchasePrice = finalBidAmount,
Expand All @@ -539,7 +540,14 @@ function arns.submitAuctionBid(name, bidAmount, bidder, timestamp, processId)
balances.transfer(ao.id, bidder, rewardForProtocol)
arns.removeAuction(name)
arns.addRecord(name, record)
return arns.getRecord(name)
return {
auction = auction,
bidder = bidder,
bidAmount = finalBidAmount,
rewardForInitiator = rewardForInitiator,
rewardForProtocol = rewardForProtocol,
record = record,
}
end

function arns.removeAuction(name)
Expand Down
94 changes: 90 additions & 4 deletions src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ Denomination = 6
DemandFactor = DemandFactor or {}
Owner = Owner or ao.env.Process.Owner
Balances = Balances or {}
if not Balances[ao.id] then -- initialize the balance for the process id
if not Balances[ao.env.Process.Id] then -- initialize the balance for the process id
Balances = {
[ao.id] = math.floor(50000000 * 1000000), -- 50M IO
[ao.env.Process.Id] = math.floor(50000000 * 1000000), -- 50M IO
[Owner] = math.floor(constants.totalTokenSupply - (50000000 * 1000000)), -- 950M IO
}
end
Expand Down Expand Up @@ -85,6 +85,7 @@ local ActionMap = {
-- auctions
AuctionInfo = "Auction-Info",
ReleaseName = "Release-Name",
AuctionBid = "Auction-Bid",
}

local function eventingPcall(ioEvent, onError, fnToCall, ...)
Expand Down Expand Up @@ -1006,8 +1007,8 @@ addEventingHandler(ActionMap.DelegateStake, utils.hasMatchingTag("Action", Actio
local shouldContinue2, gateway = eventingPcall(msg.ioEvent, function(error)
ao.send({
Target = from,
Tags = { Action = "Invalid-Delegate-Stake-Notice", Error = "Invalid-Delegate-Stake", Message = error }, -- TODO: is this still right?
Data = json.encode(error),
Tags = { Action = "Invalid-Delegate-Stake-Notice", Error = "Invalid-Delegate-Stake" }, -- TODO: is this still right?
Data = tostring(error),
})
end, gar.delegateStake, from, target, quantity, tonumber(msg.Timestamp))
if not shouldContinue2 then
Expand Down Expand Up @@ -1906,4 +1907,89 @@ addEventingHandler("auctionInfo", utils.hasMatchingTag("Action", ActionMap.Aucti
})
end)

addEventingHandler("auctionBid", utils.hasMatchingTag("Action", ActionMap.AuctionBid), function(msg)
local name = string.lower(msg.Tags.Name)
local bidAmount = msg.Tags.Quantity and tonumber(msg.Tags.Quantity) or nil -- if nil, we use the current bid price
local bidder = utils.formatAddress(msg.From)
local processId = utils.formatAddress(msg.Tags["Process-Id"])
local timestamp = tonumber(msg.Timestamp)

-- assert name, bidder, processId are provided
local checkAssertions = function()
assert(name and #name > 0, "Name is required")
assert(bidder and utils.isValidAOAddress(bidder), "Bidder is required")
assert(processId and utils.isValidAOAddress(processId), "Process-Id is required")
assert(timestamp and timestamp > 0, "Timestamp is required")
-- if bidAmount is not nil assert that it is a number
if bidAmount then
assert(
type(bidAmount) == "number" and bidAmount > 0 and utils.isInteger(bidAmount),
"Bid amount must be a positive integer"
)
end
end

local validateStatus, errorMessage = pcall(checkAssertions)
if not validateStatus then
ao.send({
Target = msg.From,
Action = "Invalid-" .. ActionMap.AuctionBid .. "-Notice",
Error = "Bad-Input",
Data = tostring(errorMessage),
})
return
end

local auction = arns.getAuction(name)
if not auction then
ao.send({
Target = msg.From,
Action = "Invalid-" .. ActionMap.AuctionBid .. "-Notice",
Error = "Auction-Not-Found",
})
return
end

local status, result = pcall(arns.submitAuctionBid, name, bidAmount, bidder, timestamp, processId)
if not status then
ao.send({
Target = msg.From,
Action = "Invalid-" .. ActionMap.AuctionBid .. "-Notice",
Error = "Auction-Bid-Error",
Data = tostring(result),
})
return
end

-- send buy record notice and auction close notice?
ao.send({
Target = bidder,
Action = ActionMap.BuyRecord .. "-Notice",
Data = json.encode(result.record),
})

ao.send({
Target = auction.initiator,
Action = "Debit-Notice",
Quantity = tostring(result.rewardForInitiator),
Data = json.encode({
auction = {
name = name,
startTimestamp = auction.startTimestamp,
endTimestamp = auction.endTimestamp,
startPrice = auction.startPrice,
floorPrice = auction.floorPrice,
initiator = auction.initiator,
type = auction.type,
years = auction.years,
},
bidder = result.bidder,
bidAmount = result.bidAmount,
rewardForInitiator = result.rewardForInitiator,
rewardForProtocol = result.rewardForProtocol,
record = result.record,
}),
})
end)

return process
Loading

0 comments on commit 80b9792

Please sign in to comment.