Skip to content

Commit

Permalink
refactor: extract getAsset and getFungibleAsset methods
Browse files Browse the repository at this point in the history
  • Loading branch information
prashantasdeveloper committed Nov 15, 2024
1 parent 71d8532 commit 2ae6f7d
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 118 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: '16.x'
node-version: '22.x'
- name: install dependencies
run: yarn --frozen-lockfile
- name: lint
Expand All @@ -27,7 +27,7 @@ jobs:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: '16.x'
node-version: '22.x'
- name: install dependencies
run: yarn --frozen-lockfile
- name: test
Expand All @@ -41,7 +41,7 @@ jobs:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: '18.x'
node-version: '22.x'
- name: install dependencies
run: yarn --frozen-lockfile
- name: build
Expand Down
34 changes: 34 additions & 0 deletions src/common/assets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Polymesh } from '@polymeshassociation/polymesh-sdk';
import { Asset, FungibleAsset } from '@polymeshassociation/polymesh-sdk/types';

import { isAssetId } from '~/common/utils';

/**
* @hidden
*/
export async function getAsset(api: Polymesh, assetInput: string): Promise<Asset> {
let asset;

if (isAssetId(assetInput)) {
asset = await api.assets.getAsset({ assetId: assetInput });
} else {
asset = await api.assets.getAsset({ ticker: assetInput });
}

return asset;
}

/**
* @hidden
*/
export async function getFungibleAsset(api: Polymesh, assetInput: string): Promise<FungibleAsset> {
let asset;

if (isAssetId(assetInput)) {
asset = await api.assets.getFungibleAsset({ assetId: assetInput });
} else {
asset = await api.assets.getFungibleAsset({ ticker: assetInput });
}

return asset;
}
10 changes: 2 additions & 8 deletions src/examples/assetInviteAgent.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PermissionGroupType } from '@polymeshassociation/polymesh-sdk/types';

import { getAsset } from '~/common/assets';
import { getClient } from '~/common/client';
import { isAssetId } from '~/common/utils';

/*
This script demonstrates Asset PIA functionality. It:
Expand All @@ -22,13 +22,7 @@ import { isAssetId } from '~/common/utils';
throw new Error('Please supply a ticker or Asset ID as an argument to the script');
}

let asset;

if (isAssetId(assetInput)) {
asset = await api.assets.getAsset({ assetId: assetInput });
} else {
asset = await api.assets.getAsset({ ticker: assetInput });
}
const asset = await getAsset(api, assetInput);
const { fullAgents } = await asset.details();

if (fullAgents.length) {
Expand Down
10 changes: 2 additions & 8 deletions src/examples/assetMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
MetadataType,
} from '@polymeshassociation/polymesh-sdk/types';

import { getAsset } from '~/common/assets';
import { getClient } from '~/common/client';
import { isAssetId } from '~/common/utils';

/*
This script showcases Metadata related functionality. It:
Expand Down Expand Up @@ -41,13 +41,7 @@ import { isAssetId } from '~/common/utils';

console.log(`\n➡️ Getting Global Asset Metadata Keys`);

let asset;

if (isAssetId(assetInput)) {
asset = await api.assets.getAsset({ assetId: assetInput });
} else {
asset = await api.assets.getAsset({ ticker: assetInput });
}
const asset = await getAsset(api, assetInput);

/**
* Fetches and displays metadata value
Expand Down
13 changes: 3 additions & 10 deletions src/examples/assets/getHolders.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { FungibleAsset } from '@polymeshassociation/polymesh-sdk/types';

import { getFungibleAsset } from '~/common/assets';
import { getClient } from '~/common/client';
import { isAssetId, parseArgs } from '~/common/utils';
import { parseArgs } from '~/common/utils';

type ScriptArgs = {
asset?: string;
Expand All @@ -27,13 +26,7 @@ type ScriptArgs = {
const identity = (await api.getSigningIdentity())!;
console.log(`Connected! Signing Identity ID: ${identity.did}`);

let asset: FungibleAsset;

if (isAssetId(assetInput)) {
asset = await api.assets.getFungibleAsset({ assetId: assetInput });
} else {
asset = await api.assets.getFungibleAsset({ ticker: assetInput });
}
const asset = await getFungibleAsset(api, assetInput);

console.log(`Preparing to list token holders for ${asset.id}`);

Expand Down
14 changes: 4 additions & 10 deletions src/examples/assets/issueTokens.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { BigNumber } from '@polymeshassociation/polymesh-sdk';
import { FungibleAsset } from '@polymeshassociation/polymesh-sdk/types';

import { getFungibleAsset } from '~/common/assets';
import { getClient } from '~/common/client';
import { isAssetId, parseArgs } from '~/common/utils';
import { parseArgs } from '~/common/utils';

type ScriptArgs = {
amount?: number;
Expand All @@ -18,7 +18,7 @@ type ScriptArgs = {
const { asset: assetInput, amount } = parseArgs<ScriptArgs>(process.argv.slice(2));

if (!assetInput) {
throw new Error('Please supply a ticker as an argument to the script');
throw new Error('Please supply a ticker or Asset ID as an argument to the script');
}

if (!amount) {
Expand All @@ -33,13 +33,7 @@ type ScriptArgs = {
const identity = (await api.getSigningIdentity())!;
console.log(`Connected! Signing Identity ID: ${identity.did}`);

let asset: FungibleAsset;

if (isAssetId(assetInput)) {
asset = await api.assets.getFungibleAsset({ assetId: assetInput });
} else {
asset = await api.assets.getFungibleAsset({ ticker: assetInput });
}
const asset = await getFungibleAsset(api, assetInput);

console.log(`Preparing to issue ${amount} of tokens for ${asset.id}`);

Expand Down
14 changes: 4 additions & 10 deletions src/examples/assets/redeemTokens.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { BigNumber } from '@polymeshassociation/polymesh-sdk';
import { FungibleAsset } from '@polymeshassociation/polymesh-sdk/types';

import { getFungibleAsset } from '~/common/assets';
import { getClient } from '~/common/client';
import { isAssetId, parseArgs } from '~/common/utils';
import { parseArgs } from '~/common/utils';

type ScriptArgs = {
amount?: number;
Expand All @@ -18,7 +18,7 @@ type ScriptArgs = {
const { asset: assetInput, amount } = parseArgs<ScriptArgs>(process.argv.slice(2));

if (!assetInput) {
throw new Error('Please supply a ticker as an argument to the script');
throw new Error('Please supply a ticker or asset ID as an argument to the script');
}

if (!amount) {
Expand All @@ -33,13 +33,7 @@ type ScriptArgs = {
const identity = (await api.getSigningIdentity())!;
console.log(`Connected! Signing Identity ID: ${identity.did}`);

let asset: FungibleAsset;

if (isAssetId(assetInput)) {
asset = await api.assets.getFungibleAsset({ assetId: assetInput });
} else {
asset = await api.assets.getFungibleAsset({ ticker: assetInput });
}
const asset = await getFungibleAsset(api, assetInput);

console.log(`Preparing to redeem ${amount} of tokens for ${asset.id}`);

Expand Down
11 changes: 2 additions & 9 deletions src/examples/checkpoints.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { FungibleAsset } from '@polymeshassociation/polymesh-sdk/types';

import { getFungibleAsset } from '~/common/assets';
import { getClient } from '~/common/client';
import { isAssetId } from '~/common/utils';

/*
This script showcases Checkpoints related functionality. It:
Expand All @@ -28,12 +26,7 @@ import { isAssetId } from '~/common/utils';
throw new Error('Please supply a ticker or Asset Id as an argument to the script');
}

let asset: FungibleAsset;
if (isAssetId(assetInput)) {
asset = await api.assets.getFungibleAsset({ assetId: assetInput });
} else {
asset = await api.assets.getFungibleAsset({ ticker: assetInput });
}
const asset = await getFungibleAsset(api, assetInput);

const details = await asset.details();
console.log(`Asset found! Current asset name is: ${details.name}`);
Expand Down
12 changes: 3 additions & 9 deletions src/examples/compliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {
ScopeType,
} from '@polymeshassociation/polymesh-sdk/types';

import { getAsset } from '~/common/assets';
import { getClient } from '~/common/client';
import { isAssetId, toHumanObject } from '~/common/utils';
import { toHumanObject } from '~/common/utils';

/*
This script showcases Compliance related functionality. Covered functionality:
Expand Down Expand Up @@ -36,14 +37,7 @@ import { isAssetId, toHumanObject } from '~/common/utils';
throw new Error('Please supply a ticker or Asset ID as an argument to the script');
}

// Get Asset for the given ticker
let asset;

if (isAssetId(assetInput)) {
asset = await api.assets.getAsset({ assetId: assetInput });
} else {
asset = await api.assets.getAsset({ ticker: assetInput });
}
const asset = await getAsset(api, assetInput);

console.log(`\n\nAsset found! Current asset name is: ${(await asset.details()).name}`);

Expand Down
11 changes: 3 additions & 8 deletions src/examples/dividendDistributions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { BigNumber } from '@polymeshassociation/polymesh-sdk';
import { FungibleAsset, TargetTreatment } from '@polymeshassociation/polymesh-sdk/types';
import { TargetTreatment } from '@polymeshassociation/polymesh-sdk/types';
import { isCheckpoint } from '@polymeshassociation/polymesh-sdk/utils';

import { getFungibleAsset } from '~/common/assets';
import { getClient } from '~/common/client';
import { isAssetId } from '~/common/utils';

/*
This script showcases Dividend Distribution related functionality. It:
Expand Down Expand Up @@ -31,12 +31,7 @@ import { isAssetId } from '~/common/utils';
throw new Error('Please supply a ticker or Asset Id as an argument to the script');
}

let asset: FungibleAsset;
if (isAssetId(assetInput)) {
asset = await api.assets.getFungibleAsset({ assetId: assetInput });
} else {
asset = await api.assets.getFungibleAsset({ ticker: assetInput });
}
const asset = await getFungibleAsset(api, assetInput);
console.log(`Asset found! Current asset name is: ${(await asset.details()).name}`);

const originPortfolio = await identity.portfolios.getPortfolio({ portfolioId: new BigNumber(1) });
Expand Down
12 changes: 3 additions & 9 deletions src/examples/externalAgents.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Asset, AssetTx, PermissionType, TxGroup } from '@polymeshassociation/polymesh-sdk/types';
import { AssetTx, PermissionType, TxGroup } from '@polymeshassociation/polymesh-sdk/types';
import P from 'bluebird';

import { getAsset } from '~/common/assets';
import { getClient } from '~/common/client';
import { isAssetId } from '~/common/utils';

/*
This script showcases External Agents related functionality. It:
Expand Down Expand Up @@ -31,13 +31,7 @@ import { isAssetId } from '~/common/utils';
throw new Error('Please supply a ticker as an argument to the script');
}

let asset: Asset;

if (isAssetId(assetInput)) {
asset = await api.assets.getAsset({ assetId: assetInput });
} else {
asset = await api.assets.getAsset({ ticker: assetInput });
}
const asset = await getAsset(api, assetInput);

const { name } = await asset.details();
console.log(`Asset found! Current asset name is: ${name}`);
Expand Down
10 changes: 2 additions & 8 deletions src/examples/investInSto.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { BigNumber } from '@polymeshassociation/polymesh-sdk';
import {
FungibleAsset,
OfferingBalanceStatus,
OfferingSaleStatus,
OfferingTimingStatus,
} from '@polymeshassociation/polymesh-sdk/types';

import { getFungibleAsset } from '~/common/assets';
import { getClient } from '~/common/client';
import { isAssetId } from '~/common/utils';

/*
This script showcases Offering investment functionality. It:
Expand All @@ -26,12 +25,7 @@ import { isAssetId } from '~/common/utils';

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const identity = (await api.getSigningIdentity())!;
let asset: FungibleAsset;
if (isAssetId(assetInput)) {
asset = await api.assets.getFungibleAsset({ assetId: assetInput });
} else {
asset = await api.assets.getFungibleAsset({ ticker: assetInput });
}
const asset = await getFungibleAsset(api, assetInput);

const fundingPortfolio = await identity.portfolios.getPortfolio();
const purchasePortfolio = await identity.portfolios.getPortfolio({
Expand Down
11 changes: 3 additions & 8 deletions src/examples/securityTokenCaa.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FungibleAsset, ModuleName, PermissionType } from '@polymeshassociation/polymesh-sdk/types';
import { ModuleName, PermissionType } from '@polymeshassociation/polymesh-sdk/types';

import { getFungibleAsset } from '~/common/assets';
import { getClient } from '~/common/client';
import { isAssetId } from '~/common/utils';

/*
This script demonstrates Asset CAA functionality. It:
Expand All @@ -22,12 +22,7 @@ import { isAssetId } from '~/common/utils';
throw new Error('Please supply a ticker or Asset ID as an argument to the script');
}

let asset: FungibleAsset;
if (isAssetId(assetInput)) {
asset = await api.assets.getFungibleAsset({ assetId: assetInput });
} else {
asset = await api.assets.getFungibleAsset({ ticker: assetInput });
}
const asset = await getFungibleAsset(api, assetInput);
const corporateActionAgents = await asset.corporateActions.getAgents();

if (corporateActionAgents.length) {
Expand Down
11 changes: 2 additions & 9 deletions src/examples/stos.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { BigNumber } from '@polymeshassociation/polymesh-sdk';
import { FungibleAsset } from '@polymeshassociation/polymesh-sdk/types';

import { getFungibleAsset } from '~/common/assets';
import { getClient } from '~/common/client';
import { isAssetId } from '~/common/utils';

/*
This script showcases Offering functionality. It:
Expand All @@ -26,13 +25,7 @@ import { isAssetId } from '~/common/utils';
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const identity = (await api.getSigningIdentity())!;

let asset: FungibleAsset;

if (isAssetId(assetInput)) {
asset = await api.assets.getFungibleAsset({ assetId: assetInput });
} else {
asset = await api.assets.getFungibleAsset({ ticker: assetInput });
}
const asset = await getFungibleAsset(api, assetInput);
const [venue] = await identity.getVenues();

const offeringPortfolio = await identity.portfolios.getPortfolio();
Expand Down
Loading

0 comments on commit 2ae6f7d

Please sign in to comment.