Skip to content

Commit 56bd7a7

Browse files
Merge branch 'master' of github.com:bitpay/bitcore
2 parents 44b49b9 + 659494f commit 56bd7a7

File tree

3 files changed

+49
-39
lines changed

3 files changed

+49
-39
lines changed

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,17 @@ class CoinModel extends BaseModel<ICoin> {
100100

101101
async getBalanceAtTime(params: { query: any; time: string; chain: string; network: string }) {
102102
let { query, time, chain, network } = params;
103-
const block = await BlockStorage.collection.findOne({
104-
$query: {
105-
chain,
106-
network,
107-
timeNormalized: { $lte: new Date(time) }
108-
},
109-
$orderBy: { timeNormalized: -1 }
110-
});
103+
const [block] = await BlockStorage.collection
104+
.find({
105+
$query: {
106+
chain,
107+
network,
108+
timeNormalized: { $lte: new Date(time) }
109+
}
110+
})
111+
.limit(1)
112+
.sort({ timeNormalized: -1 })
113+
.toArray();
111114
const blockHeight = block!.height;
112115
const combinedQuery = Object.assign(
113116
{},

packages/bitcore-node/test/helpers/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { StateStorage } from '../../src/models/state';
2-
import * as sinon from 'sinon';
2+
import sinon from 'sinon';
33
import { BlockStorage } from '../../src/models/block';
44
import { TransactionStorage } from '../../src/models/transaction';
55
import { CoinStorage } from '../../src/models/coin';
@@ -58,3 +58,10 @@ export function mockStorage(toReturn, collectionMethods = {}) {
5858
} as any;
5959
return Storage;
6060
}
61+
62+
export function mockModel(collectionName: string, toReturn: any, collectionMethods = {}) {
63+
const isStubbed: sinon.SinonStub = Storage.db!.collection as sinon.SinonStub;
64+
if (isStubbed.withArgs) {
65+
isStubbed.withArgs(collectionName).returns(mockCollection(toReturn, collectionMethods));
66+
}
67+
}

packages/bitcore-node/test/unit/models/coin.unit.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { SpentHeightIndicators } from '../../../src/types/Coin';
44
import { ObjectId } from 'mongodb';
55
import sinon from 'sinon';
66
import { BlockStorage } from '../../../src/models/block';
7-
import { mockStorage } from '../../helpers/index.js';
7+
import { mockStorage, mockModel } from '../../helpers/index.js';
88

9-
describe('Coin Model', function () {
9+
describe('Coin Model', function() {
1010
describe('_apiTransform', () => {
1111
it('should return the transform object with coin info', () => {
1212
let id = new ObjectId();
@@ -91,41 +91,41 @@ describe('Coin Model', function () {
9191
sandbox.restore();
9292
});
9393

94-
it('should return an object with confirmed, unconfirmed, and balance when additional time parameter is passed in', async () => {
95-
let id = new ObjectId("5c364e342ab5602e97a56f0e");
94+
it('should return an object with confirmed, unconfirmed, and balance when additional time parameter is passed in', async () => {
95+
let id = new ObjectId('5c364e342ab5602e97a56f0e');
9696
let chain = 'BTC';
9797
let network = 'regtest';
9898
let time = new Date().toISOString();
9999
let query = { wallets: id, 'wallets.0': { $exists: true } };
100-
let matchObject = {
101-
"$or": [
100+
let matchObject = {
101+
$or: [
102102
{
103-
"spentHeight": {
104-
"$gt": 123
103+
spentHeight: {
104+
$gt: 123
105105
}
106106
},
107107
{
108-
"spentHeight": {
109-
"$lt": 0
108+
spentHeight: {
109+
$lt: 0
110110
}
111111
}
112112
],
113-
"mintHeight": {
114-
"$lte": 123
113+
mintHeight: {
114+
$lte: 123
115115
},
116-
"wallets": new ObjectId('5c364e342ab5602e97a56f0e'),
117-
"wallets.0": { '$exists': true },
118-
}
116+
wallets: new ObjectId('5c364e342ab5602e97a56f0e'),
117+
'wallets.0': { $exists: true }
118+
};
119119

120120
let blockModelHeight = { height: 123 };
121-
mockStorage([{_id: 'confirmed', balance: 123123}, {_id: 'unconfirmed', balance: 1}]);
122-
Object.assign(BlockStorage.collection.findOne, sandbox.stub().resolves(blockModelHeight));
121+
mockModel('coins', [{ _id: 'confirmed', balance: 123123 }, { _id: 'unconfirmed', balance: 1 }]);
122+
mockModel('blocks', blockModelHeight);
123123
let coinModelAggregateSpy = CoinStorage.collection.aggregate as sinon.SinonSpy;
124-
let blockModelFindOneSpy = BlockStorage.collection.findOne as sinon.SinonSpy;
124+
let blockModelFindSpy = BlockStorage.collection.find as sinon.SinonSpy;
125125

126-
const result = await CoinStorage.getBalanceAtTime({query, time, chain, network});
127-
expect(coinModelAggregateSpy.called).to.deep.equal(true, "CoinStorage.aggregation should have been called");
128-
expect(blockModelFindOneSpy.called).to.deep.equal(true, 'BlockModel.findOne should have been called');
126+
const result = await CoinStorage.getBalanceAtTime({ query, time, chain, network });
127+
expect(coinModelAggregateSpy.called).to.deep.equal(true, 'CoinStorage.aggregation should have been called');
128+
expect(blockModelFindSpy.called).to.deep.equal(true, 'BlockModel.find should have been called');
129129
expect(coinModelAggregateSpy.getCall(0).args[0][0].$match).to.deep.equal(matchObject);
130130
expect(result).to.has.property('confirmed');
131131
expect(result).to.has.property('unconfirmed');
@@ -143,20 +143,20 @@ describe('Coin Model', function () {
143143
sandbox.restore();
144144
});
145145

146-
it('should return an object with confirmed, unconfirmed, and balance', async () => {
147-
let id = new ObjectId("5c364e342ab5602e97a56f0e");
146+
it('should return an object with confirmed, unconfirmed, and balance', async () => {
147+
let id = new ObjectId('5c364e342ab5602e97a56f0e');
148148
let query = {
149-
"wallets": id,
150-
'wallets.0': { "$exists": true },
151-
"spentHeight": { "$lt": 0 },
152-
"mintHeight": { "$gt": -3 }
149+
wallets: id,
150+
'wallets.0': { $exists: true },
151+
spentHeight: { $lt: 0 },
152+
mintHeight: { $gt: -3 }
153153
};
154154

155-
mockStorage([{_id: 'confirmed', balance: 123123}, {_id: 'unconfirmed', balance: 1}]);
155+
mockStorage([{ _id: 'confirmed', balance: 123123 }, { _id: 'unconfirmed', balance: 1 }]);
156156
let coinModelAggregateSpy = CoinStorage.collection.aggregate as sinon.SinonSpy;
157157

158-
const result = await CoinStorage.getBalance({query});
159-
expect(coinModelAggregateSpy.called).to.deep.equal(true, "CoinStorage.aggregation should have been called");
158+
const result = await CoinStorage.getBalance({ query });
159+
expect(coinModelAggregateSpy.called).to.deep.equal(true, 'CoinStorage.aggregation should have been called');
160160
expect(coinModelAggregateSpy.getCall(0).args[0][0].$match).to.deep.equal(query);
161161
expect(result).to.has.property('confirmed');
162162
expect(result).to.has.property('unconfirmed');

0 commit comments

Comments
 (0)