Skip to content

Commit

Permalink
fix: make types strict to prevent accidental deletion via unused type…
Browse files Browse the repository at this point in the history
…s, the optionality is a trap, they are required
  • Loading branch information
peter-sanderson committed Jan 14, 2025
1 parent 6bc3217 commit 184ebbb
Show file tree
Hide file tree
Showing 14 changed files with 363 additions and 24 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/test-blockchain-link.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ on:
- cron: "0 0 * * *"
pull_request:
paths:
- "packages/blockchain-link"
- "packages/blockchain-link-utils"
- "packages/blockchain-link-types"
- "packages/blockchain-link/**"
- "packages/blockchain-link-utils/**"
- "packages/blockchain-link-types/**"
- "packages/e2e-utils/src/fixtures/blockbook.ts"
- "packages/e2e-utils/src/mocks/backendServer.ts"
# dependencies of packages/blockchain-link
- "packages/utxo-lib"
- "packages/utils"
- "packages/utxo-lib/**"
- "packages/utils/**"
- "docker/docker-blockchain-link-test.sh"
- ".github/workflows/blockchain-link-test.yml"
- "yarn.lock"
Expand Down
8 changes: 5 additions & 3 deletions packages/blockchain-link-types/src/blockfrost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ export interface Address {
address: string;
path: string;
transfers: number;
balance?: string;
sent?: string;
received?: string;
balance: string;
sent: string;
received: string;
}

export interface AccountAddresses {
Expand Down Expand Up @@ -223,6 +223,7 @@ export type AccountUtxo = {
export interface UtxosData extends AddressUtxoContent {
blockInformation: BlockContent;
}

export interface AssetBalance {
unit: string; // policy id + encoded name
quantity: string;
Expand Down Expand Up @@ -297,4 +298,5 @@ declare function FSend(
params: AccountBalanceHistoryParams,
): Promise<AccountBalanceHistory[]>;
declare function FSend(method: 'ESTIMATE_FEE', params: EstimateFeeParams): Promise<Fee>;

export type Send = typeof FSend;
6 changes: 3 additions & 3 deletions packages/blockchain-link-types/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ export interface Address {
path: string;
transfers: number;
// decimal: number,
balance?: string;
sent?: string;
received?: string;
balance: string;
sent: string;
received: string;
}

export interface AccountAddresses {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import { sumVinVout } from '@trezor/blockchain-link-utils';
import { transformTransaction } from '@trezor/blockchain-link-utils/src/blockbook';
import type { GetAccountBalanceHistory as Req } from '@trezor/blockchain-link-types/src/messages';
import type { GetAccountBalanceHistory as Res } from '@trezor/blockchain-link-types/src/responses';
import type { AccountAddresses, Transaction } from '@trezor/blockchain-link-types/src/common';
import { AccountAddresses, Address, Transaction } from '@trezor/blockchain-link-types/src/common';
import type { HistoryTx } from '@trezor/blockchain-link-types/src/electrum';

import { Api, tryGetScripthash, getTransactions, discoverAddress, AddressHistory } from '../utils';

const transformAddress = (addr: AddressHistory) => ({
const transformAddress = (addr: AddressHistory): Address => ({
address: addr.address,
path: addr.path,
transfers: addr.history.length,
balance: '0',
sent: '0',
received: '0',
});

const aggregateTransactions = (txs: (Transaction & { blockTime: number })[], groupBy = 3600) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ const getAccountInfo: Api<Req, Res> = async (client, payload) => {
path,
transfers: history.length,
balance: confirmed.toString(), // TODO or confirmed + unconfirmed?
sent: '0',
received: '0',
});

const addresses = {
Expand All @@ -134,6 +136,9 @@ const getAccountInfo: Api<Req, Res> = async (client, payload) => {
address,
path,
transfers,
balance: '0',
sent: '0',
received: '0',
...(['tokenBalances', 'txids', 'txs'].includes(details) && transfers
? {
balance,
Expand Down
9 changes: 5 additions & 4 deletions packages/coinjoin/src/backend/getAccountInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
ScanAccountCheckpoint,
PrederivedAddress,
AccountCache,
Address,
} from '../types/backend';
import { getAccountUtxo } from './getAccountUtxo';
import { CoinjoinAddressController } from './CoinjoinAddressController';
Expand Down Expand Up @@ -39,7 +40,7 @@ const sumBalance = (current: number, tx: Transaction) => current + getDelta(tx);

const enhanceAddress =
(transactions: Transaction[]) =>
({ address, path }: PrederivedAddress) => {
({ address, path }: PrederivedAddress): Address => {
const txs = transactions.filter(tx => doesTxContainAddress(address)(tx.details));
const sent = sumAddressValues(txs, address, tx => tx.details.vin);
const received = sumAddressValues(txs, address, tx => tx.details.vout);
Expand All @@ -48,9 +49,9 @@ const enhanceAddress =
address,
path,
transfers: txs.length,
balance: txs.length ? (received - sent).toString() : undefined,
sent: txs.length ? sent.toString() : undefined,
received: txs.length ? received.toString() : undefined,
balance: txs.length ? (received - sent).toString() : '0',
sent: txs.length ? sent.toString() : '0',
received: txs.length ? received.toString() : '0',
};
};

Expand Down
11 changes: 10 additions & 1 deletion packages/connect/src/types/api/__tests__/bitcoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,16 @@ export const signTransaction = async (api: TrezorConnect) => {
addresses: {
used: [],
unused: [],
change: [{ path: 'm/44', address: 'a', transfers: 0 }],
change: [
{
path: 'm/44',
address: 'a',
transfers: 0,
sent: '0',
balance: '0',
received: '0',
},
],
},
},
coin: 'btc',
Expand Down
Loading

0 comments on commit 184ebbb

Please sign in to comment.