Skip to content

Commit

Permalink
feat: account balance quote estimates for bot's currently handling as…
Browse files Browse the repository at this point in the history
…sets (chrisleekr#353)
  • Loading branch information
Alessandro Marchioro authored Oct 11, 2021
1 parent 06eb81b commit 2ad3c71
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 21 deletions.
10 changes: 10 additions & 0 deletions app/frontend/webserver/handlers/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('index', () => {
let mockHandleGridTradeArchiveDelete;
let mockHandleClosedTradesSetPeriod;
let mockHandle404;
let mockHandleStatus;

let mockLoginLimiter;

Expand All @@ -18,6 +19,7 @@ describe('index', () => {
mockHandleGridTradeArchiveDelete = jest.fn().mockResolvedValue(true);
mockHandleClosedTradesSetPeriod = jest.fn().mockResolvedValue(true);
mockHandle404 = jest.fn().mockResolvedValue(true);
mockHandleStatus = jest.fn().mockResolvedValue(true);

mockLoginLimiter = jest.fn().mockReturnValue(true);

Expand All @@ -41,6 +43,10 @@ describe('index', () => {
handle404: mockHandle404
}));

jest.mock('../status', () => ({
handleStatus: mockHandleStatus
}));

index = require('../index');
await index.setHandlers('logger', 'app', {
loginLimiter: mockLoginLimiter
Expand Down Expand Up @@ -71,6 +77,10 @@ describe('index', () => {
);
});

it('triggers handleStatus', () => {
expect(mockHandleStatus).toHaveBeenCalledWith('logger', 'app');
});

it('triggers handle404', () => {
expect(mockHandle404).toHaveBeenCalledWith('logger', 'app');
});
Expand Down
26 changes: 26 additions & 0 deletions app/frontend/webserver/handlers/__tests__/status.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* eslint-disable global-require */
describe('webserver/handlers/status', () => {
const appMock = {};

let resSendMock;

beforeEach(async () => {
resSendMock = jest.fn().mockResolvedValue(true);
appMock.get = jest.fn().mockImplementation((_path, func) => {
func(null, { send: resSendMock });
});
const loggerMock = require('../../../../helpers/logger');

const { handleStatus } = require('../status');

await handleStatus(loggerMock, appMock);
});

it('triggers res.send', () => {
expect(resSendMock).toHaveBeenCalledWith({
success: true,
status: 200,
message: 'OK'
});
});
});
21 changes: 4 additions & 17 deletions app/frontend/webserver/handlers/status.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
const requestIp = require('request-ip');

const handleStatus = async (funcLogger, app) => {
const logger = funcLogger.child({ endpoint: '/status' });

app.route('/status').get(async (req, res) => {
const clientIp = requestIp.getClientIp(req);

logger.info(
{
clientIp
},
'handle status monitoring endpoint'
);

return res.send({
app.get('/status', (req, res) =>
res.send({
success: true,
status: 200,
message: 'OK'
});
});
})
);
};

module.exports = { handleStatus };
3 changes: 3 additions & 0 deletions public/css/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ input[type='number'] {
display: flex;
flex-flow: row wrap;
}
.account-asset-row-valignfix {
opacity: 0;
}

.account-asset-label {
flex: 1 40%;
Expand Down
25 changes: 22 additions & 3 deletions public/js/AccountWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,33 @@
/* eslint-disable no-undef */
class AccountWrapper extends React.Component {
render() {
const { accountInfo, dustTransfer, sendWebSocket, isAuthenticated } =
this.props;
const {
accountInfo,
dustTransfer,
sendWebSocket,
isAuthenticated,
quoteEstimates
} = this.props;

const assets = accountInfo.balances.map((balance, index) => {
let quoteEstimate = quoteEstimates.filter(
elem => elem.baseAsset === balance.asset
);

if (quoteEstimate.length == 1) {
quoteEstimate = {
quote: quoteEstimate[0]['quoteAsset'],
estimate: quoteEstimate[0]['estimatedValue']
};
} else {
quoteEstimate = null;
}

return (
<AccountWrapperAsset
key={`account-wrapper-` + index}
balance={balance}></AccountWrapperAsset>
balance={balance}
quoteEstimate={quoteEstimate}></AccountWrapperAsset>
);
});

Expand Down
16 changes: 15 additions & 1 deletion public/js/AccountWrapperAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* eslint-disable no-undef */
class AccountWrapperAsset extends React.Component {
render() {
const { balance } = this.props;
const { balance, quoteEstimate } = this.props;

return (
<div className='account-wrapper-asset pt-2 pl-2 pr-2 pb-0'>
Expand All @@ -29,6 +29,20 @@ class AccountWrapperAsset extends React.Component {
{parseFloat(balance.locked).toFixed(5)}
</span>
</div>
{quoteEstimate !== null ? (
<div className='account-asset-row'>
<span className='account-asset-label text-success font-weight-bold'>
In {quoteEstimate.quote}:
</span>
<span className='account-asset-value text-success font-weight-bold'>
{parseFloat(quoteEstimate.estimate).toFixed(5)}
</span>
</div>
) : (
<div className='account-asset-row account-asset-row-valignfix'>
<span className='account-asset-label'>placeholder</span>
</div>
)}
</div>
</div>
);
Expand Down
1 change: 1 addition & 0 deletions public/js/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ class App extends React.Component {
accountInfo={accountInfo}
dustTransfer={dustTransfer}
sendWebSocket={this.sendWebSocket}
quoteEstimates={symbolEstimates}
/>
<ProfitLossWrapper
isAuthenticated={isAuthenticated}
Expand Down

0 comments on commit 2ad3c71

Please sign in to comment.