Skip to content

Commit

Permalink
fix: manual merge
Browse files Browse the repository at this point in the history
  • Loading branch information
shuffledex committed Jun 1, 2020
2 parents 8e15c62 + bb40707 commit 5070943
Show file tree
Hide file tree
Showing 29 changed files with 466 additions and 379 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -12,7 +12,7 @@
"fetch:harvester-types": "node scripts/fetchHarvesterTypes.js",
"fetch-definitions": "node scripts/fetchDefinitions.js",
"generate:defs": "ts-node --skip-project node_modules/.bin/polkadot-types-from-defs --package polymesh-types --input ./src/polkadot",
"generate:meta": "ts-node --skip-project node_modules/.bin/polkadot-types-from-chain --package polymesh-types --endpoint wss://pmf.polymath.network --output ./src/polkadot --strict",
"generate:meta": "ts-node --skip-project node_modules/.bin/polkadot-types-from-chain --package polymesh-types --endpoint wss://pme.polymath.network --output ./src/polkadot --strict",
"generate:tags": "node scripts/generateTxTags.js",
"test": "jest --coverage",
"build:ts": "ttsc -b",
Expand Down
2 changes: 1 addition & 1 deletion scripts/fetchDefinitions.js
Expand Up @@ -6,7 +6,7 @@ const rimraf = require('rimraf');
const util = require('util');

const dirName = path.resolve('src', 'polkadot', 'polymesh');
const urlPath = 'https://pmf.polymath.network/code';
const urlPath = 'https://pme.polymath.network/code';

rimraf.sync(dirName);
fs.mkdirSync(dirName);
Expand Down
2 changes: 1 addition & 1 deletion scripts/generateTxTags.js
Expand Up @@ -6,7 +6,7 @@ const { stringCamelCase, stringLowerFirst, stringUpperFirst } = require('@polkad
const fs = require('fs');
const path = require('path');

const websocket = new w3cwebsocket('wss://pmf.polymath.network');
const websocket = new w3cwebsocket('wss://pme.polymath.network');
websocket.onopen = () => {
websocket.send('{"id":"1","jsonrpc":"2.0","method":"state_getMetadata","params":[]}');
};
Expand Down
131 changes: 77 additions & 54 deletions src/Polymesh.ts
@@ -1,4 +1,5 @@
import { ApiPromise, Keyring, WsProvider } from '@polkadot/api';
import { ApiPromise, WsProvider } from '@polkadot/api';
import { Signer } from '@polkadot/api/types';
import { InMemoryCache, NormalizedCacheObject } from 'apollo-cache-inmemory';
import { ApolloClient, ApolloQueryResult } from 'apollo-client';
import { ApolloLink } from 'apollo-link';
Expand All @@ -24,10 +25,12 @@ import { didsWithClaims, eventByIndexedArgs } from '~/harvester/queries';
import { Query } from '~/harvester/types';
import {
ClaimData,
CommonKeyring,
Ensured,
ErrorCode,
HarvesterConfig,
SubCallback,
UiKeyring,
UnsubCallback,
} from '~/types';
import { SignerType } from '~/types/internal';
Expand All @@ -40,6 +43,20 @@ import {
} from '~/utils';
import { MAX_TICKER_LENGTH } from '~/utils/constants';

interface ConnectParamsBase {
nodeUrl: string;
signer?: Signer;
}

/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* @hidden
*/
function isUiKeyring(keyring: any): keyring is UiKeyring {
return !!keyring.keyring;
}
/* eslint-enable @typescript-eslint/no-explicit-any */

/**
* Main entry point of the Polymesh SDK
*/
Expand All @@ -53,47 +70,47 @@ export class Polymesh {
this.context = context;
}

static async connect(params: { nodeUrl: string; accountSeed: string }): Promise<Polymesh>;
static async connect(params: ConnectParamsBase & { accountSeed: string }): Promise<Polymesh>;

static async connect(params: { nodeUrl: string; keyring: Keyring }): Promise<Polymesh>;

static async connect(params: { nodeUrl: string; accountUri: string }): Promise<Polymesh>;
static async connect(
params: ConnectParamsBase & {
keyring: CommonKeyring | UiKeyring;
}
): Promise<Polymesh>;

static async connect(params: {
nodeUrl: string;
accountSeed: string;
harvester: HarvesterConfig;
}): Promise<Polymesh>;
static async connect(params: ConnectParamsBase & { accountUri: string }): Promise<Polymesh>;

static async connect(params: {
nodeUrl: string;
keyring: Keyring;
harvester: HarvesterConfig;
}): Promise<Polymesh>;
static async connect(
params: ConnectParamsBase & { accountSeed: string; harvester: HarvesterConfig }
): Promise<Polymesh>;

static async connect(params: {
nodeUrl: string;
accountUri: string;
harvester: HarvesterConfig;
}): Promise<Polymesh>;
static async connect(
params: ConnectParamsBase & {
keyring: CommonKeyring | UiKeyring;
harvester: HarvesterConfig;
}
): Promise<Polymesh>;

static async connect(params: { nodeUrl: string; harvester: HarvesterConfig }): Promise<Polymesh>;
static async connect(
params: ConnectParamsBase & { accountUri: string; harvester: HarvesterConfig }
): Promise<Polymesh>;

static async connect(params: { nodeUrl: string }): Promise<Polymesh>;
static async connect(params: ConnectParamsBase): Promise<Polymesh>;

/**
* Create the instance and connect to the Polymesh node
*/
static async connect(params: {
nodeUrl: string;
accountSeed?: string;
keyring?: Keyring;
accountUri?: string;
harvester?: HarvesterConfig;
}): Promise<Polymesh> {
const { nodeUrl, accountSeed, keyring, accountUri, harvester } = params;
static async connect(
params: ConnectParamsBase & {
accountSeed?: string;
keyring?: CommonKeyring | UiKeyring;
accountUri?: string;
harvester?: HarvesterConfig;
}
): Promise<Polymesh> {
const { nodeUrl, accountSeed, keyring, accountUri, signer, harvester } = params;
let polymeshApi: ApiPromise;
let harvesterClient: ApolloClient<NormalizedCacheObject>;
let harvesterClient: ApolloClient<NormalizedCacheObject> | null = null;

try {
const { types, rpc } = polymesh;
Expand All @@ -104,53 +121,59 @@ export class Polymesh {
rpc,
});

harvesterClient = new ApolloClient({
link: setContext((_, { headers }) => {
return {
headers: {
...headers,
'x-api-key': harvester ? harvester.key : '',
},
};
}).concat(
ApolloLink.from([
new HttpLink({
uri: harvester ? harvester.link : '',
}),
])
),
cache: new InMemoryCache(),
});
if (harvester) {
harvesterClient = new ApolloClient({
link: setContext((_, { headers }) => {
return {
headers: {
...headers,
'x-api-key': harvester.key,
},
};
}).concat(
ApolloLink.from([
new HttpLink({
uri: harvester.link,
}),
])
),
cache: new InMemoryCache(),
});
}

const isApolloConfigured = typeof harvester !== 'undefined';
if (signer) {
polymeshApi.setSigner(signer);
}

let context: Context;

if (accountSeed) {
context = await Context.create({
polymeshApi,
isApolloConfigured,
harvesterClient,
seed: accountSeed,
});
} else if (keyring) {
let keyringInstance: CommonKeyring;
if (isUiKeyring(keyring)) {
keyringInstance = keyring.keyring;
} else {
keyringInstance = keyring;
}
context = await Context.create({
polymeshApi,
isApolloConfigured,
harvesterClient,
keyring,
keyring: keyringInstance,
});
} else if (accountUri) {
context = await Context.create({
polymeshApi,
isApolloConfigured,
harvesterClient,
uri: accountUri,
});
} else {
context = await Context.create({
polymeshApi,
isApolloConfigured,
harvesterClient,
});
}
Expand Down
52 changes: 43 additions & 9 deletions src/__tests__/Polymesh.ts
@@ -1,4 +1,5 @@
import { Keyring } from '@polkadot/api';
import { Signer } from '@polkadot/api/types';
import { ApolloLink, GraphQLRequest } from 'apollo-link';
import * as apolloLinkContextModule from 'apollo-link-context';
import BigNumber from 'bignumber.js';
Expand Down Expand Up @@ -65,8 +66,7 @@ describe('Polymesh Class', () => {
sinon.assert.calledOnce(createStub);
sinon.assert.calledWith(createStub, {
polymeshApi: dsMockUtils.getApiInstance(),
isApolloConfigured: false,
harvesterClient: dsMockUtils.getHarvesterClient(),
harvesterClient: null,
seed: accountSeed,
});
});
Expand All @@ -83,8 +83,24 @@ describe('Polymesh Class', () => {
sinon.assert.calledOnce(createStub);
sinon.assert.calledWith(createStub, {
polymeshApi: dsMockUtils.getApiInstance(),
isApolloConfigured: false,
harvesterClient: dsMockUtils.getHarvesterClient(),
harvesterClient: null,
keyring,
});
});

test('should instantiate Context with a ui keyring and return a Polymesh instance', async () => {
const keyring = {} as Keyring;
const createStub = dsMockUtils.getContextCreateStub();

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

sinon.assert.calledOnce(createStub);
sinon.assert.calledWith(createStub, {
polymeshApi: dsMockUtils.getApiInstance(),
harvesterClient: null,
keyring,
});
});
Expand All @@ -101,8 +117,7 @@ describe('Polymesh Class', () => {
sinon.assert.calledOnce(createStub);
sinon.assert.calledWith(createStub, {
polymeshApi: dsMockUtils.getApiInstance(),
isApolloConfigured: false,
harvesterClient: dsMockUtils.getHarvesterClient(),
harvesterClient: null,
uri: accountUri,
});
});
Expand All @@ -124,12 +139,31 @@ describe('Polymesh Class', () => {
sinon.assert.calledOnce(createStub);
sinon.assert.calledWith(createStub, {
polymeshApi: dsMockUtils.getApiInstance(),
isApolloConfigured: true,
harvesterClient: dsMockUtils.getHarvesterClient(),
uri: accountUri,
});
});

test('should set an optional signer for the polkadot API', async () => {
const accountSeed = 'Alice'.padEnd(32, ' ');
const createStub = dsMockUtils.getContextCreateStub();
const signer = 'signer' as Signer;

await Polymesh.connect({
nodeUrl: 'wss://some.url',
accountSeed,
signer,
});

sinon.assert.calledOnce(createStub);
sinon.assert.calledWith(createStub, {
polymeshApi: dsMockUtils.getApiInstance(),
harvesterClient: null,
seed: accountSeed,
});
sinon.assert.calledWith(dsMockUtils.getApiInstance().setSigner, signer);
});

test('should throw if Context fails in the connection process', async () => {
dsMockUtils.throwOnApiCreation();
const nodeUrl = 'wss://some.url';
Expand Down Expand Up @@ -704,7 +738,7 @@ describe('Polymesh Class', () => {
returnValue: dsMockUtils.createMockSecurityToken({
/* eslint-disable @typescript-eslint/camelcase */
owner_did: dsMockUtils.createMockIdentityId(),
name: dsMockUtils.createMockTokenName(ticker),
name: dsMockUtils.createMockAssetName(ticker),
asset_type: dsMockUtils.createMockAssetType(),
divisible: dsMockUtils.createMockBool(),
link_id: dsMockUtils.createMockU64(),
Expand All @@ -729,7 +763,7 @@ describe('Polymesh Class', () => {
returnValue: dsMockUtils.createMockSecurityToken({
/* eslint-disable @typescript-eslint/camelcase */
owner_did: dsMockUtils.createMockIdentityId(),
name: dsMockUtils.createMockTokenName(),
name: dsMockUtils.createMockAssetName(),
asset_type: dsMockUtils.createMockAssetType(),
divisible: dsMockUtils.createMockBool(),
link_id: dsMockUtils.createMockU64(),
Expand Down
11 changes: 1 addition & 10 deletions src/api/entities/SecurityToken/TokenHolders.ts
@@ -1,20 +1,11 @@
import BigNumber from 'bignumber.js';

import { Identity } from '~/api/entities/Identity';
import { IdentityBalance } from '~/api/entities/types';
import { Namespace } from '~/base';
import { IdentityId } from '~/polkadot';
import { balanceToBigNumber, identityIdToString, stringToTicker } from '~/utils';

import { SecurityToken } from './';

/**
* Represents the balance of a token holder
*/
export interface IdentityBalance {
identity: Identity;
balance: BigNumber;
}

/**
* Handles all Security Token Holders related functionality
*/
Expand Down
3 changes: 2 additions & 1 deletion src/api/entities/SecurityToken/__tests__/TokenHolders.ts
Expand Up @@ -6,10 +6,11 @@ import { Identity } from '~/api/entities/Identity';
import { Namespace } from '~/base';
import { IdentityId, Ticker } from '~/polkadot';
import { dsMockUtils, entityMockUtils } from '~/testUtils/mocks';
import { IdentityBalance } from '~/types';
import { tuple } from '~/types/utils';
import * as utilsModule from '~/utils';

import { IdentityBalance, TokenHolders } from '../TokenHolders';
import { TokenHolders } from '../TokenHolders';

describe('TokenHolders class', () => {
beforeAll(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/api/entities/SecurityToken/__tests__/index.ts
Expand Up @@ -61,7 +61,7 @@ describe('SecurityToken class', () => {
returnValue: dsMockUtils.createMockSecurityToken({
/* eslint-disable @typescript-eslint/camelcase */
owner_did: dsMockUtils.createMockIdentityId(owner),
name: dsMockUtils.createMockTokenName(ticker),
name: dsMockUtils.createMockAssetName(ticker),
asset_type: dsMockUtils.createMockAssetType(assetType),
divisible: dsMockUtils.createMockBool(isDivisible),
link_id: dsMockUtils.createMockU64(3),
Expand Down

0 comments on commit 5070943

Please sign in to comment.