Skip to content

Commit

Permalink
fix(arns): move auction out of buy record
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed May 13, 2024
1 parent 4dbdb09 commit c75c307
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 60 deletions.
22 changes: 8 additions & 14 deletions contract/spec/arns_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ describe("arns", function()

describe("buyRecord", function()
it("should add a valid lease buyRecord to records objec and transfer balance to the protocol", function()
local status, result =
pcall(arns.buyRecord, "test-name", "lease", 1, "Bob", false, timestamp, testProcessId)
local status, result = pcall(arns.buyRecord, "test-name", "lease", 1, "Bob", timestamp, testProcessId)
assert.is_true(status)
assert.are.same({
purchasePrice = 1500,
Expand All @@ -46,7 +45,7 @@ describe("arns", function()
end)

it("defaults to 1 year and lease when not provided", function()
local status, result = pcall(arns.buyRecord, "test-name", nil, nil, "Bob", false, timestamp, testProcessId)
local status, result = pcall(arns.buyRecord, "test-name", nil, nil, "Bob", timestamp, testProcessId)
assert.is_true(status)
assert.are.same({
purchasePrice = 1500,
Expand Down Expand Up @@ -76,7 +75,7 @@ describe("arns", function()
"should validate a permabuy request and add the record to global state and deduct balance from caller",
function()
local status, result =
pcall(arns.buyRecord, "test-permabuy", "permabuy", 1, "Bob", false, timestamp, testProcessId)
pcall(arns.buyRecord, "test-permabuy", "permabuy", 1, "Bob", timestamp, testProcessId)
assert.is_true(status)
assert.are.same({
purchasePrice = 3000,
Expand Down Expand Up @@ -113,8 +112,7 @@ describe("arns", function()
undernameCount = 10,
}
arns.records["test-name"] = existingRecord
local status, result =
pcall(arns.buyRecord, "test-name", "lease", 1, "Bob", false, timestamp, testProcessId)
local status, result = pcall(arns.buyRecord, "test-name", "lease", 1, "Bob", timestamp, testProcessId)
assert.is_false(status)
assert.match("Name is already registered", result)
assert.are.same(existingRecord, arns.records["test-name"])
Expand All @@ -126,8 +124,7 @@ describe("arns", function()
endTimestamp = 1000,
}
arns.reserved["test-name"] = reservedName
local status, result =
pcall(arns.buyRecord, "test-name", "lease", 1, "Bob", false, timestamp, testProcessId)
local status, result = pcall(arns.buyRecord, "test-name", "lease", 1, "Bob", timestamp, testProcessId)
assert.is_false(status)
assert.match("Name is reserved", result)
assert.are.same({}, arns.records)
Expand All @@ -139,8 +136,7 @@ describe("arns", function()
target = "Bob",
endTimestamp = 1000,
}
local status, result =
pcall(arns.buyRecord, "test-name", "lease", 1, "Bob", false, timestamp, testProcessId)
local status, result = pcall(arns.buyRecord, "test-name", "lease", 1, "Bob", timestamp, testProcessId)
local expectation = {
endTimestamp = timestamp + constants.oneYearMs,
processId = testProcessId,
Expand All @@ -156,17 +152,15 @@ describe("arns", function()

it("should throw an error if the record is in auction", function()
arns.auctions["test-name"] = {}
local status, result =
pcall(arns.buyRecord, "test-name", "lease", 1, "Bob", false, timestamp, testProcessId)
local status, result = pcall(arns.buyRecord, "test-name", "lease", 1, "Bob", timestamp, testProcessId)
assert.is_false(status)
assert.match("Name is in auction", result)
assert.are.same({}, arns.records)
end)

it("should throw an error if the user does not have enough funds", function()
token.balances["Bob"] = 0
local status, result =
pcall(arns.buyRecord, "test-name", "lease", 1, "Bob", false, timestamp, testProcessId)
local status, result = pcall(arns.buyRecord, "test-name", "lease", 1, "Bob", timestamp, testProcessId)
assert.is_false(status)
assert.match("Insufficient funds", result)
assert.are.same({}, arns.records)
Expand Down
53 changes: 7 additions & 46 deletions contract/src/arns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ local arns = {
fees = constants.genesisFees,
}

function arns.buyRecord(name, purchaseType, years, from, auction, timestamp, processId)
function arns.buyRecord(name, purchaseType, years, from, timestamp, processId)
-- don't catch, let the caller handle the error
arns.assertValidBuyRecord(name, years, purchaseType, auction, processId)
arns.assertValidBuyRecord(name, years, purchaseType, processId)
if purchaseType == nil then
purchaseType = "lease" -- set to lease by default
end
Expand Down Expand Up @@ -42,6 +42,10 @@ function arns.buyRecord(name, purchaseType, years, from, auction, timestamp, pro
error("Name is reserved")
end

if arns.isNameRequiredToBeAuction(name, purchaseType) then
error("Name is required to be auctioned")
end

local newRecord = {
processId = processId,
startTimestamp = timestamp,
Expand Down Expand Up @@ -207,7 +211,7 @@ function arns.calculateYearsBetweenTimestamps(startTimestamp, endTimestamp)
return yearsRemainingFloat
end

function arns.assertValidBuyRecord(name, years, purchaseType, auction, processId)
function arns.assertValidBuyRecord(name, years, purchaseType, processId)
-- Validate the presence and type of the 'name' field
if type(name) ~= "string" then
error("name is required and must be a string.")
Expand Down Expand Up @@ -251,13 +255,6 @@ function arns.assertValidBuyRecord(name, years, purchaseType, auction, processId
error("cannot permabuy name 11 characters or below at this time")
end
end

-- Validate the 'auction' field if present, ensuring it is a boolean value
if auction then
if type(auction) ~= "boolean" then
error("auction must be a boolean.")
end
end
end

function arns.assertValidExtendLease(record, currentTimestamp, years)
Expand Down Expand Up @@ -300,12 +297,6 @@ function arns.getMaxAllowedYearsExtensionForRecord(record, currentTimestamp)
return constants.maxLeaseLengthYears - yearsRemainingOnLease
end

-- This function is used to validate the increase of undernames for a record
-- It checks if the qty is within the allowed range and if the record exists
-- @param record The record to be validated
-- @param qty The quantity of undernames to be added
-- @param currentTimestamp The current timestamp
-- @return boolean, string The first return value indicates whether the increase is valid (true) or not (false),
function arns.assertValidIncreaseUndername(record, qty, currentTimestamp)
if not record then
error("Name is not registered")
Expand All @@ -327,36 +318,6 @@ function arns.assertValidIncreaseUndername(record, qty, currentTimestamp)
return true
end

function arns.isActiveReservedName(caller, reservedName, currentTimestamp)
if not reservedName then
return false
end

local target = reservedName.target
local endTimestamp = reservedName.endTimestamp
local permanentlyReserved = not target and not endTimestamp

if permanentlyReserved then
return true
end

local isCallerTarget = caller ~= nil and target == caller
local isActiveReservation = endTimestamp and endTimestamp > currentTimestamp

-- If the caller is not the target, and it's still active - the name is considered reserved
if not isCallerTarget and isActiveReservation then
return true
end
return false
end

function arns.isShortNameRestricted(name, currentTimestamp)
return (
#name < constants.MINIMUM_ALLOWED_NAME_LENGTH
and currentTimestamp < constants.SHORT_NAME_RESERVATION_UNLOCK_TIMESTAMP
)
end

function arns.isNameRequiredToBeAuction(name, type)
return (type == "permabuy" and #name < 12)
end
Expand Down

0 comments on commit c75c307

Please sign in to comment.