Skip to content

Commit

Permalink
chore(test): add tests for getTokenCost
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Oct 18, 2024
1 parent 4f6ec11 commit 3eda830
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
50 changes: 50 additions & 0 deletions spec/arns_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,56 @@ describe("arns", function()
end)
end

describe("getTokenCost", function()
it("should return the correct token cost for a lease", function()
local baseFee = 500000000
local years = 2
local demandFactor = 1.0
local expectedCost = ((years * baseFee * 0.20) + baseFee) * demandFactor
local intendedAction = {
intent = "Buy-Record",
purchaseType = "lease",
years = 2,
name = "test-name",
}
assert.are.equal(expectedCost, arns.getTokenCost(intendedAction))
end)
it("should return the correct token cost for a permabuy", function()
local baseFee = 500000000
local demandFactor = 1.0
local expectedCost = ((baseFee * 0.2 * 20) + baseFee) * demandFactor
local intendedAction = {
intent = "Buy-Record",
purchaseType = "permabuy",
name = "test-name",
}
assert.are.equal(expectedCost, arns.getTokenCost(intendedAction))
end)
it("should return the correct token cost for an undername", function()
_G.NameRegistry.records["test-name"] = {
endTimestamp = constants.oneYearMs,
processId = testProcessId,
purchasePrice = 600000000,
startTimestamp = 0,
type = "lease",
undernameLimit = 10,
}
local baseFee = 500000000
local undernamePercentageFee = 0.001
local increaseQty = 5
local demandFactor = 1.0
local yearsRemaining = 0.5
local expectedCost = baseFee * increaseQty * undernamePercentageFee * yearsRemaining * demandFactor
local intendedAction = {
intent = "Increase-Undername-Limit",
quantity = 5,
name = "test-name",
currentTimestamp = constants.oneYearMs / 2,
}
assert.are.equal(expectedCost, arns.getTokenCost(intendedAction))
end)
end)

describe("pruneRecords", function()
it("should prune records", function()
local currentTimestamp = 1000000000
Expand Down
9 changes: 5 additions & 4 deletions src/arns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,7 @@ function arns.calculateLeaseFee(baseFee, years, demandFactor)
end

function arns.calculateAnnualRenewalFee(baseFee, years)
local nameAnnualRegistrationFee = baseFee * constants.ANNUAL_PERCENTAGE_FEE
local totalAnnualRenewalCost = nameAnnualRegistrationFee * years
local totalAnnualRenewalCost = baseFee * constants.ANNUAL_PERCENTAGE_FEE * years
return math.floor(totalAnnualRenewalCost)
end

Expand Down Expand Up @@ -369,9 +368,11 @@ function arns.getTokenCost(intendedAction)
local purchaseType = intendedAction.purchaseType
local years = intendedAction.years
local name = intendedAction.name
assert(purchaseType == "lease" or purchaseType == "permabuy", "PurchaseType is invalid.")
assert(years >= 1 and years <= 5, "Years is invalid. Must be an integer between 1 and 5")
assert(type(name) == "string", "Name is required and must be a string.")
assert(purchaseType == "lease" or purchaseType == "permabuy", "PurchaseType is invalid.")
if purchaseType == "lease" then
assert(years >= 1 and years <= 5, "Years is invalid. Must be an integer between 1 and 5")
end
local baseFee = demand.getFees()[#name]
tokenCost = arns.calculateRegistrationFee(purchaseType, baseFee, years, demand.getDemandFactor())
elseif intendedAction.intent == "Extend-Lease" then
Expand Down

0 comments on commit 3eda830

Please sign in to comment.