Skip to content

Commit

Permalink
chore: devnet to testnet (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler authored Oct 16, 2024
2 parents 318b447 + 4c7c917 commit 287e500
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 12 deletions.
33 changes: 30 additions & 3 deletions src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ Logo = Logo or "qUjrTmHdVjXX4D6rU6Fik02bUOzWkOR6oOqUg39g4-s"
Denomination = 6
DemandFactor = DemandFactor or {}
Owner = Owner or ao.env.Process.Owner
Protocol = Protocol or ao.env.Process.Id
Balances = Balances or {}
if not Balances[ao.id] then -- initialize the balance for the process id
if not Balances[Protocol] then -- initialize the balance for the process id
Balances = {
[ao.id] = math.floor(50000000 * 1000000), -- 50M IO
[Protocol] = math.floor(50000000 * 1000000), -- 50M IO
[Owner] = math.floor(constants.totalTokenSupply - (50000000 * 1000000)), -- 950M IO
}
end
Expand Down Expand Up @@ -1325,23 +1326,34 @@ addEventingHandler(
addEventingHandler("totalTokenSupply", utils.hasMatchingTag("Action", "Total-Token-Supply"), function(msg)
-- add all the balances
local totalSupply = 0
local circulatingSupply = 0
local lockedSupply = 0
local stakedSupply = 0
local delegatedSupply = 0
local withdrawSupply = 0
local protocolBalance = balances.getBalance(Protocol)
local balances = balances.getBalances()
for _, balance in pairs(balances) do
totalSupply = totalSupply + balance
circulatingSupply = circulatingSupply + balance
end
-- gateways and delegates
local gateways = gar.getGateways()
for _, gateway in pairs(gateways) do
totalSupply = totalSupply + gateway.operatorStake + gateway.totalDelegatedStake
stakedSupply = stakedSupply + gateway.operatorStake
delegatedSupply = delegatedSupply + gateway.totalDelegatedStake
for _, delegate in pairs(gateway.delegates) do
-- check vaults
for _, vault in pairs(delegate.vaults) do
totalSupply = totalSupply + vault.balance
withdrawSupply = withdrawSupply + vault.balance
end
end
-- iterate through vaults
for _, vault in pairs(gateway.vaults) do
totalSupply = totalSupply + vault.balance
withdrawSupply = withdrawSupply + vault.balance
end
end

Expand All @@ -1351,14 +1363,29 @@ addEventingHandler("totalTokenSupply", utils.hasMatchingTag("Action", "Total-Tok
-- they may have several vaults iterate through them
for _, vault in pairs(vaultsForAddress) do
totalSupply = totalSupply + vault.balance
lockedSupply = lockedSupply + vault.balance
end
end

ao.send({
Target = msg.From,
Action = "Total-Token-Supply-Notice",
["Total-Token-Supply"] = totalSupply,
Data = json.encode(totalSupply),
["Circulating-Supply"] = circulatingSupply,
["Locked-Supply"] = lockedSupply,
["Staked-Supply"] = stakedSupply,
["Delegated-Supply"] = delegatedSupply,
["Withdraw-Supply"] = withdrawSupply,
["Protocol-Balance"] = protocolBalance,
Data = json.encode({
total = totalSupply,
circulating = circulatingSupply,
locked = lockedSupply,
staked = stakedSupply,
delegated = delegatedSupply,
withdrawn = withdrawSupply,
protocolBalance = protocolBalance,
}),
})
end)

Expand Down
37 changes: 33 additions & 4 deletions tests/arns.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import assert from 'node:assert';
import {
AO_LOADER_HANDLER_ENV,
DEFAULT_HANDLE_OPTIONS,
PROCESS_ID,
STUB_ADDRESS,
} from '../tools/constants.mjs';

Expand Down Expand Up @@ -75,10 +76,38 @@ describe('ArNS', async () => {
delete buyRecordEvent['Start-Timestamp'];
delete buyRecordEvent['End-Timestamp'];
const expectedRemainingBalance = {
"0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa": 0,
"1111111111111111111111111111111111111111111": 950000000000000,
}
assert.deepEqual(buyRecordEvent, { _e: 1, 'Purchase-Type': 'lease', 'DF-Purchases-This-Period': 1, 'DF-Revenue-This-Period': 600000000, 'DF-Current-Demand-Factor': 1, Action: 'Buy-Record', 'Name-Length': 9, 'Purchase-Price': 600000000, 'Base-Registration-Fee': 500000000, 'DF-Current-Period': 1, 'DF-Trailing-Period-Revenues': [0,0,0,0,0,0], 'DF-Trailing-Period-Purchases': [0,0,0,0,0,0,0], Cron: false, Cast: false, 'Undername-Limit': 10, Name: 'test-name', Years: '1', 'DF-Consecutive-Periods-With-Min-Demand-Factor': 0, 'Process-Id': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', From: sender, 'From-Formatted': sender, 'Message-Id': '1111111111111111111111111111111111111111111', 'Records-Count': 1, 'Protocol-Balance': 950000000000000, 'Reserved-Records-Count': 0, 'Remaining-Balance': expectedRemainingBalance[sender] });
'0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa': 0,
'1111111111111111111111111111111111111111111': 949999400000000,
[PROCESS_ID]: 50000600000000,
};
assert.deepEqual(buyRecordEvent, {
_e: 1,
'Purchase-Type': 'lease',
'DF-Purchases-This-Period': 1,
'DF-Revenue-This-Period': 600000000,
'DF-Current-Demand-Factor': 1,
Action: 'Buy-Record',
'Name-Length': 9,
'Purchase-Price': 600000000,
'Base-Registration-Fee': 500000000,
'DF-Current-Period': 1,
'DF-Trailing-Period-Revenues': [0, 0, 0, 0, 0, 0],
'DF-Trailing-Period-Purchases': [0, 0, 0, 0, 0, 0, 0],
Cron: false,
Cast: false,
'Undername-Limit': 10,
Name: 'test-name',
Years: '1',
'DF-Consecutive-Periods-With-Min-Demand-Factor': 0,
'Process-Id': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
From: sender,
'From-Formatted': sender,
'Message-Id': '1111111111111111111111111111111111111111111',
'Records-Count': 1,
'Protocol-Balance': expectedRemainingBalance[PROCESS_ID],
'Reserved-Records-Count': 0,
'Remaining-Balance': expectedRemainingBalance[sender],
});

// fetch the record
const realRecord = await handle(
Expand Down
57 changes: 57 additions & 0 deletions tests/handlers.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,61 @@ describe('handlers', async () => {
'should only have 3 handlers',
); // forces us to think critically about the order of handlers so intended to be sensitive to changes
});

describe('token supply', () => {
it('should always be 1 billion IO', async () => {
const supplyResult = await handle({
Tags: [
{
name: 'Action',
value: 'Total-Token-Supply',
},
],
});

// assert no errors
assert.deepEqual(supplyResult.Messages[0].Error, undefined);

// assert correct tag in message by finding the index of the tag in the message
const notice = supplyResult.Messages?.[0]?.Tags?.find(
(tag) =>
tag.name === 'Action' &&
tag.value === 'Total-Token-Supply-Notice',
);
assert.ok(notice, 'should have a Total-Token-Supply-Notice tag');

const supplyData = JSON.parse(supplyResult.Messages[0].Data);

assert.ok(
supplyData.total === 1000000000 * 1000000,
'total supply should be 1 billion IO but was ' + supplyData.total,
);
assert.ok(
supplyData.circulating === 1000000000 * 1000000,
'circulating supply should be 1 billion IO but was ' +
supplyData.circulating,
);
assert.ok(
supplyData.locked === 0,
'locked supply should be 0 but was ' + supplyData.locked,
);
assert.ok(
supplyData.staked === 0,
'staked supply should be 0 but was ' + supplyData.staked,
);
assert.ok(
supplyData.delegated === 0,
'delegated supply should be 0 but was ' + supplyData.delegated,
);
assert.ok(
supplyData.withdrawn === 0,
'withdrawn supply should be 0 but was ' + supplyData.withdrawn,
);

assert.ok(
supplyData.protocolBalance === 50000000000000,
'protocol balance should be 50M IO but was ' + supplyData.protocolBalance,
);
});
});
});
52 changes: 48 additions & 4 deletions tests/monitor/monitor.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { DockerComposeEnvironment, Wait } from 'testcontainers';

const io = IO.init({
process: new AOProcess({
processId: process.env.IO_PROCESS_ID || IO_TESTNET_PROCESS_ID,
processId: process.env.IO_PROCESS_ID || IO_DEVNET_PROCESS_ID,
ao: connect({
CU_URL: 'http://localhost:6363',
}),
Expand Down Expand Up @@ -125,10 +125,54 @@ describe('setup', () => {

describe('token supply', () => {
it('should always be 1 billion IO', async () => {
const totalSupply = await io.getTokenSupply();
const supplyData = await io.getTokenSupply();
console.log('supplyData', supplyData);
console.log(await io.getBalances({ limit: 1000 }));
assert(
totalSupply === 1000000000 * 1000000,
`Total supply is not 1 billion IO: ${totalSupply}`,
supplyData.total === 1000000000 * 1000000,
`Total supply is not 1 billion IO: ${supplyData.total}`,
);
assert(
supplyData.protocolBalance > 0,
`Protocol balance is empty: ${supplyData.protocolBalance}`,
);
assert(
supplyData.circulating >= 0,
`Circulating supply is undefined: ${supplyData.circulating}`,
);
assert(
supplyData.locked >= 0,
`Locked supply is undefined: ${supplyData.locked}`,
);
assert(
supplyData.staked >= 0,
`Staked supply is undefined: ${supplyData.staked}`,
);
assert(
supplyData.withdrawn >= 0,
`Withdrawn supply is undefined: ${supplyData.withdrawn}`,
);
assert(
supplyData.delegated >= 0,
`Delegated supply is undefined: ${supplyData.delegated}`,
);

const computedCirculating =
supplyData.total - supplyData.locked - supplyData.staked - supplyData.delegated- supplyData.withdrawn;
assert(
supplyData.circulating === computedCirculating,
`Circulating supply (${supplyData.circulating}) is not equal to the sum of total, locked, staked, delegated, and withdrawn (${computedCirculating})`,
);

const computedTotal =
supplyData.circulating +
supplyData.locked +
supplyData.withdrawn +
supplyData.staked +
supplyData.delegated;
assert(
supplyData.total === computedTotal,
`Total supply (${supplyData.total}) is not equal to the sum of protocol balance, circulating, locked, staked, and delegated (${computedTotal})`,
);
});
});
Expand Down
3 changes: 2 additions & 1 deletion tools/constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export const PROCESS_ID = 'PROCESS_ID'.padEnd(43, '1');
export const STUB_ADDRESS = ''.padEnd(43, '1');
/* ao READ-ONLY Env Variables */
export const AO_LOADER_HANDLER_ENV = {
Process: {
Id: STUB_ADDRESS,
Id: PROCESS_ID,
Owner: STUB_ADDRESS,
Tags: [{ name: 'Authority', value: 'XXXXXX' }],
},
Expand Down

0 comments on commit 287e500

Please sign in to comment.