Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions packages/bitcore-wallet-service/src/lib/blockchainexplorers/v8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,48 @@ export class V8 {
});
}

getAaveUserAccountData(opts: { address: string; version?: string }, cb) {
const url = this.baseUrl + '/aave/account/' + opts.address + '?version=' + (opts.version || 'v3');
logger.debug('[v8.js] GETTING AAVE USER ACCOUNT DATA %o', url);
this.request
.get(url, {})
.then(accountData => {
accountData = JSON.parse(accountData);
return cb(null, accountData);
})
.catch(err => {
return cb(err);
});
}

getAaveReserveData(opts: { asset: string; version?: string }, cb) {
const url = this.baseUrl + '/aave/reserve/' + opts.asset + '?version=' + (opts.version || 'v3');
logger.debug('[v8.js] GETTING AAVE RESERVE DATA %o', url);
this.request
.get(url, {})
.then(reserveData => {
reserveData = JSON.parse(reserveData);
return cb(null, reserveData);
})
.catch(err => {
return cb(err);
});
}

getAaveReserveTokensAddresses(opts: { asset: string; version?: string }, cb) {
const url = this.baseUrl + '/aave/reserve-tokens/' + opts.asset + '?version=' + (opts.version || 'v3');
logger.debug('[v8.js] GETTING AAVE RESERVE TOKENS ADDRESSES %o', url);
this.request
.get(url, {})
.then(tokensAddresses => {
tokensAddresses = JSON.parse(tokensAddresses);
return cb(null, tokensAddresses);
})
.catch(err => {
return cb(err);
});
}

getMultisigTxpsInfo(opts: { multisigContractAddress: string }, cb) {
const url = this.baseUrl + '/ethmultisig/txps/' + opts.multisigContractAddress;
logger.debug('[v8.js] CHECKING CONTRACT TXPS INFO %o', url);
Expand Down
13 changes: 13 additions & 0 deletions packages/bitcore-wallet-service/src/lib/expressapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Common } from './common';
import { ClientError } from './errors/clienterror';
import { Errors } from './errors/errordefinitions';
import { logger, transports } from './logger';
import { AaveRouter } from './routes/aave';
import { error } from './routes/helpers';
import { createWalletLimiter } from './routes/middleware/createWalletLimiter';
import { LogMiddleware } from './routes/middleware/log';
Expand Down Expand Up @@ -973,6 +974,17 @@ export class ExpressApp {
});
});

router.post('/v1/token/allowance', (req, res) => {
getServerWithAuth(req, res, async server => {
try {
const allowance = await server.getTokenAllowance(req.body);
res.json(allowance);
} catch (err) {
returnError(err, res, req);
}
});
});
Comment thread
tmcollins4 marked this conversation as resolved.
Outdated

router.get('/v1/sendmaxinfo/', (req, res) => {
getServerWithAuth(req, res, server => {
const q = req.query;
Expand Down Expand Up @@ -2391,6 +2403,7 @@ export class ExpressApp {
});

/** Imported routes */
router.use(new AaveRouter({ returnError, getServerWithAuth }).router);
router.use(new TssRouter({ returnError, opts }).router);


Expand Down
51 changes: 51 additions & 0 deletions packages/bitcore-wallet-service/src/lib/routes/aave.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import express from 'express';
import * as Types from '../../types/expressapp';

interface AaveRouterOpts {
returnError: Types.ReturnErrorFn;
getServerWithAuth: Types.GetServerWithAuthFn;
}

export class AaveRouter {
router: express.Router;

constructor(params: AaveRouterOpts) {
const { returnError, getServerWithAuth } = params;
const router = express.Router();

router.post('/v1/service/aave/userAccountData', (req, res) => {
getServerWithAuth(req, res, async server => {
try {
const accountData = await server.getAaveUserAccountData(req.body);
res.json(accountData);
} catch (err) {
returnError(err, res, req);
}
});
});

router.post('/v1/service/aave/reserveData', (req, res) => {
getServerWithAuth(req, res, async server => {
try {
const reserveData = await server.getAaveReserveData(req.body);
res.json(reserveData);
} catch (err) {
returnError(err, res, req);
}
});
});

router.post('/v1/service/aave/reserveTokensAddresses', (req, res) => {
getServerWithAuth(req, res, async server => {
try {
const tokensAddresses = await server.getAaveReserveTokensAddresses(req.body);
res.json(tokensAddresses);
} catch (err) {
returnError(err, res, req);
}
});
});

this.router = router;
}
}
56 changes: 56 additions & 0 deletions packages/bitcore-wallet-service/src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2559,6 +2559,62 @@ export class WalletService implements IWalletService {
});
}

getTokenAllowance(opts) {
const bc = this._getBlockchainExplorer(opts.chain || Defaults.EVM_CHAIN, opts.network);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should add some validation to opts, and maybe a param type or at least JSDoc to this fn as well. Something like

    if (!checkRequired(opts, ['chain', 'network', 'tokenAddress', 'ownerAddress', 'spenderAddress'])) {
      throw new Error('Missing required parameters');
    }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added input validation and JSDocs on relevant methods
Added methods to BWC

return new Promise((resolve, reject) => {
if (!bc) return reject(new Error('Could not get blockchain explorer instance'));
bc.getTokenAllowance(opts, (err, allowance) => {
if (err) {
this.logw('Error getting token allowance:', err);
return reject(err);
}
return resolve(allowance);
Comment thread
tmcollins4 marked this conversation as resolved.
});
});
}

getAaveUserAccountData(opts) {
const bc = this._getBlockchainExplorer(opts.chain || Defaults.EVM_CHAIN, opts.network);
return new Promise((resolve, reject) => {
if (!bc) return reject(new Error('Could not get blockchain explorer instance'));
bc.getAaveUserAccountData(opts, (err, accountData) => {
if (err) {
this.logw('Error getting Aave user account data:', err);
return reject(err);
}
return resolve(accountData);
});
});
}

getAaveReserveData(opts) {
const bc = this._getBlockchainExplorer(opts.chain || Defaults.EVM_CHAIN, opts.network);
return new Promise((resolve, reject) => {
if (!bc) return reject(new Error('Could not get blockchain explorer instance'));
bc.getAaveReserveData(opts, (err, reserveData) => {
if (err) {
this.logw('Error getting Aave reserve data:', err);
return reject(err);
}
return resolve(reserveData);
});
});
}

getAaveReserveTokensAddresses(opts) {
const bc = this._getBlockchainExplorer(opts.chain || Defaults.EVM_CHAIN, opts.network);
return new Promise((resolve, reject) => {
if (!bc) return reject(new Error('Could not get blockchain explorer instance'));
bc.getAaveReserveTokensAddresses(opts, (err, tokensAddresses) => {
if (err) {
this.logw('Error getting Aave reserve tokens addresses:', err);
return reject(err);
}
return resolve(tokensAddresses);
});
});
}

getMultisigTxpsInfo(opts) {
const bc = this._getBlockchainExplorer(opts.chain || Defaults.EVM_CHAIN, opts.network);
return new Promise((resolve, reject) => {
Expand Down
105 changes: 105 additions & 0 deletions packages/bitcore-wallet-service/test/expressapp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,111 @@ describe('ExpressApp', function() {
});

});
describe('Aave service routes', function() {
it('/v1/service/aave/userAccountData', function(done) {
const server = {
getAaveUserAccountData: sinon.stub().resolves({ totalCollateralBase: '1000', healthFactor: '2.0' }),
};
sandbox.stub(WalletService, 'initialize').callsArg(1);
sandbox.stub(WalletService, 'getInstanceWithAuth').callsArgWith(1, null, server);
start(ExpressApp, function() {
const requestOptions = {
url: testHost + ':' + testPort + config.basePath + '/v1/service/aave/userAccountData',
method: 'post',
json: { chain: 'eth', network: 'mainnet', address: '0x123', version: 'v3' },
headers: {
'x-identity': 'identity',
'x-signature': 'signature'
}
};
request(requestOptions, function(err, res, body) {
should.not.exist(err);
res.statusCode.should.equal(200);
body.totalCollateralBase.should.equal('1000');
body.healthFactor.should.equal('2.0');
done();
});
});
});

it('/v1/service/aave/reserveData', function(done) {
const server = {
getAaveReserveData: sinon.stub().resolves({ currentVariableBorrowRate: '35000000' }),
};
sandbox.stub(WalletService, 'initialize').callsArg(1);
sandbox.stub(WalletService, 'getInstanceWithAuth').callsArgWith(1, null, server);
start(ExpressApp, function() {
const requestOptions = {
url: testHost + ':' + testPort + config.basePath + '/v1/service/aave/reserveData',
method: 'post',
json: { chain: 'eth', network: 'mainnet', asset: '0xabc', version: 'v3' },
headers: {
'x-identity': 'identity',
'x-signature': 'signature'
}
};
request(requestOptions, function(err, res, body) {
should.not.exist(err);
res.statusCode.should.equal(200);
body.currentVariableBorrowRate.should.equal('35000000');
done();
});
});
});

it('/v1/service/aave/reserveTokensAddresses', function(done) {
const server = {
getAaveReserveTokensAddresses: sinon.stub().resolves({ variableDebtTokenAddress: '0xdef' }),
};
sandbox.stub(WalletService, 'initialize').callsArg(1);
sandbox.stub(WalletService, 'getInstanceWithAuth').callsArgWith(1, null, server);
start(ExpressApp, function() {
const requestOptions = {
url: testHost + ':' + testPort + config.basePath + '/v1/service/aave/reserveTokensAddresses',
method: 'post',
json: { chain: 'eth', network: 'mainnet', asset: '0xabc', version: 'v3' },
headers: {
'x-identity': 'identity',
'x-signature': 'signature'
}
};
request(requestOptions, function(err, res, body) {
should.not.exist(err);
res.statusCode.should.equal(200);
body.variableDebtTokenAddress.should.equal('0xdef');
done();
});
});
});
});

describe('Token allowance', function() {
it('/v1/token/allowance', function(done) {
const server = {
getTokenAllowance: sinon.stub().resolves(5000000),
};
sandbox.stub(WalletService, 'initialize').callsArg(1);
sandbox.stub(WalletService, 'getInstanceWithAuth').callsArgWith(1, null, server);
start(ExpressApp, function() {
const requestOptions = {
url: testHost + ':' + testPort + config.basePath + '/v1/token/allowance',
method: 'post',
json: { chain: 'eth', network: 'mainnet', tokenAddress: '0xtoken', ownerAddress: '0xowner', spenderAddress: '0xspender' },
headers: {
'x-identity': 'identity',
'x-signature': 'signature'
}
};
request(requestOptions, function(err, res, body) {
should.not.exist(err);
res.statusCode.should.equal(200);
body.should.equal(5000000);
done();
});
});
});
});
Comment thread
tmcollins4 marked this conversation as resolved.

describe('Clear cache', function() {
it('/v1/clearcache/', function(done) {
const resolveStub = sinon.stub().callsFake( () => { return Promise.resolve(true);});
Expand Down
Loading
Loading