Skip to content

Commit

Permalink
chore: update balance initalization
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Jun 25, 2024
1 parent 9cd568d commit 27085dd
Showing 1 changed file with 107 additions and 29 deletions.
136 changes: 107 additions & 29 deletions src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,31 @@ Denomination = 6
DemandFactor = DemandFactor or {}
Owner = Owner or ao.env.Process.Owner
Balances = Balances or {}
if #Balances == 0 then -- initialize the balance for the process id
if not Balances[ao.id] then -- initialize the balance for the process id
Balances = {
[ao.id] = math.floor(50000000 * 1000000), -- 50M IO
[Owner] = math.floor(constants.totalTokenSupply - (50000000 * 1000000)), -- 950M IO
}
end
-- Balances = {
-- ["GaQrvEMKBpkjofgnBi_B3IgIDmY_XYelVLB6GcRGrHc"] = 50028901160806,
-- ["7waR8v4STuwPnTck1zFVkQqJh5K9q9Zik4Y5-5dV7nk"] = 1000000000000,
-- ["ZjmB2vEUlHlJ7-rgJkYP09N5IzLPhJyStVrK5u9dDEo"] = 971807587006,
-- ["nszYSUJvtlFXssccPaQWZaVpkXgJHcVM7XhcP5NEt7w"] = 1000000000000,
-- ["TeWsA2tuo4aFnhWy-ZiP5t2FYXLisp3s4KagrX9LXEI"] = 940000000000000,
-- ["hNtcagQ0tlXOLM8uhwI408efFSI5DiqHGoP_BqUfzOQ"] = 1000000000000,
-- ["NdZ3YRwMB2AMwwFYjKn1g88Y9nRybTo0qhS1ORq_E7g"] = 1000000000000,
-- ["N4h8M9A9hasa3tF47qQyNvcKjm4APBKuFs7vqUVm-SI"] = 999291252188,
-- ["GtDQcrr2QRdoZ-lKto_S_SpzEwiZiHVaj3x4jAgRh4o"] = 1000000000000,
-- ["6Z-ifqgVi1jOwMvSNwKWs6ewUEQ0gU9eo4aHYC3rN1M"] = 1000000000000,
-- ["9jfM0uzGNc9Mkhjo1ixGoqM7ygSem9wx_EokiVgi0Bs"] = 1000000000000,
-- ["1H7WZIWhzwTH9FIcnuMqYkTsoyv1OTfGa_amvuYwrgo"] = 1000000000000,
-- }
Vaults = Vaults or {}
GatewayRegistry = GatewayRegistry or {}
NameRegistry = NameRegistry or {}
Epochs = Epochs or {}
LastTickedEpoch = LastTickedEpoch or 0
LastTickedEpochIndex = LastTickedEpochIndex or -1

local utils = require("utils")
local json = require("json")
Expand Down Expand Up @@ -172,7 +186,7 @@ Handlers.add(ActionMap.CreateVault, utils.hasMatchingTag("Action", ActionMap.Cre
ao.send({
Target = msg.From,
Tags = { Action = "Vault-Created-Notice" },
Data = tostring(json.encode(result)),
Data = json.encode(result),
})
end
end)
Expand Down Expand Up @@ -221,12 +235,12 @@ Handlers.add(ActionMap.VaultedTransfer, utils.hasMatchingTag("Action", ActionMap
Recipient = msg.Tags.Recipient,
Quantity = msg.Tags.Quantity,
Tags = { Action = "Debit-Notice" },
Data = tostring(json.encode(result)),
Data = json.encode(result),
})
ao.send({
Target = msg.Tags.Recipient,
Tags = { Action = "Vaulted-Credit-Notice" },
Data = tostring(json.encode(result)),
Data = json.encode(result),
})
end
end)
Expand Down Expand Up @@ -262,7 +276,7 @@ Handlers.add(ActionMap.ExtendVault, utils.hasMatchingTag("Action", ActionMap.Ext
ao.send({
Target = msg.From,
Tags = { Action = "Vault-Extended-Notice" },
Data = tostring(json.encode(result)),
Data = json.encode(result),
})
end
end)
Expand Down Expand Up @@ -298,7 +312,7 @@ Handlers.add(ActionMap.IncreaseVault, utils.hasMatchingTag("Action", ActionMap.I
ao.send({
Target = msg.From,
Tags = { Action = "Vault-Increased-Notice" },
Data = tostring(json.encode(result)),
Data = json.encode(result),
})
end
end)
Expand Down Expand Up @@ -348,7 +362,7 @@ Handlers.add(ActionMap.BuyRecord, utils.hasMatchingTag("Action", ActionMap.BuyRe
ao.send({
Target = msg.From,
Tags = { Action = "Buy-Record-Notice", Name = msg.Tags.Name },
Data = tostring(json.encode(result)),
Data = json.encode(result),
})
end
end)
Expand Down Expand Up @@ -384,7 +398,7 @@ Handlers.add(ActionMap.ExtendLease, utils.hasMatchingTag("Action", ActionMap.Ext
ao.send({
Target = msg.From,
Tags = { Action = "Extend-Lease-Notice", Name = msg.Tags.Name },
Data = tostring(json.encode(result)),
Data = json.encode(result),
})
end
end)
Expand Down Expand Up @@ -426,7 +440,7 @@ Handlers.add(
ao.send({
Target = msg.From,
Tags = { Action = "Increase-Undername-Limit-Notice", Name = msg.Tags.Name },
Data = tostring(json.encode(result)),
Data = json.encode(result),
})
end
end
Expand Down Expand Up @@ -472,7 +486,7 @@ Handlers.add(ActionMap.TokenCost, utils.hasMatchingTag("Action", ActionMap.Token
years = tonumber(msg.Tags.Years) or 1,
quantity = tonumber(msg.Tags.Quantity),
purchaseType = msg.Tags["Purchase-Type"] or "lease",
currentTimestamp = tonumber(msg.Timestamp),
currentTimestamp = tonumber(msg.Timestamp) or tonumber(msg.Tags.Timestamp),
})
if not status then
ao.send({
Expand All @@ -483,8 +497,8 @@ Handlers.add(ActionMap.TokenCost, utils.hasMatchingTag("Action", ActionMap.Token
else
ao.send({
Target = msg.From,
Tags = { Action = "Token-Cost-Notice", TokenCost = tostring(result) },
Data = tostring(json.encode(result)),
Tags = { Action = "Token-Cost-Notice", ["Token-Cost"] = tostring(result) },
Data = json.encode(result),
})
end
end)
Expand Down Expand Up @@ -522,7 +536,7 @@ Handlers.add(ActionMap.JoinNetwork, utils.hasMatchingTag("Action", ActionMap.Joi
ao.send({
Target = msg.From,
Tags = { Action = "Join-Network-Notice" },
Data = tostring(json.encode(result)),
Data = json.encode(result),
})
end
end)
Expand All @@ -539,7 +553,7 @@ Handlers.add(ActionMap.LeaveNetwork, utils.hasMatchingTag("Action", ActionMap.Le
ao.send({
Target = msg.From,
Tags = { Action = "Leave-Network-Notice" },
Data = tostring(json.encode(result)),
Data = json.encode(result),
})
end
end)
Expand Down Expand Up @@ -578,7 +592,7 @@ Handlers.add(
ao.send({
Target = msg.From,
Tags = { Action = "Increase-Operator-Stake-Notice" },
Data = tostring(json.encode(result)),
Data = json.encode(result),
})
end
end
Expand Down Expand Up @@ -617,7 +631,7 @@ Handlers.add(
ao.send({
Target = msg.From,
Tags = { Action = "Decrease-Operator-Stake-Notice" },
Data = tostring(json.encode(result)),
Data = json.encode(result),
})
end
end
Expand Down Expand Up @@ -649,13 +663,13 @@ Handlers.add(ActionMap.DelegateStake, utils.hasMatchingTag("Action", ActionMap.D
ao.send({
Target = msg.From,
Tags = { Action = "Invalid-Delegate-Stake-Notice", Error = "Invalid-Delegate-Stake", Message = result },
Data = tostring(json.encode(result)),
Data = json.encode(result),
})
else
ao.send({
Target = msg.From,
Tags = { Action = "Delegate-Stake-Notice", Gateway = msg.Tags.Target },
Data = tostring(json.encode(result)),
Data = json.encode(result),
})
end
end)
Expand Down Expand Up @@ -789,38 +803,101 @@ Handlers.add(ActionMap.SaveObservations, utils.hasMatchingTag("Action", ActionMa
end
end)

Handlers.add("Epoch-Settings", utils.hasMatchingTag("Action", "Epoch-Settings"), function(msg)
local epochSettings = epochs.getSettings()
ao.send({
Target = msg.From,
Action = "Epoch-Settings-Notice",
Data = json.encode(epochSettings),
})
end)

-- TICK HANDLER
Handlers.add("tick", utils.hasMatchingTag("Action", "Tick"), function(msg)
local timestamp = tonumber(msg.Timestamp)
-- TODO: how do we make this update atomic so that the state is changed all or nothing (should we?)
local lastTickedEpochIndex = LastTickedEpoch
local currentEpochIndex = epochs.getEpochIndexForTimestamp(timestamp)
local function tickState(timestamp, blockHeight, hashchain)
-- assert this is a write interaction and we have a timetsamp
assert(msg.Timestamp, "Timestamp is required for a tick interaction")
-- tick the things that only require timestamp and don't need to happen for every epoch
local function tickState(timestamp)
arns.pruneRecords(timestamp)
arns.pruneReservedNames(timestamp)
vaults.pruneVaults(timestamp)
gar.pruneGateways(timestamp)
end

local previousState = {
Balances = utils.deepCopy(Balances),
Vaults = utils.deepCopy(Vaults),
GatewayRegistry = utils.deepCopy(GatewayRegistry),
NameRegistry = utils.deepCopy(NameRegistry),
Epochs = utils.deepCopy(Epochs),
DemandFactor = utils.deepCopy(DemandFactor),
LastTickedEpochIndex = utils.deepCopy(LastTickedEpochIndex),
}
local msgTimestamp = tonumber(msg.Timestamp)

-- tick the state and demand factor using just the timestamp
local stateStatus, stateResult = pcall(tickState, msgTimestamp)
if not stateStatus then
-- reset the state to previous state
Balances = previousState.Balances
Vaults = previousState.Vaults
GatewayRegistry = previousState.GatewayRegistry
NameRegistry = previousState.NameRegistry
Epochs = previousState.Epochs
DemandFactor = previousState.DemandFactor
LastTickedEpochIndex = previousState.LastTickedEpochIndex
ao.send({
Target = msg.From,
Action = "Invalid-Tick-Notice",
Error = "Invalid-Tick",
Data = json.encode(stateResult),
})
end

-- tick and distribute rewards for every index between the last ticked epoch and the current epoch
local function tickEpochs(timestamp, blockHeight, hashchain)
-- update demand factor if necessary
demand.updateDemandFactor(timestamp)
epochs.distributeRewardsForEpoch(timestamp)
epochs.createEpoch(timestamp, tonumber(blockHeight), hashchain)
end

local lastTickedEpochIndex = LastTickedEpochIndex
local currentEpochIndex = epochs.getEpochIndexForTimestamp(msgTimestamp)
-- if epoch index is -1 then we are before the genesis epoch and we should not tick
if currentEpochIndex < 0 then
ao.send({
Target = msg.From,
Action = "Invalid-Tick-Notice",
Error = "Invalid-Tick",
Data = json.encode("Cannot tick before genesis epoch"),
})
end

-- tick and distribute rewards for every index between the last ticked epoch and the current epoch
for i = lastTickedEpochIndex + 1, currentEpochIndex - 1 do
for i = lastTickedEpochIndex + 1, currentEpochIndex do
local previousState = {
Balances = utils.deepCopy(Balances),
Vaults = utils.deepCopy(Vaults),
GatewayRegistry = utils.deepCopy(GatewayRegistry),
NameRegistry = utils.deepCopy(NameRegistry),
Epochs = utils.deepCopy(Epochs),
DemandFactor = utils.deepCopy(DemandFactor),
LastTickedEpochIndex = utils.deepCopy(LastTickedEpochIndex),
}
local _, _, epochDistributionTimestamp = epochs.getEpochTimestampsForIndex(i)
-- TODO: if we need to "recover" epochs, we can't rely on just the current message hashchain and block height
local status, result = pcall(tickState, epochDistributionTimestamp, msg["Block-Height"], msg["Hash-Chain"])
-- use the minimum of the msg timestamp or the epoch distribution timestamp, this ensures an epoch gets created for the genesis block and that we don't try and distribute before an epoch is created
local tickTimestamp = math.min(msgTimestamp or 0, epochDistributionTimestamp)
-- TODO: if we need to "recover" epochs, we can't rely on just the current message hashchain and block height, we should set the prescribed observers and names to empty arrays and distribute rewards accordingly
local status, result = pcall(tickEpochs, tickTimestamp, msg["Block-Height"], msg["Hash-Chain"])
if status then
ao.send({ Target = msg.From, Action = "Tick-Notice", Data = json.encode(result) })
LastTickedEpoch = i -- update the last ticked state
LastTickedEpochIndex = i -- update the last ticked state
ao.send({
Target = msg.From,
Action = "Tick-Notice",
LastTickedEpochIndex = LastTickedEpochIndex,
Data = json.encode(result),
})
else
-- reset the state to previous state
Balances = previousState.Balances
Expand All @@ -829,6 +906,7 @@ Handlers.add("tick", utils.hasMatchingTag("Action", "Tick"), function(msg)
NameRegistry = previousState.NameRegistry
Epochs = previousState.Epochs
DemandFactor = previousState.DemandFactor
LastTickedEpochIndex = previousState.LastTickedEpochIndex
ao.send({
Target = msg.From,
Action = "Invalid-Tick-Notice",
Expand Down

0 comments on commit 27085dd

Please sign in to comment.