Skip to content

Commit

Permalink
fix(supply): modify the json implementation to not modify large numbe… (
Browse files Browse the repository at this point in the history
#116)

…rs if full integers
  • Loading branch information
dtfiedler authored Oct 22, 2024
2 parents 73b46b0 + 9f651e8 commit d6fd771
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 70 deletions.
8 changes: 7 additions & 1 deletion src/json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ local function encode_number(val)
if val ~= val or val <= -math.huge or val >= math.huge then
error("unexpected number value '" .. tostring(val) .. "'")
end
return string.format("%.14g", val)
if math.floor(val) == val then
-- Large integers: avoid scientific notation and print as an integer
return string.format("%.0f", val)
else
-- Decimals: use the 'g' format to print floating point with precision, up to 14 significant digits
return string.format("%.14g", val)
end
end

local type_func_map = {
Expand Down
14 changes: 7 additions & 7 deletions src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1595,13 +1595,13 @@ addEventingHandler("totalTokenSupply", utils.hasMatchingTag("Action", "Total-Tok
ao.send({
Target = msg.From,
Action = "Total-Token-Supply-Notice",
["Total-Token-Supply"] = totalSupply,
["Circulating-Supply"] = circulatingSupply,
["Locked-Supply"] = lockedSupply,
["Staked-Supply"] = stakedSupply,
["Delegated-Supply"] = delegatedSupply,
["Withdraw-Supply"] = withdrawSupply,
["Protocol-Balance"] = protocolBalance,
["Total-Token-Supply"] = tostring(totalSupply),
["Circulating-Supply"] = tostring(circulatingSupply),
["Locked-Supply"] = tostring(lockedSupply),
["Staked-Supply"] = tostring(stakedSupply),
["Delegated-Supply"] = tostring(delegatedSupply),
["Withdraw-Supply"] = tostring(withdrawSupply),
["Protocol-Balance"] = tostring(protocolBalance),
Data = json.encode({
-- TODO: we are losing precision on these values unexpectedly. This has been brought to the AO team - for now the tags should be correct as they are stringified
total = totalSupply,
Expand Down
124 changes: 62 additions & 62 deletions tests/monitor/monitor.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -174,78 +174,78 @@ describe('setup', () => {
);

// TODO: there is an unknown precision loss on these values, we are discussing why with Forward. Once fixed, uncomment these tests
// const { items: balances } = await io.getBalances({
// limit: 10_000,
// });
const { items: balances } = await io.getBalances({
limit: 10_000,
});

// const protocolBalance = await io.getBalance({
// address: processId,
// });
const protocolBalance = await io.getBalance({
address: processId,
});

// assert(
// protocolBalance === supplyData.protocolBalance,
// `Protocol balance is not equal to the balance provided by the contract: ${protocolBalance} !== ${supplyData.protocolBalance}`,
// );
assert(
protocolBalance === supplyData.protocolBalance,
`Protocol balance is not equal to the balance provided by the contract: ${protocolBalance} !== ${supplyData.protocolBalance}`,
);

// const totalBalances = balances.reduce(
// (acc, curr) => acc + curr.balance,
// 0,
// );
// const circulating = totalBalances - protocolBalance;
// assert(
// circulating === supplyData.circulating,
// `Circulating supply is not equal to the sum of the balances minus the protocol balance: ${circulating} !== ${supplyData.circulating}`,
// );
const totalBalances = balances.reduce(
(acc, curr) => acc + curr.balance,
0,
);
const circulating = totalBalances - protocolBalance;
assert(
circulating === supplyData.circulating,
`Circulating supply is not equal to the sum of the balances minus the protocol balance: ${circulating} !== ${supplyData.circulating}`,
);

// // get the supply staked
// const { items: gateways } = await io.getGateways({
// limit: 1000,
// });
// get the supply staked
const { items: gateways } = await io.getGateways({
limit: 1000,
});

// const staked = gateways.reduce(
// (acc, curr) => acc + curr.operatorStake,
// 0,
// );
const staked = gateways.reduce(
(acc, curr) => acc + curr.operatorStake,
0,
);

// assert(
// staked === supplyData.staked,
// `Staked supply is not equal to the sum of the operator stakes: ${staked} !== ${supplyData.staked}`,
// );
assert(
staked === supplyData.staked,
`Staked supply is not equal to the sum of the operator stakes: ${staked} !== ${supplyData.staked}`,
);

// const delegated = gateways.reduce(
// (acc, curr) => acc + curr.totalDelegatedStake,
// 0,
// );
const delegated = gateways.reduce(
(acc, curr) => acc + curr.totalDelegatedStake,
0,
);

// assert(
// delegated === supplyData.delegated,
// `Delegated supply is not equal to the sum of the total delegated stakes: ${delegated} !== ${supplyData.delegated}`,
// );
assert(
delegated === supplyData.delegated,
`Delegated supply is not equal to the sum of the total delegated stakes: ${delegated} !== ${supplyData.delegated}`,
);

// const computedTotal =
// supplyData.circulating +
// supplyData.locked +
// supplyData.withdrawn +
// supplyData.staked +
// supplyData.delegated +
// supplyData.protocolBalance;
// assert(
// supplyData.total === computedTotal &&
// computedTotal === 1000000000 * 1000000,
// `Computed total supply (${computedTotal}) is not equal to the sum of protocol balance, circulating, locked, staked, and delegated and withdrawn provided by the contract (${supplyData.total}) and does not match the expected total of 1 billion IO`,
// );
const computedTotal =
supplyData.circulating +
supplyData.locked +
supplyData.withdrawn +
supplyData.staked +
supplyData.delegated +
supplyData.protocolBalance;
assert(
supplyData.total === computedTotal &&
computedTotal === 1000000000 * 1000000,
`Computed total supply (${computedTotal}) is not equal to the sum of protocol balance, circulating, locked, staked, and delegated and withdrawn provided by the contract (${supplyData.total}) and does not match the expected total of 1 billion IO`,
);

// const computedCirculating =
// supplyData.total -
// supplyData.locked -
// supplyData.staked -
// supplyData.delegated -
// supplyData.withdrawn -
// supplyData.protocolBalance;
// assert(
// supplyData.circulating === computedCirculating,
// `Computed circulating supply (${computedCirculating}) is not equal to the total supply minus protocol balance, locked, staked, delegated, and withdrawn provided by the contract (${supplyData.circulating})`,
// );
const computedCirculating =
supplyData.total -
supplyData.locked -
supplyData.staked -
supplyData.delegated -
supplyData.withdrawn -
supplyData.protocolBalance;
assert(
supplyData.circulating === computedCirculating,
`Computed circulating supply (${computedCirculating}) is not equal to the total supply minus protocol balance, locked, staked, delegated, and withdrawn provided by the contract (${supplyData.circulating})`,
);
});
});

Expand Down

0 comments on commit d6fd771

Please sign in to comment.