generated from PolymeshAssociation/typescript-boilerplate
/
modifyOfferingTimes.ts
155 lines (135 loc) · 3.72 KB
/
modifyOfferingTimes.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
import BigNumber from 'bignumber.js';
import { Moment } from 'polymesh-types/types';
import { Asset, Offering, PolymeshError, Procedure } from '~/internal';
import { ErrorCode, OfferingSaleStatus, OfferingTimingStatus, TxTags } from '~/types';
import { ProcedureAuthorization } from '~/types/internal';
import { bigNumberToU64, dateToMoment, stringToTicker } from '~/utils/conversion';
/**
* @hidden
*/
function validateInput(
sale: OfferingSaleStatus,
newStart: Date | undefined,
start: Date,
newEnd: Date | null | undefined,
end: Date | null,
timing: OfferingTimingStatus
): void {
if ([OfferingSaleStatus.Closed, OfferingSaleStatus.ClosedEarly].includes(sale)) {
throw new PolymeshError({
code: ErrorCode.UnmetPrerequisite,
message: 'The Offering is already closed',
});
}
const areSameTimes =
(!newStart || start === newStart) && (newEnd === undefined || end === newEnd);
if (areSameTimes) {
throw new PolymeshError({
code: ErrorCode.NoDataChange,
message: 'Nothing to modify',
});
}
const now = new Date();
if (timing === OfferingTimingStatus.Expired) {
throw new PolymeshError({
code: ErrorCode.UnmetPrerequisite,
message: 'The Offering has already ended',
});
}
if (timing !== OfferingTimingStatus.NotStarted && newStart) {
throw new PolymeshError({
code: ErrorCode.UnmetPrerequisite,
message: 'Cannot modify the start time of an Offering that already started',
});
}
const datesAreInThePast = (newStart && now > newStart) || (newEnd && now > newEnd);
if (datesAreInThePast) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: 'New dates are in the past',
});
}
}
export type ModifyOfferingTimesParams =
| {
/**
* new start time (optional, will be left the same if not passed)
*/
start?: Date;
/**
* new end time (optional, will be left th same if not passed). A null value means the Offering doesn't end
*/
end: Date | null;
}
| {
start: Date;
end?: Date | null;
}
| { start: Date; end: Date | null };
/**
* @hidden
*/
export type Params = ModifyOfferingTimesParams & {
id: BigNumber;
ticker: string;
};
/**
* @hidden
*/
export async function prepareModifyOfferingTimes(
this: Procedure<Params, void>,
args: Params
): Promise<void> {
const {
context: {
polymeshApi: {
tx: { sto: txSto },
},
},
context,
} = this;
const { ticker, id, start: newStart, end: newEnd } = args;
const offering = new Offering({ ticker, id }, context);
const {
status: { sale, timing },
end,
start,
} = await offering.details();
validateInput(sale, newStart, start, newEnd, end, timing);
const rawTicker = stringToTicker(ticker, context);
const rawId = bigNumberToU64(id, context);
const rawStart = newStart ? dateToMoment(newStart, context) : dateToMoment(start, context);
let rawEnd: Moment | null;
if (newEnd === null) {
rawEnd = null;
} else if (!newEnd) {
rawEnd = end && dateToMoment(end, context);
} else {
rawEnd = dateToMoment(newEnd, context);
}
this.addTransaction({
transaction: txSto.modifyFundraiserWindow,
args: [rawTicker, rawId, rawStart, rawEnd],
});
}
/**
* @hidden
*/
export function getAuthorization(
this: Procedure<Params, void>,
{ ticker }: Params
): ProcedureAuthorization {
const { context } = this;
return {
permissions: {
transactions: [TxTags.sto.ModifyFundraiserWindow],
assets: [new Asset({ ticker }, context)],
portfolios: [],
},
};
}
/**
* @hidden
*/
export const modifyOfferingTimes = (): Procedure<Params, void> =>
new Procedure(prepareModifyOfferingTimes, getAuthorization);