From 179e30d6b71caa389f492c02fc1f78c9d6e6f214 Mon Sep 17 00:00:00 2001 From: dtfiedler Date: Wed, 8 May 2024 16:32:40 -0400 Subject: [PATCH] fix(df): add handler to main.lua --- contract/spec/demand_spec.lua | 24 +++++------ contract/spec/setup.lua | 4 +- contract/src/demand.lua | 11 +++-- contract/src/main.lua | 79 +++++++++++++++++++++++++---------- 4 files changed, 79 insertions(+), 39 deletions(-) diff --git a/contract/spec/demand_spec.lua b/contract/spec/demand_spec.lua index 06d17d3a..1ec22310 100644 --- a/contract/spec/demand_spec.lua +++ b/contract/spec/demand_spec.lua @@ -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, @@ -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, @@ -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) diff --git a/contract/spec/setup.lua b/contract/spec/setup.lua index 2afc1daf..4bf18974 100644 --- a/contract/spec/setup.lua +++ b/contract/spec/setup.lua @@ -14,8 +14,8 @@ _G.Handlers = { utils = { reply = function() return true - end - } + end, + }, } os.clock = function() diff --git a/contract/src/demand.lua b/contract/src/demand.lua index 109fd28e..a0d30cae 100644 --- a/contract/src/demand.lua +++ b/contract/src/demand.lua @@ -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 @@ -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 @@ -106,3 +109,5 @@ end function DemandFactor:getDemandFactor() return self.currentDemandFactor end + +return demand diff --git a/contract/src/main.lua b/contract/src/main.lua index 2b467d0a..2d9d34f7 100644 --- a/contract/src/main.lua +++ b/contract/src/main.lua @@ -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", @@ -20,6 +28,7 @@ local ActionMap = { DecreaseOperatorStake = "DecreaseOperatorStake", UpdateGatewaySettings = "UpdateGatewaySettings", SaveObservations = "SaveObservations", + DemandFactor = "DemandFactor" } -- Handlers for contract functions @@ -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) @@ -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) @@ -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;