Skip to content
This repository was archived by the owner on Jul 6, 2022. It is now read-only.

Commit 5620da9

Browse files
author
Victor Wiebe
committed
fix: cappedSTO example issues full overhaul with token purchase
1 parent 1ee5289 commit 5620da9

File tree

1 file changed

+100
-14
lines changed

1 file changed

+100
-14
lines changed

examples/cappedSTO.ts

Lines changed: 100 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { RedundantSubprovider, RPCSubprovider, Web3ProviderEngine } from '@0x/subproviders';
2-
import { ModuleRegistryEvents, BigNumber } from '@polymathnetwork/abi-wrappers';
2+
import { ModuleRegistryEvents, BigNumber, CappedSTOEvents } from '@polymathnetwork/abi-wrappers';
33
import ModuleFactoryWrapper from '../src/contract_wrappers/modules/module_factory_wrapper';
44
import { ApiConstructorParams, PolymathAPI } from '../src/PolymathAPI';
5-
import { bytes32ToString } from '../src/utils/convert';
65
import { FundRaiseType, ModuleName, ModuleType } from '../src';
76

87
// This file acts as a valid sandbox.ts file in root directory for adding a cappedSTO module on an unlocked node (like ganache)
@@ -20,9 +19,46 @@ window.addEventListener('load', async () => {
2019
// Instantiate the API
2120
const polymathAPI = new PolymathAPI(params);
2221

23-
const ticker = 'TEST';
24-
const tokenAddress = await polymathAPI.securityTokenRegistry.getSecurityTokenAddress(ticker);
25-
const TEST = await polymathAPI.tokenFactory.getSecurityTokenInstanceFromAddress(tokenAddress);
22+
// Get some poly tokens in your account and the security token
23+
const myAddress = await polymathAPI.getAccount();
24+
await polymathAPI.getPolyTokens({ amount: new BigNumber(1000000), address: myAddress });
25+
26+
// Prompt to setup your ticker and token name
27+
const ticker = prompt('Ticker', '');
28+
const tokenName = prompt('Token Name', '');
29+
30+
// Double check available
31+
await polymathAPI.securityTokenRegistry.isTickerAvailable({
32+
ticker: ticker!,
33+
});
34+
// Get the ticker fee and approve the security token registry to spend
35+
const tickerFee = await polymathAPI.securityTokenRegistry.getTickerRegistrationFee();
36+
await polymathAPI.polyToken.approve({
37+
spender: await polymathAPI.securityTokenRegistry.address(),
38+
value: tickerFee,
39+
});
40+
// Register a ticker
41+
await polymathAPI.securityTokenRegistry.registerTicker({
42+
ticker: ticker!,
43+
tokenName: tokenName!,
44+
});
45+
// Get the st launch fee and approve the security token registry to spend
46+
const securityTokenLaunchFee = await polymathAPI.securityTokenRegistry.getSecurityTokenLaunchFee();
47+
await polymathAPI.polyToken.approve({
48+
spender: await polymathAPI.securityTokenRegistry.address(),
49+
value: securityTokenLaunchFee,
50+
});
51+
52+
await polymathAPI.securityTokenRegistry.generateNewSecurityToken({
53+
name: tokenName!,
54+
ticker: ticker!,
55+
tokenDetails: 'details',
56+
divisible: true,
57+
treasuryWallet: myAddress,
58+
protocolVersion: '0',
59+
});
60+
61+
console.log('Security Token Generated');
2662

2763
const moduleStringName = 'CappedSTO';
2864
const moduleName = ModuleName.CappedSTO;
@@ -43,17 +79,16 @@ window.addEventListener('load', async () => {
4379
});
4480
const resultNames = await Promise.all(names);
4581

46-
const finalNames = resultNames.map(name => {
47-
return bytes32ToString(name);
48-
});
49-
const index = finalNames.indexOf(moduleStringName);
82+
const index = resultNames.indexOf(moduleStringName);
5083

84+
// Create a Security Token Instance
85+
const tickerSecurityTokenInstance = await polymathAPI.tokenFactory.getSecurityTokenInstanceFromTicker(ticker!);
5186
const factory = await polymathAPI.moduleFactory.getModuleFactory(modules[index]);
5287
const setupCost = await factory.setupCostInPoly();
5388

5489
// Get some poly tokens on the security token instance
5590
await polymathAPI.polyToken.transfer({
56-
to: await TEST.address(),
91+
to: await tickerSecurityTokenInstance.address(),
5792
value: setupCost,
5893
});
5994

@@ -69,21 +104,72 @@ window.addEventListener('load', async () => {
69104
},
70105
});
71106

72-
await TEST.addModule({
107+
// Get General TM Address to whitelist transfers
108+
const generalTMAddress = (await tickerSecurityTokenInstance.getModulesByName({
109+
moduleName: ModuleName.GeneralTransferManager,
110+
}))[0];
111+
const generalTM = await polymathAPI.moduleFactory.getModuleInstance({
112+
name: ModuleName.GeneralTransferManager,
113+
address: generalTMAddress,
114+
});
115+
116+
await generalTM.modifyKYCData({
117+
investor: myAddress,
118+
canSendAfter: new Date(),
119+
canReceiveAfter: new Date(),
120+
expiryTime: new Date(2020, 0),
121+
txData: {
122+
from: await polymathAPI.getAccount(),
123+
},
124+
});
125+
126+
const startTime = new Date(Date.now() + 10000);
127+
await tickerSecurityTokenInstance.addModule({
73128
moduleName,
74129
address: modules[index],
75130
maxCost: setupCost,
76131
budget: setupCost,
77132
archived: false,
78133
data: {
79-
startTime: new Date(),
80-
endTime: new Date(2025, 8),
134+
startTime,
135+
endTime: new Date(2031, 1),
81136
cap: new BigNumber(10),
82137
rate: new BigNumber(10),
83138
fundRaiseType: FundRaiseType.ETH.valueOf(),
84139
fundsReceiver: await polymathAPI.getAccount(),
85140
},
86141
});
87142

88-
TEST.unsubscribeAll();
143+
const cappedSTOAddress = (await tickerSecurityTokenInstance.getModulesByName({
144+
moduleName: ModuleName.CappedSTO,
145+
}))[0];
146+
147+
const cappedSTO = await polymathAPI.moduleFactory.getModuleInstance({
148+
name: ModuleName.CappedSTO,
149+
address: cappedSTOAddress,
150+
});
151+
152+
const sleep = (milliseconds: number) => {
153+
console.log('Sleeping until the STO starts');
154+
return new Promise(resolve => setTimeout(resolve, milliseconds));
155+
};
156+
await sleep(10000);
157+
158+
// Subscribe to event of update dividend dates
159+
await cappedSTO.subscribeAsync({
160+
eventName: CappedSTOEvents.TokenPurchase,
161+
indexFilterValues: {},
162+
callback: async (error, log) => {
163+
if (error) {
164+
console.log(error);
165+
} else {
166+
console.log('Token Purchased!', log);
167+
}
168+
},
169+
});
170+
171+
await cappedSTO.buyTokens({ beneficiary: await polymathAPI.getAccount(), value: new BigNumber(1) });
172+
console.log('Buy Tokens has been called');
173+
174+
tickerSecurityTokenInstance.unsubscribeAll();
89175
});

0 commit comments

Comments
 (0)