Refactor ERC20 token transfer proxy and it's tests

This commit is contained in:
Leonid Logvinov
2018-06-27 11:47:14 +03:00
parent a655cd046c
commit 3aef323c13
2 changed files with 27 additions and 29 deletions

View File

@@ -1,75 +1,73 @@
import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { Web3Wrapper } from '@0xproject/web3-wrapper';
import { ContractAbi } from '@0xproject/types'; import { ContractAbi } from 'ethereum-types';
import * as _ from 'lodash'; import * as _ from 'lodash';
import { artifacts } from '../artifacts'; import { artifacts } from '../artifacts';
import { assert } from '../utils/assert'; import { assert } from '../utils/assert';
import { ContractWrapper } from './contract_wrapper'; import { ContractWrapper } from './contract_wrapper';
import { TokenTransferProxyContract } from './generated/token_transfer_proxy'; import { ERC20ProxyContract } from './generated/erc20_proxy';
/** /**
* This class includes the functionality related to interacting with the TokenTransferProxy contract. * This class includes the functionality related to interacting with the ERC20Proxy contract.
*/ */
export class TokenTransferProxyWrapper extends ContractWrapper { export class ERC20ProxyWrapper extends ContractWrapper {
public abi: ContractAbi = artifacts.TokenTransferProxy.abi; public abi: ContractAbi = artifacts.ERC20Proxy.compilerOutput.abi;
private _tokenTransferProxyContractIfExists?: TokenTransferProxyContract; private _erc20ProxyContractIfExists?: ERC20ProxyContract;
private _contractAddressIfExists?: string; private _contractAddressIfExists?: string;
constructor(web3Wrapper: Web3Wrapper, networkId: number, contractAddressIfExists?: string) { constructor(web3Wrapper: Web3Wrapper, networkId: number, contractAddressIfExists?: string) {
super(web3Wrapper, networkId); super(web3Wrapper, networkId);
this._contractAddressIfExists = contractAddressIfExists; this._contractAddressIfExists = contractAddressIfExists;
} }
/** /**
* Check if the Exchange contract address is authorized by the TokenTransferProxy contract. * Check if the Exchange contract address is authorized by the ERC20Proxy contract.
* @param exchangeContractAddress The hex encoded address of the Exchange contract to call. * @param exchangeContractAddress The hex encoded address of the Exchange contract to call.
* @return Whether the exchangeContractAddress is authorized. * @return Whether the exchangeContractAddress is authorized.
*/ */
public async isAuthorizedAsync(exchangeContractAddress: string): Promise<boolean> { public async isAuthorizedAsync(exchangeContractAddress: string): Promise<boolean> {
assert.isETHAddressHex('exchangeContractAddress', exchangeContractAddress); assert.isETHAddressHex('exchangeContractAddress', exchangeContractAddress);
const normalizedExchangeContractAddress = exchangeContractAddress.toLowerCase(); const normalizedExchangeContractAddress = exchangeContractAddress.toLowerCase();
const tokenTransferProxyContractInstance = await this._getTokenTransferProxyContractAsync(); const ERC20ProxyContractInstance = await this._getERC20ProxyContractAsync();
const isAuthorized = await tokenTransferProxyContractInstance.authorized.callAsync( const isAuthorized = await ERC20ProxyContractInstance.authorized.callAsync(normalizedExchangeContractAddress);
normalizedExchangeContractAddress,
);
return isAuthorized; return isAuthorized;
} }
/** /**
* Get the list of all Exchange contract addresses authorized by the TokenTransferProxy contract. * Get the list of all Exchange contract addresses authorized by the ERC20Proxy contract.
* @return The list of authorized addresses. * @return The list of authorized addresses.
*/ */
public async getAuthorizedAddressesAsync(): Promise<string[]> { public async getAuthorizedAddressesAsync(): Promise<string[]> {
const tokenTransferProxyContractInstance = await this._getTokenTransferProxyContractAsync(); const ERC20ProxyContractInstance = await this._getERC20ProxyContractAsync();
const authorizedAddresses = await tokenTransferProxyContractInstance.getAuthorizedAddresses.callAsync(); const authorizedAddresses = await ERC20ProxyContractInstance.getAuthorizedAddresses.callAsync();
return authorizedAddresses; return authorizedAddresses;
} }
/** /**
* Retrieves the Ethereum address of the TokenTransferProxy contract deployed on the network * Retrieves the Ethereum address of the ERC20Proxy contract deployed on the network
* that the user-passed web3 provider is connected to. * that the user-passed web3 provider is connected to.
* @returns The Ethereum address of the TokenTransferProxy contract being used. * @returns The Ethereum address of the ERC20Proxy contract being used.
*/ */
public getContractAddress(): string { public getContractAddress(): string {
const contractAddress = this._getContractAddress(artifacts.TokenTransferProxy, this._contractAddressIfExists); const contractAddress = this._getContractAddress(artifacts.ERC20Proxy, this._contractAddressIfExists);
return contractAddress; return contractAddress;
} }
// tslint:disable-next-line:no-unused-variable // tslint:disable-next-line:no-unused-variable
private _invalidateContractInstance(): void { private _invalidateContractInstance(): void {
delete this._tokenTransferProxyContractIfExists; delete this._erc20ProxyContractIfExists;
} }
private async _getTokenTransferProxyContractAsync(): Promise<TokenTransferProxyContract> { private async _getERC20ProxyContractAsync(): Promise<ERC20ProxyContract> {
if (!_.isUndefined(this._tokenTransferProxyContractIfExists)) { if (!_.isUndefined(this._erc20ProxyContractIfExists)) {
return this._tokenTransferProxyContractIfExists; return this._erc20ProxyContractIfExists;
} }
const [abi, address] = await this._getContractAbiAndAddressFromArtifactsAsync( const [abi, address] = await this._getContractAbiAndAddressFromArtifactsAsync(
artifacts.TokenTransferProxy, artifacts.ERC20Proxy,
this._contractAddressIfExists, this._contractAddressIfExists,
); );
const contractInstance = new TokenTransferProxyContract( const contractInstance = new ERC20ProxyContract(
abi, abi,
address, address,
this._web3Wrapper.getProvider(), this._web3Wrapper.getProvider(),
this._web3Wrapper.getContractDefaults(), this._web3Wrapper.getContractDefaults(),
); );
this._tokenTransferProxyContractIfExists = contractInstance; this._erc20ProxyContractIfExists = contractInstance;
return this._tokenTransferProxyContractIfExists; return this._erc20ProxyContractIfExists;
} }
} }

View File

@@ -9,7 +9,7 @@ import { provider } from './utils/web3_wrapper';
chaiSetup.configure(); chaiSetup.configure();
const expect = chai.expect; const expect = chai.expect;
describe('TokenTransferProxyWrapper', () => { describe('ERC20ProxyWrapper', () => {
let contractWrappers: ContractWrappers; let contractWrappers: ContractWrappers;
const config = { const config = {
networkId: constants.TESTRPC_NETWORK_ID, networkId: constants.TESTRPC_NETWORK_ID,
@@ -19,15 +19,15 @@ describe('TokenTransferProxyWrapper', () => {
}); });
describe('#isAuthorizedAsync', () => { describe('#isAuthorizedAsync', () => {
it('should return false if the address is not authorized', async () => { it('should return false if the address is not authorized', async () => {
const isAuthorized = await contractWrappers.proxy.isAuthorizedAsync(constants.NULL_ADDRESS); const isAuthorized = await contractWrappers.erc20Proxy.isAuthorizedAsync(constants.NULL_ADDRESS);
expect(isAuthorized).to.be.false(); expect(isAuthorized).to.be.false();
}); });
}); });
describe('#getAuthorizedAddressesAsync', () => { describe('#getAuthorizedAddressesAsync', () => {
it('should return the list of authorized addresses', async () => { it('should return the list of authorized addresses', async () => {
const authorizedAddresses = await contractWrappers.proxy.getAuthorizedAddressesAsync(); const authorizedAddresses = await contractWrappers.erc20Proxy.getAuthorizedAddressesAsync();
for (const authorizedAddress of authorizedAddresses) { for (const authorizedAddress of authorizedAddresses) {
const isAuthorized = await contractWrappers.proxy.isAuthorizedAsync(authorizedAddress); const isAuthorized = await contractWrappers.erc20Proxy.isAuthorizedAsync(authorizedAddress);
expect(isAuthorized).to.be.true(); expect(isAuthorized).to.be.true();
} }
}); });