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(balances): return balance as string #303

Merged
merged 4 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1830,12 +1830,12 @@ addEventingHandler(ActionMap.Balance, Handlers.utils.hasMatchingTag("Action", Ac
local target = msg.Tags.Target or msg.Tags.Address or msg.Tags.Recipient or msg.From
local balance = balances.getBalance(target)

-- must adhere to token.lua spec for arconnect compatibility
-- must adhere to token.lua spec defined by https://github.com/permaweb/aos/blob/15dd81ee596518e2f44521e973b8ad1ce3ee9945/blueprints/token.lua
Send(msg, {
Target = msg.From,
Action = "Balance-Notice",
Account = target,
Data = balance,
Data = tostring(balance),
Balance = tostring(balance),
Ticker = Ticker,
})
Expand Down
27 changes: 27 additions & 0 deletions tests/balances.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getBalance, getBalances, startMemory } from './helpers.mjs';
import { describe, it } from 'node:test';
import assert from 'node:assert';
import { STUB_ADDRESS, PROCESS_OWNER } from '../tools/constants.mjs';

describe('Balances', async () => {
it('should return the balance for a specific address', async () => {
const balance = await getBalance({
address: PROCESS_OWNER,
memory: startMemory,
});
assert.equal(balance, 950000000000000);
});

it('should return 0 for a non-existent address', async () => {
const balance = await getBalance({
address: STUB_ADDRESS,
memory: startMemory,
});
assert.equal(balance, 0);
});

it('should return dictionary of all balances', async () => {
const balances = await getBalances({ memory: startMemory });
assert.equal(balances[PROCESS_OWNER], 950000000000000);
});
});
26 changes: 24 additions & 2 deletions tests/helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,30 @@ export const getBalance = async ({
memory,
timestamp = STUB_TIMESTAMP,
}) => {
const balances = await getBalances({ memory, timestamp });
return balances[address];
const result = await handle({
options: {
Tags: [
{ name: 'Action', value: 'Balance' },
{ name: 'Address', value: address },
],
},
timestamp,
memory,
});
// enforce the token.lua "spec" as defined by https://github.com/permaweb/aos/blob/15dd81ee596518e2f44521e973b8ad1ce3ee9945/blueprints/token.lua
assert(
['Action', 'Balance', 'Account', 'Ticker'].every((tag) =>
result.Messages[0].Tags.map((t) => t.name).includes(tag),
),
`Tags are not in compliance with the token.lua spec. ${JSON.stringify(result.Messages[0].Tags, null, 2)}`,
);
assert(
typeof result.Messages[0].Data === 'string' &&
!isNaN(Number(result.Messages[0].Data)),
'Balance is invalid. It is not a string which is out of compliance with the token.lua spec',
);
const balance = JSON.parse(result.Messages[0].Data);
return balance;
};

export const transfer = async ({
Expand Down
11 changes: 11 additions & 0 deletions tests/monitor/monitor.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ describe('setup', () => {
);
}
});

it('should always be able to fetch the protocol balance and it should be greater than 0', async () => {
const balance = await io.getBalance({
address: processId,
});
// assert it is greater than 0 and response is a number
assert(
balance >= 0 && typeof balance === 'number',
'Balance is not valid',
);
});
});

describe('distribution totals', () => {
Expand Down
Loading