Skip to content

Commit

Permalink
fix!: Update polkadot-js/api and account for chainProperties updates (#…
Browse files Browse the repository at this point in the history
…402)

Co-authored-by: David <[email protected]>
  • Loading branch information
emostov and dvdplm committed Jan 28, 2021
1 parent 5390aa7 commit 37acc7e
Show file tree
Hide file tree
Showing 6 changed files with 322 additions and 215 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
"test": "jest --silent"
},
"dependencies": {
"@polkadot/api": "^3.5.1",
"@polkadot/apps-config": "^0.76.1",
"@polkadot/util-crypto": "^5.3.1",
"@polkadot/api": "^3.6.4",
"@polkadot/apps-config": "^0.77.1",
"@polkadot/util-crypto": "^5.4.4",
"@substrate/calc": "^0.1.3",
"confmgr": "^1.0.6",
"express": "^4.17.1",
Expand All @@ -53,8 +53,8 @@
"@types/jest": "^26.0.20",
"@types/morgan": "^1.9.2",
"@types/triple-beam": "^1.3.2",
"@typescript-eslint/eslint-plugin": "4.14.0",
"@typescript-eslint/parser": "4.14.0",
"@typescript-eslint/eslint-plugin": "4.14.1",
"@typescript-eslint/parser": "4.14.1",
"eslint": "^7.17.0",
"eslint-config-prettier": "^7.2.0",
"eslint-plugin-prettier": "^3.3.1",
Expand Down
3 changes: 2 additions & 1 deletion src/controllers/accounts/AccountsBalanceInfoController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ export default class AccountsBalanceController extends AbstractController<Accoun
const tokenArg =
typeof token === 'string'
? token.toUpperCase()
: this.api.registry.chainToken;
: // We assume the first token is the native token
this.api.registry.chainTokens[0].toUpperCase();

const hash = await this.getHashFromAt(at);

Expand Down
89 changes: 89 additions & 0 deletions src/services/accounts/AccountsBalanceInfoService.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { AccountInfo, Address, Hash } from '@polkadot/types/interfaces';

import { sanitizeNumbers } from '../../sanitize/sanitizeNumbers';
import { blockHash789629, mockApi, testAddress } from '../test-helpers/mock';
import * as accountsBalanceInfo789629 from '../test-helpers/responses/accounts/balanceInfo789629.json';
Expand All @@ -18,5 +23,89 @@ describe('AccountsBalanceInfoService', () => {
)
).toStrictEqual(accountsBalanceInfo789629);
});

describe('token recognition', () => {
let tempQueryTokens: any,
tempQueryBalance: any,
mockTokensLocksAt: jest.Mock<any>,
mockTokenAccountAt: jest.Mock<any>,
mockBalancesLocksAt: jest.Mock<any>;
beforeAll(() => {
// Important: these temp values should never be reassinged. They are used so we can assign
// the mockApi properties back to their original values after this sub-section of tests run.
tempQueryTokens = mockApi.query.tokens;
tempQueryBalance = mockApi.query.balances;

const tokensAccountAt = async (
hash: Hash,
address: Address
): Promise<any> =>
(((await mockApi.query.system.account.at(
hash,
address
)) as unknown) as AccountInfo).data;
// Wrap our functions in a jest mock so we can collect data on how they are called
mockTokensLocksAt = jest.fn(mockApi.query.balances.locks.at);
mockTokenAccountAt = jest.fn(tokensAccountAt);
mockApi.query.tokens = {
locks: { at: mockTokensLocksAt },
accounts: { at: mockTokenAccountAt },
} as any;

mockBalancesLocksAt = jest.fn(mockApi.query.balances.locks.at);
mockApi.query.balances = {
locks: { at: mockBalancesLocksAt },
} as any;
});

afterEach(() => {
// Clear data about how the mocks where called after each `it` test.
mockTokensLocksAt.mockClear();
mockTokenAccountAt.mockClear();
mockBalancesLocksAt.mockClear();
});

afterAll(() => {
mockApi.query.tokens = tempQueryTokens;
mockApi.query.balances = tempQueryBalance;
});

it('only has `["DOT"]` (all uppercase chars) for the mockApi registry', () => {
expect(mockApi.registry.chainTokens).toStrictEqual(['DOT']);
expect(mockApi.registry.chainDecimals).toStrictEqual([12]);
});

it('querrys tokens pallet storage with a non-native token', async () => {
expect(
(sanitizeNumbers(
await accountsBalanceInfoService.fetchAccountBalanceInfo(
blockHash789629,
testAddress,
'fOoToKeN'
)
) as any).tokenSymbol
).toEqual('fOoToKeN');

expect(mockTokensLocksAt).toBeCalled();
expect(mockTokenAccountAt).toBeCalled();
expect(mockBalancesLocksAt).not.toBeCalled();
});

it('does not query tokens pallet storage with the native token', async () => {
expect(
(sanitizeNumbers(
await accountsBalanceInfoService.fetchAccountBalanceInfo(
blockHash789629,
testAddress,
'doT'
)
) as any).tokenSymbol
).toEqual('doT');

expect(mockTokensLocksAt).not.toBeCalled();
expect(mockTokenAccountAt).not.toBeCalled();
expect(mockBalancesLocksAt).toBeCalled();
});
});
});
});
3 changes: 2 additions & 1 deletion src/services/accounts/AccountsBalanceInfoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export class AccountsBalanceInfoService extends AbstractService {
const { api } = this;

let locks, header, accountInfo, accountData;
if (token === api.registry.chainToken) {
// We assume the first token is the native token
if (token.toUpperCase() === api.registry.chainTokens[0].toUpperCase()) {
[header, locks, accountInfo] = await Promise.all([
api.rpc.chain.getHeader(hash),
api.query.balances.locks.at(hash, address),
Expand Down
4 changes: 2 additions & 2 deletions src/services/test-helpers/responses/runtime/spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"properties": {
"ss58Format": "0",
"tokenDecimals": "12",
"tokenSymbol": "DOT"
"tokenDecimals": ["12"],
"tokenSymbol": ["DOT"]
}
}
Loading

0 comments on commit 37acc7e

Please sign in to comment.