Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(primary-names): add patch for adding demand factor data to primar… #408

Merged
merged 5 commits into from
Mar 10, 2025

Conversation

dtfiedler
Copy link
Collaborator

@dtfiedler dtfiedler commented Mar 10, 2025

…y name requests

Steps to test:

  1. Deploy new process against original module
❯ aos df-data-event-patch-2  --module=CWxzoe4IoNpFHiykadZWphZtLWybDF8ocNi7gmK6zCg --cu-url https://localhost:6363
          _____                   _______                   _____          
         /\    \                 /::\    \                 /\    \         
        /::\    \               /::::\    \               /::\    \        
       /::::\    \             /::::::\    \             /::::\    \       
      /::::::\    \           /::::::::\    \           /::::::\    \      
     /:::/\:::\    \         /:::/~~\:::\    \         /:::/\:::\    \     
    /:::/__\:::\    \       /:::/    \:::\    \       /:::/__\:::\    \    
   /::::\   \:::\    \     /:::/    / \:::\    \      \:::\   \:::\    \   
  /::::::\   \:::\    \   /:::/____/   \:::\____\   ___\:::\   \:::\    \  
 /:::/\:::\   \:::\    \ |:::|    |     |:::|    | /\   \:::\   \:::\    \ 
/:::/  \:::\   \:::\____\|:::|____|     |:::|    |/::\   \:::\   \:::\____\
\::/    \:::\  /:::/    / \:::\    \   /:::/    / \:::\   \:::\   \::/    /
 \/____/ \:::\/:::/    /   \:::\    \ /:::/    /   \:::\   \:::\   \/____/ 
          \::::::/    /     \:::\    /:::/    /     \:::\   \:::\    \     
           \::::/    /       \:::\__/:::/    /       \:::\   \:::\____\    
           /:::/    /         \::::::::/    /         \:::\  /:::/    /    
          /:::/    /           \::::::/    /           \:::\/:::/    /     
         /:::/    /             \::::/    /             \::::::/    /      
        /:::/    /               \::/____/               \::::/    /       
        \::/    /                 ~~                      \::/    /        
         \/____/                                           \/____/         
                                                                           
Welcome to AOS: Your operating system for AO, the decentralized open access supercomputer.
Type ".load-blueprint chat" to join the community chat and ask questions!
Using CU: https://localhost:6363

AOS Client Version: 2.0.4. 2024
Type "Ctrl-C" twice to exit

Your AOS process:  4_zQY9ZhaiiODch4r1YCUKQWq6-CQcs1TdJc3AQHzQY
  1. Give balance to test address via AOS
[email protected][Inbox:1]> Balances[ao.id] = Balances[ao.id] - 1000000
{ "_e": 1, "Message-Id": "7zOtAV2QWo4aqSvtZnoNAt6xM22x48jSK-cKtfSUZ4s", "From": "TeWsA2tuo4aFnhWy-ZiP5t2FYXLisp3s4KagrX9LXEI", "Action": "Eval", "Timestamp": 1741618445018}
[email protected][Inbox:1]> Balances["trkCV2r8suvQYcEgJBKAspcx0oVFCrDOhd7aU-VXmDo"] = 1000000
{ "_e": 1, "Message-Id": "pwxdD4vpoJXOKa0crdyuEz-6KJr0w6L_vOOs43h3Ol0", "From": "TeWsA2tuo4aFnhWy-ZiP5t2FYXLisp3s4KagrX9LXEI", "Action": "Eval", "Timestamp": 1741618468668}
  1. Load the patch file
[email protected][Inbox:1]> .load patches/2025-03-10-create-primary-name-request-demand-factor.lua
Loading...  patches/2025-03-10-create-primary-name-request-demand-factor.lua
  1. Request a primary name with the test wallet
❯ ar.io request-primary-name --name ao --cu-url http://localhost:6363 --ario-process-id 4_zQY9ZhaiiODch4r1YCUKQWq6-CQcs1TdJc3AQHzQY --wallet-file test-wallet.json
✔ Are you sure you want to request the primary name ao? … yes

{
  "id": "5qpKoQvJYkfrrxd7JGLrqSe2eTS9zpMJONLYh-EnoAQ",
  "result": {
    "request": {
      "name": "ao",
      "endTimestamp": 1742223323568,
      "startTimestamp": 1741618523568
    },
    "fundingPlan": {
      "stakes": [],
      "address": "trkCV2r8suvQYcEgJBKAspcx0oVFCrDOhd7aU-VXmDo",
      "balance": 1000000,
      "shortfall": 0
    },
    "baseNameOwner": "HY021r2MQL9Zi0qSNFAQ9QRshIc2mNPYf65pZBP04cE",
    "demandFactor": {
      "trailingPeriodPurchases": [
        0,
        0,
        0,
        0,
        0,
        0,
        0
      ],
      "fees": [
        1000000000000,
        200000000000,
        20000000000,
        10000000000,
        2500000000,
        1500000000,
        800000000,
        500000000,
        400000000,
        350000000,
        300000000,
        250000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000,
        200000000
      ],
      "currentPeriod": 1,
      "trailingPeriodRevenues": [
        0,
        0,
        0,
        0,
        0,
        0,
        0
      ],
      "revenueThisPeriod": 1000000,
      "currentDemandFactor": 1,
      "purchasesThisPeriod": 1,
      "consecutivePeriodsWithMinDemandFactor": 0
    },
    "fundingResult": {
      "newWithdrawVaults": [],
      "totalFunded": 1000000
    }
  }
}
  1. Review the event data [TODO]

baseNameOwner = record.processId,
fundingPlan = fundingPlan,
fundingResult = fundingResult,
df = demand.getDemandFactorInfo(),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

includes the df data

Comment on lines +98 to +182

local function Send(msg, response)
if msg.reply then
--- Reference: https://github.com/permaweb/aos/blob/main/blueprints/patch-legacy-reply.lua
msg.reply(response)
else
ao.send(response)
end
end

local function assertValidFundFrom(fundFrom)
if fundFrom == nil then
return
end
local validFundFrom = utils.createLookupTable({ "any", "balance", "stakes" })
assert(validFundFrom[fundFrom], "Invalid fund from type. Must be one of: any, balance, stakes")
end

local function addPrimaryNameCounts(ioEvent)
ioEvent:addField("Total-Primary-Names", utils.lengthOfTable(primaryNames.getUnsafePrimaryNames()))
ioEvent:addField("Total-Primary-Name-Requests", utils.lengthOfTable(primaryNames.getUnsafePrimaryNameRequests()))
end

local function adjustSuppliesForFundingPlan(fundingPlan, rewardForInitiator)
if not fundingPlan then
return
end
rewardForInitiator = rewardForInitiator or 0
local totalActiveStakesUsed = utils.reduce(fundingPlan.stakes, function(acc, _, stakeSpendingPlan)
return acc + stakeSpendingPlan.delegatedStake
end, 0)
local totalWithdrawStakesUsed = utils.reduce(fundingPlan.stakes, function(acc, _, stakeSpendingPlan)
return acc
+ utils.reduce(stakeSpendingPlan.vaults, function(acc2, _, vaultBalance)
return acc2 + vaultBalance
end, 0)
end, 0)
LastKnownStakedSupply = LastKnownStakedSupply - totalActiveStakesUsed
LastKnownWithdrawSupply = LastKnownWithdrawSupply - totalWithdrawStakesUsed
LastKnownCirculatingSupply = LastKnownCirculatingSupply - fundingPlan.balance + rewardForInitiator
end

local function addResultFundingPlanFields(ioEvent, result)
ioEvent:addFieldsWithPrefixIfExist(result.fundingPlan, "FP-", { "balance" })
local fundingPlanVaultsCount = 0
local fundingPlanStakesAmount = utils.reduce(
result.fundingPlan and result.fundingPlan.stakes or {},
function(acc, _, delegation)
return acc
+ delegation.delegatedStake
+ utils.reduce(delegation.vaults, function(acc2, _, vaultAmount)
fundingPlanVaultsCount = fundingPlanVaultsCount + 1
return acc2 + vaultAmount
end, 0)
end,
0
)
if fundingPlanStakesAmount > 0 then
ioEvent:addField("FP-Stakes-Amount", fundingPlanStakesAmount)
end
if fundingPlanVaultsCount > 0 then
ioEvent:addField("FP-Vaults-Count", fundingPlanVaultsCount)
end
local newWithdrawVaultsTallies = utils.reduce(
result.fundingResult and result.fundingResult.newWithdrawVaults or {},
function(acc, _, newWithdrawVault)
acc.totalBalance = acc.totalBalance
+ utils.reduce(newWithdrawVault, function(acc2, _, vault)
acc.count = acc.count + 1
return acc2 + vault.balance
end, 0)
return acc
end,
{ count = 0, totalBalance = 0 }
)
if newWithdrawVaultsTallies.count > 0 then
ioEvent:addField("New-Withdraw-Vaults-Count", newWithdrawVaultsTallies.count)
ioEvent:addField("New-Withdraw-Vaults-Total-Balance", newWithdrawVaultsTallies.totalBalance)
end
adjustSuppliesForFundingPlan(result.fundingPlan, result.returnedName and result.returnedName.rewardForInitiator)
end

--- @param ioEvent ARIOEvent
--- @param primaryNameResult CreatePrimaryNameResult|PrimaryNameRequestApproval
local function addPrimaryNameRequestData(ioEvent, primaryNameResult)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately, these all need to be added as they are not available in the global scope

Comment on lines 189 to 200
-- add the demand factor data to the ioEvent
if primaryNameResult.df and type(primaryNameResult.df) == "table" then
ioEvent:addField("DF-Trailing-Period-Purchases", (primaryNameResult.df.trailingPeriodPurchases or {}))
ioEvent:addField("DF-Trailing-Period-Revenues", (primaryNameResult.df.trailingPeriodRevenues or {}))
ioEvent:addFieldsWithPrefixIfExist(primaryNameResult.df, "DF-", {
"currentPeriod",
"currentDemandFactor",
"consecutivePeriodsWithMinDemandFactor",
"revenueThisPeriod",
"purchasesThisPeriod",
})
end
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the DF data is now included in the event

fedellen
fedellen previously approved these changes Mar 10, 2025
@dtfiedler dtfiedler marked this pull request as ready for review March 10, 2025 14:52
@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.35%. Comparing base (7c43410) to head (273b227).
Report is 3 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop     #408   +/-   ##
========================================
  Coverage    91.35%   91.35%           
========================================
  Files           10       10           
  Lines         3181     3182    +1     
========================================
+ Hits          2906     2907    +1     
  Misses         275      275           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@dtfiedler dtfiedler merged commit 3c857bf into develop Mar 10, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants