Skip to content

Commit

Permalink
feat: get the treasury balance
Browse files Browse the repository at this point in the history
  • Loading branch information
shuffledex committed Jul 14, 2020
1 parent 964aae7 commit 57e8303
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Polymesh.ts
Expand Up @@ -5,6 +5,7 @@ import { ApolloClient, ApolloQueryResult } from 'apollo-client';
import { ApolloLink } from 'apollo-link';
import { setContext } from 'apollo-link-context';
import { HttpLink } from 'apollo-link-http';
import BigNumber from 'bignumber.js';
import { polymesh } from 'polymesh-types/definitions';

import { Identity, SecurityToken, TickerReservation } from '~/api/entities';
Expand Down Expand Up @@ -640,6 +641,31 @@ export class Polymesh {
};
}

/**
* Get the Treasury POLYX balance
*
* @note can be subscribed to
*/
public getTreasuryBalance(): Promise<BigNumber>;
public getTreasuryBalance(callback: SubCallback<BigNumber>): Promise<UnsubCallback>;

// eslint-disable-next-line require-jsdoc
public async getTreasuryBalance(
callback?: SubCallback<BigNumber>
): Promise<BigNumber | UnsubCallback> {
const accountId = this.getTreasury();

if (callback) {
return this.context.accountBalance(accountId, ({ free }) => {
// eslint-disable-next-line standard/no-callback-literal
callback(free);
});
}

const { free } = await this.getAccountBalance({ accountId });
return free;
}

// TODO @monitz87: remove when the dApp team no longer needs it
/* istanbul ignore next: only for testing purposes */
/**
Expand Down
42 changes: 42 additions & 0 deletions src/__tests__/Polymesh.ts
Expand Up @@ -1025,6 +1025,48 @@ describe('Polymesh Class', () => {
});
});

describe.only('method: getTreasuryBalance', () => {
let fakeBalance: AccountBalance;

beforeAll(() => {
fakeBalance = {
free: new BigNumber(500000),
locked: new BigNumber(0),
};
dsMockUtils.configureMocks({ contextOptions: { balance: fakeBalance } });
});

test('should return the POLYX balance of the treasury account', async () => {
const polymesh = await Polymesh.connect({
nodeUrl: 'wss://some.url',
});

const result = await polymesh.getTreasuryBalance();
expect(result).toEqual(fakeBalance.free);
});

test.only('should allow subscription', async () => {
const unsubCallback = 'unsubCallback';

const accountBalanceStub = dsMockUtils
.getContextInstance()
.accountBalance.resolves(unsubCallback);

const polymesh = await Polymesh.connect({
nodeUrl: 'wss://some.url',
});

const callback = (() => 1 as unknown) as SubCallback<BigNumber>;
const result = await polymesh.getTreasuryBalance(callback);
expect(result).toEqual(unsubCallback);
sinon.assert.calledWithExactly(
accountBalanceStub,
'5EYCAe5ijAx5xEfZdpCna3grUpY1M9M5vLUH5vpmwV1EnaYR',
callback
);
});
});

describe('method: onConnectionError', () => {
test('should call the supplied listener when the event is emitted and return an unsubscribe callback', async () => {
const polkadot = dsMockUtils.getApiInstance();
Expand Down

0 comments on commit 57e8303

Please sign in to comment.