generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.ts
271 lines (234 loc) · 6.65 KB
/
index.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import { Bytes, Option } from '@polkadot/types';
import { PalletStoFundraiser } from '@polkadot/types/lookup';
import BigNumber from 'bignumber.js';
import {
closeOffering,
Context,
Entity,
FungibleAsset,
Identity,
investInOffering,
modifyOfferingTimes,
toggleFreezeOffering,
} from '~/internal';
import { investmentsQuery } from '~/middleware/queries';
import { Query } from '~/middleware/types';
import {
InvestInOfferingParams,
ModifyOfferingTimesParams,
NoArgsProcedureMethod,
ProcedureMethod,
ResultSet,
SubCallback,
UnsubCallback,
} from '~/types';
import { Ensured } from '~/types/utils';
import { bigNumberToU64, fundraiserToOfferingDetails, stringToTicker } from '~/utils/conversion';
import { calculateNextKey, createProcedureMethod, toHumanReadable } from '~/utils/internal';
import { Investment, OfferingDetails } from './types';
export interface UniqueIdentifiers {
id: BigNumber;
ticker: string;
}
export interface HumanReadable {
id: string;
ticker: string;
}
/**
* Represents an Asset Offering in the Polymesh blockchain
*/
export class Offering extends Entity<UniqueIdentifiers, HumanReadable> {
/**
* @hidden
* Check if a value is of type {@link UniqueIdentifiers}
*/
public static override isUniqueIdentifiers(identifier: unknown): identifier is UniqueIdentifiers {
const { id, ticker } = identifier as UniqueIdentifiers;
return id instanceof BigNumber && typeof ticker === 'string';
}
/**
* identifier number of the Offering
*/
public id: BigNumber;
/**
* Asset being offered
*/
public asset: FungibleAsset;
/**
* @hidden
*/
public constructor(identifiers: UniqueIdentifiers, context: Context) {
super(identifiers, context);
const { id, ticker } = identifiers;
this.id = id;
this.asset = new FungibleAsset({ ticker }, context);
this.freeze = createProcedureMethod(
{
getProcedureAndArgs: () => [toggleFreezeOffering, { ticker, id, freeze: true }],
voidArgs: true,
},
context
);
this.unfreeze = createProcedureMethod(
{
getProcedureAndArgs: () => [toggleFreezeOffering, { ticker, id, freeze: false }],
voidArgs: true,
},
context
);
this.close = createProcedureMethod(
{ getProcedureAndArgs: () => [closeOffering, { ticker, id }], voidArgs: true },
context
);
this.modifyTimes = createProcedureMethod(
{ getProcedureAndArgs: args => [modifyOfferingTimes, { ticker, id, ...args }] },
context
);
this.invest = createProcedureMethod(
{ getProcedureAndArgs: args => [investInOffering, { ticker, id, ...args }] },
context
);
}
/**
* Retrieve the Offering's details
*
* @note can be subscribed to
*/
public details(): Promise<OfferingDetails>;
public details(callback: SubCallback<OfferingDetails>): Promise<UnsubCallback>;
// eslint-disable-next-line require-jsdoc
public async details(
callback?: SubCallback<OfferingDetails>
): Promise<OfferingDetails | UnsubCallback> {
const {
context: {
polymeshApi: {
query: { sto },
},
},
id,
asset: { ticker },
context,
} = this;
const assembleResult = (
rawFundraiser: Option<PalletStoFundraiser>,
rawName: Option<Bytes>
): OfferingDetails => {
return fundraiserToOfferingDetails(rawFundraiser.unwrap(), rawName.unwrap(), context);
};
const rawTicker = stringToTicker(ticker, context);
const rawU64 = bigNumberToU64(id, context);
const fetchName = (): Promise<Option<Bytes>> => sto.fundraiserNames(rawTicker, rawU64);
if (callback) {
const fundraiserName = await fetchName();
return sto.fundraisers(rawTicker, rawU64, fundraiserData => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises -- callback errors should be handled by the caller
callback(assembleResult(fundraiserData, fundraiserName));
});
}
const [fundraiser, name] = await Promise.all([sto.fundraisers(rawTicker, rawU64), fetchName()]);
return assembleResult(fundraiser, name);
}
/**
* Close the Offering
*/
public close: NoArgsProcedureMethod<void>;
/**
* Freeze the Offering
*/
public freeze: NoArgsProcedureMethod<Offering>;
/**
* Unfreeze the Offering
*/
public unfreeze: NoArgsProcedureMethod<Offering>;
/**
* Modify the start/end time of the Offering
*
* @throws if:
* - Trying to modify the start time on an Offering that already started
* - Trying to modify anything on an Offering that already ended
* - Trying to change start or end time to a past date
*/
public modifyTimes: ProcedureMethod<ModifyOfferingTimesParams, void>;
/**
* Invest in the Offering
*
* @note required roles:
* - Purchase Portfolio Custodian
* - Funding Portfolio Custodian
*/
public invest: ProcedureMethod<InvestInOfferingParams, void>;
/**
* Retrieve all investments made on this Offering
*
* @param opts.size - page size
* @param opts.start - page offset
*
* @note supports pagination
* @note uses the middleware V2
*/
public async getInvestments(
opts: {
size?: BigNumber;
start?: BigNumber;
} = {}
): Promise<ResultSet<Investment>> {
const {
context,
id,
asset: { ticker },
} = this;
const { size, start } = opts;
const {
data: {
investments: { nodes, totalCount },
},
} = await context.queryMiddleware<Ensured<Query, 'investments'>>(
investmentsQuery(
{
stoId: id.toNumber(),
offeringToken: ticker,
},
size,
start
)
);
const count = new BigNumber(totalCount);
const data = nodes.map(({ investorId: did, offeringTokenAmount, raiseTokenAmount }) => ({
investor: new Identity({ did }, context),
soldAmount: new BigNumber(offeringTokenAmount).shiftedBy(-6),
investedAmount: new BigNumber(raiseTokenAmount).shiftedBy(-6),
}));
const next = calculateNextKey(count, data.length, start);
return {
data,
next,
count,
};
}
/**
* Determine whether this Offering exists on chain
*/
public async exists(): Promise<boolean> {
const {
asset: { ticker },
id,
context,
} = this;
const fundraiser = await context.polymeshApi.query.sto.fundraisers(
stringToTicker(ticker, context),
bigNumberToU64(id, context)
);
return fundraiser.isSome;
}
/**
* Return the Offering's ID and Asset ticker
*/
public toHuman(): HumanReadable {
const { asset, id } = this;
return toHumanReadable({
ticker: asset,
id,
});
}
}