Skip to content

Commit 894cec5

Browse files
feat(api): break balance response into confirmed and unconfirmed components
BREAKING CHANGE: balance response has changed from `{balance: number}` to `{confirmed: number, unconfirmed: number}`
1 parent 4f24286 commit 894cec5

File tree

6 files changed

+25
-18
lines changed

6 files changed

+25
-18
lines changed

packages/bitcore-node/src/models/coin.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,32 @@ class Coin extends BaseModel<ICoin> {
6464
);
6565
}
6666

67-
getBalance(params: { query: any }) {
67+
async getBalance(params: { query: any }): Promise<{ confirmed: number, unconfirmed: number }> {
6868
let { query } = params;
6969
query = Object.assign(query, {
7070
spentHeight: { $lt: SpentHeightIndicators.minimum },
7171
mintHeight: { $gt: SpentHeightIndicators.conflicting }
7272
});
73-
return this.collection
74-
.aggregate<{ balance: number }>([
73+
const result = await this.collection
74+
.aggregate<{ _id: string, balance: number }>([
7575
{ $match: query },
76-
{ $project: { value: 1, _id: 0 } },
76+
{
77+
$project: {
78+
value: 1,
79+
status: {$cond: { if: { $gte: ['$mintHeight', SpentHeightIndicators.minimum]}, then: 'confirmed', else: 'unconfirmed' }},
80+
_id: 0
81+
}
82+
},
7783
{
7884
$group: {
79-
_id: null,
85+
_id: '$status',
8086
balance: { $sum: '$value' }
87+
8188
}
82-
},
83-
{ $project: { _id: false } }
89+
}
8490
])
8591
.toArray();
92+
return result.reduce((acc, cur) => { acc[cur._id] = cur.balance; return acc; }, {confirmed: 0, unconfirmed: 0}) as {confirmed: number, unconfirmed: number};
8693
}
8794

8895
resolveAuthhead(mintTxid: string, chain?: string, network?: string) {

packages/bitcore-node/src/providers/chain-state/erc20/erc20.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ export class ERC20StateProvider extends ETHStateProvider
2020

2121
async getBalanceForAddress(params: CSP.GetBalanceForAddressParams) {
2222
const { network, address } = params;
23-
const balance: number = await this.erc20For(network)
23+
const balance= await this.erc20For(network)
2424
.methods.balanceOf(address)
2525
.call();
26-
return [{ balance }];
26+
return { confirmed: balance, unconfirmed: 0};
2727
};
2828
}

packages/bitcore-node/src/providers/chain-state/eth/eth.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ export class ETHStateProvider extends InternalStateProvider
3434

3535
async getBalanceForAddress(
3636
params: CSP.GetBalanceForAddressParams
37-
): Promise<{ balance: number }[]> {
37+
) {
3838
const { network, address } = params;
3939
const balance = Number(await this.getRPC(network).getBalance(address));
40-
return [{ balance }];
40+
return {confirmed: balance, unconfirmed: 0};
4141
}
4242

4343
async getBlock(params: CSP.GetBlockParams) {
@@ -68,13 +68,13 @@ export class ETHStateProvider extends InternalStateProvider
6868
let addressBalancePromises = addresses.map(({ address }) =>
6969
this.getBalanceForAddress({ chain: this.chain, network, address })
7070
);
71-
let addressBalances = await Promise.all<{ balance: number }[]>(
71+
let addressBalances = await Promise.all<{ confirmed: number, unconfirmed: number }>(
7272
addressBalancePromises
7373
);
7474
let balance = addressBalances.reduce(
7575
(prev, cur) => Number(prev) + Number(cur[0].balance),
7676
0
7777
);
78-
return [{ balance }];
78+
return {confirmed: balance, unconfirmed: 0};
7979
}
8080
}

packages/bitcore-node/src/routes/api/address.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ router.get('/:address/balance', async function(req, res) {
3838
network,
3939
address
4040
});
41-
return res.send((result && result[0]) || { balance: 0 });
41+
return res.send(result || { confirmed: 0, unconfirmed: 0 });
4242
} catch (err) {
4343
return res.status(500).send(err);
4444
}

packages/bitcore-node/src/routes/api/wallet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ router.get('/:pubKey/balance', authenticate, async (req: AuthenticatedRequest, r
181181
network,
182182
wallet: req.wallet!
183183
});
184-
return res.send((result && result[0]) || { balance: 0 });
184+
return res.send(result || { confirmed: 0, unconfirmed: 0 });
185185
} catch (err) {
186186
return res.status(500).json(err);
187187
}

packages/bitcore-node/src/types/namespaces/ChainStateProvider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,16 @@ export declare namespace CSP {
110110
export type Provider<T> = { get(params: { chain: string }): T };
111111
export type ChainStateProvider = Provider<IChainStateService> & IChainStateService;
112112
export interface IChainStateService {
113-
getBalanceForAddress(params: GetBalanceForAddressParams): Promise<{ balance: number }[]>;
114-
getBalanceForWallet(params: GetBalanceForWalletParams): Promise<{ balance: number }[]>;
113+
getBalanceForAddress(params: GetBalanceForAddressParams): Promise<{ confirmed: number, unconfirmed: number }>;
114+
getBalanceForWallet(params: GetBalanceForWalletParams): Promise<{ confirmed: number, unconfirmed: number }>;
115115
getBlock(params: GetBlockParams): Promise<IBlock | string>;
116116
streamBlocks(params: StreamBlocksParams): any;
117117
getFee(params: GetEstimateSmartFeeParams): any;
118118
broadcastTransaction(params: BroadcastTransactionParams): Promise<any>;
119119
createWallet(params: CreateWalletParams): Promise<IWallet>;
120120
getWallet(params: GetWalletParams): Promise<IWallet | null>;
121121
updateWallet(params: UpdateWalletParams): Promise<{}>;
122-
getWalletBalance(params: GetWalletBalanceParams): Promise<{ balance: number }[]>;
122+
getWalletBalance(params: GetWalletBalanceParams): Promise<{ confirmed: number, unconfirmed: number }>;
123123
streamAddressUtxos(params: StreamAddressUtxosParams): any;
124124
streamAddressTransactions(params: StreamAddressUtxosParams): any;
125125
streamTransactions(params: StreamTransactionsParams): any;

0 commit comments

Comments
 (0)