Skip to content

Commit

Permalink
feat(api): break balance response into confirmed and unconfirmed comp…
Browse files Browse the repository at this point in the history
…onents

BREAKING CHANGE: balance response has changed from `{balance: number}` to `{confirmed: number,
unconfirmed: number}`
  • Loading branch information
nitsujlangston committed Dec 27, 2018
1 parent 4f24286 commit 894cec5
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 18 deletions.
21 changes: 14 additions & 7 deletions packages/bitcore-node/src/models/coin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,32 @@ class Coin extends BaseModel<ICoin> {
);
}

getBalance(params: { query: any }) {
async getBalance(params: { query: any }): Promise<{ confirmed: number, unconfirmed: number }> {
let { query } = params;
query = Object.assign(query, {
spentHeight: { $lt: SpentHeightIndicators.minimum },
mintHeight: { $gt: SpentHeightIndicators.conflicting }
});
return this.collection
.aggregate<{ balance: number }>([
const result = await this.collection
.aggregate<{ _id: string, balance: number }>([
{ $match: query },
{ $project: { value: 1, _id: 0 } },
{
$project: {
value: 1,
status: {$cond: { if: { $gte: ['$mintHeight', SpentHeightIndicators.minimum]}, then: 'confirmed', else: 'unconfirmed' }},
_id: 0
}
},
{
$group: {
_id: null,
_id: '$status',
balance: { $sum: '$value' }

}
},
{ $project: { _id: false } }
}
])
.toArray();
return result.reduce((acc, cur) => { acc[cur._id] = cur.balance; return acc; }, {confirmed: 0, unconfirmed: 0}) as {confirmed: number, unconfirmed: number};
}

resolveAuthhead(mintTxid: string, chain?: string, network?: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export class ERC20StateProvider extends ETHStateProvider

async getBalanceForAddress(params: CSP.GetBalanceForAddressParams) {
const { network, address } = params;
const balance: number = await this.erc20For(network)
const balance= await this.erc20For(network)
.methods.balanceOf(address)
.call();
return [{ balance }];
return { confirmed: balance, unconfirmed: 0};
};
}
8 changes: 4 additions & 4 deletions packages/bitcore-node/src/providers/chain-state/eth/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ export class ETHStateProvider extends InternalStateProvider

async getBalanceForAddress(
params: CSP.GetBalanceForAddressParams
): Promise<{ balance: number }[]> {
) {
const { network, address } = params;
const balance = Number(await this.getRPC(network).getBalance(address));
return [{ balance }];
return {confirmed: balance, unconfirmed: 0};
}

async getBlock(params: CSP.GetBlockParams) {
Expand Down Expand Up @@ -68,13 +68,13 @@ export class ETHStateProvider extends InternalStateProvider
let addressBalancePromises = addresses.map(({ address }) =>
this.getBalanceForAddress({ chain: this.chain, network, address })
);
let addressBalances = await Promise.all<{ balance: number }[]>(
let addressBalances = await Promise.all<{ confirmed: number, unconfirmed: number }>(
addressBalancePromises
);
let balance = addressBalances.reduce(
(prev, cur) => Number(prev) + Number(cur[0].balance),
0
);
return [{ balance }];
return {confirmed: balance, unconfirmed: 0};
}
}
2 changes: 1 addition & 1 deletion packages/bitcore-node/src/routes/api/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ router.get('/:address/balance', async function(req, res) {
network,
address
});
return res.send((result && result[0]) || { balance: 0 });
return res.send(result || { confirmed: 0, unconfirmed: 0 });
} catch (err) {
return res.status(500).send(err);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/bitcore-node/src/routes/api/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ router.get('/:pubKey/balance', authenticate, async (req: AuthenticatedRequest, r
network,
wallet: req.wallet!
});
return res.send((result && result[0]) || { balance: 0 });
return res.send(result || { confirmed: 0, unconfirmed: 0 });
} catch (err) {
return res.status(500).json(err);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,16 @@ export declare namespace CSP {
export type Provider<T> = { get(params: { chain: string }): T };
export type ChainStateProvider = Provider<IChainStateService> & IChainStateService;
export interface IChainStateService {
getBalanceForAddress(params: GetBalanceForAddressParams): Promise<{ balance: number }[]>;
getBalanceForWallet(params: GetBalanceForWalletParams): Promise<{ balance: number }[]>;
getBalanceForAddress(params: GetBalanceForAddressParams): Promise<{ confirmed: number, unconfirmed: number }>;
getBalanceForWallet(params: GetBalanceForWalletParams): Promise<{ confirmed: number, unconfirmed: number }>;
getBlock(params: GetBlockParams): Promise<IBlock | string>;
streamBlocks(params: StreamBlocksParams): any;
getFee(params: GetEstimateSmartFeeParams): any;
broadcastTransaction(params: BroadcastTransactionParams): Promise<any>;
createWallet(params: CreateWalletParams): Promise<IWallet>;
getWallet(params: GetWalletParams): Promise<IWallet | null>;
updateWallet(params: UpdateWalletParams): Promise<{}>;
getWalletBalance(params: GetWalletBalanceParams): Promise<{ balance: number }[]>;
getWalletBalance(params: GetWalletBalanceParams): Promise<{ confirmed: number, unconfirmed: number }>;
streamAddressUtxos(params: StreamAddressUtxosParams): any;
streamAddressTransactions(params: StreamAddressUtxosParams): any;
streamTransactions(params: StreamTransactionsParams): any;
Expand Down

0 comments on commit 894cec5

Please sign in to comment.