generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Portfolios.ts
151 lines (126 loc) · 4.14 KB
/
Portfolios.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import BigNumber from 'bignumber.js';
import { PortfolioId } from 'polymesh-types/types';
import { DefaultPortfolio, Identity, Namespace, NumberedPortfolio } from '~/api/entities';
import { createPortfolio, deletePortfolio } from '~/api/procedures';
import { PolymeshError, TransactionQueue } from '~/base';
import { PortfolioNumber } from '~/polkadot';
import { ErrorCode } from '~/types';
import { identityIdToString, numberToU64, stringToIdentityId, u64ToBigNumber } from '~/utils';
/**
* Handles all Portfolio related functionality on the Identity side
*/
export class Portfolios extends Namespace<Identity> {
/**
* Retrieve all the Portfolios owned by this Identity
*/
public async getPortfolios(): Promise<[DefaultPortfolio, ...NumberedPortfolio[]]> {
const {
context,
context: {
polymeshApi: {
query: { portfolio },
},
},
parent: { did },
} = this;
const identityId = stringToIdentityId(did, context);
const rawPortfolios = await portfolio.portfolios.entries(identityId);
const portfolios: [DefaultPortfolio, ...NumberedPortfolio[]] = [
new DefaultPortfolio({ did }, context),
];
rawPortfolios.forEach(([key]) => {
portfolios.push(
new NumberedPortfolio({ id: u64ToBigNumber(key.args[1] as PortfolioNumber), did }, context)
);
});
return portfolios;
}
/**
* Retrieve all Portfolios custodied by this Identity
*/
public async getCustodiedPortfolios(): Promise<(DefaultPortfolio | NumberedPortfolio)[]> {
const {
context,
context: {
polymeshApi: {
query: { portfolio },
},
},
parent: { did: custodianDid },
} = this;
const custodian = stringToIdentityId(custodianDid, context);
const portfolioEntries = await portfolio.portfoliosInCustody.entries(custodian);
return portfolioEntries.map(([{ args }]) => {
const { did: ownerDid, kind } = args[1] as PortfolioId;
const did = identityIdToString(ownerDid);
if (kind.isDefault) {
return new DefaultPortfolio({ did }, context);
}
const id = u64ToBigNumber(kind.asUser);
return new NumberedPortfolio({ did, id }, context);
});
}
/**
* Retrieve whether this Identity possesses a Portfolio with a certain ID
*/
public async portfolioExists(args: { portfolioId: BigNumber }): Promise<boolean> {
const {
context,
context: {
polymeshApi: {
query: { portfolio },
},
},
parent: { did },
} = this;
const { portfolioId } = args;
const identityId = stringToIdentityId(did, context);
const rawPortfolioNumber = numberToU64(portfolioId, context);
const rawPortfolioName = await portfolio.portfolios(identityId, rawPortfolioNumber);
return !rawPortfolioName.isEmpty;
}
/**
* Retrieve a numbered Portfolio or the default Portfolio if Portfolio ID is not passed
*
* @param args.porfolioId - optional, defaults to the default portfolio
*/
public async getPortfolio(args?: {
portfolioId: BigNumber;
}): Promise<DefaultPortfolio | NumberedPortfolio> {
const {
context,
parent: { did },
} = this;
const portfolioId = args?.portfolioId;
if (!portfolioId) {
return new DefaultPortfolio({ did }, context);
}
const exists = await this.portfolioExists({ portfolioId });
if (!exists) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: "The Portfolio doesn't exist",
});
}
return new NumberedPortfolio({ id: portfolioId, did }, context);
}
/**
* Create a new Portfolio for the Identity
*/
public create(args: { name: string }): Promise<TransactionQueue<NumberedPortfolio>> {
return createPortfolio.prepare(args, this.context);
}
/**
* Delete a Portfolio by ID
*/
public delete(args: {
portfolio: BigNumber | NumberedPortfolio;
}): Promise<TransactionQueue<void>> {
const {
parent: { did },
} = this;
const { portfolio } = args;
const id = portfolio instanceof BigNumber ? portfolio : portfolio.id;
return deletePortfolio.prepare({ id, did }, this.context);
}
}