From ec4d6cbc0ea0b9dd55ed75e786cba917f3564e02 Mon Sep 17 00:00:00 2001 From: dtfiedler Date: Mon, 30 Dec 2024 14:13:56 -0600 Subject: [PATCH 1/4] fix(balances): return balance as string --- src/main.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.lua b/src/main.lua index 5c88597e..ede59e6c 100644 --- a/src/main.lua +++ b/src/main.lua @@ -1835,7 +1835,7 @@ addEventingHandler(ActionMap.Balance, Handlers.utils.hasMatchingTag("Action", Ac Target = msg.From, Action = "Balance-Notice", Account = target, - Data = balance, + Data = tostring(balance), Balance = tostring(balance), Ticker = Ticker, }) From ee185e7f0fea51a4a04234e060f5150453009b3d Mon Sep 17 00:00:00 2001 From: dtfiedler Date: Mon, 30 Dec 2024 15:11:25 -0600 Subject: [PATCH 2/4] chore(test): add additional test around balances --- tests/balances.test.mjs | 27 +++++++++++++++++++++++++++ tests/helpers.mjs | 14 ++++++++++++-- tests/monitor/monitor.test.mjs | 11 +++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 tests/balances.test.mjs diff --git a/tests/balances.test.mjs b/tests/balances.test.mjs new file mode 100644 index 00000000..8f9f368c --- /dev/null +++ b/tests/balances.test.mjs @@ -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); + }); +}); diff --git a/tests/helpers.mjs b/tests/helpers.mjs index f5be73ae..dfe8593e 100644 --- a/tests/helpers.mjs +++ b/tests/helpers.mjs @@ -123,8 +123,18 @@ 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, + }); + const balance = JSON.parse(result.Messages[0].Data); + return balance; }; export const transfer = async ({ diff --git a/tests/monitor/monitor.test.mjs b/tests/monitor/monitor.test.mjs index 0d4caa7f..8565f805 100644 --- a/tests/monitor/monitor.test.mjs +++ b/tests/monitor/monitor.test.mjs @@ -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', () => { From 3e6e498345c3f90aa79a3e63d18f1ba3e37a5404 Mon Sep 17 00:00:00 2001 From: dtfiedler Date: Mon, 30 Dec 2024 15:23:39 -0600 Subject: [PATCH 3/4] chore(docs): add link to token.lua "spec" --- src/main.lua | 2 +- tests/helpers.mjs | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main.lua b/src/main.lua index ede59e6c..73d922ae 100644 --- a/src/main.lua +++ b/src/main.lua @@ -1830,7 +1830,7 @@ 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", diff --git a/tests/helpers.mjs b/tests/helpers.mjs index dfe8593e..66fa6965 100644 --- a/tests/helpers.mjs +++ b/tests/helpers.mjs @@ -133,6 +133,17 @@ export const getBalance = async ({ timestamp, memory, }); + // enforce the token.lua "spec" as defined by https://github.com/permaweb/aos/blob/15dd81ee596518e2f44521e973b8ad1ce3ee9945/blueprints/token.lua + assert( + result.Messages[0].Tags.every( + (tag) => tag.name in ['Action', 'Address', 'Balance'], + ), + 'Tags are not in compliance with the token.lua spec.', + ); + assert( + typeof result.Messages[0].Data === 'string', + '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; }; From 36a51c78e0c7b6c1028cf3751851f562abc8ef3a Mon Sep 17 00:00:00 2001 From: dtfiedler Date: Mon, 30 Dec 2024 16:31:51 -0600 Subject: [PATCH 4/4] chore(test): update test --- tests/helpers.mjs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/helpers.mjs b/tests/helpers.mjs index 66fa6965..eb5b79d5 100644 --- a/tests/helpers.mjs +++ b/tests/helpers.mjs @@ -135,13 +135,14 @@ export const getBalance = async ({ }); // enforce the token.lua "spec" as defined by https://github.com/permaweb/aos/blob/15dd81ee596518e2f44521e973b8ad1ce3ee9945/blueprints/token.lua assert( - result.Messages[0].Tags.every( - (tag) => tag.name in ['Action', 'Address', 'Balance'], + ['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.', + `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', + 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);