Skip to content

Commit

Permalink
feat: quitCustody procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
shuffledex committed Jul 6, 2021
1 parent 555119e commit 72369e7
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/api/entities/Portfolio/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Identity,
moveFunds,
MoveFundsParams,
quitCustody,
SecurityToken,
setCustodian,
SetCustodianParams,
Expand Down Expand Up @@ -76,6 +77,10 @@ export class Portfolio extends Entity<UniqueIdentifiers> {
{ getProcedureAndArgs: args => [moveFunds, { ...args, from: this }] },
context
);
this.quitCustody = createProcedureMethod(
{ getProcedureAndArgs: () => [quitCustody, { portfolio: this }] },
context
);
}

/**
Expand Down Expand Up @@ -191,7 +196,6 @@ export class Portfolio extends Entity<UniqueIdentifiers> {
* @note required role:
* - Portfolio Custodian
*/

public setCustodian: ProcedureMethod<SetCustodianParams, void>;

/**
Expand All @@ -200,9 +204,16 @@ export class Portfolio extends Entity<UniqueIdentifiers> {
* @note required role:
* - Portfolio Custodian
*/

public moveFunds: ProcedureMethod<MoveFundsParams, void>;

/**
* Returns the custody of the portfolio to the portfolio owner unilaterally
*
* @note required role:
* - Portfolio Custodian
*/
public quitCustody: ProcedureMethod<void, void>;

/**
* Retrieve the custodian Identity of this Portfolio
*
Expand Down
82 changes: 82 additions & 0 deletions src/api/procedures/quitCustody.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { assertPortfolioExists } from '~/api/procedures/utils';
import { DefaultPortfolio, NumberedPortfolio, PolymeshError, Procedure } from '~/internal';
import { ErrorCode, RoleType, TxTags } from '~/types';
import { PortfolioId, ProcedureAuthorization } from '~/types/internal';
import { portfolioIdToMeshPortfolioId, portfolioLikeToPortfolioId } from '~/utils/conversion';

/**
* @hidden
*/
export type Params = {
portfolio: DefaultPortfolio | NumberedPortfolio;
};

/**
* @hidden
*/
export async function prepareQuitCustody(
this: Procedure<Params, void>,
args: Params
): Promise<void> {
const {
context: {
polymeshApi: { tx },
},
context,
} = this;

const { portfolio } = args;

const {
owner: { did },
} = portfolio;

const { did: currentDid } = await context.getCurrentIdentity();

if (currentDid === did) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: 'The Portfolio Custodian is the Current Identity',
});
}

const portfolioId = portfolioLikeToPortfolioId(portfolio);

await assertPortfolioExists(portfolioId, context);

const rawPortfolioId = portfolioIdToMeshPortfolioId(portfolioId, context);

this.addTransaction(tx.portfolio.quitPortfolioCustody, {}, rawPortfolioId);
}

/**
* @hidden
*/
export function getAuthorization(
this: Procedure<Params>,
{ portfolio }: Params
): ProcedureAuthorization {
const {
owner: { did },
} = portfolio;
let portfolioId: PortfolioId = { did };

if (portfolio instanceof NumberedPortfolio) {
portfolioId = { ...portfolioId, number: portfolio.id };
}

return {
signerPermissions: {
transactions: [TxTags.portfolio.QuitPortfolioCustody],
tokens: [],
portfolios: [portfolio],
},
identityRoles: [{ type: RoleType.PortfolioCustodian, portfolioId }],
};
}

/**
* @hidden
*/
export const quitCustody = (): Procedure<Params, void> =>
new Procedure(prepareQuitCustody, getAuthorization);
1 change: 1 addition & 0 deletions src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,4 @@ export { toggleFreezeSecondaryKeys } from '~/api/procedures/toggleFreezeSecondar
export { modifyVenue, ModifyVenueParams } from '~/api/procedures/modifyVenue';
export { leaveIdentity } from '~/api/procedures/leaveIdentity';
export { claimClassicTicker, ClaimClassicTickerParams } from '~/api/procedures/claimClassicTicker';
export { quitCustody } from '~/api/procedures/quitCustody';

0 comments on commit 72369e7

Please sign in to comment.