diff options
Diffstat (limited to 'packages/0x.js')
-rw-r--r-- | packages/0x.js/src/types.ts | 8 | ||||
-rw-r--r-- | packages/0x.js/src/web3_wrapper.ts | 13 | ||||
-rw-r--r-- | packages/0x.js/test/token_wrapper_test.ts | 6 |
3 files changed, 21 insertions, 6 deletions
diff --git a/packages/0x.js/src/types.ts b/packages/0x.js/src/types.ts index 69fd3ba03..a8da05205 100644 --- a/packages/0x.js/src/types.ts +++ b/packages/0x.js/src/types.ts @@ -2,9 +2,12 @@ import BigNumber from 'bignumber.js'; import * as Web3 from 'web3'; export enum ZeroExError { - ContractDoesNotExist = 'CONTRACT_DOES_NOT_EXIST', ExchangeContractDoesNotExist = 'EXCHANGE_CONTRACT_DOES_NOT_EXIST', ZRXContractDoesNotExist = 'ZRX_CONTRACT_DOES_NOT_EXIST', + EtherTokenContractDoesNotExist = 'ETHER_TOKEN_CONTRACT_DOES_NOT_EXIST', + TokenTransferProxyContractDoesNotExist = 'TOKEN_TRANSFER_PROXY_CONTRACT_DOES_NOT_EXIST', + TokenRegistryContractDoesNotExist = 'TOKEN_REGISTRY_CONTRACT_DOES_NOT_EXIST', + TokenContractDoesNotExist = 'TOKEN_CONTRACT_DOES_NOT_EXIST', UnhandledError = 'UNHANDLED_ERROR', UserHasNoAssociatedAddress = 'USER_HAS_NO_ASSOCIATED_ADDRESSES', InvalidSignature = 'INVALID_SIGNATURE', @@ -443,7 +446,10 @@ export interface TransactionReceiptWithDecodedLogs extends TransactionReceipt { logs: Array<LogWithDecodedArgs<DecodedLogArgs>|Web3.LogEntry>; } +export type ArtifactContractName = 'ZRX'|'TokenTransferProxy'|'TokenRegistry'|'Token'|'Exchange'|'EtherToken'; + export interface Artifact { + contract_name: ArtifactContractName; abi: Web3.ContractAbi; networks: { [networkId: number]: { diff --git a/packages/0x.js/src/web3_wrapper.ts b/packages/0x.js/src/web3_wrapper.ts index 557cd28e5..a031de486 100644 --- a/packages/0x.js/src/web3_wrapper.ts +++ b/packages/0x.js/src/web3_wrapper.ts @@ -4,7 +4,7 @@ import * as _ from 'lodash'; import * as Web3 from 'web3'; import {Contract} from './contract'; -import {Artifact, TransactionReceipt, ZeroExError} from './types'; +import {Artifact, ArtifactContractName, TransactionReceipt, ZeroExError} from './types'; interface RawLogEntry { logIndex: string|null; @@ -17,6 +17,15 @@ interface RawLogEntry { topics: string[]; } +const CONTRACT_NAME_TO_NOT_FOUND_ERROR: {[contractName: string]: ZeroExError} = { + ZRX: ZeroExError.ZRXContractDoesNotExist, + EtherToken: ZeroExError.EtherTokenContractDoesNotExist, + Token: ZeroExError.TokenContractDoesNotExist, + TokenRegistry: ZeroExError.TokenRegistryContractDoesNotExist, + TokenTransferProxy: ZeroExError.TokenTransferProxyContractDoesNotExist, + Exchange: ZeroExError.ExchangeContractDoesNotExist, +}; + export class Web3Wrapper { private web3: Web3; private networkId: number; @@ -77,7 +86,7 @@ export class Web3Wrapper { } const doesContractExist = await this.doesContractExistAtAddressAsync(contractAddress); if (!doesContractExist) { - throw new Error(ZeroExError.ContractDoesNotExist); + throw new Error(CONTRACT_NAME_TO_NOT_FOUND_ERROR[artifact.contract_name]); } const contractInstance = this.getContractInstance<A>( artifact.abi, contractAddress, diff --git a/packages/0x.js/test/token_wrapper_test.ts b/packages/0x.js/test/token_wrapper_test.ts index abd4243c3..d0ca0144a 100644 --- a/packages/0x.js/test/token_wrapper_test.ts +++ b/packages/0x.js/test/token_wrapper_test.ts @@ -86,7 +86,7 @@ describe('TokenWrapper', () => { const toAddress = coinbase; return expect(zeroEx.token.transferAsync( nonExistentTokenAddress, fromAddress, toAddress, transferAmount, - )).to.be.rejectedWith(ZeroExError.ContractDoesNotExist); + )).to.be.rejectedWith(ZeroExError.TokenContractDoesNotExist); }); }); describe('#transferFromAsync', () => { @@ -159,7 +159,7 @@ describe('TokenWrapper', () => { const nonExistentTokenAddress = '0x9dd402f14d67e001d8efbe6583e51bf9706aa065'; return expect(zeroEx.token.transferFromAsync( nonExistentTokenAddress, fromAddress, toAddress, senderAddress, new BigNumber(42), - )).to.be.rejectedWith(ZeroExError.ContractDoesNotExist); + )).to.be.rejectedWith(ZeroExError.TokenContractDoesNotExist); }); }); describe('#getBalanceAsync', () => { @@ -175,7 +175,7 @@ describe('TokenWrapper', () => { const nonExistentTokenAddress = '0x9dd402f14d67e001d8efbe6583e51bf9706aa065'; const ownerAddress = coinbase; return expect(zeroEx.token.getBalanceAsync(nonExistentTokenAddress, ownerAddress)) - .to.be.rejectedWith(ZeroExError.ContractDoesNotExist); + .to.be.rejectedWith(ZeroExError.TokenContractDoesNotExist); }); it('should return a balance of 0 for a non-existent owner address', async () => { const token = tokens[0]; |