Skip to content

Instantly share code, notes, and snippets.

@allquantor
Created February 6, 2025 07:38
Show Gist options
  • Save allquantor/6b5853d84970092453cea2897117d6c3 to your computer and use it in GitHub Desktop.
Save allquantor/6b5853d84970092453cea2897117d6c3 to your computer and use it in GitHub Desktop.
local store = require("store")
-- Validate delegation factor is in (0,1]
local function validate_factor(factor)
if factor <= 0 or factor > 1 then
error("Delegation factor must be > 0 and <= 1")
end
end
-- Stub: Check for circular delegation.
local function check_for_cycle(wallet_from, wallet_to)
-- For now, we assume there is no cycle.
return false
end
-- Stub: Enforce maximum delegations per wallet.
local function enforce_max_delegations(wallet_from)
-- For now, we assume wallet_from is within the allowed limit.
return true
end
------------------------------------------------------------
-- Core Function: delegation_oracle
------------------------------------------------------------
local function delegation_oracle(delegation)
-- Unpack input delegation data
local wallet_from = delegation.wallet_from
local wallet_to = delegation.wallet_to
local factor = delegation.factor
local timestamp = delegation.timestamp
-- 1. Validate the delegation factor.
validate_factor(factor)
-- 2. Optionally, check for circular delegations.
if check_for_cycle(wallet_from, wallet_to) then
error("Cycle detected in delegation " .. wallet_from .. " -> " .. wallet_to)
end
-- 3. Optionally, enforce maximum delegations limit.
if not enforce_max_delegations(wallet_from) then
error("Maximum delegations reached for wallet " .. wallet_from)
end
-- 4. Retrieve the base_mint data structure from the store.
local base_mint = store.get("base_mint")
if not base_mint then
error("base_mint data structure not found")
end
-- 5. Search for the entry with wallet_from.
local found = false
for _, entry in ipairs(base_mint) do
if entry.wallet == wallet_from and entry.period then
-- assuming period exists means it is active for this mint cycle
found = true
local original_amount = entry.amount
-- 6. Compute the delegated amount based on the factor.
local delegated_amount = original_amount * factor
-- 7. Update the existing entry reducing its amount.
entry.amount = original_amount - delegated_amount
-- 8. Append a new entry for wallet_to with the delegated amount and given timestamp.
table.insert(base_mint, {
wallet = wallet_to,
amount = delegated_amount,
period = timestamp
})
break
end
end
if not found then
error("Wallet '" .. wallet_from .. "' not found in base_mint")
end
-- 9. Write the updated base_mint back to the store.
store.set("base_mint", base_mint)
end
------------------------------------------------------------
-- Return public interface
------------------------------------------------------------
return {
delegation_oracle = delegation_oracle,
validate_factor = validate_factor, -- helper: can be toggled or extended
check_for_cycle = check_for_cycle, -- stub for cycle detection
enforce_max_delegations = enforce_max_delegations -- stub for max delegations
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment