Skip to content

Commit

Permalink
fix(df): add handler to main.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed May 8, 2024
1 parent ddfeceb commit 179e30d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 39 deletions.
24 changes: 12 additions & 12 deletions contract/spec/demand_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,44 @@ require("demand")
local constants = require("constants")

describe("demand", function()
local fees = Fees:init(constants.genesisFees)
local fees = Fees:new(constants.genesisFees)
local settings = constants.DEMAND_SETTINGS

it("should tally name purchase", function()
local df = DemandFactor:init(settings, fees)
local df = DemandFactor:new(settings, fees)
df:tallyNamePurchase(1)
assert.are.equal(1, df.purchasesThisPeriod)
assert.are.equal(1, df.revenueThisPeriod)
end)

it("should calculate moving average of trailing purchase counts", function()
local df = DemandFactor:init(settings, fees)
local df = DemandFactor:new(settings, fees)
df.trailingPeriodPurchases = { 1, 2, 3, 4, 5, 6, 7 }
assert.are.equal(4, df:mvgAvgTrailingPurchaseCounts())
end)

it("should calculate moving average of trailing revenues", function()
local df = DemandFactor:init(settings, fees)
local df = DemandFactor:new(settings, fees)
df.trailingPeriodRevenues = { 1, 2, 3, 4, 5, 6 }
assert.are.equal(3.5, df:mvgAvgTrailingRevenues())
end)

it("should return true when demand is increasing based on revenue", function()
local df = DemandFactor:init(settings, fees)
local df = DemandFactor:new(settings, fees)
df.revenueThisPeriod = 10
df.trailingPeriodRevenues = { 10, 0, 0, 0, 0, 0, 0 }
assert.is_true(df:isDemandIncreasing())
end)

it("should return false when demand is is not increasing based on revenue", function()
local df = DemandFactor:init(settings, fees)
local df = DemandFactor:new(settings, fees)
df.revenueThisPeriod = 0
df.trailingPeriodRevenues = { 0, 10, 10, 10, 10, 10 }
assert.is_false(df:isDemandIncreasing())
end)

it("should return true when demand is increasing for purchases based criteria", function()
local df = DemandFactor:init({
local df = DemandFactor:new({
movingAvgPeriodCount = 7,
periodLengthMs = 60 * 1000 * 24, -- one day
demandFactorBaseValue = 1,
Expand All @@ -55,7 +55,7 @@ describe("demand", function()
end)

it("should return false when demand is not increasing for purchases based criteria", function()
local df = DemandFactor:init({
local df = DemandFactor:new({
movingAvgPeriodCount = 7,
periodLengthMs = 60 * 1000 * 24, -- one day
demandFactorBaseValue = 1,
Expand All @@ -71,15 +71,15 @@ describe("demand", function()
end)

it("should update demand factor if timestamp is greater than period length", function()
local df = DemandFactor:init(settings, fees)
local df = DemandFactor:new(settings, fees)
local currentTimestamp = settings.periodLengthMs + 1
assert.is_true(df:shouldUpdateDemandFactor(currentTimestamp))
end)

it("should not update demand factor if timestamp is less than period length", function()
local df = DemandFactor:init(settings, fees)
local currentTimestamp = settings.periodLengthMs - 1
local calculated = math.floor((currentTimestamp - df.startTimestamp) / df.settings.periodLengthMs) + 1
local df = DemandFactor:new(settings, fees)
local currentTimestamp = settings.periodLengthMs - 1
local calculated = math.floor((currentTimestamp - df.startTimestamp) / df.settings.periodLengthMs) + 1
assert.is_false(df:shouldUpdateDemandFactor(currentTimestamp))
end)
end)
4 changes: 2 additions & 2 deletions contract/spec/setup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ _G.Handlers = {
utils = {
reply = function()
return true
end
}
end,
},
}

os.clock = function()
Expand Down
11 changes: 8 additions & 3 deletions contract/src/demand.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
local demand = { _version = "0.0.1" }


Fees = {}
Fees.__index = Fees

function Fees:init(genesisFees)
function Fees:new(genesisFees)
local fees = {} -- our new object
setmetatable(fees, Fees) -- make Account handle lookup
fees = genesisFees
Expand All @@ -17,8 +20,8 @@ end
DemandFactor = {}
DemandFactor.__index = DemandFactor

function DemandFactor:init(settings, fees)
setmetatable({}, DemandFactor) -- make Account handle lookup
function DemandFactor:new(settings, fees)
local self = setmetatable({}, DemandFactor) -- make DemandFactor lookup table
self.startTimestamp = os.clock() -- TODO: The timestamp at which the contract was initialized
self.currentPeriod = 1 -- TODO: the # of days since the last demand factor adjustment
self.trailingPeriodPurchases = { 0, 0, 0, 0, 0, 0, 0 } -- Acts as a ring buffer of trailing period purchase counts
Expand Down Expand Up @@ -106,3 +109,5 @@ end
function DemandFactor:getDemandFactor()
return self.currentDemandFactor
end

return demand
79 changes: 57 additions & 22 deletions contract/src/main.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
-- Adjust package.path to include the current directory
local process = { _version = "0.0.1" }

local token = require("token")
local arns = require("arns")
local gar = require("gar")
local utils = require("utils")
local json = require("json")
local constants = require("constants")

if not Demand then
require('demand') -- Load the demand module
Demand = DemandFactor:init(constants.DEMAND_SETTINGS, fees)
end

local ActionMap = {
Transfer = "Transfer",
Expand All @@ -20,6 +28,7 @@ local ActionMap = {
DecreaseOperatorStake = "DecreaseOperatorStake",
UpdateGatewaySettings = "UpdateGatewaySettings",
SaveObservations = "SaveObservations",
DemandFactor = "DemandFactor"
}

-- Handlers for contract functions
Expand All @@ -29,56 +38,58 @@ Handlers.add(ActionMap.Transfer, utils.hasMatchingTag("Action", ActionMap.Transf
-- Send Debit-Notice to the Sender
ao.send({
Target = msg.From,
Action = 'Debit-Notice',
Action = "Debit-Notice",
Recipient = msg.Tags.Recipient,
Quantity = tostring(msg.Tags.Quantity),
Data = "You transferred " .. msg.Tags.Quantity .. " to " .. msg.Tags.Recipient
Data = "You transferred " .. msg.Tags.Quantity .. " to " .. msg.Tags.Recipient,
})
if msg.Tags.Function and msg.Tags.Parameters then
-- Send Credit-Notice to the Recipient and include the function and parameters tags
ao.send({
Target = msg.Tags.Recipient,
Action = 'Credit-Notice',
Action = "Credit-Notice",
Sender = msg.From,
Quantity = tostring(msg.Tags.Quantity),
Function = tostring(msg.Tags.Function),
Parameters = msg.Tags.Parameters,
Data = "You received " ..
msg.Tags.Quantity .. " from " .. msg.Tags.Recipient ..
" with the instructions for function " .. msg.Tags.Function ..
" with the parameters " .. msg.Tags.Parameters
Data = "You received "
.. msg.Tags.Quantity
.. " from "
.. msg.Tags.Recipient
.. " with the instructions for function "
.. msg.Tags.Function
.. " with the parameters "
.. msg.Tags.Parameters,
})
else
-- Send Credit-Notice to the Recipient
ao.send({
Target = msg.Tags.Recipient,
Action = 'Credit-Notice',
Action = "Credit-Notice",
Sender = msg.From,
Quantity = tostring(msg.Tags.Quantity),
Data = "You received " ..
msg.Tags.Quantity ..
" from " .. msg.Tags.Recipient
Data = "You received " .. msg.Tags.Quantity .. " from " .. msg.Tags.Recipient,
})
end
else
ao.send({
Target = msg.From,
Tags = { Action = 'Transfer-Error', ['Message-Id'] = msg.Id, Error = tostring(err) },
Data = tostring(err)
Tags = { Action = "Transfer-Error", ["Message-Id"] = msg.Id, Error = tostring(err) },
Data = tostring(err),
})
end
end)

Handlers.add(ActionMap.GetBalance, utils.hasMatchingTag('Action', ActionMap.GetBalance), function(msg)
Handlers.add(ActionMap.GetBalance, utils.hasMatchingTag("Action", ActionMap.GetBalance), function(msg)
local result = token.getBalance(msg.Tags.Target, msg.From)
ao.send({
Target = msg.From,
Balance = tostring(result),
Data = json.encode(tonumber(result))
Data = json.encode(tonumber(result)),
})
end)

Handlers.add(ActionMap.GetBalances, utils.hasMatchingTag('Action', ActionMap.GetBalances), function(msg)
Handlers.add(ActionMap.GetBalances, utils.hasMatchingTag("Action", ActionMap.GetBalances), function(msg)
local result = token.getBalances()
ao.send({ Target = msg.From, Data = json.encode(result) })
end)
Expand All @@ -88,19 +99,30 @@ Handlers.add(ActionMap.Vault, utils.hasMatchingTag("Action", ActionMap.Vault), f
end)

Handlers.add(ActionMap.BuyRecord, utils.hasMatchingTag("Action", ActionMap.BuyRecord), function(msg)
local result, err = arns.buyRecord(msg.Tags.Name, msg.Tags.PurchaseType, msg.Tags.Years, msg.From, msg.Tags.Auction,
msg.Timestamp, msg.Tags.ProcessId)
local result, err = arns.buyRecord(
msg.Tags.Name,
msg.Tags.PurchaseType,
msg.Tags.Years,
msg.From,
msg.Tags.Auction,
msg.Timestamp,
msg.Tags.ProcessId
)
if err then
ao.send({
Target = msg.From,
Tags = { Action = 'ArNS-Invalid-Buy-Record-Notice', Name = tostring(msg.Tags.Name), ProcessId = tostring(msg.Tags.ProcessId) },
Data = tostring(err)
Tags = {
Action = "ArNS-Invalid-Buy-Record-Notice",
Name = tostring(msg.Tags.Name),
ProcessId = tostring(msg.Tags.ProcessId),
},
Data = tostring(err),
})
else
ao.send({
Target = msg.From,
Tags = { Action = 'ArNS-Purchase-Notice', Sender = msg.From },
Data = tostring(json.encode(result))
Tags = { Action = "ArNS-Purchase-Notice", Sender = msg.From },
Data = tostring(json.encode(result)),
})
end
end)
Expand Down Expand Up @@ -156,3 +178,16 @@ Handlers.add(
Handlers.add(ActionMap.SaveObservations, utils.hasMatchingTag("Action", ActionMap.SaveObservations), function(msg)
gar.saveObservations(msg)
end)

-- handler showing how we can fetch data from classes in lua
Handlers.add(ActionMap.DemandFactor, utils.hasMatchingTag("Action", ActionMap.DemandFactor), function(msg)
-- wrap in a protected call, and return the result or error accoringly to sender
local status, result = pcall(function() return Demand:getDemandFactor() end)
if status then
ao.send({ Target = msg.From, Data = tostring(result) })
else
ao.send({ Target = msg.From, Error = json.encode(result) })
end
end)

return process;

0 comments on commit 179e30d

Please sign in to comment.