From b6ac132c789939414e2aba911246e6a931facea4 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Wed, 11 Jul 2018 18:53:50 +0200 Subject: Refactor 0x.js to V2 --- packages/0x.js/src/0x.ts | 82 +++++++++++++--------- packages/0x.js/src/artifacts.ts | 7 ++ packages/0x.js/src/index.ts | 52 ++++++++------ .../zero_ex_private_network_config_schema.ts | 6 +- .../zero_ex_public_network_config_schema.ts | 3 +- 5 files changed, 93 insertions(+), 57 deletions(-) create mode 100644 packages/0x.js/src/artifacts.ts (limited to 'packages/0x.js/src') diff --git a/packages/0x.js/src/0x.ts b/packages/0x.js/src/0x.ts index c6315ac7a..597bc07a5 100644 --- a/packages/0x.js/src/0x.ts +++ b/packages/0x.js/src/0x.ts @@ -4,19 +4,22 @@ import { ContractWrappersConfig, ERC20ProxyWrapper, ERC20TokenWrapper, + ERC721ProxyWrapper, + ERC721TokenWrapper, EtherTokenWrapper, ExchangeWrapper, } from '@0xproject/contract-wrappers'; import { + ecSignOrderHashAsync, generatePseudoRandomSalt, - getOrderHashHex, - isValidOrderHash, - isValidSignature, - signOrderHashAsync, + isValidSignatureAsync, + MessagePrefixOpts, + orderHashUtils, } from '@0xproject/order-utils'; -import { ECSignature, Order, Provider, SignedOrder, TransactionReceiptWithDecodedLogs } from '@0xproject/types'; +import { ECSignature, Order, SignedOrder } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import { Provider, TransactionReceiptWithDecodedLogs } from 'ethereum-types'; import { constants } from './utils/constants'; @@ -36,21 +39,28 @@ export class ZeroEx { */ public exchange: ExchangeWrapper; /** + * An instance of the ERC20TokenWrapper class containing methods for interacting with any ERC20 token smart contract. */ + public erc20Token: ERC20TokenWrapper; /** - * An instance of the TokenWrapper class containing methods for interacting with any ERC20 token smart contract. + * An instance of the ERC721TokenWrapper class containing methods for interacting with any ERC721 token smart contract. */ - public token: TokenWrapper; + public erc721Token: ERC721TokenWrapper; /** * An instance of the EtherTokenWrapper class containing methods for interacting with the * wrapped ETH ERC20 token smart contract. */ public etherToken: EtherTokenWrapper; /** - * An instance of the TokenTransferProxyWrapper class containing methods for interacting with the - * tokenTransferProxy smart contract. + * An instance of the ERC20ProxyWrapper class containing methods for interacting with the + * ERC20 proxy smart contract. */ - public proxy: TokenTransferProxyWrapper; + public erc20Proxy: ERC20ProxyWrapper; + /** + * An instance of the ERC721ProxyWrapper class containing methods for interacting with the + * ERC721 proxy smart contract. + */ + public erc721Proxy: ERC721ProxyWrapper; private _contractWrappers: ContractWrappers; /** * Generates a pseudo-random 256-bit salt. @@ -61,24 +71,13 @@ export class ZeroEx { public static generatePseudoRandomSalt(): BigNumber { return generatePseudoRandomSalt(); } - /** - * Verifies that the elliptic curve signature `signature` was generated - * by signing `data` with the private key corresponding to the `signerAddress` address. - * @param data The hex encoded data signed by the supplied signature. - * @param signature An object containing the elliptic curve signature parameters. - * @param signerAddress The hex encoded address that signed the data, producing the supplied signature. - * @return Whether the signature is valid for the supplied signerAddress and data. - */ - public static isValidSignature(data: string, signature: ECSignature, signerAddress: string): boolean { - return isValidSignature(data, signature, signerAddress); - } /** * Computes the orderHash for a supplied order. * @param order An object that conforms to the Order or SignedOrder interface definitions. * @return The resulting orderHash from hashing the supplied order. */ public static getOrderHashHex(order: Order | SignedOrder): string { - return getOrderHashHex(order); + return orderHashUtils.getOrderHashHex(order); } /** * Checks if the supplied hex encoded order hash is valid. @@ -88,7 +87,7 @@ export class ZeroEx { * @return Whether the supplied orderHash has the expected format. */ public static isValidOrderHash(orderHash: string): boolean { - return isValidOrderHash(orderHash); + return orderHashUtils.isValidOrderHash(orderHash); } /** * A unit amount is defined as the amount of a token above the specified decimal places (integer part). @@ -129,11 +128,30 @@ export class ZeroEx { assert.isWeb3Provider('provider', provider); this._contractWrappers = new ContractWrappers(provider, config); - this.proxy = this._contractWrappers.proxy; - this.token = this._contractWrappers.token; + this.erc20Proxy = this._contractWrappers.erc20Proxy; + this.erc721Proxy = this._contractWrappers.erc721Proxy; + this.erc20Token = this._contractWrappers.erc20Token; + this.erc721Token = this._contractWrappers.erc721Token; this.exchange = this._contractWrappers.exchange; this.etherToken = this._contractWrappers.etherToken; } + /** + * Verifies that the provided signature is valid according to the 0x Protocol smart contracts + * @param provider The Web3Provider to use. Some signature types involve checking contract state. + * @param data The hex encoded data signed by the supplied signature. + * @param signature The hex encoded signature. + * @param signerAddress The hex encoded address that signed the data, producing the supplied signature. + * @return Whether the signature is valid for the supplied signerAddress and data. + */ + public async isValidSignatureAsync(data: string, signature: string, signerAddress: string): Promise { + const isValid = await isValidSignatureAsync( + this._contractWrappers.getProvider(), + data, + signature, + signerAddress, + ); + return isValid; + } /** * Sets a new web3 provider for 0x.js. Updating the provider will stop all * subscriptions so you will need to re-subscribe to all events relevant to your app after this call. @@ -166,23 +184,21 @@ export class ZeroEx { * @param orderHash Hex encoded orderHash to sign. * @param signerAddress The hex encoded Ethereum address you wish to sign it with. This address * must be available via the Provider supplied to 0x.js. - * @param shouldAddPersonalMessagePrefix Some signers add the personal message prefix `\x19Ethereum Signed Message` - * themselves (e.g Parity Signer, Ledger, TestRPC) and others expect it to already be done by the client - * (e.g Metamask). Depending on which signer this request is going to, decide on whether to add the prefix - * before sending the request. + * @param MessagePrefixOpts Options regarding the desired prefix and whether to add it before calling `eth_sign` * @return An object containing the Elliptic curve signature parameters generated by signing the orderHash. */ - public async signOrderHashAsync( + public async ecSignOrderHashAsync( orderHash: string, signerAddress: string, - shouldAddPersonalMessagePrefix: boolean, + messagePrefixOpts: MessagePrefixOpts, ): Promise { - return signOrderHashAsync( + const signature = await ecSignOrderHashAsync( this._contractWrappers.getProvider(), orderHash, signerAddress, - shouldAddPersonalMessagePrefix, + messagePrefixOpts, ); + return signature; } /** * Waits for a transaction to be mined and returns the transaction receipt. diff --git a/packages/0x.js/src/artifacts.ts b/packages/0x.js/src/artifacts.ts new file mode 100644 index 000000000..f68969d28 --- /dev/null +++ b/packages/0x.js/src/artifacts.ts @@ -0,0 +1,7 @@ +import { ContractArtifact } from '@0xproject/sol-compiler'; + +import * as ZRXToken from './artifacts/ZRXToken.json'; + +export const artifacts = { + ZRXToken: (ZRXToken as any) as ContractArtifact, +}; diff --git a/packages/0x.js/src/index.ts b/packages/0x.js/src/index.ts index c79dbdb77..6dfc0bacc 100644 --- a/packages/0x.js/src/index.ts +++ b/packages/0x.js/src/index.ts @@ -1,30 +1,34 @@ export { ZeroEx } from './0x'; +export { MessagePrefixType, MessagePrefixOpts } from '@0xproject/order-utils'; + export { - BlockParamLiteral, - FilterObject, - BlockParam, - ContractEventArg, ExchangeContractErrs, - LogWithDecodedArgs, Order, - Provider, SignedOrder, ECSignature, OrderStateValid, OrderStateInvalid, OrderState, Token, +} from '@0xproject/types'; + +export { + BlockParamLiteral, + FilterObject, + BlockParam, + LogWithDecodedArgs, + ContractEventArg, + Provider, TransactionReceipt, TransactionReceiptWithDecodedLogs, -} from '@0xproject/types'; +} from 'ethereum-types'; export { EventCallback, ContractEvent, IndexedFilterValues, BlockRange, - OrderCancellationRequest, OrderFillRequest, ContractEventArgs, MethodOpts, @@ -34,18 +38,24 @@ export { DecodedLogEvent, OnOrderStateChangeCallback, ContractWrappersError, - EtherTokenContractEventArgs, - WithdrawalContractEventArgs, - DepositContractEventArgs, - EtherTokenEvents, - TransferContractEventArgs, - ApprovalContractEventArgs, - TokenContractEventArgs, - TokenEvents, - LogErrorContractEventArgs, - LogCancelContractEventArgs, - LogFillContractEventArgs, - ExchangeContractEventArgs, - ExchangeEvents, + WETH9Events, + WETH9WithdrawalEventArgs, + WETH9ApprovalEventArgs, + WETH9EventArgs, + WETH9DepositEventArgs, + WETH9TransferEventArgs, + ERC20TokenTransferEventArgs, + ERC20TokenApprovalEventArgs, + ERC20TokenEvents, + ERC20TokenEventArgs, + ERC721TokenApprovalEventArgs, + ERC721TokenApprovalForAllEventArgs, + ERC721TokenTransferEventArgs, + ERC721TokenEvents, + ExchangeCancelUpToEventArgs, + ExchangeAssetProxyRegisteredEventArgs, + ExchangeFillEventArgs, + ExchangeCancelEventArgs, + ExchangeEventArgs, ContractWrappersConfig, } from '@0xproject/contract-wrappers'; diff --git a/packages/0x.js/src/schemas/zero_ex_private_network_config_schema.ts b/packages/0x.js/src/schemas/zero_ex_private_network_config_schema.ts index 6036b2695..378b86e77 100644 --- a/packages/0x.js/src/schemas/zero_ex_private_network_config_schema.ts +++ b/packages/0x.js/src/schemas/zero_ex_private_network_config_schema.ts @@ -8,7 +8,8 @@ export const zeroExPrivateNetworkConfigSchema = { gasPrice: { $ref: '/Number' }, zrxContractAddress: { $ref: '/Address' }, exchangeContractAddress: { $ref: '/Address' }, - tokenTransferProxyContractAddress: { $ref: '/Address' }, + erc20ProxyContractAddress: { $ref: '/Address' }, + erc721ProxyContractAddress: { $ref: '/Address' }, orderWatcherConfig: { type: 'object', properties: { @@ -28,6 +29,7 @@ export const zeroExPrivateNetworkConfigSchema = { 'networkId', 'zrxContractAddress', 'exchangeContractAddress', - 'tokenTransferProxyContractAddress', + 'erc20ProxyContractAddress', + 'erc721ProxyContractAddress', ], }; diff --git a/packages/0x.js/src/schemas/zero_ex_public_network_config_schema.ts b/packages/0x.js/src/schemas/zero_ex_public_network_config_schema.ts index 7bedb7438..f2a9a4d56 100644 --- a/packages/0x.js/src/schemas/zero_ex_public_network_config_schema.ts +++ b/packages/0x.js/src/schemas/zero_ex_public_network_config_schema.ts @@ -22,7 +22,8 @@ export const zeroExPublicNetworkConfigSchema = { gasPrice: { $ref: '/Number' }, zrxContractAddress: { $ref: '/Address' }, exchangeContractAddress: { $ref: '/Address' }, - tokenTransferProxyContractAddress: { $ref: '/Address' }, + erc20ProxyContractAddress: { $ref: '/Address' }, + erc721ProxyContractAddress: { $ref: '/Address' }, orderWatcherConfig: { type: 'object', properties: { -- cgit v1.2.3