Skip to content

Commit

Permalink
feat: get the portfolio custodian
Browse files Browse the repository at this point in the history
  • Loading branch information
shuffledex committed Nov 4, 2020
1 parent 088b63f commit 18416d2
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/api/entities/Portfolio/__tests__/index.ts
Expand Up @@ -166,4 +166,44 @@ 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);

let portfolio = new Portfolio({ did, id }, context);
let result = await portfolio.getCustodian();

expect(result.did).toEqual(custodianDid);

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

identityIdToStringStub.returns(did);

portfolio = new Portfolio({ did, id }, context);
result = await portfolio.getCustodian();

expect(result.did).toEqual(did);
});
});
});
36 changes: 35 additions & 1 deletion src/api/entities/Portfolio/index.ts
Expand Up @@ -4,7 +4,12 @@ import { Ticker } from 'polymesh-types/types';

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

import { PortfolioBalance } from './types';

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

return values(assetBalances);
}

/**
* Retrieve the custodian of this portfolio
*/
public async getCustodian(): Promise<Identity> {
const {
owner: { did },
_id,
context: {
polymeshApi: {
query: { portfolio },
},
},
context,
} = this;

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

let custodianDid;
try {
const rawIdentityId = portfolioCustodian.unwrap();
custodianDid = identityIdToString(rawIdentityId);
} catch (_) {
custodianDid = did;
}

return new Identity({ did: custodianDid }, context);
}
}

0 comments on commit 18416d2

Please sign in to comment.