Skip to content

Commit

Permalink
test(invariants): more precise balance assertions PE-7211
Browse files Browse the repository at this point in the history
  • Loading branch information
Ariel Melendez committed Dec 9, 2024
1 parent 20476ff commit 0307691
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions tests/invariants.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import assert from 'node:assert';
import { getBalances, getVaults } from './helpers.mjs';

function assertValidBalance(balance) {
function assertValidBalance(balance, expectedMin = 1) {
assert(
balance > 0 && balance <= 1_000_000_000_000_000,
Number.isInteger(balance) &&
balance >= expectedMin &&
balance <= 1_000_000_000_000_000,
`Invariant violated: balance ${balance} is invalid`,
);
}
Expand Down Expand Up @@ -39,7 +41,7 @@ async function assertNoBalanceInvariants({ timestamp, memory }) {
timestamp,
});
for (const [address, balance] of Object.entries(balances)) {
assertValidBalance(balance);
assertValidBalance(balance, 0);
assertValidAddress(address);
}
}
Expand All @@ -61,3 +63,35 @@ async function assertNoBalanceVaultInvariants({ timestamp, memory }) {
});
}
}

async function assertNoTotalSupplyInvariants({ timestamp, memory }) {
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,
);
assertValidBalance(supplyData.circulating);
assertValidBalance(supplyData.locked, 0);
assertValidBalance(supplyData.staked, 0);
assertValidBalance(supplyData.delegated, 0);
assertValidBalance(supplyData.withdrawn, 0);
assertValidBalance(supplyData.protocolBalance, 0);
}

0 comments on commit 0307691

Please sign in to comment.