Skip to content

Commit

Permalink
fix: impement new changes
Browse files Browse the repository at this point in the history
  • Loading branch information
shuffledex committed Aug 14, 2020
1 parent fa8f09f commit 64ebd70
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 51 deletions.
53 changes: 24 additions & 29 deletions src/Polymesh.ts
Expand Up @@ -21,13 +21,7 @@ import {
import { PolymeshError, TransactionQueue } from '~/base';
import { Context } from '~/context';
import { didsWithClaims, transactions } from '~/middleware/queries';
import {
CallIdEnum,
ClaimTypeEnum,
ModuleIdEnum,
Query,
TransactionOrderByInput,
} from '~/middleware/types';
import { ClaimTypeEnum, Query, TransactionOrderByInput } from '~/middleware/types';
import {
AccountBalance,
ClaimData,
Expand All @@ -53,13 +47,15 @@ import {
booleanToBool,
calculateNextKey,
createClaim,
extrinsicIdentifierToTxTag,
linkTypeToMeshLinkType,
moduleAddressToString,
signerToSignatory,
stringToAccountKey,
stringToTicker,
textToString,
tickerToString,
txTagToExtrinsicIdentifier,
u32ToBigNumber,
valueToDid,
} from '~/utils';
Expand Down Expand Up @@ -705,8 +701,7 @@ export class Polymesh {
opts: {
blockId?: number;
address?: string;
moduleId?: ModuleIdEnum;
callId?: CallIdEnum;
txTag?: TxTag;
success?: boolean;
size?: number;
start?: number;
Expand All @@ -715,15 +710,20 @@ export class Polymesh {
): Promise<ResultSet<ExtrinsicData>> {
const { context } = this;

const { blockId, address, moduleId, callId, success, size, start, orderBy } = opts;
const { blockId, address, txTag, success, size, start, orderBy } = opts;

let extrinsicIdentifier;
if (txTag) {
extrinsicIdentifier = txTagToExtrinsicIdentifier(txTag);
}

/* eslint-disable @typescript-eslint/camelcase */
const result = await context.queryMiddleware<Ensured<Query, 'transactions'>>(
transactions({
block_id: blockId,
address: address ? stringToAccountKey(address, context).toString() : undefined,
module_id: moduleId,
call_id: callId,
module_id: extrinsicIdentifier ? extrinsicIdentifier.moduleId : undefined,
call_id: extrinsicIdentifier ? extrinsicIdentifier.callId : undefined,
success,
count: size,
skip: start,
Expand All @@ -743,35 +743,30 @@ export class Polymesh {
({
block_id,
extrinsic_idx,
extrinsic_version,
signed,
address: rawAddress,
nonce,
era,
call,
module_id,
call_id,
params,
success: txSuccess,
spec_version_id,
extrinsic_hash,
}) =>
}) => {
// TODO remove null check once types fixed
/* eslint-disable @typescript-eslint/no-non-null-assertion */
data.push({
blockId: block_id,
extrinsicIdx: extrinsic_idx,
extrinsicVersion: extrinsic_version,
signed,
blockId: block_id!,
extrinsicIdx: extrinsic_idx!,
address: rawAddress,
nonce,
era,
call,
moduleId: module_id,
callId: call_id,
nonce: nonce!,
txTag: extrinsicIdentifierToTxTag({ moduleId: module_id!, callId: call_id! }),
params,
success: !!txSuccess,
specVersionId: spec_version_id,
extrinsicHash: extrinsic_hash,
})
specVersionId: spec_version_id!,
extrinsicHash: extrinsic_hash!,
});
/* eslint-enabled @typescript-eslint/no-non-null-assertion */
}
);
/* eslint-enable @typescript-eslint/camelcase */

Expand Down
24 changes: 21 additions & 3 deletions src/__tests__/Polymesh.ts
Expand Up @@ -10,7 +10,13 @@ import { Identity, TickerReservation } from '~/api/entities';
import { modifyClaims, reserveTicker, transferPolyX } from '~/api/procedures';
import { TransactionQueue } from '~/base';
import { didsWithClaims, transactions } from '~/middleware/queries';
import { ClaimTypeEnum, ExtrinsicResult, IdentityWithClaimsResult } from '~/middleware/types';
import {
CallIdEnum,
ClaimTypeEnum,
ExtrinsicResult,
IdentityWithClaimsResult,
ModuleIdEnum,
} from '~/middleware/types';
import { Polymesh } from '~/Polymesh';
import { dsMockUtils, entityMockUtils } from '~/testUtils/mocks';
import {
Expand Down Expand Up @@ -1244,12 +1250,23 @@ describe('Polymesh Class', () => {
const context = dsMockUtils.getContextInstance();
const address = 'someAddress';
const accountKey = dsMockUtils.createMockAccountKey(address);
const txTag = TxTags.identity.CddRegisterDid;
const moduleId = ModuleIdEnum.Identity;
const callId = CallIdEnum.CddRegisterDid;

sinon
.stub(utilsModule, 'stringToAccountKey')
.withArgs(address, context)
.returns(accountKey);

sinon
.stub(utilsModule, 'txTagToExtrinsicIdentifier')
.withArgs(txTag)
.returns({
moduleId,
callId,
});

/* eslint-disable @typescript-eslint/camelcase */
const transactionsQueryResponse: ExtrinsicResult = {
totalCount: 20,
Expand Down Expand Up @@ -1283,8 +1300,8 @@ describe('Polymesh Class', () => {
transactions({
block_id: undefined,
address: accountKey.toString(),
module_id: undefined,
call_id: undefined,
module_id: moduleId,
call_id: callId,
success: undefined,
count: 2,
skip: 1,
Expand All @@ -1297,6 +1314,7 @@ describe('Polymesh Class', () => {

let result = await polymesh.getTransactionHistory({
address,
txTag,
size: 2,
start: 1,
});
Expand Down
4 changes: 0 additions & 4 deletions src/middleware/queries.ts
Expand Up @@ -231,12 +231,8 @@ export function transactions(
items {
block_id
extrinsic_idx
extrinsic_version
signed
address
nonce
era
call
module_id
call_id
params
Expand Down
23 changes: 9 additions & 14 deletions src/types/index.ts
Expand Up @@ -3,7 +3,7 @@ import { IKeyringPair, TypeDef } from '@polkadot/types/types';
import BigNumber from 'bignumber.js';

import { Identity } from '~/api/entities';
import { CallIdEnum, ModuleIdEnum } from '~/middleware/types';
import { TxTag } from '~/polkadot';

export enum TransactionStatus {
/**
Expand Down Expand Up @@ -224,20 +224,15 @@ export interface IdentityWithClaims {
}

export interface ExtrinsicData {
blockId?: number | null;
extrinsicIdx?: number | null;
extrinsicVersion?: string | null;
signed?: number | null;
blockId?: number;
extrinsicIdx?: number;
address?: string | null;
nonce?: number | null;
era?: string | null;
call?: string | null;
moduleId?: ModuleIdEnum | null;
callId?: CallIdEnum | null;
params?: object | null;
success?: boolean | null;
specVersionId?: number | null;
extrinsicHash?: string | null;
nonce?: number;
txTag: TxTag;
params?: object;
success?: boolean;
specVersionId?: number;
extrinsicHash?: string;
}

export enum ConditionType {
Expand Down
6 changes: 6 additions & 0 deletions src/types/internal.ts
Expand Up @@ -10,6 +10,7 @@ import BigNumber from 'bignumber.js';
import { DocumentNode } from 'graphql';

import { PostTransactionValue } from '~/base';
import { CallIdEnum, ModuleIdEnum } from '~/middleware/types';

/**
* Polkadot's `tx` submodule
Expand Down Expand Up @@ -114,3 +115,8 @@ export enum ClaimOperation {
Add = 'Add',
Edit = 'Edit',
}

export interface ExtrinsicIdentifier {
moduleId: ModuleIdEnum;
callId: CallIdEnum;
}
25 changes: 25 additions & 0 deletions src/utils/__tests__/index.ts
Expand Up @@ -32,6 +32,7 @@ import sinon from 'sinon';
import { Identity } from '~/api/entities';
import { ProposalState } from '~/api/entities/Proposal/types';
import { PostTransactionValue } from '~/base';
import { CallIdEnum, ModuleIdEnum } from '~/middleware/types';
import { dsMockUtils } from '~/testUtils/mocks';
import {
Authorization,
Expand Down Expand Up @@ -78,6 +79,7 @@ import {
documentNameToString,
documentToTokenDocument,
documentUriToString,
extrinsicIdentifierToTxTag,
findEventRecord,
fundingRoundNameToString,
identifierTypeToString,
Expand Down Expand Up @@ -119,6 +121,7 @@ import {
tokenDocumentToDocument,
tokenIdentifierTypeToIdentifierType,
tokenTypeToAssetType,
txTagToExtrinsicIdentifier,
txTagToProtocolOp,
u8ToTransferStatus,
u64ToBigNumber,
Expand Down Expand Up @@ -2099,6 +2102,28 @@ describe('txTagToProtocolOp', () => {
});
});

describe('txTagToExtrinsicIdentifier and extrinsicIdentifierToTxTag', () => {
test('txTagToExtrinsicIdentifier should convert a TxTag enum to a ExtrinsicIdentifier object', () => {
const value = TxTags.identity.CddRegisterDid;

const result = txTagToExtrinsicIdentifier(value);

expect(result).toEqual({
moduleId: ModuleIdEnum.Identity,
callId: CallIdEnum.CddRegisterDid,
});
});

test('extrinsicIdentifierToTxTag should convert a ExtrinsicIdentifier object to a TxTag', () => {
const result = extrinsicIdentifierToTxTag({
moduleId: ModuleIdEnum.Identity,
callId: CallIdEnum.CddRegisterDid,
});

expect(result).toEqual(TxTags.identity.CddRegisterDid);
});
});

describe('linkTypeToMeshLinkType', () => {
beforeAll(() => {
dsMockUtils.initMocks();
Expand Down
31 changes: 30 additions & 1 deletion src/utils/index.ts
Expand Up @@ -12,7 +12,7 @@ import {
import { blake2AsHex, decodeAddress, encodeAddress } from '@polkadot/util-crypto';
import BigNumber from 'bignumber.js';
import stringify from 'json-stable-stringify';
import { chunk, groupBy, isEqual, map, padEnd } from 'lodash';
import { camelCase, chunk, groupBy, isEqual, map, padEnd, snakeCase } from 'lodash';
import {
AccountKey,
AssetIdentifier,
Expand Down Expand Up @@ -42,12 +42,14 @@ import {
Signatory,
Ticker,
TxTag,
TxTags,
} from 'polymesh-types/types';

import { Identity } from '~/api/entities/Identity';
import { ProposalState } from '~/api/entities/Proposal/types';
import { PolymeshError, PostTransactionValue } from '~/base';
import { Context } from '~/context';
import { CallIdEnum, ModuleIdEnum } from '~/middleware/types';
import {
Authorization,
AuthorizationType,
Expand Down Expand Up @@ -76,6 +78,7 @@ import {
} from '~/types';
import {
AuthTarget,
ExtrinsicIdentifier,
Extrinsics,
MapMaybePostTransactionValue,
MaybePostTransactionValue,
Expand Down Expand Up @@ -989,6 +992,32 @@ export function txTagToProtocolOp(tag: TxTag, context: Context): ProtocolOp {
return context.polymeshApi.createType('ProtocolOp', value);
}

/**
* @hidden
*/
export function txTagToExtrinsicIdentifier(tag: TxTag): ExtrinsicIdentifier {
const [moduleName, extrinsicName] = tag.split('.');
return {
moduleId: moduleName.toLowerCase() as ModuleIdEnum,
callId: snakeCase(extrinsicName) as CallIdEnum,
};
}

/**
* @hidden
*/
export function extrinsicIdentifierToTxTag(extrinsicIdentifier: ExtrinsicIdentifier): TxTag {
const { moduleId, callId } = extrinsicIdentifier;
let moduleName;
for (const txTagItem in TxTags) {
if (txTagItem.toLowerCase() === moduleId) {
moduleName = txTagItem;
}
}

return `${moduleName}.${camelCase(callId)}` as TxTag;
}

/**
* @hidden
*/
Expand Down

0 comments on commit 64ebd70

Please sign in to comment.