|
| 1 | +import { |
| 2 | + BigNumber, |
| 3 | + ContractAbi, |
| 4 | + BlacklistTransferManager, |
| 5 | + BlacklistTransferManagerContract, |
| 6 | + BlacklistTransferManagerEventArgs, |
| 7 | + BlacklistTransferManagerEvents, |
| 8 | + BlacklistTransferManagerPauseEventArgs, |
| 9 | + BlacklistTransferManagerUnpauseEventArgs, |
| 10 | + BlacklistTransferManagerAddBlacklistTypeEventArgs, |
| 11 | + BlacklistTransferManagerModifyBlacklistTypeEventArgs, |
| 12 | + BlacklistTransferManagerDeleteBlacklistTypeEventArgs, |
| 13 | + BlacklistTransferManagerAddInvestorToBlacklistEventArgs, |
| 14 | + BlacklistTransferManagerDeleteInvestorFromBlacklistEventArgs, |
| 15 | + LogWithDecodedArgs, |
| 16 | + Web3Wrapper, |
| 17 | +} from '@polymathnetwork/abi-wrappers'; |
| 18 | +import { schemas } from '@0x/json-schemas'; |
| 19 | +import assert from '../../../utils/assert'; |
| 20 | +import ModuleWrapper from '../module_wrapper'; |
| 21 | +import ContractFactory from '../../../factories/contractFactory'; |
| 22 | +import { |
| 23 | + EventCallback, |
| 24 | + GetLogs, |
| 25 | + GetLogsAsyncParams, |
| 26 | + Subscribe, |
| 27 | + SubscribeAsyncParams, TransferResult, |
| 28 | + TxParams, |
| 29 | +} from '../../../types'; |
| 30 | +import {parseTransferResult, valueToWei} from '../../../utils/convert'; |
| 31 | + |
| 32 | +interface PauseSubscribeAsyncParams extends SubscribeAsyncParams { |
| 33 | + eventName: BlacklistTransferManagerEvents.Pause; |
| 34 | + callback: EventCallback<BlacklistTransferManagerPauseEventArgs>; |
| 35 | +} |
| 36 | + |
| 37 | +interface GetPauseLogsAsyncParams extends GetLogsAsyncParams { |
| 38 | + eventName: BlacklistTransferManagerEvents.Pause; |
| 39 | +} |
| 40 | + |
| 41 | +interface UnpauseSubscribeAsyncParams extends SubscribeAsyncParams { |
| 42 | + eventName: BlacklistTransferManagerEvents.Unpause; |
| 43 | + callback: EventCallback<BlacklistTransferManagerUnpauseEventArgs>; |
| 44 | +} |
| 45 | + |
| 46 | +interface GetUnpauseLogsAsyncParams extends GetLogsAsyncParams { |
| 47 | + eventName: BlacklistTransferManagerEvents.Unpause; |
| 48 | +} |
| 49 | + |
| 50 | +interface BlacklistTransferManagerSubscribeAsyncParams extends Subscribe { |
| 51 | + (params: PauseSubscribeAsyncParams): Promise<string>; |
| 52 | + (params: UnpauseSubscribeAsyncParams): Promise<string>; |
| 53 | +} |
| 54 | + |
| 55 | +interface GetBlacklistTransferManagerLogsAsyncParams extends GetLogs { |
| 56 | + (params: GetPauseLogsAsyncParams): Promise<LogWithDecodedArgs<BlacklistTransferManagerPauseEventArgs>[]>; |
| 57 | + (params: GetUnpauseLogsAsyncParams): Promise<LogWithDecodedArgs<BlacklistTransferManagerUnpauseEventArgs>[]>; |
| 58 | +} |
| 59 | + |
| 60 | +interface VerifyTransferParams { |
| 61 | + from: string; |
| 62 | + to: string; |
| 63 | + amount: BigNumber; |
| 64 | + data: string; |
| 65 | +} |
| 66 | + |
| 67 | +// Return Types |
| 68 | + |
| 69 | +interface VerifyTransfer { |
| 70 | + transferResult: TransferResult; |
| 71 | + address: string; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * This class includes the functionality related to interacting with the Blacklist Transfer Manager contract. |
| 76 | + */ |
| 77 | +export default class BlacklistTransferManagerWrapper extends ModuleWrapper { |
| 78 | + public abi: ContractAbi = BlacklistTransferManager.abi; |
| 79 | + |
| 80 | + protected contract: Promise<BlacklistTransferManagerContract>; |
| 81 | + |
| 82 | + /** |
| 83 | + * Instantiate BlacklistTransferManagerWrapper |
| 84 | + * @param web3Wrapper Web3Wrapper instance to use |
| 85 | + * @param contract |
| 86 | + * @param contractFactory |
| 87 | + */ |
| 88 | + public constructor( |
| 89 | + web3Wrapper: Web3Wrapper, |
| 90 | + contract: Promise<BlacklistTransferManagerContract>, |
| 91 | + contractFactory: ContractFactory, |
| 92 | + ) { |
| 93 | + super(web3Wrapper, contract, contractFactory); |
| 94 | + this.contract = contract; |
| 95 | + } |
| 96 | + |
| 97 | + public unpause = async (params: TxParams) => { |
| 98 | + assert.assert(await this.paused(), 'Controller not currently paused'); |
| 99 | + assert.assert(await this.isCallerTheSecurityTokenOwner(params.txData), 'Sender is not owner'); |
| 100 | + return (await this.contract).unpause.sendTransactionAsync(params.txData, params.safetyFactor); |
| 101 | + }; |
| 102 | + |
| 103 | + public paused = async () => { |
| 104 | + return (await this.contract).paused.callAsync(); |
| 105 | + }; |
| 106 | + |
| 107 | + public pause = async (params: TxParams) => { |
| 108 | + assert.assert(!(await this.paused()), 'Controller currently paused'); |
| 109 | + assert.assert(await this.isCallerTheSecurityTokenOwner(params.txData), 'Sender is not owner'); |
| 110 | + return (await this.contract).pause.sendTransactionAsync(params.txData, params.safetyFactor); |
| 111 | + }; |
| 112 | + |
| 113 | + /* |
| 114 | + * verifyTransfer |
| 115 | + */ |
| 116 | + public verifyTransfer = async (params: VerifyTransferParams): Promise<VerifyTransfer> => { |
| 117 | + assert.isETHAddressHex('from', params.from); |
| 118 | + assert.isETHAddressHex('to', params.to); |
| 119 | + const decimals = await (await this.securityTokenContract()).decimals.callAsync(); |
| 120 | + const result = await (await this.contract).verifyTransfer.callAsync( |
| 121 | + params.from, |
| 122 | + params.to, |
| 123 | + valueToWei(params.amount, decimals), |
| 124 | + params.data, |
| 125 | + ); |
| 126 | + const transferResult = parseTransferResult(result[0]); |
| 127 | + return { |
| 128 | + transferResult, |
| 129 | + address: result[1], |
| 130 | + }; |
| 131 | + }; |
| 132 | + |
| 133 | + /** |
| 134 | + * Subscribe to an event type emitted by the contract. |
| 135 | + * @return Subscription token used later to unsubscribe |
| 136 | + */ |
| 137 | + public subscribeAsync: BlacklistTransferManagerSubscribeAsyncParams = async < |
| 138 | + ArgsType extends BlacklistTransferManagerEventArgs |
| 139 | + >( |
| 140 | + params: SubscribeAsyncParams, |
| 141 | + ): Promise<string> => { |
| 142 | + assert.doesBelongToStringEnum('eventName', params.eventName, BlacklistTransferManagerEvents); |
| 143 | + assert.doesConformToSchema('indexFilterValues', params.indexFilterValues, schemas.indexFilterValuesSchema); |
| 144 | + assert.isFunction('callback', params.callback); |
| 145 | + const normalizedContractAddress = (await this.contract).address.toLowerCase(); |
| 146 | + const subscriptionToken = this.subscribeInternal<ArgsType>( |
| 147 | + normalizedContractAddress, |
| 148 | + params.eventName, |
| 149 | + params.indexFilterValues, |
| 150 | + BlacklistTransferManager.abi, |
| 151 | + params.callback, |
| 152 | + params.isVerbose, |
| 153 | + ); |
| 154 | + return subscriptionToken; |
| 155 | + }; |
| 156 | + |
| 157 | + /** |
| 158 | + * Gets historical logs without creating a subscription |
| 159 | + * @return Array of logs that match the parameters |
| 160 | + */ |
| 161 | + public getLogsAsync: GetBlacklistTransferManagerLogsAsyncParams = async < |
| 162 | + ArgsType extends BlacklistTransferManagerEventArgs |
| 163 | + >( |
| 164 | + params: GetLogsAsyncParams, |
| 165 | + ): Promise<LogWithDecodedArgs<ArgsType>[]> => { |
| 166 | + assert.doesBelongToStringEnum('eventName', params.eventName, BlacklistTransferManagerEvents); |
| 167 | + assert.doesConformToSchema('blockRange', params.blockRange, schemas.blockRangeSchema); |
| 168 | + assert.doesConformToSchema('indexFilterValues', params.indexFilterValues, schemas.indexFilterValuesSchema); |
| 169 | + const normalizedContractAddress = (await this.contract).address.toLowerCase(); |
| 170 | + const logs = await this.getLogsAsyncInternal<ArgsType>( |
| 171 | + normalizedContractAddress, |
| 172 | + params.eventName, |
| 173 | + params.blockRange, |
| 174 | + params.indexFilterValues, |
| 175 | + BlacklistTransferManager.abi, |
| 176 | + ); |
| 177 | + return logs; |
| 178 | + }; |
| 179 | +} |
0 commit comments