Skip to content

Commit 66aff5f

Browse files
Merge branch 'v8.0.0' of github.com:bitpay/bitcore into v8.0.0
2 parents 887d073 + 824a22c commit 66aff5f

File tree

6 files changed

+81
-31
lines changed

6 files changed

+81
-31
lines changed

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

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BTCStateProvider } from './btc/btc';
22
import { BCHStateProvider } from './bch/bch';
33
import { ETHStateProvider } from './eth/eth';
4-
import { BATStateProvider } from "./erc20/tokens/bat";
4+
import { BATStateProvider } from './erc20/tokens/bat';
55
import { CSP } from '../../types/namespaces/ChainStateProvider';
66
import { Chain } from '../../types/ChainNetwork';
77

@@ -14,7 +14,7 @@ const services: CSP.ChainStateServices = {
1414

1515
class ChainStateProxy implements CSP.ChainStateProvider {
1616
get({ chain }: Chain) {
17-
if(services[chain] == undefined) {
17+
if (services[chain] == undefined) {
1818
throw new Error(`Chain ${chain} doesn't have a ChainStateProvider registered`);
1919
}
2020
return services[chain];
@@ -24,6 +24,10 @@ class ChainStateProxy implements CSP.ChainStateProvider {
2424
return this.get(params).streamAddressUtxos(params);
2525
}
2626

27+
streamAddressTransactions(params: CSP.StreamAddressUtxosParams) {
28+
return this.get(params).streamAddressTransactions(params);
29+
}
30+
2731
async getBalanceForAddress(params: CSP.GetBalanceForAddressParams) {
2832
return this.get(params).getBalanceForAddress(params);
2933
}
@@ -84,11 +88,11 @@ class ChainStateProxy implements CSP.ChainStateProvider {
8488
return this.get(params).broadcastTransaction(params);
8589
}
8690

87-
registerService(currency: string, service: CSP.IChainStateService){
91+
registerService(currency: string, service: CSP.IChainStateService) {
8892
services[currency] = service;
89-
};
93+
}
9094

91-
async getCoinsForTx(params: {chain: string; network: string, txid: string }) {
95+
async getCoinsForTx(params: { chain: string; network: string; txid: string }) {
9296
return this.get(params).getCoinsForTx(params);
9397
}
9498
}

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

+20-6
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,33 @@ export class InternalStateProvider implements CSP.IChainStateService {
3030
return new RPC(username, password, host, port);
3131
}
3232

33-
streamAddressUtxos(params: CSP.StreamAddressUtxosParams) {
34-
const { network, address, limit = 10, stream, args } = params;
33+
private getAddressQuery(params: CSP.StreamAddressUtxosParams) {
34+
const {network, address, args} = params;
3535
if (typeof address !== 'string' || !this.chain || !network) {
3636
throw 'Missing required param';
3737
}
38-
let query = { chain: this.chain, network: network.toLowerCase(), address } as any;
39-
const unspent = args.unspent;
40-
if (unspent) {
41-
query.spentHeight = { $lt: 0 };
38+
const query = { chain: this.chain, network: network.toLowerCase(), address } as any;
39+
if(args.unspent) {
40+
query.spentHeight = {$lt: 0};
4241
}
42+
return query;
43+
}
44+
45+
streamAddressUtxos(params: CSP.StreamAddressUtxosParams) {
46+
const { limit = 10, stream } = params;
47+
const query = this.getAddressQuery(params);
4348
Storage.apiStreamingFind(CoinModel, query, { limit }, stream);
4449
}
4550

51+
async streamAddressTransactions(params: CSP.StreamAddressUtxosParams) {
52+
const { limit = 10, stream } = params;
53+
const query = this.getAddressQuery(params);
54+
const coins = await CoinModel.collection.find(query, {limit}).toArray();
55+
const txids = coins.map((coin) => coin.mintTxid);
56+
const txQuery = {txid: {$in: txids}};
57+
Storage.apiStreamingFind(TransactionModel, txQuery, {}, stream);
58+
}
59+
4660
async getBalanceForAddress(params: CSP.GetBalanceForAddressParams) {
4761
const { network, address } = params;
4862
let query = { chain: this.chain, network, address };

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

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@ import express = require('express');
22
const router = express.Router({ mergeParams: true });
33
import { ChainStateProvider } from '../../providers/chain-state';
44

5+
6+
7+
router.get('/:address/txs', function(req, res) {
8+
let { address, chain, network } = req.params;
9+
let { unspent, limit=10 } = req.query;
10+
let payload = {
11+
chain,
12+
network,
13+
address,
14+
limit,
15+
stream: res,
16+
args: { unspent }
17+
};
18+
ChainStateProvider.streamAddressTransactions(payload);
19+
});
20+
521
router.get('/:address', function(req, res) {
622
let { address, chain, network } = req.params;
723
let { unspent, limit=10 } = req.query;

packages/bitcore-node/src/services/storage.ts

+21-17
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { TransformableModel } from '../types/TransformableModel';
44
import logger from '../logger';
55
import config from '../config';
66
import { LoggifyClass } from '../decorators/Loggify';
7-
import { MongoClient, Db, FindOneOptions } from 'mongodb';
7+
import { MongoClient, Db, FindOneOptions, Cursor } from 'mongodb';
88
import '../models';
99

1010
export type StreamingFindOptions<T> = Partial<{
@@ -63,7 +63,7 @@ export class StorageService {
6363
stop() {}
6464

6565
validPagingProperty<T>(model: TransformableModel<T>, property: keyof T) {
66-
return model.allowedPaging.some((prop) => prop.key === property);
66+
return model.allowedPaging.some(prop => prop.key === property);
6767
}
6868

6969
/**
@@ -93,21 +93,7 @@ export class StorageService {
9393
return typecastedValue;
9494
}
9595

96-
apiStreamingFind<T>(model: TransformableModel<T>, query: any, options: StreamingFindOptions<T>, res: Response) {
97-
if (options.since !== undefined && options.paging && this.validPagingProperty(model, options.paging)) {
98-
options.since = this.typecastForDb(model, options.paging, options.since);
99-
if (options.direction && Number(options.direction) === 1) {
100-
query[options.paging] = { $gt: options.since };
101-
options.sort = { [options.paging]: 1 };
102-
} else {
103-
query[options.paging] = { $lt: options.since };
104-
options.sort = { [options.paging]: -1 };
105-
}
106-
}
107-
options.limit = Math.min(options.limit || 100, 1000);
108-
let cursor = model.collection.find(query, options).stream({
109-
transform: model._apiTransform
110-
});
96+
apiStream<T>(cursor: Cursor<T>, res: Response) {
11197
cursor.on('error', function(err) {
11298
return res.status(500).end(err.message);
11399
});
@@ -132,6 +118,24 @@ export class StorageService {
132118
res.end();
133119
});
134120
}
121+
122+
apiStreamingFind<T>(model: TransformableModel<T>, query: any, options: StreamingFindOptions<T>, res: Response) {
123+
if (options.since !== undefined && options.paging && this.validPagingProperty(model, options.paging)) {
124+
options.since = this.typecastForDb(model, options.paging, options.since);
125+
if (options.direction && Number(options.direction) === 1) {
126+
query[options.paging] = { $gt: options.since };
127+
options.sort = { [options.paging]: 1 };
128+
} else {
129+
query[options.paging] = { $lt: options.since };
130+
options.sort = { [options.paging]: -1 };
131+
}
132+
}
133+
options.limit = Math.min(options.limit || 100, 1000);
134+
let cursor = model.collection.find(query, options).stream({
135+
transform: model._apiTransform
136+
});
137+
this.apiStream(cursor, res);
138+
}
135139
}
136140

137141
export let Storage = new StorageService();

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

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export declare namespace CSP {
5858
limit: number;
5959
args: StreamAddressUtxosArgs;
6060
};
61+
6162
export type StreamTransactionsParams = ChainNetwork & {
6263
stream: Response;
6364
args: any;
@@ -98,6 +99,7 @@ export declare namespace CSP {
9899
updateWallet(params: UpdateWalletParams): Promise<{}>;
99100
getWalletBalance(params: GetWalletBalanceParams): Promise<{ balance: number }[]>;
100101
streamAddressUtxos(params: StreamAddressUtxosParams): any;
102+
streamAddressTransactions(params: StreamAddressUtxosParams): any;
101103
streamTransactions(params: StreamTransactionsParams): any;
102104
streamTransaction(params: StreamTransactionParams): any;
103105
streamWalletAddresses(params: StreamWalletAddressesParams): any;

packages/insight/app/src/pages/address/address.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { IonicPage, NavController, NavParams } from 'ionic-angular';
33
import { Http } from '@angular/http';
44
import { ApiProvider } from '../../providers/api/api';
55
import { CurrencyProvider } from '../../providers/currency/currency';
6-
import { TxsProvider, ApiInput } from '../../providers/transactions/transactions';
6+
import { TxsProvider, ApiInput, ApiTx } from '../../providers/transactions/transactions';
77

88
/**
99
* Generated class for the AddressPage page.
@@ -53,11 +53,21 @@ export class AddressPage {
5353
balance,
5454
txAppearances: apiCoin.length
5555
};
56-
this.transactions = apiCoin;
5756
this.loading = false;
5857
},
5958
err => {
60-
console.log('err is', err);
59+
console.error('err is', err);
60+
}
61+
);
62+
63+
let txurl: string = this.apiProvider.apiPrefix + '/address/' + this.addrStr + '/txs';
64+
this.http.get(txurl).subscribe(
65+
data => {
66+
let apiTx: ApiTx[] = data.json() as ApiTx[];
67+
this.transactions = apiTx.map(this.txProvider.toAppTx);
68+
},
69+
err => {
70+
console.error('err is', err);
6171
this.loading = false;
6272
}
6373
);

0 commit comments

Comments
 (0)