Skip to content

Commit

Permalink
feat: return only positive address balance
Browse files Browse the repository at this point in the history
  • Loading branch information
liorfrenkel committed Mar 6, 2020
1 parent 6675458 commit 2752025
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 1 deletion.
149 changes: 149 additions & 0 deletions server/components/api/addresses/addressesDAL.db.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
'use strict';

const test = require('blue-tape');
const truncate = require('../../../../test/lib/truncate');
const transactionsDAL = require('../transactions/transactionsDAL');
const blocksDAL = require('../blocks/blocksDAL');
const outputsDAL = require('../outputs/outputsDAL');
const inputsDAL = require('../inputs/inputsDAL');
const addressesDAL = require('../addresses/addressesDAL');
const createDemoBlocksFromTo = require('../../../../test/lib/createDemoBlocksFromTo');
const faker = require('faker');

test('addressesDAL.snapshotAddressBalancesByBlock() (DB)', async function(t) {
await wrapTest('Given no transactions', async given => {
await createDemoBlocksFromTo(1, 10);

const balance = await addressesDAL.snapshotAddressBalancesByBlock({address: 'tzn111', blockNumber: 10});

t.equal(balance.length, 0, `${given}: should return an empty array`);
});

await wrapTest('Given has an asset with positive balance', async given => {
await createDemoBlocksFromTo(1, 10);

const block = await blocksDAL.findByBlockNumber(1);
const tx = await transactionsDAL.create({
index: 0,
version: 0,
inputCount: 0,
outputCount: 1,
hash: faker.random.uuid(),
});
await outputsDAL.create({
TransactionId: tx.id,
lockType: 'PK',
address: 'tzn111',
asset: '00',
amount: '100',
index: 0,
});
await blocksDAL.addTransaction(block, tx);

const balance = await addressesDAL.snapshotAddressBalancesByBlock({address: 'tzn111', blockNumber: 10});

t.equal(balance.length, 1, `${given}: should return 1 item`);
t.equal(balance[0].amount, '100', `${given}: should have the right amount`);
});

await wrapTest('Given has 2 assets with positive balance', async given => {
await createDemoBlocksFromTo(1, 10);

const block = await blocksDAL.findByBlockNumber(1);
const tx = await transactionsDAL.create({
index: 0,
version: 0,
inputCount: 0,
outputCount: 1,
hash: faker.random.uuid(),
});
await outputsDAL.create({
TransactionId: tx.id,
lockType: 'PK',
address: 'tzn111',
asset: '00',
amount: '100',
index: 0,
});
await outputsDAL.create({
TransactionId: tx.id,
lockType: 'PK',
address: 'tzn111',
asset: '00000000a515de480812021d184014dc43124254ddc6b994331bc8abe5fbd6c04bc3c130',
amount: '200',
index: 0,
});
await blocksDAL.addTransaction(block, tx);

const balance = await addressesDAL.snapshotAddressBalancesByBlock({address: 'tzn111', blockNumber: 10});

t.equal(balance.length, 2, `${given}: should return 1 item`);
});

await wrapTest('Given has an asset with balance 0', async given => {
await createDemoBlocksFromTo(1, 10);

const block1 = await blocksDAL.findByBlockNumber(1);
const block2 = await blocksDAL.findByBlockNumber(2);
// received
const tx1 = await transactionsDAL.create({
index: 0,
version: 0,
inputCount: 0,
outputCount: 1,
hash: faker.random.uuid(),
});
const output1 = await outputsDAL.create({
TransactionId: tx1.id,
lockType: 'PK',
address: 'tzn111',
asset: '00',
amount: '100',
index: 0,
});
// sent
const tx2 = await transactionsDAL.create({
index: 0,
version: 0,
inputCount: 0,
outputCount: 1,
hash: faker.random.uuid(),
});
await inputsDAL.create({
index: 0,
outpointTXHash: tx1.hash,
outpointIndex: 0,
isMint: false,
asset: '00',
amount: '100',
TransactionId: tx2.id,
OutputId: output1.id,
});
await outputsDAL.create({
TransactionId: tx2.id,
lockType: 'PK',
address: 'tzn112',
asset: '00',
amount: '100',
index: 0,
});
await blocksDAL.addTransaction(block1, tx1);
await blocksDAL.addTransaction(block2, tx2);

const balance = await addressesDAL.snapshotAddressBalancesByBlock({address: 'tzn111', blockNumber: 10});

t.equal(balance.length, 0, `${given}: should return no items`);
});

});


test.onFinish(() => {
blocksDAL.db.sequelize.close();
});

async function wrapTest(given, test) {
await truncate();
await test(given);
}

3 changes: 2 additions & 1 deletion server/components/api/addresses/addressesDAL.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ addressesDAL.snapshotAddressBalancesByBlock = async function({
INNER JOIN "Blocks" b ON t."BlockId" = b.id AND b."blockNumber" <= :blockNumber
WHERE io.address = :address
GROUP BY io.asset) AS isums
ON osums.asset = isums.asset) AS bothsums;
ON osums.asset = isums.asset) AS bothsums
WHERE output_sum > input_sum;
`;

return sequelize.query(sql, {
Expand Down

0 comments on commit 2752025

Please sign in to comment.