Skip to content

Commit

Permalink
fix(handlers): update process handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed May 29, 2024
1 parent ff87ed0 commit 5535218
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 89 deletions.
3 changes: 1 addition & 2 deletions contract/src/arns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ local utils = require("utils")
local constants = require("constants")
local balances = require("balances")
local demand = require("demand")
local arns = {}

NameRegistry = NameRegistry or {
reserved = {},
records = {},
-- TODO: auctions
}

local arns = {}

function arns.buyRecord(name, purchaseType, years, from, timestamp, processId)
-- don't catch, let the caller handle the error
arns.assertValidBuyRecord(name, years, purchaseType, processId)
Expand Down
10 changes: 5 additions & 5 deletions contract/src/epochs.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local gar = require("gar")
local crypto = require("crypto")
local crypto = require("crypto.init")
local utils = require("utils")
local epochs = {}

Expand Down Expand Up @@ -31,10 +31,6 @@ function epochs.getEpoch(epochNumber)
return Epochs[epochNumber]
end

function epochs.getCurrentEpoch()
return epochs.getEpoch(#Epochs)
end

function epochs.getObservers()
return epochs.getCurrentEpoch().prescribedObservers
end
Expand Down Expand Up @@ -63,6 +59,10 @@ function epochs.getDistributionForEpoch(epochNumber)
return epochs.getEpoch(epochNumber).distributions
end

function epochs.getEpochFromTimestamp(timestamp)
local epochIndex = epochs.getEpochIndexForTimestamp(timestamp)
return epochs.getEpoch(epochIndex)
end
function epochs.setPrescribedObserversForEpoch(epochNumber, hashchain)
local prescribedObservers = epochs.computePrescribedObserversForEpoch(epochNumber, hashchain)
epochs.getEpoch(epochNumber).prescribedObservers = prescribedObservers
Expand Down
8 changes: 0 additions & 8 deletions contract/src/gar.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
-- gar.lua
local crypto = require("crypto.init")
local base64 = require("base64")
local balances = require("balances")
local gar = {}

Expand Down Expand Up @@ -447,12 +445,6 @@ function gar.getObserverWeightsAtTimestamp(eligbileGateways, timestamp)
return weightedObservers
end

function gar.getHashFromBase64(str)
local decodedHash = base64.decode(str)
local hashStream = crypto.utils.stream.fromString(decodedHash)
return crypto.digest.sha2_256(hashStream).asBytes()
end

function gar.isGatewayJoined(gateway, currentTimestamp)
return gateway.status == "joined" and gateway.startTimestamp <= currentTimestamp
end
Expand Down
168 changes: 94 additions & 74 deletions contract/src/main.lua
Original file line number Diff line number Diff line change
@@ -1,61 +1,117 @@
-- Adjust package.path to include the current directory
local process = { _version = "0.0.1" }

-- local ao = require("ao")
local utils = require("utils")
local json = require("json")
local ao = ao or require("ao")
local arns = require("arns")
local gar = require("gar")
local demand = require("demand")

Name = "Test IO"
Ticker = "tIO"
Logo = "Sie_26dvgyok0PZD_-iQAFOhOd5YxDTkczOLoqTTL_A"
Denomination = 6
DemandFactor = DemandFactor or {}
Balances = Balances or {}
Balances = Balances or {
[ao.id] = 1000000000 * 1000000,
}
Vaults = Vaults or {}
GatewayRegistry = GatewayRegistry or {}
NameRegistry = NameRegistry or {}
Epochs = Epochs or {}

local utils = require("utils")
local json = require("json")
local ao = ao or require("ao")
local balances = require("balances")
local arns = require("arns")
local gar = require("gar")
local demand = require("demand")
local epochs = require("epochs")

local ActionMap = {
-- reads
Info = "Info",
State = "State",
Record = "Record",
Records = "Records",
Transfer = "Transfer",
GetBalance = "Balance",
GetBalances = "Balances",
Balance = "Balance",
Balances = "Balances",
Gateway = "Gateway",
Gateways = "Gateways",
DemandFactor = "DemandFactor",
Epochs = "Epochs",
Epoch = "Epoch",
-- writes
CreateVault = "CreateVault",
VaultedTransfer = "VaultedTransfer",
ExtendVault = "ExtendVault",
IncreaseVault = "IncreaseVault",
BuyRecord = "BuyRecord",
SubmitAuctionBid = "SubmitAuctionBid",
ExtendLease = "ExtendLease",
IncreaseUndernameCount = "IncreaseUndernameCount",
JoinNetwork = "JoinNetwork",
LeaveNetwork = "LeaveNetwork",
IncreaseOperatorStake = "IncreaseOperatorStake",
DecreaseOperatorStake = "DecreaseOperatorStake",
UpdateGatewaySettings = "UpdateGatewaySettings",
GetGateway = "GetGateway",
GetGateways = "GetGateways",
SaveObservations = "SaveObservations",
DemandFactor = "DemandFactor",
DelegateStake = "DelegateStake",
DecreaseDelegateStake = "DecreaseDelegateStake",
}

-- Handlers for contract functions
-- -- Handlers for contract functions

Handlers.add("info", Handlers.utils.hasMatchingTag("Action", "Info"), function(msg)
Handlers.add(ActionMap.Info, Handlers.utils.hasMatchingTag("Action", ActionMap.Info), function(msg)
ao.send({
Target = msg.From,
Tags = { Name = Name, Ticker = Ticker, Logo = Logo, Denomination = tostring(Denomination) },
})
end)

Handlers.add(ActionMap.State, Handlers.utils.hasMatchingTag("Action", ActionMap.State), function(msg)
ao.send({
Target = msg.From,
Data = json.encode({
Name = Name,
Ticker = Ticker,
Denomination = Denomination,
Balances = Balances,
GatewayRegistry = GatewayRegistry,
NameRegistry = NameRegistry,
Epochs = Epochs,
Vaults = Vaults,
DemandFactor = DemandFactor,
}),
})
end)

Handlers.add(ActionMap.Gateways, Handlers.utils.hasMatchingTag("Action", ActionMap.Gateways), function(msg)
local gateways = gar.getGateways()
ao.send({
Target = msg.From,
Data = json.encode(gateways),
})
end)

Handlers.add(ActionMap.Gateway, Handlers.utils.hasMatchingTag("Action", ActionMap.Gateway), function(msg)
local gateway = gar.getGateway(msg.Tags.Target)
ao.send({
Target = msg.From,
Data = json.encode(gateway),
})
end)

Handlers.add(ActionMap.Balances, Handlers.utils.hasMatchingTag("Action", ActionMap.Balances), function(msg)
ao.send({
Target = msg.From,
Data = json.encode(Balances),
})
end)

Handlers.add(ActionMap.Balance, Handlers.utils.hasMatchingTag("Action", ActionMap.Balance), function(msg)
local balance = balances.getBalance(msg.Tags.Target)
ao.send({
Target = msg.From,
Data = tostring(balance),
})
end)

Handlers.add(ActionMap.Transfer, utils.hasMatchingTag("Action", ActionMap.Transfer), function(msg)
local result, err = balances.transfer(msg.Tags.Recipient, msg.From, tonumber(msg.Tags.Quantity))
if result and not msg.Cast then
Expand Down Expand Up @@ -104,20 +160,6 @@ Handlers.add(ActionMap.Transfer, utils.hasMatchingTag("Action", ActionMap.Transf
end
end)

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

Handlers.add(ActionMap.GetBalances, utils.hasMatchingTag("Action", ActionMap.GetBalances), function(msg)
local result = balances.getBalances()
ao.send({ Target = msg.From, Data = json.encode(result) })
end)

Handlers.add(ActionMap.CreateVault, utils.hasMatchingTag("Action", ActionMap.CreateVault), function(msg)
local result, err = balances.createVault(msg.From, msg.Tags.Quantity, msg.Tags.LockLength, msg.Timestamp, msg.Id)
if err then
Expand Down Expand Up @@ -229,23 +271,6 @@ Handlers.add(ActionMap.BuyRecord, utils.hasMatchingTag("Action", ActionMap.BuyRe
end
end)

Handlers.add(ActionMap.SubmitAuctionBid, utils.hasMatchingTag("Action", ActionMap.SubmitAuctionBid), function(msg)
local status, result = pcall(arns.submitAuctionBid, msg.From, msg.Tags.Name, msg.Tags.Bid, msg.Timestamp)
if not status then
ao.send({
Target = msg.From,
Tags = { Action = "Invalid-Auction-Bid" },
Data = tostring(result),
})
else
ao.send({
Target = msg.From,
Tags = { Action = "Auction-Bid-Submitted" },
Data = tostring(json.encode(result)),
})
end
end)

Handlers.add(ActionMap.ExtendLease, utils.hasMatchingTag("Action", ActionMap.ExtendLease), function(msg)
local success, result = pcall(arns.extendLease, msg.From, msg.Tags.Name, msg.Tags.Years, msg.Timestamp)
if not success then
Expand Down Expand Up @@ -420,32 +445,6 @@ Handlers.add(
end
)

Handlers.add(ActionMap.GetGateway, utils.hasMatchingTag("Action", ActionMap.GetGateway), function(msg)
local result, err = gar.getGateway(msg.Tags.Target)
if err then
ao.send({
Target = msg.From,
Tags = { Action = "GAR-Invalid-Gateway-Target" },
Data = tostring(err),
})
else
ao.send({
Target = msg.From,
Tags = { Action = "GAR-Get-Gateway" },
Data = tostring(json.encode(result)),
})
end
end)

Handlers.add(ActionMap.GetGateways, utils.hasMatchingTag("Action", ActionMap.GetGateways), function(msg)
local result = gar.getGateways()
ao.send({
Target = msg.From,
Tags = { Action = "GAR-Get-Gateways" },
Data = tostring(json.encode(result)),
})
end)

Handlers.add(ActionMap.SaveObservations, utils.hasMatchingTag("Action", ActionMap.SaveObservations), function(msg)
gar.saveObservations(msg)
end)
Expand All @@ -461,4 +460,25 @@ Handlers.add(ActionMap.DemandFactor, utils.hasMatchingTag("Action", ActionMap.De
end
end)

Handlers.add(ActionMap.Record, utils.hasMatchingTag("Action", ActionMap.Record), function(msg)
local record = arns.getRecord(msg.Tags.Name)
ao.send({ Target = msg.From, Data = json.encode(record) })
end)

Handlers.add(ActionMap.Records, utils.hasMatchingTag("Action", ActionMap.Records), function(msg)
local records = arns.getRecords()
ao.send({ Target = msg.From, Data = json.encode(records) })
end)

Handlers.add(ActionMap.Epoch, utils.hasMatchingTag("Action", ActionMap.Epochs), function(msg)
local epochIndex = msg.Tags.EpochNumber or epochs.getEpochIndexFromTimestamp(msg.Timestamp)
local epoch = epochs.getEpoch(epochIndex)
ao.send({ Target = msg.From, Data = json.encode(epoch) })
end)

Handlers.add(ActionMap.Epochs, utils.hasMatchingTag("Action", ActionMap.Epochs), function(msg)
local epochs = epochs.getEpoch(msg.Timestamp)
ao.send({ Target = msg.From, Data = json.encode(Epochs) })
end)

return process

0 comments on commit 5535218

Please sign in to comment.