Skip to content

Commit

Permalink
feat: deletePortfolio procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
shuffledex committed Oct 21, 2020
1 parent f8bb64b commit 5a2c5f1
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/api/entities/Identity/Portfolios.ts
@@ -1,3 +1,5 @@
import BigNumber from 'bignumber.js';

import { Identity, Namespace, NumberedPortfolio } from '~/api/entities';
import { createPortfolio } from '~/api/procedures';
import { TransactionQueue } from '~/base';
Expand All @@ -12,4 +14,14 @@ export class Portfolios extends Namespace<Identity> {
public createPortfolio(args: { name: string }): Promise<TransactionQueue<NumberedPortfolio>> {
return createPortfolio.prepare(args, this.context);
}

/**
* Delete a Portfolio by ID
*/
public deletePortfolio(args: { portfolioId: BigNumber }): Promise<TransactionQueue<void>> {
const {
parent: { did },
} = this;
return deletePortfolio.prepare({ id: args.portfolioId, did }, this.context);
}
}
14 changes: 13 additions & 1 deletion src/api/entities/NumberedPortfolio.ts
@@ -1,7 +1,8 @@
import BigNumber from 'bignumber.js';

import { Portfolio } from '~/api/entities';
import { Context } from '~/base';
import { deletePortfolio } from '~/api/procedures';
import { Context, TransactionQueue } from '~/base';

export interface UniqueIdentifiers {
did: string;
Expand Down Expand Up @@ -37,4 +38,15 @@ export class NumberedPortfolio extends Portfolio {

this.id = id;
}

/**
* Delete portfolio
*/
public async delete(): Promise<TransactionQueue<void>> {
const {
id,
owner: { did },
} = this;
return deletePortfolio.prepare({ did, id }, this.context);
}
}
50 changes: 50 additions & 0 deletions src/api/procedures/deletePortfolio.ts
@@ -0,0 +1,50 @@
import BigNumber from 'bignumber.js';

import { NumberedPortfolio } from '~/api/entities';
import { PolymeshError, Procedure } from '~/base';
import { ErrorCode } from '~/types';
import { numberToU64, stringToIdentityId } from '~/utils';

export interface DeletePortfolioParams {
did: string;
id: BigNumber;
}

/**
* @hidden
*/
export async function prepareDeletePortfolio(
this: Procedure<DeletePortfolioParams>,
args: DeletePortfolioParams
): Promise<void> {
const {
context: {
polymeshApi: {
query: { portfolio: queryPortfolio },
tx: { portfolio },
},
},
context,
} = this;

const { did, id } = args;

const numberedPortfolio = new NumberedPortfolio({ did, id }, context);
const isOwned = await numberedPortfolio.isOwned();

if (!isOwned) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: 'You are not the owner of this Portfolio',
});
}

// TODO @shuffledex: check portfolio balance before remove

// this.addTransaction(portfolio.deletePortfolio, {}, rawPortfolioNumber);
}

/**
* @hidden
*/
export const deletePortfolio = new Procedure(prepareDeletePortfolio);
1 change: 1 addition & 0 deletions src/api/procedures/index.ts
Expand Up @@ -39,3 +39,4 @@ export { transferPolyX, TransferPolyXParams } from './transferPolyX';
export { transferTokenOwnership, TransferTokenOwnershipParams } from './transferTokenOwnership';
// export { voteOnProposal, VoteOnProposalParams } from './voteOnProposal';
export { removePrimaryIssuanceAgent } from './removePrimaryIssuanceAgent';
export { deletePortfolio } from './deletePortfolio';

0 comments on commit 5a2c5f1

Please sign in to comment.