diff --git a/src/json.lua b/src/json.lua index d8ee08e..c215d4c 100644 --- a/src/json.lua +++ b/src/json.lua @@ -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 = { diff --git a/src/main.lua b/src/main.lua index 837832a..927891a 100644 --- a/src/main.lua +++ b/src/main.lua @@ -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, diff --git a/tests/monitor/monitor.test.mjs b/tests/monitor/monitor.test.mjs index b78cc70..8f854da 100644 --- a/tests/monitor/monitor.test.mjs +++ b/tests/monitor/monitor.test.mjs @@ -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})`, + ); }); });