Skip to content

Commit

Permalink
chore(events): add auction event data for creating new auctions
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Oct 30, 2024
1 parent d328160 commit 69491c2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 43 deletions.
83 changes: 47 additions & 36 deletions src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2497,73 +2497,84 @@ end)
-- AUCTION HANDLER
addEventingHandler("releaseName", utils.hasMatchingTag("Action", ActionMap.ReleaseName), function(msg)
-- validate the name and process id exist, then create the auction using the auction function
local name = msg.Tags.Name
local processId = msg.From
local name = string.lower(msg.Tags.Name)
local processId = utils.formatAddress(msg.From)
local record = arns.getRecord(name)
local initiator = msg.Tags.Initiator
local initiator = utils.formatAddress(msg.Tags.Initiator or msg.From)
local timestamp = tonumber(msg.Timestamp)

-- TODO: validate processId and initiator are valid addresses
if not record then
ao.send({
Target = msg.From,
Action = "Invalid-" .. ActionMap.ReleaseName .. "-Notice",
Error = "Record-Not-Found",
})
return
local checkAssertions = function()
assert(name and #name > 0, "Name is required")
assert(processId and utils.isValidAOAddress(processId), "Process-Id is required")
assert(initiator and utils.isValidAOAddress(initiator), "Initiator is required")
assert(record, "Record not found")
assert(record.type == "permabuy", "Only permabuy names can be released")
assert(record.processId == processId, "Process-Id mismatch")
end

if record.processId ~= processId then
local shouldContinue = eventingPcall(msg.ioEvent, function(error)
ao.send({
Target = msg.From,
Action = "Invalid-" .. ActionMap.ReleaseName .. "-Notice",
Error = "Process-Id-Mismatch",
Error = "Bad-Input",
Data = tostring(error),
})
return
end

if record.type ~= "permabuy" then
ao.send({
Target = msg.From,
Action = "Invalid-" .. ActionMap.ReleaseName .. "-Notice",
Error = "Invalid-Record-Type",
-- only permabought names can be released by the process that owns the name
})
end, checkAssertions)
if not shouldContinue then
return
end

-- we should be able to create the auction here
local status, auctionOrError = pcall(arns.createAuction, name, tonumber(msg.Timestamp), initiator)
if not status then
local status, auctionData = eventingPcall(msg.ioEvent, function(error)
ao.send({
Target = msg.From,
Action = "Invalid-" .. ActionMap.ReleaseName .. "-Notice",
Error = "Auction-Creation-Error",
Data = tostring(auctionOrError),
Data = tostring(error),
})
return
end
end, arns.createAuction, name, timestamp, initiator)

if not auctionOrError or not auctionOrError.name then
if not status or not auctionData then
ao.send({
Target = msg.From,
Action = "Invalid-" .. ActionMap.ReleaseName .. "-Notice",
Error = "Auction-Creation-Error",
Data = tostring(auctionOrError),
Data = tostring(auctionData),
})
return
end

-- add the auction result fields
addAuctionResultFields(msg.ioEvent, {
name = name,
auction = auctionData,
})

-- note: no change to token supply here - only on auction bids
msg.ioEvent:addField("Auctions-Count", utils.lengthOfTable(NameRegistry.auctions))
msg.ioEvent:addField("Records-Count", utils.lengthOfTable(NameRegistry.records))

local auction = {
name = auctionOrError.name,
startTimestamp = auctionOrError.startTimestamp,
endTimestamp = auctionOrError.endTimestamp,
initiator = auctionOrError.initiator,
baseFee = auctionOrError.baseFee,
demandFactor = auctionOrError.demandFactor,
settings = auctionOrError.settings,
name = name,
startTimestamp = auctionData.startTimestamp,
endTimestamp = auctionData.endTimestamp,
initiator = auctionData.initiator,
baseFee = auctionData.baseFee,
demandFactor = auctionData.demandFactor,
settings = auctionData.settings,
}

-- send to the initiator and the process that released the name
ao.send({
Target = msg.From,
Target = initiator,
Action = "Auction-Notice",
Name = name,
Data = json.encode(auction),
})
ao.send({
Target = processId,
Action = "Auction-Notice",
Name = name,
Data = json.encode(auction),
Expand Down
16 changes: 9 additions & 7 deletions tests/arns.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ describe('ArNS', async () => {
it('should create an auction for an existing permabuy record owned by a process id, accept a bid and add the new record to the registry', async () => {
// buy the name first
const processId = ''.padEnd(43, 'a');
const initiator = 'ant-owner-'.padEnd(43, '0'); // owner of the ANT at the time of release
const { mem, record: initialRecord } = await runBuyRecord({
sender: STUB_ADDRESS,
processId,
Expand All @@ -564,7 +565,7 @@ describe('ArNS', async () => {
Tags: [
{ name: 'Action', value: 'Release-Name' },
{ name: 'Name', value: 'test-name' },
{ name: 'Initiator', value: 'test-owner-of-ant' }, // simulate who the owner is of the ANT process when sending the message
{ name: 'Initiator', value: initiator }, // simulate who the owner is of the ANT process when sending the message
],
From: processId,
Owner: processId,
Expand All @@ -576,6 +577,7 @@ describe('ArNS', async () => {
const releaseNameErrorTag = releaseNameResult.Messages?.[0]?.Tags?.find(
(tag) => tag.name === 'Error',
);

assert.equal(releaseNameErrorTag, undefined);

// fetch the auction
Expand All @@ -599,7 +601,7 @@ describe('ArNS', async () => {
const expectedStartTimestamp = STUB_TIMESTAMP;
assert.deepEqual(auction, {
name: 'test-name',
initiator: 'test-owner-of-ant',
initiator: initiator,
startTimestamp: auction.startTimestamp,
endTimestamp: expectedStartTimestamp + 60 * 60 * 1000 * 24 * 14,
baseFee: 500000000,
Expand All @@ -612,7 +614,7 @@ describe('ArNS', async () => {
},
});

// // TRANSFER FROM THE OWNER TO A NEW STUB ADDRESS
// TRANSFER FROM THE OWNER TO A NEW STUB ADDRESS
const bidderAddress = 'auction-bidder-'.padEnd(43, '0');
const bidTimestamp = auction.startTimestamp + 60 * 1000; // same as the original interval but 1 minute after the auction has started
const decayRate = auction.settings.decayRate;
Expand Down Expand Up @@ -703,7 +705,7 @@ describe('ArNS', async () => {
assert.ok(debitNoticeTag);

// expect the target to be to the initiator
assert.equal(submitBidResult.Messages?.[1]?.Target, 'test-owner-of-ant');
assert.equal(submitBidResult.Messages?.[1]?.Target, initiator);

// assert the data response contains the record
const debitNoticeData = JSON.parse(submitBidResult.Messages?.[1]?.Data);
Expand Down Expand Up @@ -750,7 +752,7 @@ describe('ArNS', async () => {
initialRecord.purchasePrice +
expectedRewardForProtocol;
const balances = JSON.parse(balancesResult.Messages[0].Data);
assert.equal(balances['test-owner-of-ant'], expectedRewardForInitiator);
assert.equal(balances[initiator], expectedRewardForInitiator);
assert.equal(balances[PROCESS_ID], expectedProtocolBalance);
assert.equal(balances[bidderAddress], 0);
});
Expand Down Expand Up @@ -955,6 +957,7 @@ describe('ArNS', async () => {
it('should compute the prices of an auction at a specific interval', async () => {
// buy the name first
const processId = ''.padEnd(43, 'a');
const initiator = 'ant-owner-'.padEnd(43, '0'); // owner of the ANT at the time of release
const { mem } = await runBuyRecord({
sender: STUB_ADDRESS,
processId,
Expand All @@ -966,7 +969,7 @@ describe('ArNS', async () => {
Tags: [
{ name: 'Action', value: 'Release-Name' },
{ name: 'Name', value: 'test-name' },
{ name: 'Initiator', value: 'test-owner-of-ant' }, // simulate who the owner is of the ANT process when sending the message
{ name: 'Initiator', value: initiator }, // simulate who the owner is of the ANT process when sending the message
],
From: processId,
Owner: processId,
Expand All @@ -979,7 +982,6 @@ describe('ArNS', async () => {
(tag) => tag.name === 'Error',
);
assert.equal(releaseNameErrorTag, undefined);
assert.equal(releaseNameResult.Messages?.[0]?.Target, processId);

// fetch the auction
const auctionResult = await handle(
Expand Down

0 comments on commit 69491c2

Please sign in to comment.