-
Notifications
You must be signed in to change notification settings - Fork 5
/
LaunchTieredSto.ts
267 lines (236 loc) · 7.64 KB
/
LaunchTieredSto.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
import {
ModuleName,
BigNumber,
SecurityTokenEvents,
isUSDTieredSTO_3_0_0,
TransactionParams,
FundRaiseType,
} from '@polymathnetwork/contract-wrappers';
import { Procedure } from './Procedure';
import {
ProcedureType,
PolyTransactionTag,
ErrorCode,
LaunchTieredStoProcedureArgs,
StoType,
} from '../types';
import { PolymathError } from '../PolymathError';
import { TransferErc20 } from './TransferErc20';
import { findEvents, isValidAddress } from '../utils';
import { SecurityToken, TieredSto } from '../entities';
import { ZERO_ADDRESS } from '../utils/constants';
export class LaunchTieredSto extends Procedure<LaunchTieredStoProcedureArgs, TieredSto> {
public type = ProcedureType.LaunchTieredSto;
public async prepareTransactions() {
const { args, context } = this;
const {
symbol,
startDate,
endDate,
tiers,
nonAccreditedInvestmentLimit,
minimumInvestment,
currencies,
raisedFundsWallet,
unsoldTokensWallet,
stableCoinAddresses = [],
customCurrency,
allowPreIssuing = false,
} = args;
const {
contractWrappers,
factories: { tieredStoFactory },
} = context;
let securityToken;
try {
securityToken = await contractWrappers.tokenFactory.getSecurityTokenInstanceFromTicker(
symbol
);
} catch (err) {
throw new PolymathError({
code: ErrorCode.ProcedureValidationError,
message: `There is no Security Token with symbol ${symbol}`,
});
}
if (currencies.includes(FundRaiseType.StableCoin) && stableCoinAddresses.length === 0) {
throw new PolymathError({
code: ErrorCode.ProcedureValidationError,
message: 'Stable Coin address array cannot be empty if raising in Stable Coin',
});
}
const customOracleAddresses: string[] = [];
let denominatedCurrency = '';
if (customCurrency) {
const {
currencySymbol = 'USD',
ethOracleAddress = '',
polyOracleAddress = '',
} = customCurrency;
if (currencies.includes(FundRaiseType.ETH)) {
if (!isValidAddress(ethOracleAddress)) {
throw new PolymathError({
code: ErrorCode.ProcedureValidationError,
message: `Must provide ETH oracle for '${currencySymbol}'`,
});
} else {
customOracleAddresses.push(ethOracleAddress);
}
} else {
customOracleAddresses.push(ZERO_ADDRESS);
}
if (currencies.includes(FundRaiseType.POLY)) {
if (!isValidAddress(polyOracleAddress)) {
throw new PolymathError({
code: ErrorCode.ProcedureValidationError,
message: `Must provide POLY oracle for '${currencySymbol}'`,
});
} else {
customOracleAddresses.push(polyOracleAddress);
}
} else {
customOracleAddresses.push(ZERO_ADDRESS);
}
denominatedCurrency = currencySymbol;
}
const securityTokenAddress = await securityToken.address();
const moduleName = ModuleName.UsdTieredSTO;
const factoryAddress = await contractWrappers.getModuleFactoryAddress({
tokenAddress: securityTokenAddress,
moduleName,
});
const moduleFactory = await contractWrappers.moduleFactory.getModuleFactory(factoryAddress);
let usdCost: BigNumber | null = null;
const [polyCost, isCostInPoly, cost] = await Promise.all([
moduleFactory.setupCostInPoly(),
moduleFactory.isCostInPoly(),
moduleFactory.setupCost(),
]);
if (!isCostInPoly) {
usdCost = cost;
}
const balance = await contractWrappers.polyToken.balanceOf({ owner: securityTokenAddress });
const difference = polyCost.minus(balance);
// only transfer the required amount of POLY
if (difference.gt(new BigNumber(0))) {
await this.addProcedure(TransferErc20)({
receiver: securityTokenAddress,
amount: difference,
});
}
const ratePerTier: BigNumber[] = [];
const ratePerTierDiscountPoly: BigNumber[] = [];
const tokensPerTierTotal: BigNumber[] = [];
const tokensPerTierDiscountPoly: BigNumber[] = [];
tiers.forEach(
({
tokensOnSale,
price,
tokensWithDiscount = new BigNumber(0),
discountedPrice = new BigNumber(0),
}) => {
ratePerTier.push(price);
ratePerTierDiscountPoly.push(discountedPrice);
tokensPerTierTotal.push(tokensOnSale);
tokensPerTierDiscountPoly.push(tokensWithDiscount);
}
);
const [newStoAddress, newSto] = await this.addTransaction<
TransactionParams.SecurityToken.AddUSDTieredSTO,
[string, TieredSto]
>(securityToken.addModuleWithLabel, {
tag: PolyTransactionTag.EnableTieredSto,
fees: {
usd: usdCost,
poly: polyCost,
},
resolvers: [
async receipt => {
const { logs } = receipt;
const [event] = findEvents({
eventName: SecurityTokenEvents.ModuleAdded,
logs,
});
if (event) {
const { args: eventArgs } = event;
const { _module } = eventArgs;
return _module;
}
throw new PolymathError({
code: ErrorCode.UnexpectedEventLogs,
message:
"The Tiered STO was successfully launched but the corresponding event wasn't fired. Please report this issue to the Polymath team.",
});
},
async receipt => {
const { logs } = receipt;
const [event] = findEvents({
eventName: SecurityTokenEvents.ModuleAdded,
logs,
});
if (event) {
const { args: eventArgs } = event;
const { _module } = eventArgs;
return tieredStoFactory.fetch(
TieredSto.generateId({
securityTokenId: SecurityToken.generateId({ symbol }),
stoType: StoType.Tiered,
address: _module,
})
);
}
throw new PolymathError({
code: ErrorCode.UnexpectedEventLogs,
message:
"The Tiered STO was successfully launched but the corresponding event wasn't fired. Please report this issue to the Polymath team.",
});
},
],
})({
moduleName,
address: factoryAddress,
data: {
startTime: startDate,
endTime: endDate,
ratePerTier,
ratePerTierDiscountPoly,
tokensPerTierTotal,
tokensPerTierDiscountPoly,
nonAccreditedLimitUSD: nonAccreditedInvestmentLimit,
minimumInvestmentUSD: minimumInvestment,
fundRaiseTypes: currencies,
wallet: raisedFundsWallet,
treasuryWallet: unsoldTokensWallet,
stableTokens: stableCoinAddresses,
customOracleAddresses,
denominatedCurrency,
},
maxCost: polyCost,
archived: false,
});
if (allowPreIssuing) {
await this.addTransaction(
{
futureValue: newStoAddress,
futureMethod: async address => {
const stoModule = await contractWrappers.moduleFactory.getModuleInstance({
name: ModuleName.UsdTieredSTO,
address,
});
if (isUSDTieredSTO_3_0_0(stoModule)) {
throw new PolymathError({
code: ErrorCode.IncorrectVersion,
message:
'STO version is 3.0.0. Version 3.1.0 or greater is required for pre-issuing',
});
}
return stoModule.allowPreMinting;
},
},
{
tag: PolyTransactionTag.AllowPreMinting,
}
)({});
}
return newSto;
}
}