Skip to content

Commit

Permalink
Merge pull request #254 from PolymathNetwork/feat/MSDK-336-get-portfo…
Browse files Browse the repository at this point in the history
…lio-custodian

feat: get the portfolio custodian
  • Loading branch information
monitz87 committed Nov 10, 2020
2 parents 958d7c8 + 9589dfe commit aea1283
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/api/entities/Identity/Portfolios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export class Portfolios extends Namespace<Identity> {
}

/**
* Retrieve all Portfolios custodied by this Identity
* Retrieve all Portfolios custodied by this Identity.
* This only includes portfolios owned by a different Identity but custodied by this one.
* To fetch Portfolios owned by this Identity, use [[getPortfolios]]
*/
public async getCustodiedPortfolios(): Promise<(DefaultPortfolio | NumberedPortfolio)[]> {
const {
Expand Down
38 changes: 38 additions & 0 deletions src/api/entities/Portfolio/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,42 @@ describe('Portfolio class', () => {
expect(result[1].locked).toEqual(new BigNumber(0));
});
});

describe('method: getCustodian', () => {
let did: string;
let id: BigNumber;

beforeAll(() => {
did = 'someDid';
id = new BigNumber(1);
sinon.stub(utilsModule, 'portfolioIdToMeshPortfolioId');
});

afterAll(() => {
sinon.restore();
});

test('should return the custodian of the portfolio', async () => {
const custodianDid = 'custodianDid';
const identityIdToStringStub = sinon.stub(utilsModule, 'identityIdToString');

dsMockUtils
.createQueryStub('portfolio', 'portfolioCustodian')
.returns(dsMockUtils.createMockOption(dsMockUtils.createMockIdentityId(custodianDid)));

identityIdToStringStub.returns(custodianDid);

const portfolio = new Portfolio({ did, id }, context);

let result = await portfolio.getCustodian();
expect(result.did).toEqual(custodianDid);

dsMockUtils.createQueryStub('portfolio', 'portfolioCustodian').returns({});

identityIdToStringStub.returns(did);

result = await portfolio.getCustodian();
expect(result.did).toEqual(did);
});
});
});
37 changes: 36 additions & 1 deletion src/api/entities/Portfolio/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { Ticker } from 'polymesh-types/types';

import { Entity, Identity, SecurityToken } from '~/api/entities';
import { Context } from '~/base';
import { balanceToBigNumber, getDid, portfolioIdToMeshPortfolioId, tickerToString } from '~/utils';
import {
balanceToBigNumber,
getDid,
identityIdToString,
portfolioIdToMeshPortfolioId,
tickerToString,
} from '~/utils';

import { PortfolioBalance } from './types';

Expand Down Expand Up @@ -132,4 +138,33 @@ export class Portfolio extends Entity<UniqueIdentifiers> {

return values(assetBalances);
}

/**
* Retrieve the custodian Identity of this Portfolio
*
* @note if no custodian is set, the owner Identity is returned
*/
public async getCustodian(): Promise<Identity> {
const {
owner,
owner: { did },
_id,
context: {
polymeshApi: {
query: { portfolio },
},
},
context,
} = this;

const rawPortfolioId = portfolioIdToMeshPortfolioId({ did, number: _id }, context);
const portfolioCustodian = await portfolio.portfolioCustodian(rawPortfolioId);

try {
const rawIdentityId = portfolioCustodian.unwrap();
return new Identity({ did: identityIdToString(rawIdentityId) }, context);
} catch (_) {
return owner;
}
}
}

0 comments on commit aea1283

Please sign in to comment.