Skip to content

Commit

Permalink
chore(arns): add lua type docs and update tests for arns.lua (#162)
Browse files Browse the repository at this point in the history
A series of changes will come to add docs to functions and standardize
our error logic
  • Loading branch information
arielmelendez authored Nov 5, 2024
2 parents fcef672 + 0a748b0 commit 7a349a5
Show file tree
Hide file tree
Showing 10 changed files with 637 additions and 286 deletions.
16 changes: 6 additions & 10 deletions .luarc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@
"path": ["?.lua", "?/init.lua"]
},
"workspace": {
"library": ["src", "spec"]
"library": ["src", "spec",
"${addons}/busted/module/library",
"${3rd}/luassert/library"]
},
"diagnostics": {
"enable": true,
"globals": [
"describe",
"it",
"before_each",
"after_each",
"after_all",
"before_all"
]
"globals": []
}
}
},
"workspace.checkThirdParty": false
}
17 changes: 9 additions & 8 deletions spec/arns_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ describe("arns", function()
function()
local demandBefore = demand.getCurrentPeriodRevenue()
local purchasesBefore = demand.getCurrentPeriodPurchases()
local status, result =
pcall(arns.buyRecord, "test-name", "lease", 1, testAddress, timestamp, testProcessId)
local result = arns.buyRecord("test-name", "lease", 1, testAddress, timestamp, testProcessId)

assert.is_true(status)
assert.are.same({
purchasePrice = 600000000,
type = "lease",
Expand Down Expand Up @@ -448,10 +446,7 @@ describe("arns", function()

-- Reassign the name
local newProcessId = "test-this-is-valid-arweave-wallet-address-2"
local status, result = pcall(arns.reassignName, "test-name", testProcessId, timestamp, newProcessId)

-- Assertions
assert.is_true(status)
local result = arns.reassignName("test-name", testProcessId, timestamp, newProcessId)
assert.are.same(newProcessId, result.processId)
end)

Expand Down Expand Up @@ -819,6 +814,7 @@ describe("arns", function()
it("should create an auction and remove any existing record", function()
local auction = arns.createAuction("test-name", 1000000, "test-initiator")
local twoWeeksMs = 1000 * 60 * 60 * 24 * 14
assert(auction, "Auction should be created")
assert.are.equal(auction.name, "test-name")
assert.are.equal(auction.startTimestamp, 1000000)
assert.are.equal(auction.endTimestamp, twoWeeksMs + 1000000) -- 14 days late
Expand Down Expand Up @@ -868,6 +864,7 @@ describe("arns", function()
it("should return the correct price for an auction at a given timestamp permanently", function()
local startTimestamp = 1000000
local auction = arns.createAuction("test-name", startTimestamp, "test-initiator")
assert(auction, "Auction should be created")
local currentTimestamp = startTimestamp + 1000 * 60 * 60 * 24 * 7 -- 1 week into the auction
local decayRate = 0.02037911 / (1000 * 60 * 60 * 24 * 14)
local scalingExponent = 190
Expand All @@ -890,6 +887,7 @@ describe("arns", function()
it("should return the correct prices for an auction with for a lease", function()
local startTimestamp = 1729524023521
local auction = arns.createAuction("test-name", startTimestamp, "test-initiator")
assert(auction, "Auction should be created")
local intervalMs = 1000 * 60 * 15 -- 15 min (how granular we want to compute the prices)
local prices = auction:computePricesForAuction("lease", 1, intervalMs)
local baseFee = 500000000
Expand Down Expand Up @@ -1019,14 +1017,15 @@ describe("arns", function()
local floorPrice = baseFee + permabuyAnnualFee
local startPrice = floorPrice * 50
local auction = arns.createAuction("test-name", startTimestamp, "test-initiator")
assert(auction, "Auction should be created")
local result = arns.submitAuctionBid(
"test-name",
startPrice,
testAddressArweave,
bidTimestamp,
"test-process-id",
"permabuy",
nil
0
)
local totalDecay = auction.settings.decayRate * (bidTimestamp - startTimestamp)
local expectedPrice = math.floor(startPrice * ((1 - totalDecay) ^ auction.settings.scalingExponent))
Expand Down Expand Up @@ -1068,6 +1067,7 @@ describe("arns", function()
it("should throw an error if the bid is not high enough", function()
local startTimestamp = 1000000
local auction = arns.createAuction("test-name", startTimestamp, "test-initiator")
assert(auction, "Auction should be created")
local startPrice = auction:getPriceForAuctionAtTimestamp(startTimestamp, "permabuy", nil)
local status, error = pcall(
arns.submitAuctionBid,
Expand All @@ -1086,6 +1086,7 @@ describe("arns", function()
it("should throw an error if the bidder does not have enough balance", function()
local startTimestamp = 1000000
local auction = arns.createAuction("test-name", startTimestamp, "test-initiator")
assert(auction, "Auction should be created")
local requiredBid = auction:getPriceForAuctionAtTimestamp(startTimestamp, "permabuy", nil)
_G.Balances[testAddressArweave] = requiredBid - 1
local status, error = pcall(
Expand Down
74 changes: 36 additions & 38 deletions spec/vaults_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,25 @@ describe("vaults", function()
_G.Balances["test-this-is-valid-arweave-wallet-address-1"] = 200
local vaultOwner = "test-this-is-valid-arweave-wallet-address-1"
local lockLengthMs = constants.MIN_TOKEN_LOCK_TIME_MS
local msgId = "msgId"
local vault = vaults.createVault(vaultOwner, 100, lockLengthMs, startTimestamp, msgId)
local vaultId = "msgId"
local vault = vaults.createVault(vaultOwner, 100, lockLengthMs, startTimestamp, vaultId)
local increaseAmount = 100
local currentTimestamp = vault.startTimestamp + 1000
local increasedVault = vaults.increaseVault(vaultOwner, increaseAmount, msgId, currentTimestamp)
assert.are.same(vault.balance + increaseAmount, increasedVault.balance)
assert.are.same(vault.startTimestamp, increasedVault.startTimestamp)
assert.are.same(vault.endTimestamp, increasedVault.endTimestamp)
local increasedVault = vaults.increaseVault(vaultOwner, increaseAmount, vaultId, currentTimestamp)
assert.are.same(100 + increaseAmount, increasedVault.balance)
assert.are.same(startTimestamp, increasedVault.startTimestamp)
assert.are.same(startTimestamp + lockLengthMs, increasedVault.endTimestamp)
end)

it("should throw an error if insufficient balance", function()
_G.Balances["test-this-is-valid-arweave-wallet-address-1"] = 100
local vaultOwner = "test-this-is-valid-arweave-wallet-address-1"
local lockLengthMs = constants.MIN_TOKEN_LOCK_TIME_MS
local msgId = "msgId"
local vault = vaults.createVault(vaultOwner, 100, lockLengthMs, startTimestamp, msgId)
local vaultId = "msgId"
local vault = vaults.createVault(vaultOwner, 100, lockLengthMs, startTimestamp, vaultId)
local increaseAmount = 101
local currentTimestamp = vault.startTimestamp + 1000
local status, result = pcall(vaults.increaseVault, vaultOwner, increaseAmount, msgId, currentTimestamp)
local status, result = pcall(vaults.increaseVault, vaultOwner, increaseAmount, vaultId, currentTimestamp)
assert.is_false(status)
assert.match("Insufficient balance", result)
end)
Expand All @@ -123,13 +123,13 @@ describe("vaults", function()
_G.Balances["test-this-is-valid-arweave-wallet-address-1"] = 200
local vaultOwner = "test-this-is-valid-arweave-wallet-address-1"
local lockLengthMs = constants.MIN_TOKEN_LOCK_TIME_MS
local msgId = "msgId"
local vault = vaults.createVault(vaultOwner, 100, lockLengthMs, startTimestamp, msgId)
local vaultId = "msgId"
local vault = vaults.createVault(vaultOwner, 100, lockLengthMs, startTimestamp, vaultId)
local increaseAmount = 100
local currentTimestamp = vault.endTimestamp + 1
local status, result = pcall(vaults.increaseVault, vaultOwner, increaseAmount, msgId, currentTimestamp)
local status, result = pcall(vaults.increaseVault, vaultOwner, increaseAmount, vaultId, currentTimestamp)
assert.is_false(status)
assert.match("This vault has ended.", result)
assert.match("Vault has ended.", result)
end)

it("should throw an error if the vault not found", function()
Expand All @@ -146,38 +146,38 @@ describe("vaults", function()
_G.Balances["test-this-is-valid-arweave-wallet-address-1"] = 100
local vaultOwner = "test-this-is-valid-arweave-wallet-address-1"
local lockLengthMs = constants.MIN_TOKEN_LOCK_TIME_MS
local msgId = "msgId"
local vault = vaults.createVault(vaultOwner, 100, lockLengthMs, startTimestamp, msgId)
local vaultId = "msgId"
local vault = vaults.createVault(vaultOwner, 100, lockLengthMs, startTimestamp, vaultId)
local extendLengthMs = constants.MIN_TOKEN_LOCK_TIME_MS
local currentTimestamp = vault.startTimestamp + 1000
local extendedVault = vaults.extendVault(vaultOwner, extendLengthMs, currentTimestamp, msgId)
assert.are.same(vault.balance, extendedVault.balance)
assert.are.same(vault.startTimestamp, extendedVault.startTimestamp)
assert.are.same(vault.endTimestamp + extendLengthMs, extendedVault.endTimestamp)
local extendedVault = vaults.extendVault(vaultOwner, extendLengthMs, currentTimestamp, vaultId)
assert.are.same(100, extendedVault.balance)
assert.are.same(startTimestamp, extendedVault.startTimestamp)
assert.are.same(startTimestamp + lockLengthMs + extendLengthMs, extendedVault.endTimestamp)
end)

it("should throw an error if the vault is expired", function()
_G.Balances["test-this-is-valid-arweave-wallet-address-1"] = 100
local vaultOwner = "test-this-is-valid-arweave-wallet-address-1"
local lockLengthMs = constants.MIN_TOKEN_LOCK_TIME_MS
local msgId = "msgId"
local vault = vaults.createVault(vaultOwner, 100, lockLengthMs, startTimestamp, msgId)
local vaultId = "msgId"
local vault = vaults.createVault(vaultOwner, 100, lockLengthMs, startTimestamp, vaultId)
local extendLengthMs = constants.MIN_TOKEN_LOCK_TIME_MS
local currentTimestamp = vault.endTimestamp + 1000
local status, result = pcall(vaults.extendVault, vaultOwner, extendLengthMs, currentTimestamp, msgId)
local status, result = pcall(vaults.extendVault, vaultOwner, extendLengthMs, currentTimestamp, vaultId)
assert.is_false(status)
assert.match("This vault has ended.", result)
assert.match("Vault has ended.", result)
end)

it("should throw an error if the lock length would be larger than the maximum", function()
_G.Balances["test-this-is-valid-arweave-wallet-address-1"] = 100
local vaultOwner = "test-this-is-valid-arweave-wallet-address-1"
local lockLengthMs = constants.MAX_TOKEN_LOCK_TIME_MS
local msgId = "msgId"
local vault = vaults.createVault(vaultOwner, 100, lockLengthMs, startTimestamp, msgId)
local vaultId = "msgId"
local vault = vaults.createVault(vaultOwner, 100, lockLengthMs, startTimestamp, vaultId)
local extendLengthMs = constants.MAX_TOKEN_LOCK_TIME_MS + 1
local currentTimestamp = vault.startTimestamp + 1000
local status, result = pcall(vaults.extendVault, vaultOwner, extendLengthMs, currentTimestamp, msgId)
local status, result = pcall(vaults.extendVault, vaultOwner, extendLengthMs, currentTimestamp, vaultId)
assert.is_false(status)
assert.match(
"Invalid vault extension. Total lock time cannot be greater than "
Expand All @@ -199,11 +199,11 @@ describe("vaults", function()
_G.Balances["test-this-is-valid-arweave-wallet-address-1"] = 100
local vaultOwner = "test-this-is-valid-arweave-wallet-address-1"
local lockLengthMs = constants.MIN_TOKEN_LOCK_TIME_MS
local msgId = "msgId"
local vault = vaults.createVault(vaultOwner, 100, lockLengthMs, startTimestamp, msgId)
local vaultId = "msgId"
local vault = vaults.createVault(vaultOwner, 100, lockLengthMs, startTimestamp, vaultId)
local extendLengthMs = 0
local currentTimestamp = vault.startTimestamp + 1000
local status, result = pcall(vaults.extendVault, vaultOwner, extendLengthMs, currentTimestamp, msgId)
local status, result = pcall(vaults.extendVault, vaultOwner, extendLengthMs, currentTimestamp, vaultId)
assert.is_false(status)
assert.match("Invalid extend length. Must be a positive number.", result)
end)
Expand All @@ -218,11 +218,8 @@ describe("vaults", function()
local quantity = 50
local lockLengthMs = constants.MIN_TOKEN_LOCK_TIME_MS
local timestamp = 1000000
local msgId = "msgId"
local result, vault =
pcall(vaults.vaultedTransfer, from, recipient, quantity, lockLengthMs, timestamp, msgId)
assert.is_true(result)
assert.is_not_nil(vault)
local vaultId = "msgId"
local vault = vaults.vaultedTransfer(from, recipient, quantity, lockLengthMs, timestamp, vaultId)
assert.are.equal(50, _G.Balances[from])
assert.are.same({
balance = 50,
Expand All @@ -239,9 +236,9 @@ describe("vaults", function()
local quantity = 150
local lockLengthMs = constants.MIN_TOKEN_LOCK_TIME_MS
local timestamp = 1000000
local msgId = "msgId"
local vaultId = "msgId"
local status, result =
pcall(vaults.vaultedTransfer, from, recipient, quantity, lockLengthMs, timestamp, msgId)
pcall(vaults.vaultedTransfer, from, recipient, quantity, lockLengthMs, timestamp, vaultId)
assert.is_false(status)
assert.match("Insufficient balance", result)
end)
Expand All @@ -254,9 +251,10 @@ describe("vaults", function()
local quantity = 50
local lockLengthMs = constants.MIN_TOKEN_LOCK_TIME_MS - 1
local timestamp = 1000000
local msgId = "msgId"
local status = pcall(vaults.vaultedTransfer, from, recipient, quantity, lockLengthMs, timestamp, msgId)
local vaultId = "msgId"
local status = pcall(vaults.vaultedTransfer, from, recipient, quantity, lockLengthMs, timestamp, vaultId)
assert.is_false(status)
-- the string fails to match because the error message is not exactly as expected
end)
end)

Expand Down
Loading

0 comments on commit 7a349a5

Please sign in to comment.