Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tick): update tick to use proper timestamp #9

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions spec/balances_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,32 @@ describe("balances", function()
end)

it("should transfer tokens", function()
local status, result = pcall(balances.transfer, "test-this-is-valid-arweave-wallet-address-2", "test-this-is-valid-arweave-wallet-address-1", 100)
local status, result = pcall(
balances.transfer,
"test-this-is-valid-arweave-wallet-address-2",
"test-this-is-valid-arweave-wallet-address-1",
100
)
assert.is_true(status)
assert.are.same(result["test-this-is-valid-arweave-wallet-address-2"], balances.getBalance("test-this-is-valid-arweave-wallet-address-2"))
assert.are.same(result["test-this-is-valid-arweave-wallet-address-1"], balances.getBalance("test-this-is-valid-arweave-wallet-address-1"))
assert.are.same(
result["test-this-is-valid-arweave-wallet-address-2"],
balances.getBalance("test-this-is-valid-arweave-wallet-address-2")
)
assert.are.same(
result["test-this-is-valid-arweave-wallet-address-1"],
balances.getBalance("test-this-is-valid-arweave-wallet-address-1")
)
assert.are.equal(100, balances.getBalance("test-this-is-valid-arweave-wallet-address-2"))
assert.are.equal(0, balances.getBalance("test-this-is-valid-arweave-wallet-address-1"))
end)

it("should error on insufficient balance", function()
local status, result = pcall(balances.transfer, "test-this-is-valid-arweave-wallet-address-2", "test-this-is-valid-arweave-wallet-address-1", 101)
local status, result = pcall(
balances.transfer,
"test-this-is-valid-arweave-wallet-address-2",
"test-this-is-valid-arweave-wallet-address-1",
101
)
assert.is_false(status)
assert.match("Insufficient balance", result)
assert.are.equal(0, balances.getBalance("test-this-is-valid-arweave-wallet-address-2"))
Expand Down
2 changes: 2 additions & 0 deletions spec/demand_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe("demand", function()
fees = constants.genesisFees,
}
demand.updateSettings({
periodZeroStartTimestamp = 0,
movingAvgPeriodCount = 7,
periodLengthMs = 60 * 1000 * 24, -- one day
demandFactorBaseValue = 1,
Expand Down Expand Up @@ -112,6 +113,7 @@ describe("demand", function()
describe("purchase count criteria", function()
before_each(function()
demand.updateSettings({
periodZeroStartTimestamp = 0,
movingAvgPeriodCount = 7,
periodLengthMs = 60 * 1000 * 24, -- one day
demandFactorBaseValue = 1,
Expand Down
55 changes: 39 additions & 16 deletions src/demand.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
local constants = require("constants")
local utils = require("utils")
local json = require("json")
local demand = {}

DemandFactor = DemandFactor
or {
startTimestamp = 1719381600000, -- 06-26-2024
currentPeriod = 0,
trailingPeriodPurchases = { 0, 0, 0, 0, 0, 0, 0 }, -- Acts as a ring buffer of trailing period purchase counts
trailingPeriodRevenues = { 0, 0, 0, 0, 0, 0 }, -- Acts as a ring buffer of trailing period revenues
Expand All @@ -16,16 +14,18 @@ DemandFactor = DemandFactor
fees = constants.genesisFees,
}

local demandFactorSettings = {
movingAvgPeriodCount = 7,
periodLengthMs = 60 * 60 * 1000 * 24, -- one day in milseconds
demandFactorBaseValue = 1,
demandFactorMin = 0.5,
demandFactorUpAdjustment = 0.05,
demandFactorDownAdjustment = 0.025,
stepDownThreshold = 3,
criteria = "revenue",
}
DemandFactorSettings = DemandFactorSettings
or {
periodZeroStartTimestamp = 1719381600000, -- 06/26/2025 @ 12:00am (UTC)
movingAvgPeriodCount = 7,
periodLengthMs = 60 * 60 * 1000 * 24, -- one day in milseconds
demandFactorBaseValue = 1,
demandFactorMin = 0.5,
demandFactorUpAdjustment = 0.05,
demandFactorDownAdjustment = 0.025,
stepDownThreshold = 3,
criteria = "revenue",
}

function demand.tallyNamePurchase(qty)
demand.incrementPurchasesThisPeriodRevenue(1)
Expand Down Expand Up @@ -53,6 +53,13 @@ end
function demand.isDemandIncreasing()
local currentPeriod = demand.getCurrentPeriod()
local settings = demand.getSettings()

-- check that we have settings
if not settings then
print("No settings found")
return false
end

local purchasesLastPeriod = demand.getTrailingPeriodPurchases()[currentPeriod] or 0
local revenueInLastPeriod = demand.getTrailingPeriodRevenues()[currentPeriod] or 0
local mvgAvgOfTrailingNamePurchases = demand.mvgAvgTrailingPurchaseCounts()
Expand All @@ -68,7 +75,14 @@ end
-- update at the end of the demand if the current timestamp results in a period greater than our current state
function demand.shouldUpdateDemandFactor(currentTimestamp)
local settings = demand.getSettings()
local calculatedPeriod = math.floor((currentTimestamp - DemandFactor.startTimestamp) / settings.periodLengthMs) + 1

if not settings or not settings.periodZeroStartTimestamp then
return false
end

local calculatedPeriod = math.floor(
(currentTimestamp - settings.periodZeroStartTimestamp) / settings.periodLengthMs
) + 1
return calculatedPeriod > demand.getCurrentPeriod()
end

Expand All @@ -80,6 +94,12 @@ function demand.updateDemandFactor(timestamp)

local settings = demand.getSettings()

-- check that we have settings
if not settings then
print("No settings found")
return
end

if demand.isDemandIncreasing() then
local upAdjustment = settings.demandFactorUpAdjustment
demand.setDemandFactor(demand.getDemandFactor() * (1 + upAdjustment))
Expand Down Expand Up @@ -146,7 +166,7 @@ function demand.getFees()
end

function demand.getSettings()
return demandFactorSettings
return utils.deepCopy(DemandFactorSettings)
end

function demand.getConsecutivePeriodsWithMinDemandFactor()
Expand All @@ -160,11 +180,14 @@ function demand.getCurrentPeriod()
end

function demand.updateSettings(settings)
demandFactorSettings = settings
if not settings then
return
end
DemandFactorSettings = settings
end

function demand.updateStartTimestamp(timestamp)
DemandFactor.startTimestamp = timestamp
DemandFactorSettings.periodZeroStartTimestamp = timestamp
end

function demand.updateCurrentPeriod(period)
Expand Down
22 changes: 11 additions & 11 deletions src/epochs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ local arns = require("arns")
local epochs = {}

Epochs = Epochs or {}

local epochSettings = {
prescribedNameCount = 5,
rewardPercentage = 0.0005, -- 0.05%
maxObservers = 50,
epochZeroStartTimestamp = 1719900000000, -- July 2nd, 00:00:00 UTC
durationMs = 60 * 1000 * 60 * 24, -- 24 hours
distributionDelayMs = 60 * 1000 * 30, -- 15 blocks / 30 minutes
}
EpochSettings = EpochSettings
or {
prescribedNameCount = 5,
rewardPercentage = 0.0005, -- 0.05%
maxObservers = 50,
epochZeroStartTimestamp = 1719900000000, -- July 2nd, 00:00:00 UTC
durationMs = 60 * 1000 * 60 * 24, -- 24 hours
distributionDelayMs = 60 * 1000 * 30, -- 15 blocks / 30 minutes
}

function epochs.getEpochs()
local epochs = utils.deepCopy(Epochs) or {}
Expand All @@ -31,7 +31,7 @@ function epochs.getObservers()
end

function epochs.getSettings()
return utils.deepCopy(epochSettings)
return utils.deepCopy(EpochSettings)
end

function epochs.getObservations()
Expand Down Expand Up @@ -371,7 +371,7 @@ end

-- for testing purposes
function epochs.updateEpochSettings(newSettings)
epochSettings = newSettings
EpochSettings = newSettings
end

-- Steps
Expand Down
41 changes: 21 additions & 20 deletions src/gar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@ local gar = {}

GatewayRegistry = GatewayRegistry or {}
-- TODO: any necessary state modifcations as we iterate go here
local garSettings = {
observers = {
maxPerEpoch = 50,
tenureWeightDays = 180,
tenureWeightPeriod = 180 * 24 * 60 * 60 * 1000, -- aproximately 2 years
maxTenureWeight = 4,
},
operators = {
minStake = 50000 * 1000000, -- 50,000 IO
withdrawLengthMs = 30 * 24 * 60 * 60 * 1000, -- 30 days to lower operator stake
maxDelegates = 10000,
leaveLengthMs = 90 * 24 * 60 * 60 * 1000, -- 90 days that balance will be vaulted
},
delegates = {
minStake = 500 * 1000000, -- 500 IO
withdrawLengthMs = 30 * 24 * 60 * 60 * 1000, -- 30 days
},
}
GatewayRegistrySettings = GatewayRegistrySettings
or {
observers = {
maxPerEpoch = 50,
tenureWeightDays = 180,
tenureWeightPeriod = 180 * 24 * 60 * 60 * 1000, -- aproximately 2 years
maxTenureWeight = 4,
},
operators = {
minStake = 50000 * 1000000, -- 50,000 IO
withdrawLengthMs = 30 * 24 * 60 * 60 * 1000, -- 30 days to lower operator stake
maxDelegates = 10000,
leaveLengthMs = 90 * 24 * 60 * 60 * 1000, -- 90 days that balance will be vaulted
},
delegates = {
minStake = 500 * 1000000, -- 500 IO
withdrawLengthMs = 30 * 24 * 60 * 60 * 1000, -- 30 days
},
}

function gar.joinNetwork(from, stake, settings, observerAddress, timeStamp)
gar.assertValidGatewayParameters(from, stake, settings, observerAddress)
Expand Down Expand Up @@ -338,7 +339,7 @@ function gar.delegateStake(from, target, qty, currentTimestamp)
end

function gar.getSettings()
return garSettings
return utils.deepCopy(GatewayRegistrySettings)
end

function gar.decreaseDelegateStake(gatewayAddress, delegator, qty, currentTimestamp, messageId)
Expand Down Expand Up @@ -543,7 +544,7 @@ end

-- for test purposes
function gar.updateSettings(newSettings)
garSettings = newSettings
GatewayRegistrySettings = newSettings
end

function gar.pruneGateways(currentTimestamp)
Expand Down
Loading
Loading