Skip to content

Commit b3bf013

Browse files
author
Bernard Snowden
committed
add /v2/balance/ endpoint; fix tests
1 parent acef65e commit b3bf013

File tree

3 files changed

+107
-7
lines changed

3 files changed

+107
-7
lines changed

packages/bitcore-wallet-client/src/lib/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,7 +1593,7 @@ export class API extends EventEmitter {
15931593
// * @param {String} opts.multisigContractAddress optional: MULTISIG ETH Contract Address
15941594
// * @param {Callback} cb
15951595
// */
1596-
getBalance(opts, cb) {
1596+
getBalance(opts, cb, baseUrl) {
15971597
if (!cb) {
15981598
cb = opts;
15991599
opts = {};
@@ -1624,7 +1624,7 @@ export class API extends EventEmitter {
16241624
qs = '?' + args.join('&');
16251625
}
16261626

1627-
var url = '/v1/balance/' + qs;
1627+
var url = baseUrl + qs;
16281628
this.request.get(url, (err, inB) => {
16291629
if (err) return cb(err);
16301630
return cb(null, this.convertBalance(inB));

packages/bitcore-wallet-client/test/api.test.js

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6998,7 +6998,7 @@ describe('client API', function() {
69986998
});
69996999
});
70007000

7001-
it('should be able to recover funds from recreated wallet', function(done) {
7001+
it('should be able to recover funds from recreated wallet /v1/balance/', function(done) {
70027002
this.timeout(10000);
70037003
helpers.createAndJoinWallet(clients, keys, 2, 2, {}, () => {
70047004
clients[0].createAddress((err, addr) => {
@@ -7042,12 +7042,75 @@ describe('client API', function() {
70427042
recoveryClient.getBalance({}, (err, b) => {
70437043
balance = b.totalAmount;
70447044
next(err);
7045-
});
7045+
}, '/v1/balance/');
70467046
}, 200);
70477047
},
70487048
err => {
70497049
should.not.exist(err,err);
7050-
balance.should.equal(1e8);
7050+
balance.toString().should.equal("100000000");
7051+
done();
7052+
}
7053+
);
7054+
});
7055+
});
7056+
});
7057+
});
7058+
}
7059+
);
7060+
});
7061+
});
7062+
});
7063+
7064+
it('should be able to recover funds from recreated wallet /v2/balance/', function(done) {
7065+
this.timeout(10000);
7066+
helpers.createAndJoinWallet(clients, keys, 2, 2, {}, () => {
7067+
clients[0].createAddress((err, addr) => {
7068+
should.not.exist(err,err);
7069+
should.exist(addr);
7070+
blockchainExplorerMock.setUtxo(addr, 1, 2);
7071+
7072+
var storage = new Storage({
7073+
db: db2
7074+
});
7075+
var newApp;
7076+
var expressApp = new ExpressApp();
7077+
expressApp.start(
7078+
{
7079+
storage: storage,
7080+
blockchainExplorer: blockchainExplorerMock,
7081+
disableLogs: true
7082+
},
7083+
() => {
7084+
newApp = expressApp.app;
7085+
7086+
var recoveryClient = helpers.newClient(newApp);
7087+
recoveryClient.fromString(clients[0].toString());
7088+
7089+
recoveryClient.getStatus({}, (err, status) => {
7090+
should.exist(err);
7091+
err.should.be.an.instanceOf(Errors.NOT_AUTHORIZED);
7092+
recoveryClient.recreateWallet(err => {
7093+
should.not.exist(err,err);
7094+
recoveryClient.getStatus({}, (err, status) => {
7095+
should.not.exist(err,err);
7096+
recoveryClient.startScan({}, err => {
7097+
should.not.exist(err,err);
7098+
var balance = 0;
7099+
async.whilst(
7100+
() => {
7101+
return balance == 0;
7102+
},
7103+
next => {
7104+
setTimeout(() => {
7105+
recoveryClient.getBalance({}, (err, b) => {
7106+
balance = b.totalAmount;
7107+
next(err);
7108+
}, '/v2/balance/');
7109+
}, 200);
7110+
},
7111+
err => {
7112+
should.not.exist(err,err);
7113+
balance.toString().should.equal("100000000");
70517114
done();
70527115
}
70537116
);

packages/bitcore-wallet-service/src/lib/expressapp.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ const Defaults = Common.Defaults;
1818

1919
// / Patch bigint for JSON serealization
2020
// @ts-ignore-start
21-
BigInt.prototype.toJSON = function() { return this.toString(); }
21+
BigInt.prototype.toJSON = function() {
22+
return this.toString();
23+
};
2224
// @ts-ignore-end/
2325

2426
export class ExpressApp {
2527
app: express.Express;
2628

27-
2829
constructor() {
2930
this.app = express();
3031
}
@@ -738,6 +739,42 @@ export class ExpressApp {
738739
});
739740
});
740741

742+
router.get('/v2/balance/', (req, res) => {
743+
getServerWithAuth(req, res, server => {
744+
const opts: {
745+
coin?: string;
746+
twoStep?: boolean;
747+
tokenAddress?: string;
748+
multisigContractAddress?: string;
749+
} = {};
750+
if (req.query.coin) opts.coin = req.query.coin;
751+
if (req.query.twoStep == '1') opts.twoStep = true;
752+
if (req.query.tokenAddress) opts.tokenAddress = req.query.tokenAddress;
753+
if (req.query.multisigContractAddress) opts.multisigContractAddress = req.query.multisigContractAddress;
754+
755+
server.getBalance(opts, (err, balance) => {
756+
let balanceResponse = {};
757+
758+
if (err) return returnError(err, res, req);
759+
_.forEach(balance, (value, key) => {
760+
if (key == 'totalAmount') {
761+
balanceResponse[key] = balance[key].toString();
762+
}
763+
764+
if (key == 'byAddress') {
765+
balanceResponse[key] = balance[key];
766+
if (balanceResponse[key].amount) {
767+
balanceResponse[key].amount = balanceResponse[key].amount.toString();
768+
}
769+
} else {
770+
balanceResponse[key] = balance[key];
771+
}
772+
});
773+
res.json(balanceResponse);
774+
});
775+
});
776+
});
777+
741778
let estimateFeeLimiter;
742779

743780
if (Defaults.RateLimit.estimateFee && !opts.ignoreRateLimiter) {

0 commit comments

Comments
 (0)