-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: return only positive address balance
- Loading branch information
1 parent
6675458
commit 2752025
Showing
2 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
149 changes: 149 additions & 0 deletions
149
server/components/api/addresses/addressesDAL.db.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters