Skip to content

Commit

Permalink
chore(events): add event data to auctions
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Oct 30, 2024
1 parent 98b08a5 commit d328160
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 48 deletions.
9 changes: 9 additions & 0 deletions src/arns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,15 @@ function arns.getAuctions()
return NameRegistry.auctions or {}
end

--- Submits a bid to an auction
--- @param name string The name of the auction
--- @param bidAmount number The amount of the bid
--- @param bidder string The address of the bidder
--- @param timestamp number The timestamp of the bid
--- @param processId string The processId of the bid
--- @param type string The type of the bid
--- @param years number The number of years for the bid
--- @return table The result of the bid including the auction, bidder, bid amount, reward for initiator, reward for protocol, and record
function arns.submitAuctionBid(name, bidAmount, bidder, timestamp, processId, type, years)
local auction = arns.getAuction(name)
if not auction then
Expand Down
114 changes: 66 additions & 48 deletions src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,25 @@ local function addRecordResultFields(ioEvent, result)
end
end

local function addAuctionResultFields(ioEvent, result)
ioEvent:addFieldsIfExist(result, {
"bidAmount",
"bidder",
"rewardForInitiator",
"rewardForProtocol",
-- TODO: we could return some computed values of the bid relative to start and end price
})
ioEvent:addFieldsIfExist(result.record, { "startTimestamp", "endTimestamp", "undernameLimit", "purchasePrice" })
ioEvent:addFieldsIfExist(result.auction, {
"name",
"initiator",
"startTimestamp",
"endTimestamp",
"baseFee",
"demandFactor",
})
end

local function addSupplyData(ioEvent, supplyData)
supplyData = supplyData or {}
ioEvent:addField("Circulating-Supply", supplyData.circulatingSupply or LastKnownCirculatingSupply)
Expand Down Expand Up @@ -2695,71 +2714,70 @@ addEventingHandler("auctionBid", utils.hasMatchingTag("Action", ActionMap.Auctio
years = years or 1
end
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
local auction = arns.getAuction(name)
assert(auction, "Auction not found")
end

local auction = arns.getAuction(name)
if not auction then
local shouldContinue = eventingPcall(msg.ioEvent, function(error)
ao.send({
Target = msg.From,
Action = "Invalid-" .. ActionMap.AuctionBid .. "-Notice",
Error = "Auction-Not-Found",
Tags = { Action = "Invalid-" .. ActionMap.AuctionBid .. "-Notice", Error = "Bad-Input" },
Data = tostring(error),
})
end, checkAssertions)
if not shouldContinue then
return
end

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

local record = auctionBidOrError.record
if result ~= nil then
local record = result.record
addAuctionResultFields(msg.ioEvent, result)
LastKnownCirculatingSupply = LastKnownCirculatingSupply - record.purchasePrice
addSupplyData(msg.ioEvent)

-- send buy record notice and auction close notice
ao.send({
Target = bidder,
Action = ActionMap.BuyRecord .. "-Notice",
Data = json.encode({
name = name,
startTimestamp = record.startTimestamp,
endTimestamp = record.endTimestamp,
undernameLimit = record.undernameLimit,
purchasePrice = record.purchasePrice,
processId = record.processId,
type = record.type,
}),
})
msg.ioEvent:addField("Records-Count", utils.lengthOfTable(NameRegistry.records))
msg.ioEvent:addField("Auctions-Count", utils.lengthOfTable(NameRegistry.auctions))
-- send buy record notice and auction close notice
ao.send({
Target = result.bidder,
Action = ActionMap.BuyRecord .. "-Notice",
Data = json.encode({
name = name,
startTimestamp = record.startTimestamp,
endTimestamp = record.endTimestamp,
undernameLimit = record.undernameLimit,
purchasePrice = record.purchasePrice,
processId = record.processId,
type = record.type,
}),
})

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

return process

0 comments on commit d328160

Please sign in to comment.