|
| 1 | +import { RedundantSubprovider, RPCSubprovider, Web3ProviderEngine } from '@0x/subproviders'; |
| 2 | +import ModuleFactoryWrapper from '../src/contract_wrappers/modules/module_factory_wrapper'; |
| 3 | +import { ApiConstructorParams, PolymathAPI } from '../src/PolymathAPI'; |
| 4 | +import { ModuleName, ModuleType } from '../src'; |
| 5 | +import { BigNumber, ModuleRegistryEvents, BlacklistTransferManagerEvents } from '@polymathnetwork/abi-wrappers'; |
| 6 | + |
| 7 | +// This file acts as a valid sandbox for using a blacklist restriction transfer manager module on an unlocked node (like ganache) |
| 8 | +window.addEventListener('load', async () => { |
| 9 | + // Setup the redundant provider |
| 10 | + const providerEngine = new Web3ProviderEngine(); |
| 11 | + providerEngine.addProvider(new RedundantSubprovider([new RPCSubprovider('http://127.0.0.1:8545')])); |
| 12 | + providerEngine.start(); |
| 13 | + const params: ApiConstructorParams = { |
| 14 | + provider: providerEngine, |
| 15 | + polymathRegistryAddress: '<Deployed Polymath Registry address>', |
| 16 | + }; |
| 17 | + |
| 18 | + // Instantiate the API |
| 19 | + const polymathAPI = new PolymathAPI(params); |
| 20 | + |
| 21 | + // Get some poly tokens in your account and the security token |
| 22 | + const myAddress = await polymathAPI.getAccount(); |
| 23 | + await polymathAPI.getPolyTokens({ amount: new BigNumber(1000000), address: myAddress }); |
| 24 | + |
| 25 | + // Prompt to setup your ticker and token name |
| 26 | + const ticker = prompt('Ticker', ''); |
| 27 | + const tokenName = prompt('Token Name', ''); |
| 28 | + |
| 29 | + // Double check available |
| 30 | + await polymathAPI.securityTokenRegistry.isTickerAvailable({ |
| 31 | + ticker: ticker!, |
| 32 | + }); |
| 33 | + // Get the ticker fee and approve the security token registry to spend |
| 34 | + const tickerFee = await polymathAPI.securityTokenRegistry.getTickerRegistrationFee(); |
| 35 | + await polymathAPI.polyToken.approve({ |
| 36 | + spender: await polymathAPI.securityTokenRegistry.address(), |
| 37 | + value: tickerFee, |
| 38 | + }); |
| 39 | + // Register a ticker |
| 40 | + await polymathAPI.securityTokenRegistry.registerTicker({ |
| 41 | + ticker: ticker!, |
| 42 | + tokenName: tokenName!, |
| 43 | + }); |
| 44 | + // Get the st launch fee and approve the security token registry to spend |
| 45 | + const securityTokenLaunchFee = await polymathAPI.securityTokenRegistry.getSecurityTokenLaunchFee(); |
| 46 | + await polymathAPI.polyToken.approve({ |
| 47 | + spender: await polymathAPI.securityTokenRegistry.address(), |
| 48 | + value: securityTokenLaunchFee, |
| 49 | + }); |
| 50 | + |
| 51 | + await polymathAPI.securityTokenRegistry.generateNewSecurityToken({ |
| 52 | + name: tokenName!, |
| 53 | + ticker: ticker!, |
| 54 | + tokenDetails: 'details', |
| 55 | + divisible: true, |
| 56 | + treasuryWallet: myAddress, |
| 57 | + protocolVersion: '0', |
| 58 | + }); |
| 59 | + |
| 60 | + console.log('Security Token Generated'); |
| 61 | + |
| 62 | + const moduleName = ModuleName.BlacklistTransferManager; |
| 63 | + |
| 64 | + const modules = await polymathAPI.moduleRegistry.getModulesByType({ |
| 65 | + moduleType: ModuleType.TransferManager, |
| 66 | + }); |
| 67 | + |
| 68 | + const instances: Promise<ModuleFactoryWrapper>[] = []; |
| 69 | + modules.map(address => { |
| 70 | + instances.push(polymathAPI.moduleFactory.getModuleFactory(address)); |
| 71 | + }); |
| 72 | + const resultInstances = await Promise.all(instances); |
| 73 | + |
| 74 | + const names: Promise<string>[] = []; |
| 75 | + resultInstances.map(instanceFactory => { |
| 76 | + names.push(instanceFactory.name()); |
| 77 | + }); |
| 78 | + const resultNames = await Promise.all(names); |
| 79 | + |
| 80 | + const index = resultNames.indexOf(moduleName); |
| 81 | + |
| 82 | + // Create a Security Token Instance |
| 83 | + const tickerSecurityTokenInstance = await polymathAPI.tokenFactory.getSecurityTokenInstanceFromTicker(ticker!); |
| 84 | + |
| 85 | + // Get General TM Address to whitelist transfers |
| 86 | + const generalTMAddress = (await tickerSecurityTokenInstance.getModulesByName({ |
| 87 | + moduleName: ModuleName.GeneralTransferManager, |
| 88 | + }))[0]; |
| 89 | + const generalTM = await polymathAPI.moduleFactory.getModuleInstance({ |
| 90 | + name: ModuleName.GeneralTransferManager, |
| 91 | + address: generalTMAddress, |
| 92 | + }); |
| 93 | + |
| 94 | + await tickerSecurityTokenInstance.addModule({ |
| 95 | + moduleName, |
| 96 | + address: modules[index], |
| 97 | + archived: false, |
| 98 | + }); |
| 99 | + |
| 100 | + const blacklistTMAddress = (await tickerSecurityTokenInstance.getModulesByName({ |
| 101 | + moduleName: ModuleName.BlacklistTransferManager, |
| 102 | + }))[0]; |
| 103 | + |
| 104 | + const blacklistTM = await polymathAPI.moduleFactory.getModuleInstance({ |
| 105 | + name: ModuleName.BlacklistTransferManager, |
| 106 | + address: blacklistTMAddress, |
| 107 | + }); |
| 108 | + |
| 109 | + const randomBeneficiary1 = '0x2222222222222222222222222222222222222222'; |
| 110 | + const randomBeneficiary2 = '0x3333333333333333333333333333333333333333'; |
| 111 | + |
| 112 | + await generalTM.modifyKYCDataMulti({ |
| 113 | + investors: [myAddress, randomBeneficiary1, randomBeneficiary2], |
| 114 | + canReceiveAfter: [new Date(), new Date(), new Date()], |
| 115 | + canSendAfter: [new Date(), new Date(), new Date()], |
| 116 | + expiryTime: [new Date(2035, 1), new Date(2035, 1), new Date(2035, 1)], |
| 117 | + }); |
| 118 | + |
| 119 | + await tickerSecurityTokenInstance.issueMulti({ |
| 120 | + investors: [myAddress, randomBeneficiary1, randomBeneficiary2], |
| 121 | + values: [new BigNumber(100), new BigNumber(100), new BigNumber(100)], |
| 122 | + }); |
| 123 | + |
| 124 | + // Subscribe to event of addblacklisttype |
| 125 | + await blacklistTM.subscribeAsync({ |
| 126 | + eventName: BlacklistTransferManagerEvents.AddBlacklistType, |
| 127 | + indexFilterValues: {}, |
| 128 | + callback: async (error, log) => { |
| 129 | + if (error) { |
| 130 | + console.log(error); |
| 131 | + } else { |
| 132 | + console.log('New Lock Up added to user', log); |
| 133 | + } |
| 134 | + }, |
| 135 | + }); |
| 136 | + |
| 137 | + // TODO add blacklistTM method examples |
| 138 | + |
| 139 | + await tickerSecurityTokenInstance.transfer({ to: randomBeneficiary2, value: new BigNumber(50) }); |
| 140 | + |
| 141 | + console.log('50 tokens transferred to randomBeneficiary2'); |
| 142 | + |
| 143 | + tickerSecurityTokenInstance.unsubscribeAll(); |
| 144 | +}); |
0 commit comments