Skip to content

Commit

Permalink
[DDW-893] Integrate new API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolaglumac committed Sep 12, 2019
1 parent 184fdc8 commit ce7f934
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 3 deletions.
6 changes: 3 additions & 3 deletions cardano-sl-src.json
@@ -1,7 +1,7 @@
{
"url": "https://github.com/input-output-hk/cardano-sl",
"rev": "51ad7c0503b1c52a75a6eb36096c407934136468",
"date": "2019-08-10T03:35:47+00:00",
"sha256": "0cqaxk3k8i5z4q4b5na0pcln9fblglmn7900vp1xdzmw75pp1rrz",
"rev": "32890cf55d85b407e87b2ff13a3ae7703b6c444e",
"date": "2019-09-12T07:23:15+00:00",
"sha256": "1a0dfg9gd75f9az1r9ycf9h1whikf203y1hkd537ls1jd5bkisyp",
"fetchSubmodules": false
}
28 changes: 28 additions & 0 deletions source/renderer/app/api/api.js
Expand Up @@ -45,6 +45,7 @@ import { createWallet } from './wallets/requests/createWallet';
import { restoreWallet } from './wallets/requests/restoreWallet';
import { updateWallet } from './wallets/requests/updateWallet';
import { getWalletUtxos } from './wallets/requests/getWalletUtxos';
import { getWalletIdAndBalance } from './wallets/requests/getWalletIdAndBalance';

// utility functions
import {
Expand Down Expand Up @@ -118,6 +119,7 @@ import type {
AdaWallet,
AdaWallets,
WalletUtxos,
WalletIdAndBalance,
CreateWalletRequest,
DeleteWalletRequest,
RestoreWalletRequest,
Expand All @@ -129,6 +131,8 @@ import type {
ImportWalletFromFileRequest,
UpdateWalletRequest,
GetWalletUtxosRequest,
GetWalletIdAndBalanceRequest,
GetWalletIdAndBalanceResponse,
} from './wallets/types';

// Common errors
Expand Down Expand Up @@ -873,6 +877,30 @@ export default class AdaApi {
}
};

getWalletIdAndBalance = async (
request: GetWalletIdAndBalanceRequest
): Promise<WalletIdAndBalance> => {
const { recoveryPhrase } = request;
Logger.debug('AdaApi::getWalletIdAndBalance called');
try {
const response: GetWalletIdAndBalanceResponse = await getWalletIdAndBalance(
this.config,
{
recoveryPhrase,
}
);
Logger.debug('AdaApi::getWalletIdAndBalance success', { response });
const { walletId, balance } = response;
return {
walletId,
balance: new BigNumber(balance).dividedBy(LOVELACES_PER_ADA),
};
} catch (error) {
Logger.error('AdaApi::getWalletIdAndBalance error', { error });
throw new GenericApiError();
}
};

testReset = async (): Promise<void> => {
Logger.debug('AdaApi::testReset called');
try {
Expand Down
12 changes: 12 additions & 0 deletions source/renderer/app/api/utils/request.js
Expand Up @@ -84,6 +84,18 @@ function typedRequest<Response>(
// even if it was successful
const { statusCode, statusMessage } = response;

// TODO: remove once calculate_mnemonic is fixed
if (
options.path === '/api/internal/calculate_mnemonic' &&
statusCode === 200
) {
const data = body;
body = `{
"status": "success",
"data": ${data}
}`;
}

if (!body && statusCode >= 200 && statusCode <= 206) {
// adds status and data properties so JSON.parse doesn't throw an error
body = `{
Expand Down
21 changes: 21 additions & 0 deletions source/renderer/app/api/wallets/requests/getWalletIdAndBalance.js
@@ -0,0 +1,21 @@
// @flow
import type { RequestConfig } from '../../common/types';
import type {
GetWalletIdAndBalanceRequest,
GetWalletIdAndBalanceResponse,
} from '../types';
import { request } from '../../utils/request';

export const getWalletIdAndBalance = (
config: RequestConfig,
{ recoveryPhrase }: GetWalletIdAndBalanceRequest
): Promise<GetWalletIdAndBalanceResponse> =>
request(
{
method: 'POST',
path: '/api/internal/calculate_mnemonic',
...config,
},
{},
recoveryPhrase
);
16 changes: 16 additions & 0 deletions source/renderer/app/api/wallets/types.js
@@ -1,4 +1,6 @@
// @flow
import BigNumber from 'bignumber.js';

export type AdaWallet = {
createdAt: string,
syncState: WalletSyncState,
Expand Down Expand Up @@ -48,6 +50,11 @@ export type WalletUtxos = {
},
};

export type WalletIdAndBalance = {
walletId: string,
balance: BigNumber,
};

// req/res Wallet types
export type CreateWalletRequest = {
name: string,
Expand All @@ -69,6 +76,15 @@ export type GetWalletUtxosRequest = {
walletId: string,
};

export type GetWalletIdAndBalanceRequest = {
recoveryPhrase: Array<string>,
};

export type GetWalletIdAndBalanceResponse = {
walletId: string,
balance: number,
};

export type RestoreWalletRequest = {
recoveryPhrase: string,
walletName: string,
Expand Down

0 comments on commit ce7f934

Please sign in to comment.