generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Portfolios.ts
160 lines (135 loc) · 4.11 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
152
153
154
155
156
157
158
159
160
import BigNumber from 'bignumber.js';
import {
Context,
DefaultPortfolio,
deletePortfolio,
Identity,
Namespace,
NumberedPortfolio,
PolymeshError,
} from '~/internal';
import { ErrorCode, PaginationOptions, ProcedureMethod, ResultSet } from '~/types';
import { identityIdToString, stringToIdentityId, u64ToBigNumber } from '~/utils/conversion';
import { createProcedureMethod, requestPaginated } from '~/utils/internal';
/**
* Handles all Portfolio related functionality on the Identity side
*/
export class Portfolios extends Namespace<Identity> {
/**
* @hidden
*/
constructor(parent: Identity, context: Context) {
super(parent, context);
const { did } = parent;
this.delete = createProcedureMethod(
{
getProcedureAndArgs: args => {
const { portfolio } = args;
const id = portfolio instanceof BigNumber ? portfolio : portfolio.id;
return [deletePortfolio, { id, did }];
},
},
context
);
}
/**
* 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]), did }, context));
});
return portfolios;
}
/**
* 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 {@link getPortfolios}
*
* @note supports pagination
*/
public async getCustodiedPortfolios(
paginationOpts?: PaginationOptions
): Promise<ResultSet<DefaultPortfolio | NumberedPortfolio>> {
const {
context,
context: {
polymeshApi: {
query: { portfolio },
},
},
parent: { did: custodianDid },
} = this;
const custodian = stringToIdentityId(custodianDid, context);
const { entries: portfolioEntries, lastKey: next } = await requestPaginated(
portfolio.portfoliosInCustody,
{
arg: custodian,
paginationOpts,
}
);
const data = portfolioEntries.map(([{ args }]) => {
const { did: ownerDid, kind } = args[1];
const did = identityIdToString(ownerDid);
if (kind.isDefault) {
return new DefaultPortfolio({ did }, context);
}
const id = u64ToBigNumber(kind.asUser);
return new NumberedPortfolio({ did, id }, context);
});
return {
data,
next,
};
}
/**
* Retrieve a Numbered Portfolio or the Default Portfolio if Portfolio ID is not passed
*
* @param args.portfolioId - optional, defaults to the Default Portfolio
*/
public async getPortfolio(): Promise<DefaultPortfolio>;
public async getPortfolio(args: { portfolioId: BigNumber }): Promise<NumberedPortfolio>;
// eslint-disable-next-line require-jsdoc
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 numberedPortfolio = new NumberedPortfolio({ id: portfolioId, did }, context);
const exists = await numberedPortfolio.exists();
if (!exists) {
throw new PolymeshError({
code: ErrorCode.DataUnavailable,
message: "The Portfolio doesn't exist",
});
}
return numberedPortfolio;
}
/**
* Delete a Portfolio by ID
*
* @note required role:
* - Portfolio Custodian
*/
public delete: ProcedureMethod<{ portfolio: BigNumber | NumberedPortfolio }, void>;
}