-
-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathgetAccountInfo.ts
172 lines (155 loc) · 6.44 KB
/
getAccountInfo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { discovery } from '@trezor/utxo-lib';
import { sortTxsFromLatest } from '@trezor/blockchain-link-utils';
import { transformTransaction } from '@trezor/blockchain-link-utils/src/blockbook';
import type { ElectrumAPI } from '@trezor/blockchain-link-types/src/electrum';
import type { GetAccountInfo as Req } from '@trezor/blockchain-link-types/src/messages';
import type { GetAccountInfo as Res } from '@trezor/blockchain-link-types/src/responses';
import type { VinVout } from '@trezor/blockchain-link-types/src/blockbook';
import type { Address, Transaction } from '@trezor/blockchain-link-types';
import { Api, tryGetScripthash, discoverAddress, AddressHistory, getTransactions } from '../utils';
// const PAGE_DEFAULT = 0;
const PAGE_SIZE_DEFAULT = 25;
type AddressInfo = Omit<AddressHistory, 'scripthash'> & {
confirmed: number;
unconfirmed: number;
};
const getBalances =
(client: ElectrumAPI) =>
(addresses: AddressHistory[]): Promise<AddressInfo[]> =>
Promise.all(
addresses.map(async ({ address, path, history, scripthash, empty }) => {
const { confirmed, unconfirmed } = history.length
? await client.request('blockchain.scripthash.get_balance', scripthash)
: {
confirmed: 0,
unconfirmed: 0,
};
return {
address,
path,
history,
empty,
confirmed,
unconfirmed,
};
}),
);
const getPagination = (perPage: number, txs: Transaction[]) => ({
index: 1,
// Intentionally incorrect as Electrum backend doesn't support pagination yet but Suite needs total page count
size: perPage,
total: Math.ceil(txs.length / perPage),
});
export const sumAddressValues = <T>(
transactions: T[],
address: string,
getVinVouts: (tr: T) => VinVout[],
) =>
transactions
.flatMap(tx =>
getVinVouts(tx)
.filter(({ addresses }) => addresses?.includes(address))
.map(({ value }) => (value ? Number.parseFloat(value) : 0)),
)
.reduce((a, b) => a + b, 0);
const getAccountInfo: Api<Req, Res> = async (client, payload) => {
const { descriptor, details = 'basic', pageSize = PAGE_SIZE_DEFAULT } = payload;
const network = client.getInfo()?.network;
const parsed = tryGetScripthash(descriptor, network);
if (parsed.valid) {
const { confirmed, unconfirmed, history } = await Promise.all([
client.request('blockchain.scripthash.get_balance', parsed.scripthash),
client.request('blockchain.scripthash.get_history', parsed.scripthash),
]).then(([{ confirmed, unconfirmed }, history]) => ({
confirmed,
unconfirmed,
history,
}));
const historyUnconfirmed = history.filter(r => r.height <= 0).length;
const transactions =
details === 'txs'
? await getTransactions(client, history)
.then(txs => txs.map(tx => transformTransaction(tx, descriptor)))
.then(sortTxsFromLatest)
: undefined;
return {
descriptor,
balance: confirmed.toString(),
availableBalance: (confirmed + unconfirmed).toString(),
empty: !history.length,
history: {
total: history.length - historyUnconfirmed,
unconfirmed: historyUnconfirmed,
transactions,
},
page: details === 'txs' ? getPagination(pageSize, transactions ?? []) : undefined,
};
}
const discover = discoverAddress(client);
const receive = await discovery(discover, descriptor, 'receive', network).then(
getBalances(client),
);
const change = await discovery(discover, descriptor, 'change', network).then(
getBalances(client),
);
const batch = receive.concat(change);
const [confirmed, unconfirmed] = batch.reduce(
([c, u], { confirmed, unconfirmed }) => [c + confirmed, u + unconfirmed],
[0, 0],
);
const history = batch.flatMap(({ history }) => history);
const historyUnconfirmed = history.filter(r => r.height <= 0).length;
const transformAddressInfo = ({ address, path, history, confirmed }: AddressInfo): Address => ({
address,
path,
transfers: history.length,
balance: confirmed.toString(), // TODO or confirmed + unconfirmed?
});
const addresses = {
change: change.map(transformAddressInfo),
unused: receive.filter(recv => !recv.history.length).map(transformAddressInfo),
used: receive.filter(recv => recv.history.length).map(transformAddressInfo),
};
const transactions = ['tokenBalances', 'txids', 'txs'].includes(details)
? await getTransactions(client, history)
.then(txs => txs.map(tx => transformTransaction(tx, addresses)))
.then(sortTxsFromLatest)
: [];
const extendAddressInfo = ({ address, path, transfers, balance }: Address): Address => ({
address,
path,
transfers,
...(['tokenBalances', 'txids', 'txs'].includes(details) && transfers
? {
balance,
sent: sumAddressValues(transactions, address, tx => tx.details.vin).toString(),
received: sumAddressValues(
transactions,
address,
tx => tx.details.vout,
).toString(),
}
: {}),
});
return {
descriptor,
balance: confirmed.toString(),
availableBalance: (confirmed + unconfirmed).toString(),
empty: !history.length,
history: {
total: history.length - historyUnconfirmed,
unconfirmed: historyUnconfirmed,
transactions: details === 'txs' ? transactions : undefined,
},
addresses:
details !== 'basic'
? {
change: addresses.change.map(extendAddressInfo),
unused: addresses.unused.map(extendAddressInfo),
used: addresses.used.map(extendAddressInfo),
}
: undefined,
page: details === 'txs' ? getPagination(pageSize, transactions) : undefined,
};
};
export default getAccountInfo;