Skip to content

Commit

Permalink
refactor: typing improvements (#720)
Browse files Browse the repository at this point in the history
  • Loading branch information
glevco authored Aug 15, 2024
1 parent 5084bb8 commit fa06d4b
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 27 deletions.
33 changes: 33 additions & 0 deletions __tests__/__fixtures__/tx_history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ export default [
{
tx_id: '0000000110eb9ec96e255a09d6ae7d856bff53453773bae5500cee2905db670e',
version: 0,
signalBits: 0,
nonce: 0,
tokens: [],
weight: 22.719884359974895,
timestamp: 1613538740,
is_voided: false,
Expand Down Expand Up @@ -31,6 +34,9 @@ export default [
{
tx_id: '0000000211673128a49ea700c7b3efbbbc4b3bfe5bf289bc5a1967f12d3c30b5',
version: 0,
signalBits: 0,
nonce: 0,
tokens: [],
weight: 21.911255937644835,
timestamp: 1613519819,
is_voided: false,
Expand Down Expand Up @@ -60,6 +66,9 @@ export default [
{
tx_id: '0000000292236abbb41a2201409b85084631389950f76f7da0d8734b51b31758',
version: 0,
signalBits: 0,
nonce: 0,
tokens: [],
weight: 22.255565408819944,
timestamp: 1613524608,
is_voided: false,
Expand Down Expand Up @@ -89,6 +98,9 @@ export default [
{
tx_id: '00000003508b2c24066365cffe23cd77896dad01a665c64cd4797654c6acab92',
version: 0,
signalBits: 0,
nonce: 0,
tokens: [],
weight: 21.7848190101318,
timestamp: 1613529833,
is_voided: true,
Expand Down Expand Up @@ -118,6 +130,9 @@ export default [
{
tx_id: '0000000419625e2587c225fb49f36278c9da681ec05e039125307b8aef3d3d30',
version: 0,
signalBits: 0,
nonce: 0,
tokens: [],
weight: 22.227347437740097,
timestamp: 1613522627,
is_voided: false,
Expand Down Expand Up @@ -147,6 +162,9 @@ export default [
{
tx_id: '0000000fa2157d34a56b89c08e1783cd1103f6e6807bb01ccff1d920b31d1052',
version: 0,
signalBits: 0,
nonce: 0,
tokens: [],
weight: 26.450454121121226,
timestamp: 1613678350,
is_voided: false,
Expand Down Expand Up @@ -176,6 +194,9 @@ export default [
{
tx_id: '0000000fa2157d34a56b89c08e1783cd1103f6e6807bb01ccff1d920b31d1053',
version: 0,
signalBits: 0,
nonce: 0,
tokens: [],
weight: 26.450454121121226,
timestamp: 1613678350,
is_voided: false,
Expand Down Expand Up @@ -205,6 +226,9 @@ export default [
{
tx_id: '0000000fa2157d34a56b89c08e1783cd1103f6e6807bb01ccff1d920b31d1054',
version: 0,
signalBits: 0,
nonce: 0,
tokens: [],
weight: 26.450454121121226,
timestamp: 1613678350,
is_voided: false,
Expand Down Expand Up @@ -234,6 +258,9 @@ export default [
{
tx_id: '0000000fa2157d34a56b89c08e1783cd1103f6e6807bb01ccff1d920b31d1055',
version: 0,
signalBits: 0,
nonce: 0,
tokens: [],
weight: 26.450454121121226,
timestamp: 1613678350,
is_voided: false,
Expand Down Expand Up @@ -263,6 +290,9 @@ export default [
{
tx_id: '0000000fa2157d34a56b89c08e1783cd1103f6e6807bb01ccff1d920b31d1056',
version: 0,
signalBits: 0,
nonce: 0,
tokens: [],
weight: 26.450454121121226,
timestamp: 1613678370,
is_voided: false,
Expand Down Expand Up @@ -292,6 +322,9 @@ export default [
{
tx_id: '0000000fa2157d34a56b89c08e1783cd1103f6e6807bb01ccff1d920b31d1057',
version: 0,
signalBits: 0,
nonce: 0,
tokens: [],
weight: 26.450454121121226,
timestamp: 1613678370,
is_voided: false,
Expand Down
33 changes: 24 additions & 9 deletions __tests__/storage/common_store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { HDPrivateKey } from 'bitcore-lib';
import { LevelDBStore, MemoryStore, Storage } from '../../src/storage';
import { TOKEN_AUTHORITY_MASK, TOKEN_MINT_MASK, GAP_LIMIT } from '../../src/constants';
import walletUtils from '../../src/utils/wallet';
import { ILockedUtxo, IUtxo, OutputValueType } from '../../src/types';

const DATA_DIR = './testdata.leveldb';

Expand Down Expand Up @@ -41,7 +42,7 @@ describe('locked utxo methods', () => {

// helper functions

async function countUtxos(store) {
async function countUtxos(store: LevelDBStore) {
let utxoCount = 0;
let lutxoCount = 0;
for await (const _ of store.utxoIter()) {
Expand All @@ -56,13 +57,26 @@ describe('locked utxo methods', () => {
};
}

function getLockedUtxo(txId, address, timelock, height, value, token, token_data) {
function getLockedUtxo(
txId,
address,
timelock,
height,
value: OutputValueType,
token,
token_data
): ILockedUtxo {
return {
index: 0,
tx: {
tx_id: txId,
height,
version: 1,
signalBits: 0,
weight: 0,
nonce: 0,
parents: [],
tokens: [],
timestamp: tsFromDate(tsBefore),
is_voided: false,
inputs: [],
Expand All @@ -73,13 +87,14 @@ describe('locked utxo methods', () => {
token,
spent_by: null,
decoded: { type: 'P2PKH', address, timelock },
script: '',
},
],
},
};
}

function getUtxoFromLocked(lutxo) {
function getUtxoFromLocked(lutxo: ILockedUtxo): IUtxo {
const { tx, index } = lutxo;
const { outputs } = tx;
const output = outputs[index];
Expand All @@ -94,20 +109,20 @@ describe('locked utxo methods', () => {
authorities: 0,
timelock,
type: tx.version,
height: tx.height,
height: tx.height || null,
};
}

// actual test body

async function testLockedUtxoMethods(store) {
async function testLockedUtxoMethods(store: LevelDBStore) {
const lockedUtxos = [
// utxo to be unlocked by time
getLockedUtxo(
'tx01',
'WYiD1E8n5oB9weZ8NMyM3KoCjKf1KCjWAZ',
tsFromDate(tsBefore),
null,
undefined,
100, // value
'00', // token
0 // token_data
Expand All @@ -117,7 +132,7 @@ describe('locked utxo methods', () => {
'tx02',
'WYiD1E8n5oB9weZ8NMyM3KoCjKf1KCjWAZ',
tsFromDate(tsAfter),
null,
undefined,
100, // value
'00', // token
0 // token_data
Expand All @@ -127,7 +142,7 @@ describe('locked utxo methods', () => {
'tx03',
'WYBwT3xLpDnHNtYZiU52oanupVeDKhAvNp',
tsFromDate(tsBefore),
null,
undefined,
100, // value
'01', // token
0 // token_data
Expand All @@ -137,7 +152,7 @@ describe('locked utxo methods', () => {
'tx04',
'WYBwT3xLpDnHNtYZiU52oanupVeDKhAvNp',
tsFromDate(tsBefore),
null,
undefined,
TOKEN_MINT_MASK, // value, mint
'01', // token
TOKEN_AUTHORITY_MASK | 1 // token_data
Expand Down
8 changes: 7 additions & 1 deletion __tests__/storage/leveldb/leveldb_store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,13 @@ test('token methods', async () => {

await store.editTokenMeta('00', {
numTransactions: 10,
balance: { tokens: { locked: 1, unlocked: 2 } },
balance: {
tokens: { locked: 1, unlocked: 2 },
authorities: {
mint: { locked: 1, unlocked: 2 },
melt: { locked: 1, unlocked: 2 },
},
},
});
await expect(store.tokenIndex.getTokenMetadata('00')).resolves.toMatchObject({
numTransactions: 10,
Expand Down
38 changes: 33 additions & 5 deletions __tests__/storage/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
import * as cryptoUtils from '../../src/utils/crypto';
import { InvalidPasswdError } from '../../src/errors';
import Network from '../../src/models/network';
import { WALLET_FLAGS } from '../../src/types';
import { ILockedUtxo, IUtxo, OutputValueType, WALLET_FLAGS } from '../../src/types';

const DATA_DIR = './testdata.leveldb';

Expand Down Expand Up @@ -68,6 +68,13 @@ describe('handleStop', () => {
timestamp: 123,
inputs: [],
outputs: [],
is_voided: false,
nonce: 0,
parents: [],
signalBits: 0,
tokens: [],
version: 0,
weight: 0,
});
await storage.registerToken(testToken);
const address0 = await storage.getAddressAtIndex(0);
Expand Down Expand Up @@ -138,6 +145,13 @@ describe('handleStop', () => {
timestamp: 1234,
inputs: [],
outputs: [],
is_voided: false,
nonce: 0,
parents: [],
signalBits: 0,
tokens: [],
version: 0,
weight: 0,
});

// handleStop with cleanAddresses = true
Expand Down Expand Up @@ -418,13 +432,26 @@ describe('process locked utxos', () => {
await processLockedUtxoTest(store);
});

function getLockedUtxo(txId, address, timelock, height, value, token, token_data) {
function getLockedUtxo(
txId,
address,
timelock,
height,
value: OutputValueType,
token,
token_data
): ILockedUtxo {
return {
index: 0,
tx: {
tx_id: txId,
height,
version: 1,
signalBits: 0,
weight: 0,
nonce: 0,
parents: [],
tokens: [],
timestamp: timelock,
is_voided: false,
inputs: [],
Expand All @@ -439,13 +466,14 @@ describe('process locked utxos', () => {
address,
timelock,
},
script: '',
},
],
},
};
}

function getUtxoFromLocked(lutxo) {
function getUtxoFromLocked(lutxo: ILockedUtxo): IUtxo {
const { tx, index } = lutxo;
const { outputs } = tx;
const output = outputs[index];
Expand Down Expand Up @@ -484,7 +512,7 @@ describe('process locked utxos', () => {
'tx01',
'WYiD1E8n5oB9weZ8NMyM3KoCjKf1KCjWAZ',
tsUnLocked,
null,
undefined,
100, // value
'00', // token
0 // token_data
Expand All @@ -494,7 +522,7 @@ describe('process locked utxos', () => {
'tx02',
'WYiD1E8n5oB9weZ8NMyM3KoCjKf1KCjWAZ',
tsLocked,
null,
undefined,
100, // value
'00', // token
0 // token_data
Expand Down
8 changes: 8 additions & 0 deletions __tests__/utils/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ describe('_updateTokensData', () => {
success: true,
name: mockToken.name,
symbol: mockToken.symbol,
mint: [],
melt: [],
total: 0,
transactions_count: 0,
});

// Execute
Expand Down Expand Up @@ -174,6 +178,10 @@ describe('_updateTokensData', () => {
success: true,
name: mockToken.name,
symbol: mockToken.symbol,
mint: [],
melt: [],
total: 0,
transactions_count: 0,
});

// Execute
Expand Down
Loading

0 comments on commit fa06d4b

Please sign in to comment.