generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
NumberedPortfolio.ts
145 lines (126 loc) · 3.62 KB
/
NumberedPortfolio.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
import { Bytes } from '@polkadot/types';
import BigNumber from 'bignumber.js';
import { Context, PolymeshError, Portfolio, renamePortfolio } from '~/internal';
import { portfolioQuery } from '~/middleware/queries';
import { Query } from '~/middleware/types';
import { ErrorCode, EventIdentifier, ProcedureMethod, RenamePortfolioParams } from '~/types';
import { Ensured } from '~/types/utils';
import {
bigNumberToU64,
bytesToString,
middlewareEventDetailsToEventIdentifier,
stringToIdentityId,
} from '~/utils/conversion';
import { createProcedureMethod, optionize } from '~/utils/internal';
export interface UniqueIdentifiers {
did: string;
id: BigNumber;
}
/**
* Represents a numbered (non-default) Portfolio for an Identity
*/
export class NumberedPortfolio extends Portfolio {
/**
* @hidden
* Check if a value is of type {@link UniqueIdentifiers}
*/
public static override isUniqueIdentifiers(identifier: unknown): identifier is UniqueIdentifiers {
const { did, id } = identifier as UniqueIdentifiers;
return typeof did === 'string' && id instanceof BigNumber;
}
/**
* Portfolio identifier number
*/
public id: BigNumber;
/**
* @hidden
*/
public constructor(identifiers: UniqueIdentifiers, context: Context) {
super(identifiers, context);
const { id, did } = identifiers;
this.id = id;
this.modifyName = createProcedureMethod(
{ getProcedureAndArgs: args => [renamePortfolio, { ...args, did, id }] },
context
);
}
/**
* Rename portfolio
*
* @note Only the owner is allowed to rename the Portfolio.
*/
public modifyName: ProcedureMethod<RenamePortfolioParams, NumberedPortfolio>;
/**
* Return the Portfolio name
*/
public async getName(): Promise<string> {
const {
owner: { did },
id,
context: {
polymeshApi: {
query: { portfolio },
},
isV5,
},
context,
} = this;
const rawPortfolioName = await portfolio.portfolios(did, bigNumberToU64(id, context));
if (!isV5 && rawPortfolioName.isNone) {
throw new PolymeshError({
code: ErrorCode.DataUnavailable,
message: "The Portfolio doesn't exist",
});
}
if (isV5) {
return bytesToString(rawPortfolioName as unknown as Bytes);
} else {
return bytesToString(rawPortfolioName.unwrap());
}
}
/**
* Retrieve the identifier data (block number, date and event index) of the event that was emitted when this Portfolio was created
*
* @note uses the middlewareV2
* @note there is a possibility that the data is not ready by the time it is requested. In that case, `null` is returned
*/
public async createdAt(): Promise<EventIdentifier | null> {
const {
owner: { did },
id,
context,
} = this;
const {
data: {
portfolios: {
nodes: [node],
},
},
} = await context.queryMiddleware<Ensured<Query, 'portfolios'>>(
portfolioQuery({
identityId: did,
number: id.toNumber(),
})
);
return optionize(middlewareEventDetailsToEventIdentifier)(node?.createdBlock, node?.eventIdx);
}
/**
* Return whether this Portfolio exists
*/
public async exists(): Promise<boolean> {
const {
owner: { did },
id,
context,
context: {
polymeshApi: {
query: { portfolio },
},
},
} = this;
const identityId = stringToIdentityId(did, context);
const rawPortfolioNumber = bigNumberToU64(id, context);
const size = await portfolio.portfolios.size(identityId, rawPortfolioNumber);
return !size.isZero();
}
}