Skip to content

Commit

Permalink
chore(test): check message data and tags in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Oct 22, 2024
1 parent fef030f commit 3cc1140
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 37 deletions.
1 change: 0 additions & 1 deletion src/arns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,6 @@ function arns.submitAuctionBid(name, bidAmount, bidder, timestamp, processId, ty
processId = processId,
startTimestamp = timestamp,
endTimestamp = type == "lease" and timestamp + constants.oneYearMs * years or nil,
type = type,
undernameLimit = constants.DEFAULT_UNDERNAME_COUNT,
purchasePrice = finalBidAmount,
}
Expand Down
76 changes: 48 additions & 28 deletions src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ DemandFactor = DemandFactor or {}
Owner = Owner or ao.env.Process.Owner
Protocol = Protocol or ao.env.Process.Id
Balances = Balances or {}
if not Balances[ao.env.Process.Id] then -- initialize the balance for the process id
if not Balances[Protocol] then -- initialize the balance for the process id
Balances = {
[ao.env.Process.Id] = math.floor(50000000 * 1000000), -- 50M IO
[Protocol] = math.floor(50000000 * 1000000), -- 50M IO
[Owner] = math.floor(constants.totalTokenSupply - (50000000 * 1000000)), -- 950M IO
}
end
Expand Down Expand Up @@ -1150,7 +1150,7 @@ 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" }, -- TODO: is this still right?
Tags = { Action = "Invalid-Delegate-Stake-Notice", Error = tostring(error) },
Data = tostring(error),
})
end, gar.delegateStake, from, target, quantity, tonumber(msg.Timestamp))
Expand Down Expand Up @@ -2175,30 +2175,40 @@ addEventingHandler("releaseName", utils.hasMatchingTag("Action", ActionMap.Relea
end

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

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

local auction = {
name = auctionOrError.name,
startTimestamp = auctionOrError.startTimestamp,
endTimestamp = auctionOrError.endTimestamp,
initiator = auctionOrError.initiator,
baseFee = auctionOrError.baseFee,
demandFactor = auctionOrError.demandFactor,
settings = auctionOrError.settings,
}
ao.send({
Target = initiator,
Action = "Auction-Notice",
Name = name,
Data = json.encode({
startTimestamp = result.startTimestamp,
endTimestamp = result.endTimestamp,
startPrice = result.startPrice,
floorPrice = result.floorPrice,
currentPrice = result.startPrice,
initiator = result.initiator,
type = result.type,
years = result.years,
}),
Data = json.encode(auction),
})
return
end)
Expand Down Expand Up @@ -2242,7 +2252,7 @@ addEventingHandler("auctionPrices", utils.hasMatchingTag("Action", ActionMap.Auc
local timestamp = tonumber(msg.Tags.Timestamp or msg.Timestamp)
local type = msg.Tags.Type or "permabuy"
local years = msg.Tags.Years and tonumber(msg.Tags.Years) or nil
local intervalMs = msg.Tags["Price-Interval"] and tonumber(msg.Tags["Price-Interval"]) or 15 * 60 * 1000 -- 15 minute intervals
local intervalMs = msg.Tags["Price-Interval-Ms"] and tonumber(msg.Tags["Price-Interval-Ms"]) or 15 * 60 * 1000 -- 15 minute intervals by default

if not auction then
ao.send({
Expand Down Expand Up @@ -2331,37 +2341,47 @@ addEventingHandler("auctionBid", utils.hasMatchingTag("Action", ActionMap.Auctio
return
end

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

-- send buy record notice and auction close notice?
local record = auctionBidOrError.record

-- send buy record notice and auction close notice
ao.send({
Target = bidder,
Action = ActionMap.BuyRecord .. "-Notice",
Data = json.encode(result.record),
Data = json.encode({
name = name,
startTimestamp = record.startTimestamp,
endTimestamp = record.endTimestamp,
undernameLimit = record.undernameLimit,
purchasePrice = record.purchasePrice,
processId = record.processId,
}),
})

ao.send({
Target = auction.initiator,
Action = "Debit-Notice",
Quantity = tostring(result.rewardForInitiator),
Quantity = tostring(auctionBidOrError.rewardForInitiator),
Data = json.encode({
type = type,
years = years,
name = auction.name,
bidder = result.bidder,
bidAmount = result.bidAmount,
rewardForInitiator = result.rewardForInitiator,
rewardForProtocol = result.rewardForProtocol,
record = result.record,
name = name,
bidder = auctionBidOrError.bidder,
bidAmount = auctionBidOrError.bidAmount,
rewardForInitiator = auctionBidOrError.rewardForInitiator,
rewardForProtocol = auctionBidOrError.rewardForProtocol,
record = record,
}),
})
end)
Expand Down
48 changes: 40 additions & 8 deletions tests/arns.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -534,18 +534,55 @@ describe('ArNS', async () => {
assert.equal(submitBidResult.Messages.length, 2);

// should send a buy record notice
const buyRecordNoticeTag = submitBidResult.Messages[0].Tags.find(
const buyRecordNoticeTag = submitBidResult.Messages?.[0]?.Tags?.find(
(tag) => tag.name === 'Action' && tag.value === 'Buy-Record-Notice',
);

assert.ok(buyRecordNoticeTag);

console.log(submitBidResult.Messages[0]);

// expect the target tag to be the bidder
assert.equal(submitBidResult.Messages?.[0]?.Target, bidderAddress);

const expectedRecord = {
processId,
purchasePrice: expectedPurchasePrice,
startTimestamp: bidTimestamp,
undernameLimit: 10,
};
const expectedRewardForInitiator = Math.floor(expectedPurchasePrice * 0.5);
const expectedRewardForProtocol =
expectedPurchasePrice - expectedRewardForInitiator;

// assert the data response contains the record
const buyRecordNoticeData = JSON.parse(submitBidResult.Messages?.[0]?.Data);
assert.deepEqual(buyRecordNoticeData, {
name: 'test-name',
...expectedRecord,
});

// should send a debit notice
const debitNoticeTag = submitBidResult.Messages[1].Tags.find(
const debitNoticeTag = submitBidResult.Messages?.[1]?.Tags?.find(
(tag) => tag.name === 'Action' && tag.value === 'Debit-Notice',
);
assert.ok(debitNoticeTag);

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

// assert the data response contains the record
const debitNoticeData = JSON.parse(submitBidResult.Messages?.[1]?.Data);
assert.deepEqual(debitNoticeData, {
record: expectedRecord,
bidder: bidderAddress,
bidAmount: expectedPurchasePrice,
rewardForInitiator: expectedRewardForInitiator,
rewardForProtocol: expectedRewardForProtocol,
name: 'test-name',
type: 'permabuy',
});

// should add the record to the registry
const recordResult = await handle(
{
Expand All @@ -558,16 +595,11 @@ describe('ArNS', async () => {
submitBidResult.Memory,
);

const expectedRewardForInitiator = Math.floor(expectedPurchasePrice * 0.5);
const expectedRewardForProtocol =
expectedPurchasePrice - expectedRewardForInitiator;

const record = JSON.parse(recordResult.Messages[0].Data);
const record = JSON.parse(recordResult.Messages?.[0]?.Data);
assert.deepEqual(record, {
processId,
purchasePrice: expectedPurchasePrice,
startTimestamp: bidTimestamp,
type: 'permabuy',
undernameLimit: 10,
});

Expand Down

0 comments on commit 3cc1140

Please sign in to comment.