diff options
Diffstat (limited to 'packages/order-utils/src')
27 files changed, 2056 insertions, 0 deletions
diff --git a/packages/order-utils/src/abstract/abstract_balance_and_proxy_allowance_fetcher.ts b/packages/order-utils/src/abstract/abstract_balance_and_proxy_allowance_fetcher.ts new file mode 100644 index 000000000..b2760d98e --- /dev/null +++ b/packages/order-utils/src/abstract/abstract_balance_and_proxy_allowance_fetcher.ts @@ -0,0 +1,6 @@ +import { BigNumber } from '@0xproject/utils'; + +export abstract class AbstractBalanceAndProxyAllowanceFetcher { + public abstract async getBalanceAsync(assetData: string, userAddress: string): Promise<BigNumber>; + public abstract async getProxyAllowanceAsync(assetData: string, userAddress: string): Promise<BigNumber>; +} diff --git a/packages/order-utils/src/abstract/abstract_balance_and_proxy_allowance_lazy_store.ts b/packages/order-utils/src/abstract/abstract_balance_and_proxy_allowance_lazy_store.ts new file mode 100644 index 000000000..38e08b7fe --- /dev/null +++ b/packages/order-utils/src/abstract/abstract_balance_and_proxy_allowance_lazy_store.ts @@ -0,0 +1,11 @@ +import { BigNumber } from '@0xproject/utils'; + +export abstract class AbstractBalanceAndProxyAllowanceLazyStore { + public abstract async getBalanceAsync(assetData: string, userAddress: string): Promise<BigNumber>; + public abstract async getProxyAllowanceAsync(assetData: string, userAddress: string): Promise<BigNumber>; + public abstract setBalance(assetData: string, userAddress: string, balance: BigNumber): void; + public abstract deleteBalance(assetData: string, userAddress: string): void; + public abstract setProxyAllowance(assetData: string, userAddress: string, proxyAllowance: BigNumber): void; + public abstract deleteProxyAllowance(assetData: string, userAddress: string): void; + public abstract deleteAll(): void; +} diff --git a/packages/order-utils/src/abstract/abstract_order_filled_cancelled_fetcher.ts b/packages/order-utils/src/abstract/abstract_order_filled_cancelled_fetcher.ts new file mode 100644 index 000000000..865ea4e43 --- /dev/null +++ b/packages/order-utils/src/abstract/abstract_order_filled_cancelled_fetcher.ts @@ -0,0 +1,7 @@ +import { BigNumber } from '@0xproject/utils'; + +export abstract class AbstractOrderFilledCancelledFetcher { + public abstract async getFilledTakerAmountAsync(orderHash: string): Promise<BigNumber>; + public abstract async isOrderCancelledAsync(orderHash: string): Promise<boolean>; + public abstract getZRXAssetData(): string; +} diff --git a/packages/order-utils/src/abstract/abstract_order_filled_cancelled_lazy_store.ts b/packages/order-utils/src/abstract/abstract_order_filled_cancelled_lazy_store.ts new file mode 100644 index 000000000..617bcb224 --- /dev/null +++ b/packages/order-utils/src/abstract/abstract_order_filled_cancelled_lazy_store.ts @@ -0,0 +1,12 @@ +import { BigNumber } from '@0xproject/utils'; + +export abstract class AbstractOrderFilledCancelledLazyStore { + public abstract async getFilledTakerAmountAsync(orderHash: string): Promise<BigNumber>; + public abstract async getIsCancelledAsync(orderHash: string): Promise<boolean>; + public abstract setFilledTakerAmount(orderHash: string, balance: BigNumber): void; + public abstract deleteFilledTakerAmount(orderHash: string): void; + public abstract setIsCancelled(orderHash: string, isCancelled: boolean): void; + public abstract deleteIsCancelled(orderHash: string): void; + public abstract deleteAll(): void; + public abstract getZRXAssetData(): string; +} diff --git a/packages/order-utils/src/artifacts.ts b/packages/order-utils/src/artifacts.ts new file mode 100644 index 000000000..3d2d1e953 --- /dev/null +++ b/packages/order-utils/src/artifacts.ts @@ -0,0 +1,14 @@ +import { ContractArtifact } from '@0xproject/sol-compiler'; + +import * as DummyERC20Token from './artifacts/DummyERC20Token.json'; +import * as ERC20Proxy from './artifacts/ERC20Proxy.json'; +import * as Exchange from './artifacts/Exchange.json'; +import * as IValidator from './artifacts/IValidator.json'; +import * as IWallet from './artifacts/IWallet.json'; +export const artifacts = { + ERC20Proxy: (ERC20Proxy as any) as ContractArtifact, + DummyERC20Token: (DummyERC20Token as any) as ContractArtifact, + Exchange: (Exchange as any) as ContractArtifact, + IWallet: (IWallet as any) as ContractArtifact, + IValidator: (IValidator as any) as ContractArtifact, +}; diff --git a/packages/order-utils/src/assert.ts b/packages/order-utils/src/assert.ts new file mode 100644 index 000000000..f8db7ac63 --- /dev/null +++ b/packages/order-utils/src/assert.ts @@ -0,0 +1,37 @@ +import { assert as sharedAssert } from '@0xproject/assert'; +// HACK: We need those two unused imports because they're actually used by sharedAssert which gets injected here +// tslint:disable:no-unused-variable +import { Schema } from '@0xproject/json-schemas'; +import { ECSignature, SignatureType } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; +// tslint:enable:no-unused-variable +import * as _ from 'lodash'; + +import { utils } from './utils'; + +export const assert = { + ...sharedAssert, + async isSenderAddressAsync( + variableName: string, + senderAddressHex: string, + web3Wrapper: Web3Wrapper, + ): Promise<void> { + sharedAssert.isETHAddressHex(variableName, senderAddressHex); + const isSenderAddressAvailable = await web3Wrapper.isSenderAddressAvailableAsync(senderAddressHex); + sharedAssert.assert( + isSenderAddressAvailable, + `Specified ${variableName} ${senderAddressHex} isn't available through the supplied web3 provider`, + ); + }, + isOneOfExpectedSignatureTypes(signature: string, signatureTypes: SignatureType[]): void { + sharedAssert.isHexString('signature', signature); + const signatureTypeIndexIfExists = utils.getSignatureTypeIndexIfExists(signature); + const isExpectedSignatureType = _.includes(signatureTypes, signatureTypeIndexIfExists); + if (!isExpectedSignatureType) { + throw new Error( + `Unexpected signatureType: ${signatureTypeIndexIfExists}. Valid signature types: ${signatureTypes}`, + ); + } + }, +}; diff --git a/packages/order-utils/src/asset_data_utils.ts b/packages/order-utils/src/asset_data_utils.ts new file mode 100644 index 000000000..0c0b59548 --- /dev/null +++ b/packages/order-utils/src/asset_data_utils.ts @@ -0,0 +1,139 @@ +import { AssetProxyId, ERC20AssetData, ERC721AssetData } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; +import ethAbi = require('ethereumjs-abi'); +import ethUtil = require('ethereumjs-util'); + +import { constants } from './constants'; + +export const assetDataUtils = { + /** + * Encodes an ERC20 token address into a hex encoded assetData string, usable in the makerAssetData or + * takerAssetData fields in a 0x order. + * @param tokenAddress The ERC20 token address to encode + * @return The hex encoded assetData string + */ + encodeERC20AssetData(tokenAddress: string): string { + return ethUtil.bufferToHex(ethAbi.simpleEncode('ERC20Token(address)', tokenAddress)); + }, + /** + * Decodes an ERC20 assetData hex string into it's corresponding ERC20 tokenAddress & assetProxyId + * @param assetData Hex encoded assetData string to decode + * @return An object containing the decoded tokenAddress & assetProxyId + */ + decodeERC20AssetData(assetData: string): ERC20AssetData { + const data = ethUtil.toBuffer(assetData); + if (data.byteLength < constants.ERC20_ASSET_DATA_BYTE_LENGTH) { + throw new Error( + `Could not decode ERC20 Proxy Data. Expected length of encoded data to be at least ${ + constants.ERC20_ASSET_DATA_BYTE_LENGTH + }. Got ${data.byteLength}`, + ); + } + const assetProxyId = ethUtil.bufferToHex(data.slice(0, constants.SELECTOR_LENGTH)); + if (assetProxyId !== AssetProxyId.ERC20) { + throw new Error( + `Could not decode ERC20 Proxy Data. Expected Asset Proxy Id to be ERC20 (${ + AssetProxyId.ERC20 + }), but got ${assetProxyId}`, + ); + } + const [tokenAddress] = ethAbi.rawDecode(['address'], data.slice(constants.SELECTOR_LENGTH)); + return { + assetProxyId, + tokenAddress: ethUtil.addHexPrefix(tokenAddress), + }; + }, + /** + * Encodes an ERC721 token address into a hex encoded assetData string, usable in the makerAssetData or + * takerAssetData fields in a 0x order. + * @param tokenAddress The ERC721 token address to encode + * @param tokenId The ERC721 tokenId to encode + * @return The hex encoded assetData string + */ + encodeERC721AssetData(tokenAddress: string, tokenId: BigNumber): string { + // TODO: Pass `tokendId` as a BigNumber. + return ethUtil.bufferToHex( + ethAbi.simpleEncode( + 'ERC721Token(address,uint256)', + tokenAddress, + `0x${tokenId.toString(constants.BASE_16)}`, + ), + ); + }, + /** + * Decodes an ERC721 assetData hex string into it's corresponding ERC721 tokenAddress, tokenId & assetProxyId + * @param assetData Hex encoded assetData string to decode + * @return An object containing the decoded tokenAddress, tokenId & assetProxyId + */ + decodeERC721AssetData(assetData: string): ERC721AssetData { + const data = ethUtil.toBuffer(assetData); + if (data.byteLength < constants.ERC721_ASSET_DATA_MINIMUM_BYTE_LENGTH) { + throw new Error( + `Could not decode ERC721 Asset Data. Expected length of encoded data to be at least ${ + constants.ERC721_ASSET_DATA_MINIMUM_BYTE_LENGTH + }. Got ${data.byteLength}`, + ); + } + const assetProxyId = ethUtil.bufferToHex(data.slice(0, constants.SELECTOR_LENGTH)); + if (assetProxyId !== AssetProxyId.ERC721) { + throw new Error( + `Could not decode ERC721 Asset Data. Expected Asset Proxy Id to be ERC721 (${ + AssetProxyId.ERC721 + }), but got ${assetProxyId}`, + ); + } + const [tokenAddress, tokenId] = ethAbi.rawDecode(['address', 'uint256'], data.slice(constants.SELECTOR_LENGTH)); + return { + assetProxyId, + tokenAddress: ethUtil.addHexPrefix(tokenAddress), + tokenId: new BigNumber(tokenId.toString()), + }; + }, + /** + * Decode and return the assetProxyId from the assetData + * @param assetData Hex encoded assetData string to decode + * @return The assetProxyId + */ + decodeAssetProxyId(assetData: string): AssetProxyId { + const encodedAssetData = ethUtil.toBuffer(assetData); + if (encodedAssetData.byteLength < constants.SELECTOR_LENGTH) { + throw new Error( + `Could not decode assetData. Expected length of encoded data to be at least 4. Got ${ + encodedAssetData.byteLength + }`, + ); + } + const encodedAssetProxyId = encodedAssetData.slice(0, constants.SELECTOR_LENGTH); + const assetProxyId = decodeAssetProxyId(encodedAssetProxyId); + return assetProxyId; + }, + /** + * Decode any assetData into it's corresponding assetData object + * @param assetData Hex encoded assetData string to decode + * @return Either a ERC20 or ERC721 assetData object + */ + decodeAssetDataOrThrow(assetData: string): ERC20AssetData | ERC721AssetData { + const assetProxyId = assetDataUtils.decodeAssetProxyId(assetData); + switch (assetProxyId) { + case AssetProxyId.ERC20: + const erc20AssetData = assetDataUtils.decodeERC20AssetData(assetData); + return erc20AssetData; + case AssetProxyId.ERC721: + const erc721AssetData = assetDataUtils.decodeERC721AssetData(assetData); + return erc721AssetData; + default: + throw new Error(`Unrecognized asset proxy id: ${assetProxyId}`); + } + }, +}; + +function decodeAssetProxyId(encodedAssetProxyId: Buffer): AssetProxyId { + const hexString = ethUtil.bufferToHex(encodedAssetProxyId); + if (hexString === AssetProxyId.ERC20) { + return AssetProxyId.ERC20; + } + if (hexString === AssetProxyId.ERC721) { + return AssetProxyId.ERC721; + } + throw new Error(`Invalid ProxyId: ${hexString}`); +} diff --git a/packages/order-utils/src/constants.ts b/packages/order-utils/src/constants.ts new file mode 100644 index 000000000..c23578c20 --- /dev/null +++ b/packages/order-utils/src/constants.ts @@ -0,0 +1,16 @@ +import { BigNumber } from '@0xproject/utils'; + +export const constants = { + NULL_ADDRESS: '0x0000000000000000000000000000000000000000', + NULL_BYTES: '0x', + // tslint:disable-next-line:custom-no-magic-numbers + UNLIMITED_ALLOWANCE_IN_BASE_UNITS: new BigNumber(2).pow(256).minus(1), + TESTRPC_NETWORK_ID: 50, + ADDRESS_LENGTH: 20, + ERC20_ASSET_DATA_BYTE_LENGTH: 36, + ERC721_ASSET_DATA_MINIMUM_BYTE_LENGTH: 53, + SELECTOR_LENGTH: 4, + BASE_16: 16, + INFINITE_TIMESTAMP_SEC: new BigNumber(2524604400), // Close to infinite + ZERO_AMOUNT: new BigNumber(0), +}; diff --git a/packages/order-utils/src/crypto.ts b/packages/order-utils/src/crypto.ts new file mode 100644 index 000000000..0f1504a72 --- /dev/null +++ b/packages/order-utils/src/crypto.ts @@ -0,0 +1,46 @@ +import BN = require('bn.js'); +import ABI = require('ethereumjs-abi'); +import ethUtil = require('ethereumjs-util'); +import * as _ from 'lodash'; + +export const crypto = { + /** + * We convert types from JS to Solidity as follows: + * BigNumber -> uint256 + * number -> uint8 + * string -> string + * boolean -> bool + * valid Ethereum address -> address + */ + solSHA3(args: any[]): Buffer { + return crypto._solHash(args, ABI.soliditySHA3); + }, + solSHA256(args: any[]): Buffer { + return crypto._solHash(args, ABI.soliditySHA256); + }, + _solHash(args: any[], hashFunction: (types: string[], values: any[]) => Buffer): Buffer { + const argTypes: string[] = []; + _.each(args, (arg, i) => { + const isNumber = _.isFinite(arg); + if (isNumber) { + argTypes.push('uint8'); + } else if (arg.isBigNumber) { + argTypes.push('uint256'); + const base = 10; + args[i] = new BN(arg.toString(base), base); + } else if (ethUtil.isValidAddress(arg)) { + argTypes.push('address'); + } else if (_.isString(arg)) { + argTypes.push('string'); + } else if (_.isBuffer(arg) || _.isTypedArray(arg)) { + argTypes.push('bytes'); + } else if (_.isBoolean(arg)) { + argTypes.push('bool'); + } else { + throw new Error(`Unable to guess arg type: ${arg}`); + } + }); + const hash = hashFunction(argTypes, args); + return hash; + }, +}; diff --git a/packages/order-utils/src/eip712_utils.ts b/packages/order-utils/src/eip712_utils.ts new file mode 100644 index 000000000..2594e6d6d --- /dev/null +++ b/packages/order-utils/src/eip712_utils.ts @@ -0,0 +1,93 @@ +import ethUtil = require('ethereumjs-util'); +import * as _ from 'lodash'; + +import { crypto } from './crypto'; +import { EIP712Schema, EIP712Types } from './types'; + +const EIP191_PREFIX = '\x19\x01'; +const EIP712_DOMAIN_NAME = '0x Protocol'; +const EIP712_DOMAIN_VERSION = '2'; +const EIP712_VALUE_LENGTH = 32; + +const EIP712_DOMAIN_SCHEMA: EIP712Schema = { + name: 'EIP712Domain', + parameters: [ + { name: 'name', type: EIP712Types.String }, + { name: 'version', type: EIP712Types.String }, + { name: 'verifyingContract', type: EIP712Types.Address }, + ], +}; + +export const EIP712Utils = { + /** + * Compiles the EIP712Schema and returns the hash of the schema. + * @param schema The EIP712 schema. + * @return The hash of the compiled schema + */ + compileSchema(schema: EIP712Schema): Buffer { + const eip712Schema = EIP712Utils._encodeType(schema); + const eip712SchemaHashBuffer = crypto.solSHA3([eip712Schema]); + return eip712SchemaHashBuffer; + }, + /** + * Merges the EIP712 hash of a struct with the DomainSeparator for 0x v2. + * @param hashStruct the EIP712 hash of a struct + * @param contractAddress the exchange contract address + * @return The hash of an EIP712 message with domain separator prefixed + */ + createEIP712Message(hashStruct: Buffer, contractAddress: string): Buffer { + const domainSeparatorHashBuffer = EIP712Utils._getDomainSeparatorHashBuffer(contractAddress); + const messageBuff = crypto.solSHA3([EIP191_PREFIX, domainSeparatorHashBuffer, hashStruct]); + return messageBuff; + }, + pad32Address(address: string): Buffer { + const addressBuffer = ethUtil.toBuffer(address); + const addressPadded = EIP712Utils.pad32Buffer(addressBuffer); + return addressPadded; + }, + pad32Buffer(buffer: Buffer): Buffer { + const bufferPadded = ethUtil.setLengthLeft(buffer, EIP712_VALUE_LENGTH); + return bufferPadded; + }, + _getDomainSeparatorSchemaBuffer(): Buffer { + return EIP712Utils.compileSchema(EIP712_DOMAIN_SCHEMA); + }, + _getDomainSeparatorHashBuffer(exchangeAddress: string): Buffer { + const domainSeparatorSchemaBuffer = EIP712Utils._getDomainSeparatorSchemaBuffer(); + const encodedData = EIP712Utils._encodeData(EIP712_DOMAIN_SCHEMA, { + name: EIP712_DOMAIN_NAME, + version: EIP712_DOMAIN_VERSION, + verifyingContract: exchangeAddress, + }); + const domainSeparatorHashBuff2 = crypto.solSHA3([domainSeparatorSchemaBuffer, ...encodedData]); + return domainSeparatorHashBuff2; + }, + _encodeType(schema: EIP712Schema): string { + const namedTypes = _.map(schema.parameters, ({ name, type }) => `${type} ${name}`); + const namedTypesJoined = namedTypes.join(','); + const encodedType = `${schema.name}(${namedTypesJoined})`; + return encodedType; + }, + _encodeData(schema: EIP712Schema, data: { [key: string]: any }): any { + const encodedValues = []; + for (const parameter of schema.parameters) { + const value = data[parameter.name]; + if (parameter.type === EIP712Types.String || parameter.type === EIP712Types.Bytes) { + encodedValues.push(crypto.solSHA3([ethUtil.toBuffer(value)])); + } else if (parameter.type === EIP712Types.Uint256) { + encodedValues.push(value); + } else if (parameter.type === EIP712Types.Address) { + encodedValues.push(EIP712Utils.pad32Address(value)); + } else { + throw new Error(`Unable to encode ${parameter.type}`); + } + } + return encodedValues; + }, + structHash(schema: EIP712Schema, data: { [key: string]: any }): Buffer { + const encodedData = EIP712Utils._encodeData(schema, data); + const schemaHash = EIP712Utils.compileSchema(schema); + const hashBuffer = crypto.solSHA3([schemaHash, ...encodedData]); + return hashBuffer; + }, +}; diff --git a/packages/order-utils/src/exchange_transfer_simulator.ts b/packages/order-utils/src/exchange_transfer_simulator.ts new file mode 100644 index 000000000..c3a4f9c2a --- /dev/null +++ b/packages/order-utils/src/exchange_transfer_simulator.ts @@ -0,0 +1,116 @@ +import { ExchangeContractErrs } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; + +import { AbstractBalanceAndProxyAllowanceLazyStore } from './abstract/abstract_balance_and_proxy_allowance_lazy_store'; +import { constants } from './constants'; +import { TradeSide, TransferType } from './types'; + +enum FailureReason { + Balance = 'balance', + ProxyAllowance = 'proxyAllowance', +} + +const ERR_MSG_MAPPING = { + [FailureReason.Balance]: { + [TradeSide.Maker]: { + [TransferType.Trade]: ExchangeContractErrs.InsufficientMakerBalance, + [TransferType.Fee]: ExchangeContractErrs.InsufficientMakerFeeBalance, + }, + [TradeSide.Taker]: { + [TransferType.Trade]: ExchangeContractErrs.InsufficientTakerBalance, + [TransferType.Fee]: ExchangeContractErrs.InsufficientTakerFeeBalance, + }, + }, + [FailureReason.ProxyAllowance]: { + [TradeSide.Maker]: { + [TransferType.Trade]: ExchangeContractErrs.InsufficientMakerAllowance, + [TransferType.Fee]: ExchangeContractErrs.InsufficientMakerFeeAllowance, + }, + [TradeSide.Taker]: { + [TransferType.Trade]: ExchangeContractErrs.InsufficientTakerAllowance, + [TransferType.Fee]: ExchangeContractErrs.InsufficientTakerFeeAllowance, + }, + }, +}; + +export class ExchangeTransferSimulator { + private readonly _store: AbstractBalanceAndProxyAllowanceLazyStore; + private static _throwValidationError( + failureReason: FailureReason, + tradeSide: TradeSide, + transferType: TransferType, + ): never { + const errMsg = ERR_MSG_MAPPING[failureReason][tradeSide][transferType]; + throw new Error(errMsg); + } + constructor(store: AbstractBalanceAndProxyAllowanceLazyStore) { + this._store = store; + } + /** + * Simulates transferFrom call performed by a proxy + * @param assetData Data of the asset being transferred. Includes + * it's identifying information and assetType, + * e.g address for ERC20, address & tokenId for ERC721 + * @param from Owner of the transferred tokens + * @param to Recipient of the transferred tokens + * @param amountInBaseUnits The amount of tokens being transferred + * @param tradeSide Is Maker/Taker transferring + * @param transferType Is it a fee payment or a value transfer + */ + public async transferFromAsync( + assetData: string, + from: string, + to: string, + amountInBaseUnits: BigNumber, + tradeSide: TradeSide, + transferType: TransferType, + ): Promise<void> { + // HACK: When simulating an open order (e.g taker is NULL_ADDRESS), we don't want to adjust balances/ + // allowances for the taker. We do however, want to increase the balance of the maker since the maker + // might be relying on those funds to fill subsequent orders or pay the order's fees. + if (from === constants.NULL_ADDRESS && tradeSide === TradeSide.Taker) { + await this._increaseBalanceAsync(assetData, to, amountInBaseUnits); + return; + } + const balance = await this._store.getBalanceAsync(assetData, from); + const proxyAllowance = await this._store.getProxyAllowanceAsync(assetData, from); + if (proxyAllowance.lessThan(amountInBaseUnits)) { + ExchangeTransferSimulator._throwValidationError(FailureReason.ProxyAllowance, tradeSide, transferType); + } + if (balance.lessThan(amountInBaseUnits)) { + ExchangeTransferSimulator._throwValidationError(FailureReason.Balance, tradeSide, transferType); + } + await this._decreaseProxyAllowanceAsync(assetData, from, amountInBaseUnits); + await this._decreaseBalanceAsync(assetData, from, amountInBaseUnits); + await this._increaseBalanceAsync(assetData, to, amountInBaseUnits); + } + private async _decreaseProxyAllowanceAsync( + assetData: string, + userAddress: string, + amountInBaseUnits: BigNumber, + ): Promise<void> { + const proxyAllowance = await this._store.getProxyAllowanceAsync(assetData, userAddress); + // HACK: This code assumes that all tokens with an UNLIMITED_ALLOWANCE_IN_BASE_UNITS set, + // are UnlimitedAllowanceTokens. This is however not true, it just so happens that all + // DummyERC20Tokens we use in tests are. + if (!proxyAllowance.eq(constants.UNLIMITED_ALLOWANCE_IN_BASE_UNITS)) { + this._store.setProxyAllowance(assetData, userAddress, proxyAllowance.minus(amountInBaseUnits)); + } + } + private async _increaseBalanceAsync( + assetData: string, + userAddress: string, + amountInBaseUnits: BigNumber, + ): Promise<void> { + const balance = await this._store.getBalanceAsync(assetData, userAddress); + this._store.setBalance(assetData, userAddress, balance.plus(amountInBaseUnits)); + } + private async _decreaseBalanceAsync( + assetData: string, + userAddress: string, + amountInBaseUnits: BigNumber, + ): Promise<void> { + const balance = await this._store.getBalanceAsync(assetData, userAddress); + this._store.setBalance(assetData, userAddress, balance.minus(amountInBaseUnits)); + } +} diff --git a/packages/order-utils/src/globals.d.ts b/packages/order-utils/src/globals.d.ts new file mode 100644 index 000000000..94e63a32d --- /dev/null +++ b/packages/order-utils/src/globals.d.ts @@ -0,0 +1,6 @@ +declare module '*.json' { + const json: any; + /* tslint:disable */ + export default json; + /* tslint:enable */ +} diff --git a/packages/order-utils/src/index.ts b/packages/order-utils/src/index.ts new file mode 100644 index 000000000..681fbc904 --- /dev/null +++ b/packages/order-utils/src/index.ts @@ -0,0 +1,27 @@ +export { orderHashUtils } from './order_hash'; +export { + isValidSignatureAsync, + isValidPresignedSignatureAsync, + isValidWalletSignatureAsync, + isValidValidatorSignatureAsync, + isValidECSignature, + ecSignOrderHashAsync, + addSignedMessagePrefix, + parseECSignature, +} from './signature_utils'; +export { orderFactory } from './order_factory'; +export { constants } from './constants'; +export { crypto } from './crypto'; +export { generatePseudoRandomSalt } from './salt'; +export { CreateOrderOpts, OrderError, EIP712Parameter, EIP712Schema, EIP712Types } from './types'; +export { AbstractBalanceAndProxyAllowanceFetcher } from './abstract/abstract_balance_and_proxy_allowance_fetcher'; +export { AbstractOrderFilledCancelledFetcher } from './abstract/abstract_order_filled_cancelled_fetcher'; +export { BalanceAndProxyAllowanceLazyStore } from './store/balance_and_proxy_allowance_lazy_store'; +export { OrderFilledCancelledLazyStore } from './store/order_filled_cancelled_lazy_store'; +export { RemainingFillableCalculator } from './remaining_fillable_calculator'; +export { OrderStateUtils } from './order_state_utils'; +export { assetDataUtils } from './asset_data_utils'; +export { EIP712Utils } from './eip712_utils'; +export { OrderValidationUtils } from './order_validation_utils'; +export { ExchangeTransferSimulator } from './exchange_transfer_simulator'; +export { marketUtils } from './market_utils'; diff --git a/packages/order-utils/src/market_utils.ts b/packages/order-utils/src/market_utils.ts new file mode 100644 index 000000000..681059ddf --- /dev/null +++ b/packages/order-utils/src/market_utils.ts @@ -0,0 +1,133 @@ +import { schemas } from '@0xproject/json-schemas'; +import { SignedOrder } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; +import * as _ from 'lodash'; + +import { assert } from './assert'; +import { constants } from './constants'; + +export const marketUtils = { + /** + * Takes an array of orders and returns a subset of those orders that has enough makerAssetAmount (taking into account on-chain balances, + * allowances, and partial fills) in order to fill the input makerAssetFillAmount plus slippageBufferAmount. Iterates from first order to last. + * Sort the input by ascending rate in order to get the subset of orders that will cost the least ETH. + * @param signedOrders An array of objects that conform to the SignedOrder interface. All orders should specify the same makerAsset. + * All orders should specify WETH as the takerAsset. + * @param remainingFillableMakerAssetAmounts An array of BigNumbers corresponding to the signedOrders parameter. + * You can use OrderStateUtils @0xproject/order-utils to perform blockchain lookups + * for these values. + * @param makerAssetFillAmount The amount of makerAsset desired to be filled. + * @param slippageBufferAmount An additional amount of makerAsset to be covered by the result in case of trade collisions or partial fills. + * @return Resulting orders and remaining fill amount that could not be covered by the input. + */ + findOrdersThatCoverMakerAssetFillAmount( + signedOrders: SignedOrder[], + remainingFillableMakerAssetAmounts: BigNumber[], + makerAssetFillAmount: BigNumber, + slippageBufferAmount: BigNumber = constants.ZERO_AMOUNT, + ): { resultOrders: SignedOrder[]; remainingFillAmount: BigNumber } { + assert.doesConformToSchema('signedOrders', signedOrders, schemas.signedOrdersSchema); + _.forEach(remainingFillableMakerAssetAmounts, (amount, index) => + assert.isValidBaseUnitAmount(`remainingFillableMakerAssetAmount[${index}]`, amount), + ); + assert.isValidBaseUnitAmount('makerAssetFillAmount', makerAssetFillAmount); + assert.isValidBaseUnitAmount('slippageBufferAmount', slippageBufferAmount); + assert.assert( + signedOrders.length === remainingFillableMakerAssetAmounts.length, + 'Expected signedOrders.length to equal remainingFillableMakerAssetAmounts.length', + ); + // calculate total amount of makerAsset needed to be filled + const totalFillAmount = makerAssetFillAmount.plus(slippageBufferAmount); + // iterate through the signedOrders input from left to right until we have enough makerAsset to fill totalFillAmount + const result = _.reduce( + signedOrders, + ({ resultOrders, remainingFillAmount }, order, index) => { + if (remainingFillAmount.lessThanOrEqualTo(constants.ZERO_AMOUNT)) { + return { resultOrders, remainingFillAmount: constants.ZERO_AMOUNT }; + } else { + const makerAssetAmountAvailable = remainingFillableMakerAssetAmounts[index]; + // if there is no makerAssetAmountAvailable do not append order to resultOrders + // if we have exceeded the total amount we want to fill set remainingFillAmount to 0 + return { + resultOrders: makerAssetAmountAvailable.gt(constants.ZERO_AMOUNT) + ? _.concat(resultOrders, order) + : resultOrders, + remainingFillAmount: BigNumber.max( + constants.ZERO_AMOUNT, + remainingFillAmount.minus(makerAssetAmountAvailable), + ), + }; + } + }, + { resultOrders: [] as SignedOrder[], remainingFillAmount: totalFillAmount }, + ); + return result; + }, + /** + * Takes an array of orders and an array of feeOrders. Returns a subset of the feeOrders that has enough ZRX (taking into account + * on-chain balances, allowances, and partial fills) in order to fill the takerFees required by signedOrders plus a + * slippageBufferAmount. Iterates from first feeOrder to last. Sort the feeOrders by ascending rate in order to get the subset of + * feeOrders that will cost the least ETH. + * @param signedOrders An array of objects that conform to the SignedOrder interface. All orders should specify ZRX as + * the makerAsset and WETH as the takerAsset. + * @param remainingFillableMakerAssetAmounts An array of BigNumbers corresponding to the signedOrders parameter. + * You can use OrderStateUtils @0xproject/order-utils to perform blockchain lookups + * for these values. + * @param signedFeeOrders An array of objects that conform to the SignedOrder interface. All orders should specify ZRX as + * the makerAsset and WETH as the takerAsset. + * @param remainingFillableFeeAmounts An array of BigNumbers corresponding to the signedFeeOrders parameter. + * You can use OrderStateUtils @0xproject/order-utils to perform blockchain lookups + * for these values. + * @param slippageBufferAmount An additional amount of fee to be covered by the result in case of trade collisions or partial fills. + * @return Resulting orders and remaining fee amount that could not be covered by the input. + */ + findFeeOrdersThatCoverFeesForTargetOrders( + signedOrders: SignedOrder[], + remainingFillableMakerAssetAmounts: BigNumber[], + signedFeeOrders: SignedOrder[], + remainingFillableFeeAmounts: BigNumber[], + slippageBufferAmount: BigNumber = constants.ZERO_AMOUNT, + ): { resultOrders: SignedOrder[]; remainingFeeAmount: BigNumber } { + assert.doesConformToSchema('signedOrders', signedOrders, schemas.signedOrdersSchema); + _.forEach(remainingFillableMakerAssetAmounts, (amount, index) => + assert.isValidBaseUnitAmount(`remainingFillableMakerAssetAmount[${index}]`, amount), + ); + assert.doesConformToSchema('signedFeeOrders', signedFeeOrders, schemas.signedOrdersSchema); + _.forEach(remainingFillableFeeAmounts, (amount, index) => + assert.isValidBaseUnitAmount(`remainingFillableFeeAmounts[${index}]`, amount), + ); + assert.isValidBaseUnitAmount('slippageBufferAmount', slippageBufferAmount); + assert.assert( + signedOrders.length === remainingFillableMakerAssetAmounts.length, + 'Expected signedOrders.length to equal remainingFillableMakerAssetAmounts.length', + ); + assert.assert( + signedOrders.length === remainingFillableMakerAssetAmounts.length, + 'Expected signedFeeOrders.length to equal remainingFillableFeeAmounts.length', + ); + // calculate total amount of ZRX needed to fill signedOrders + const totalFeeAmount = _.reduce( + signedOrders, + (accFees, order, index) => { + const makerAssetAmountAvailable = remainingFillableMakerAssetAmounts[index]; + const feeToFillMakerAssetAmountAvailable = makerAssetAmountAvailable + .mul(order.takerFee) + .div(order.makerAssetAmount); + return accFees.plus(feeToFillMakerAssetAmountAvailable); + }, + constants.ZERO_AMOUNT, + ); + const { resultOrders, remainingFillAmount } = marketUtils.findOrdersThatCoverMakerAssetFillAmount( + signedFeeOrders, + remainingFillableFeeAmounts, + totalFeeAmount, + slippageBufferAmount, + ); + return { + resultOrders, + remainingFeeAmount: remainingFillAmount, + }; + // TODO: add more orders here to cover rounding + // https://github.com/0xProject/0x-protocol-specification/blob/master/v2/forwarding-contract-specification.md#over-buying-zrx + }, +}; diff --git a/packages/order-utils/src/monorepo_scripts/postpublish.ts b/packages/order-utils/src/monorepo_scripts/postpublish.ts new file mode 100644 index 000000000..dcb99d0f7 --- /dev/null +++ b/packages/order-utils/src/monorepo_scripts/postpublish.ts @@ -0,0 +1,8 @@ +import { postpublishUtils } from '@0xproject/monorepo-scripts'; + +import * as packageJSON from '../package.json'; +import * as tsConfigJSON from '../tsconfig.json'; + +const cwd = `${__dirname}/..`; +// tslint:disable-next-line:no-floating-promises +postpublishUtils.runAsync(packageJSON, tsConfigJSON, cwd); diff --git a/packages/order-utils/src/monorepo_scripts/stage_docs.ts b/packages/order-utils/src/monorepo_scripts/stage_docs.ts new file mode 100644 index 000000000..e732ac8eb --- /dev/null +++ b/packages/order-utils/src/monorepo_scripts/stage_docs.ts @@ -0,0 +1,8 @@ +import { postpublishUtils } from '@0xproject/monorepo-scripts'; + +import * as packageJSON from '../package.json'; +import * as tsConfigJSON from '../tsconfig.json'; + +const cwd = `${__dirname}/..`; +// tslint:disable-next-line:no-floating-promises +postpublishUtils.publishDocsToStagingAsync(packageJSON, tsConfigJSON, cwd); diff --git a/packages/order-utils/src/order_factory.ts b/packages/order-utils/src/order_factory.ts new file mode 100644 index 000000000..4a6f3924b --- /dev/null +++ b/packages/order-utils/src/order_factory.ts @@ -0,0 +1,85 @@ +import { Order, SignedOrder, SignerType } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; +import { Provider } from 'ethereum-types'; +import * as _ from 'lodash'; + +import { constants } from './constants'; +import { orderHashUtils } from './order_hash'; +import { generatePseudoRandomSalt } from './salt'; +import { ecSignOrderHashAsync } from './signature_utils'; +import { CreateOrderOpts } from './types'; + +export const orderFactory = { + createOrder( + makerAddress: string, + makerAssetAmount: BigNumber, + makerAssetData: string, + takerAssetAmount: BigNumber, + takerAssetData: string, + exchangeAddress: string, + createOrderOpts: CreateOrderOpts = generateDefaultCreateOrderOpts(), + ): Order { + const defaultCreateOrderOpts = generateDefaultCreateOrderOpts(); + const order = { + makerAddress, + makerAssetAmount, + takerAssetAmount, + makerAssetData, + takerAssetData, + exchangeAddress, + takerAddress: createOrderOpts.takerAddress || defaultCreateOrderOpts.takerAddress, + senderAddress: createOrderOpts.senderAddress || defaultCreateOrderOpts.senderAddress, + makerFee: createOrderOpts.makerFee || defaultCreateOrderOpts.makerFee, + takerFee: createOrderOpts.takerFee || defaultCreateOrderOpts.takerFee, + feeRecipientAddress: createOrderOpts.feeRecipientAddress || defaultCreateOrderOpts.feeRecipientAddress, + salt: createOrderOpts.salt || defaultCreateOrderOpts.salt, + expirationTimeSeconds: + createOrderOpts.expirationTimeSeconds || defaultCreateOrderOpts.expirationTimeSeconds, + }; + return order; + }, + async createSignedOrderAsync( + provider: Provider, + makerAddress: string, + makerAssetAmount: BigNumber, + makerAssetData: string, + takerAssetAmount: BigNumber, + takerAssetData: string, + exchangeAddress: string, + createOrderOpts?: CreateOrderOpts, + ): Promise<SignedOrder> { + const order = orderFactory.createOrder( + makerAddress, + makerAssetAmount, + makerAssetData, + takerAssetAmount, + takerAssetData, + exchangeAddress, + createOrderOpts, + ); + const orderHash = orderHashUtils.getOrderHashHex(order); + const signature = await ecSignOrderHashAsync(provider, orderHash, makerAddress, SignerType.Default); + const signedOrder: SignedOrder = _.assign(order, { signature }); + return signedOrder; + }, +}; + +function generateDefaultCreateOrderOpts(): { + takerAddress: string; + senderAddress: string; + makerFee: BigNumber; + takerFee: BigNumber; + feeRecipientAddress: string; + salt: BigNumber; + expirationTimeSeconds: BigNumber; +} { + return { + takerAddress: constants.NULL_ADDRESS, + senderAddress: constants.NULL_ADDRESS, + makerFee: constants.ZERO_AMOUNT, + takerFee: constants.ZERO_AMOUNT, + feeRecipientAddress: constants.NULL_ADDRESS, + salt: generatePseudoRandomSalt(), + expirationTimeSeconds: constants.INFINITE_TIMESTAMP_SEC, + }; +} diff --git a/packages/order-utils/src/order_hash.ts b/packages/order-utils/src/order_hash.ts new file mode 100644 index 000000000..54c500653 --- /dev/null +++ b/packages/order-utils/src/order_hash.ts @@ -0,0 +1,79 @@ +import { schemas, SchemaValidator } from '@0xproject/json-schemas'; +import { Order, SignedOrder } from '@0xproject/types'; +import * as _ from 'lodash'; + +import { assert } from './assert'; +import { EIP712Utils } from './eip712_utils'; +import { EIP712Schema, EIP712Types } from './types'; + +const INVALID_TAKER_FORMAT = 'instance.takerAddress is not of a type(s) string'; + +const EIP712_ORDER_SCHEMA: EIP712Schema = { + name: 'Order', + parameters: [ + { name: 'makerAddress', type: EIP712Types.Address }, + { name: 'takerAddress', type: EIP712Types.Address }, + { name: 'feeRecipientAddress', type: EIP712Types.Address }, + { name: 'senderAddress', type: EIP712Types.Address }, + { name: 'makerAssetAmount', type: EIP712Types.Uint256 }, + { name: 'takerAssetAmount', type: EIP712Types.Uint256 }, + { name: 'makerFee', type: EIP712Types.Uint256 }, + { name: 'takerFee', type: EIP712Types.Uint256 }, + { name: 'expirationTimeSeconds', type: EIP712Types.Uint256 }, + { name: 'salt', type: EIP712Types.Uint256 }, + { name: 'makerAssetData', type: EIP712Types.Bytes }, + { name: 'takerAssetData', type: EIP712Types.Bytes }, + ], +}; + +export const orderHashUtils = { + /** + * Checks if the supplied hex encoded order hash is valid. + * Note: Valid means it has the expected format, not that an order with the orderHash exists. + * Use this method when processing orderHashes submitted as user input. + * @param orderHash Hex encoded orderHash. + * @return Whether the supplied orderHash has the expected format. + */ + isValidOrderHash(orderHash: string): boolean { + // Since this method can be called to check if any arbitrary string conforms to an orderHash's + // format, we only assert that we were indeed passed a string. + assert.isString('orderHash', orderHash); + const schemaValidator = new SchemaValidator(); + const isValid = schemaValidator.validate(orderHash, schemas.orderHashSchema).valid; + return isValid; + }, + /** + * 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. + */ + getOrderHashHex(order: SignedOrder | Order): string { + try { + assert.doesConformToSchema('order', order, schemas.orderSchema, [schemas.hexSchema]); + } catch (error) { + if (_.includes(error.message, INVALID_TAKER_FORMAT)) { + const errMsg = + 'Order taker must be of type string. If you want anyone to be able to fill an order - pass ZeroEx.NULL_ADDRESS'; + throw new Error(errMsg); + } + throw error; + } + + const orderHashBuff = orderHashUtils.getOrderHashBuffer(order); + const orderHashHex = `0x${orderHashBuff.toString('hex')}`; + return orderHashHex; + }, + /** + * Computes the orderHash for a supplied order and returns it as a Buffer + * @param order An object that conforms to the Order or SignedOrder interface definitions. + * @return The resulting orderHash from hashing the supplied order as a Buffer + */ + getOrderHashBuffer(order: SignedOrder | Order): Buffer { + const orderParamsHashBuff = EIP712Utils.structHash(EIP712_ORDER_SCHEMA, order); + const orderHashBuff = EIP712Utils.createEIP712Message(orderParamsHashBuff, order.exchangeAddress); + return orderHashBuff; + }, + _getOrderSchemaBuffer(): Buffer { + return EIP712Utils.compileSchema(EIP712_ORDER_SCHEMA); + }, +}; diff --git a/packages/order-utils/src/order_state_utils.ts b/packages/order-utils/src/order_state_utils.ts new file mode 100644 index 000000000..189bf4180 --- /dev/null +++ b/packages/order-utils/src/order_state_utils.ts @@ -0,0 +1,284 @@ +import { + ExchangeContractErrs, + OrderRelevantState, + OrderState, + OrderStateInvalid, + OrderStateValid, + SignedOrder, +} from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; + +import { AbstractBalanceAndProxyAllowanceFetcher } from './abstract/abstract_balance_and_proxy_allowance_fetcher'; +import { AbstractOrderFilledCancelledFetcher } from './abstract/abstract_order_filled_cancelled_fetcher'; +import { orderHashUtils } from './order_hash'; +import { RemainingFillableCalculator } from './remaining_fillable_calculator'; +import { utils } from './utils'; + +interface SidedOrderRelevantState { + isMakerSide: boolean; + traderBalance: BigNumber; + traderProxyAllowance: BigNumber; + traderFeeBalance: BigNumber; + traderFeeProxyAllowance: BigNumber; + filledTakerAssetAmount: BigNumber; + remainingFillableAssetAmount: BigNumber; +} + +const ACCEPTABLE_RELATIVE_ROUNDING_ERROR = 0.0001; + +export class OrderStateUtils { + private readonly _balanceAndProxyAllowanceFetcher: AbstractBalanceAndProxyAllowanceFetcher; + private readonly _orderFilledCancelledFetcher: AbstractOrderFilledCancelledFetcher; + private static _validateIfOrderIsValid( + signedOrder: SignedOrder, + sidedOrderRelevantState: SidedOrderRelevantState, + ): void { + const isMakerSide = sidedOrderRelevantState.isMakerSide; + const availableTakerAssetAmount = signedOrder.takerAssetAmount.minus( + sidedOrderRelevantState.filledTakerAssetAmount, + ); + if (availableTakerAssetAmount.eq(0)) { + throw new Error(ExchangeContractErrs.OrderRemainingFillAmountZero); + } + + if (sidedOrderRelevantState.traderBalance.eq(0)) { + throw new Error( + isMakerSide + ? ExchangeContractErrs.InsufficientMakerBalance + : ExchangeContractErrs.InsufficientTakerBalance, + ); + } + if (sidedOrderRelevantState.traderProxyAllowance.eq(0)) { + throw new Error( + isMakerSide + ? ExchangeContractErrs.InsufficientMakerAllowance + : ExchangeContractErrs.InsufficientTakerAllowance, + ); + } + if (!signedOrder.makerFee.eq(0)) { + if (sidedOrderRelevantState.traderFeeBalance.eq(0)) { + throw new Error( + isMakerSide + ? ExchangeContractErrs.InsufficientMakerFeeBalance + : ExchangeContractErrs.InsufficientTakerFeeBalance, + ); + } + if (sidedOrderRelevantState.traderFeeProxyAllowance.eq(0)) { + throw new Error( + isMakerSide + ? ExchangeContractErrs.InsufficientMakerFeeAllowance + : ExchangeContractErrs.InsufficientTakerFeeAllowance, + ); + } + } + + let minFillableTakerAssetAmountWithinNoRoundingErrorRange; + if (isMakerSide) { + minFillableTakerAssetAmountWithinNoRoundingErrorRange = signedOrder.takerAssetAmount + .dividedBy(ACCEPTABLE_RELATIVE_ROUNDING_ERROR) + .dividedBy(signedOrder.makerAssetAmount); + } else { + minFillableTakerAssetAmountWithinNoRoundingErrorRange = signedOrder.makerAssetAmount + .dividedBy(ACCEPTABLE_RELATIVE_ROUNDING_ERROR) + .dividedBy(signedOrder.takerAssetAmount); + } + + if ( + sidedOrderRelevantState.remainingFillableAssetAmount.lessThan( + minFillableTakerAssetAmountWithinNoRoundingErrorRange, + ) + ) { + throw new Error(ExchangeContractErrs.OrderFillRoundingError); + } + } + constructor( + balanceAndProxyAllowanceFetcher: AbstractBalanceAndProxyAllowanceFetcher, + orderFilledCancelledFetcher: AbstractOrderFilledCancelledFetcher, + ) { + this._balanceAndProxyAllowanceFetcher = balanceAndProxyAllowanceFetcher; + this._orderFilledCancelledFetcher = orderFilledCancelledFetcher; + } + public async getOpenOrderStateAsync(signedOrder: SignedOrder): Promise<OrderState> { + const orderRelevantState = await this.getOpenOrderRelevantStateAsync(signedOrder); + const orderHash = orderHashUtils.getOrderHashHex(signedOrder); + const sidedOrderRelevantState = { + isMakerSide: true, + traderBalance: orderRelevantState.makerBalance, + traderProxyAllowance: orderRelevantState.makerProxyAllowance, + traderFeeBalance: orderRelevantState.makerFeeBalance, + traderFeeProxyAllowance: orderRelevantState.makerFeeProxyAllowance, + filledTakerAssetAmount: orderRelevantState.filledTakerAssetAmount, + remainingFillableAssetAmount: orderRelevantState.remainingFillableMakerAssetAmount, + }; + try { + OrderStateUtils._validateIfOrderIsValid(signedOrder, sidedOrderRelevantState); + const orderState: OrderStateValid = { + isValid: true, + orderHash, + orderRelevantState, + }; + return orderState; + } catch (err) { + const orderState: OrderStateInvalid = { + isValid: false, + orderHash, + error: err.message, + }; + return orderState; + } + } + public async getOpenOrderRelevantStateAsync(signedOrder: SignedOrder): Promise<OrderRelevantState> { + const isMaker = true; + const sidedOrderRelevantState = await this._getSidedOrderRelevantStateAsync( + isMaker, + signedOrder, + signedOrder.takerAddress, + ); + const remainingFillableTakerAssetAmount = sidedOrderRelevantState.remainingFillableAssetAmount + .times(signedOrder.takerAssetAmount) + .dividedToIntegerBy(signedOrder.makerAssetAmount); + + const orderRelevantState = { + makerBalance: sidedOrderRelevantState.traderBalance, + makerProxyAllowance: sidedOrderRelevantState.traderProxyAllowance, + makerFeeBalance: sidedOrderRelevantState.traderFeeBalance, + makerFeeProxyAllowance: sidedOrderRelevantState.traderFeeProxyAllowance, + filledTakerAssetAmount: sidedOrderRelevantState.filledTakerAssetAmount, + remainingFillableMakerAssetAmount: sidedOrderRelevantState.remainingFillableAssetAmount, + remainingFillableTakerAssetAmount, + }; + return orderRelevantState; + } + public async getMaxFillableTakerAssetAmountAsync( + signedOrder: SignedOrder, + takerAddress: string, + ): Promise<BigNumber> { + // Get max fillable amount for an order, considering the makers ability to fill + let isMaker = true; + const orderRelevantMakerState = await this._getSidedOrderRelevantStateAsync( + isMaker, + signedOrder, + signedOrder.takerAddress, + ); + const remainingFillableTakerAssetAmountGivenMakersStatus = signedOrder.makerAssetAmount.eq(0) + ? new BigNumber(0) + : utils.getPartialAmount( + orderRelevantMakerState.remainingFillableAssetAmount, + signedOrder.makerAssetAmount, + signedOrder.takerAssetAmount, + ); + + // Get max fillable amount for an order, considering the takers ability to fill + isMaker = false; + const orderRelevantTakerState = await this._getSidedOrderRelevantStateAsync(isMaker, signedOrder, takerAddress); + const remainingFillableTakerAssetAmountGivenTakersStatus = orderRelevantTakerState.remainingFillableAssetAmount; + + // The min of these two in the actualy max fillable by either party + const fillableTakerAssetAmount = BigNumber.min([ + remainingFillableTakerAssetAmountGivenMakersStatus, + remainingFillableTakerAssetAmountGivenTakersStatus, + ]); + + return fillableTakerAssetAmount; + } + public async getMaxFillableTakerAssetAmountForFailingOrderAsync( + signedOrder: SignedOrder, + takerAddress: string, + ): Promise<BigNumber> { + // Get min of taker balance & allowance + const takerAssetBalanceOfTaker = await this._balanceAndProxyAllowanceFetcher.getBalanceAsync( + signedOrder.takerAssetData, + takerAddress, + ); + const takerAssetAllowanceOfTaker = await this._balanceAndProxyAllowanceFetcher.getProxyAllowanceAsync( + signedOrder.takerAssetData, + takerAddress, + ); + const minTakerAssetAmount = BigNumber.min([takerAssetBalanceOfTaker, takerAssetAllowanceOfTaker]); + + // get remainingFillAmount + const orderHash = orderHashUtils.getOrderHashHex(signedOrder); + const filledTakerAssetAmount = await this._orderFilledCancelledFetcher.getFilledTakerAmountAsync(orderHash); + const remainingFillTakerAssetAmount = signedOrder.takerAssetAmount.minus(filledTakerAssetAmount); + + if (minTakerAssetAmount.gte(remainingFillTakerAssetAmount)) { + return remainingFillTakerAssetAmount; + } else { + return minTakerAssetAmount; + } + } + private async _getSidedOrderRelevantStateAsync( + isMakerSide: boolean, + signedOrder: SignedOrder, + takerAddress: string, + ): Promise<SidedOrderRelevantState> { + let traderAddress; + let assetData; + let assetAmount; + let feeAmount; + if (isMakerSide) { + traderAddress = signedOrder.makerAddress; + assetData = signedOrder.makerAssetData; + assetAmount = signedOrder.makerAssetAmount; + feeAmount = signedOrder.makerFee; + } else { + traderAddress = takerAddress; + assetData = signedOrder.takerAssetData; + assetAmount = signedOrder.takerAssetAmount; + feeAmount = signedOrder.takerFee; + } + const zrxAssetData = this._orderFilledCancelledFetcher.getZRXAssetData(); + const isAssetZRX = assetData === zrxAssetData; + + const traderBalance = await this._balanceAndProxyAllowanceFetcher.getBalanceAsync(assetData, traderAddress); + const traderProxyAllowance = await this._balanceAndProxyAllowanceFetcher.getProxyAllowanceAsync( + assetData, + traderAddress, + ); + const traderFeeBalance = await this._balanceAndProxyAllowanceFetcher.getBalanceAsync( + zrxAssetData, + traderAddress, + ); + const traderFeeProxyAllowance = await this._balanceAndProxyAllowanceFetcher.getProxyAllowanceAsync( + zrxAssetData, + traderAddress, + ); + + const transferrableTraderAssetAmount = BigNumber.min([traderProxyAllowance, traderBalance]); + const transferrableFeeAssetAmount = BigNumber.min([traderFeeProxyAllowance, traderFeeBalance]); + + const orderHash = orderHashUtils.getOrderHashHex(signedOrder); + const filledTakerAssetAmount = await this._orderFilledCancelledFetcher.getFilledTakerAmountAsync(orderHash); + const totalMakerAssetAmount = signedOrder.makerAssetAmount; + const totalTakerAssetAmount = signedOrder.takerAssetAmount; + const isOrderCancelled = await this._orderFilledCancelledFetcher.isOrderCancelledAsync(orderHash); + const remainingTakerAssetAmount = isOrderCancelled + ? new BigNumber(0) + : totalTakerAssetAmount.minus(filledTakerAssetAmount); + const remainingMakerAssetAmount = remainingTakerAssetAmount.eq(0) + ? new BigNumber(0) + : remainingTakerAssetAmount.times(totalMakerAssetAmount).dividedToIntegerBy(totalTakerAssetAmount); + const remainingAssetAmount = isMakerSide ? remainingMakerAssetAmount : remainingTakerAssetAmount; + + const remainingFillableCalculator = new RemainingFillableCalculator( + feeAmount, + assetAmount, + isAssetZRX, + transferrableTraderAssetAmount, + transferrableFeeAssetAmount, + remainingAssetAmount, + ); + const remainingFillableAssetAmount = remainingFillableCalculator.computeRemainingFillable(); + + const sidedOrderRelevantState = { + isMakerSide, + traderBalance, + traderProxyAllowance, + traderFeeBalance, + traderFeeProxyAllowance, + filledTakerAssetAmount, + remainingFillableAssetAmount, + }; + return sidedOrderRelevantState; + } +} diff --git a/packages/order-utils/src/order_validation_utils.ts b/packages/order-utils/src/order_validation_utils.ts new file mode 100644 index 000000000..67d747081 --- /dev/null +++ b/packages/order-utils/src/order_validation_utils.ts @@ -0,0 +1,210 @@ +import { RevertReason, SignedOrder } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; +import { Provider } from 'ethereum-types'; +import * as _ from 'lodash'; + +import { OrderError, TradeSide, TransferType } from './types'; + +import { AbstractOrderFilledCancelledFetcher } from './abstract/abstract_order_filled_cancelled_fetcher'; +import { constants } from './constants'; +import { ExchangeTransferSimulator } from './exchange_transfer_simulator'; +import { orderHashUtils } from './order_hash'; +import { isValidSignatureAsync } from './signature_utils'; +import { utils } from './utils'; + +export class OrderValidationUtils { + private readonly _orderFilledCancelledFetcher: AbstractOrderFilledCancelledFetcher; + public static isRoundingError(numerator: BigNumber, denominator: BigNumber, target: BigNumber): boolean { + // Solidity's mulmod() in JS + // Source: https://solidity.readthedocs.io/en/latest/units-and-global-variables.html#mathematical-and-cryptographic-functions + if (denominator.eq(0)) { + throw new Error('denominator cannot be 0'); + } + const remainder = target.mul(numerator).mod(denominator); + if (remainder.eq(0)) { + return false; // no rounding error + } + + // tslint:disable-next-line:custom-no-magic-numbers + const errPercentageTimes1000000 = remainder.mul(1000000).div(numerator.mul(target)); + // tslint:disable-next-line:custom-no-magic-numbers + const isError = errPercentageTimes1000000.gt(1000); + return isError; + } + public static async validateFillOrderBalancesAllowancesThrowIfInvalidAsync( + exchangeTradeEmulator: ExchangeTransferSimulator, + signedOrder: SignedOrder, + fillTakerAssetAmount: BigNumber, + senderAddress: string, + zrxAssetData: string, + ): Promise<void> { + try { + const fillMakerTokenAmount = utils.getPartialAmount( + fillTakerAssetAmount, + signedOrder.takerAssetAmount, + signedOrder.makerAssetAmount, + ); + await exchangeTradeEmulator.transferFromAsync( + signedOrder.makerAssetData, + signedOrder.makerAddress, + senderAddress, + fillMakerTokenAmount, + TradeSide.Maker, + TransferType.Trade, + ); + await exchangeTradeEmulator.transferFromAsync( + signedOrder.takerAssetData, + senderAddress, + signedOrder.makerAddress, + fillTakerAssetAmount, + TradeSide.Taker, + TransferType.Trade, + ); + const makerFeeAmount = utils.getPartialAmount( + fillTakerAssetAmount, + signedOrder.takerAssetAmount, + signedOrder.makerFee, + ); + await exchangeTradeEmulator.transferFromAsync( + zrxAssetData, + signedOrder.makerAddress, + signedOrder.feeRecipientAddress, + makerFeeAmount, + TradeSide.Maker, + TransferType.Fee, + ); + const takerFeeAmount = utils.getPartialAmount( + fillTakerAssetAmount, + signedOrder.takerAssetAmount, + signedOrder.takerFee, + ); + await exchangeTradeEmulator.transferFromAsync( + zrxAssetData, + senderAddress, + signedOrder.feeRecipientAddress, + takerFeeAmount, + TradeSide.Taker, + TransferType.Fee, + ); + } catch (err) { + throw new Error(RevertReason.TransferFailed); + } + } + private static _validateRemainingFillAmountNotZeroOrThrow( + takerAssetAmount: BigNumber, + filledTakerTokenAmount: BigNumber, + ): void { + if (takerAssetAmount.eq(filledTakerTokenAmount)) { + throw new Error(RevertReason.OrderUnfillable); + } + } + private static _validateOrderNotExpiredOrThrow(expirationTimeSeconds: BigNumber): void { + const currentUnixTimestampSec = utils.getCurrentUnixTimestampSec(); + if (expirationTimeSeconds.lessThan(currentUnixTimestampSec)) { + throw new Error(RevertReason.OrderUnfillable); + } + } + constructor(orderFilledCancelledFetcher: AbstractOrderFilledCancelledFetcher) { + this._orderFilledCancelledFetcher = orderFilledCancelledFetcher; + } + public async validateOrderFillableOrThrowAsync( + exchangeTradeEmulator: ExchangeTransferSimulator, + signedOrder: SignedOrder, + zrxAssetData: string, + expectedFillTakerTokenAmount?: BigNumber, + ): Promise<void> { + const orderHash = orderHashUtils.getOrderHashHex(signedOrder); + const filledTakerTokenAmount = await this._orderFilledCancelledFetcher.getFilledTakerAmountAsync(orderHash); + OrderValidationUtils._validateRemainingFillAmountNotZeroOrThrow( + signedOrder.takerAssetAmount, + filledTakerTokenAmount, + ); + OrderValidationUtils._validateOrderNotExpiredOrThrow(signedOrder.expirationTimeSeconds); + let fillTakerAssetAmount = signedOrder.takerAssetAmount.minus(filledTakerTokenAmount); + if (!_.isUndefined(expectedFillTakerTokenAmount)) { + fillTakerAssetAmount = expectedFillTakerTokenAmount; + } + await OrderValidationUtils.validateFillOrderBalancesAllowancesThrowIfInvalidAsync( + exchangeTradeEmulator, + signedOrder, + fillTakerAssetAmount, + signedOrder.takerAddress, + zrxAssetData, + ); + } + public async validateFillOrderThrowIfInvalidAsync( + exchangeTradeEmulator: ExchangeTransferSimulator, + provider: Provider, + signedOrder: SignedOrder, + fillTakerAssetAmount: BigNumber, + takerAddress: string, + zrxAssetData: string, + ): Promise<BigNumber> { + if (signedOrder.makerAssetAmount.eq(0) || signedOrder.takerAssetAmount.eq(0)) { + throw new Error(RevertReason.OrderUnfillable); + } + if (fillTakerAssetAmount.eq(0)) { + throw new Error(RevertReason.InvalidTakerAmount); + } + const orderHash = orderHashUtils.getOrderHashHex(signedOrder); + const isValid = await isValidSignatureAsync( + provider, + orderHash, + signedOrder.signature, + signedOrder.makerAddress, + ); + if (!isValid) { + throw new Error(OrderError.InvalidSignature); + } + const filledTakerTokenAmount = await this._orderFilledCancelledFetcher.getFilledTakerAmountAsync(orderHash); + OrderValidationUtils._validateRemainingFillAmountNotZeroOrThrow( + signedOrder.takerAssetAmount, + filledTakerTokenAmount, + ); + if (signedOrder.takerAddress !== constants.NULL_ADDRESS && signedOrder.takerAddress !== takerAddress) { + throw new Error(RevertReason.InvalidTaker); + } + OrderValidationUtils._validateOrderNotExpiredOrThrow(signedOrder.expirationTimeSeconds); + const remainingTakerTokenAmount = signedOrder.takerAssetAmount.minus(filledTakerTokenAmount); + const desiredFillTakerTokenAmount = remainingTakerTokenAmount.lessThan(fillTakerAssetAmount) + ? remainingTakerTokenAmount + : fillTakerAssetAmount; + await OrderValidationUtils.validateFillOrderBalancesAllowancesThrowIfInvalidAsync( + exchangeTradeEmulator, + signedOrder, + desiredFillTakerTokenAmount, + takerAddress, + zrxAssetData, + ); + + const wouldRoundingErrorOccur = OrderValidationUtils.isRoundingError( + desiredFillTakerTokenAmount, + signedOrder.takerAssetAmount, + signedOrder.makerAssetAmount, + ); + if (wouldRoundingErrorOccur) { + throw new Error(RevertReason.RoundingError); + } + return filledTakerTokenAmount; + } + public async validateFillOrKillOrderThrowIfInvalidAsync( + exchangeTradeEmulator: ExchangeTransferSimulator, + provider: Provider, + signedOrder: SignedOrder, + fillTakerAssetAmount: BigNumber, + takerAddress: string, + zrxAssetData: string, + ): Promise<void> { + const filledTakerTokenAmount = await this.validateFillOrderThrowIfInvalidAsync( + exchangeTradeEmulator, + provider, + signedOrder, + fillTakerAssetAmount, + takerAddress, + zrxAssetData, + ); + if (filledTakerTokenAmount !== fillTakerAssetAmount) { + throw new Error(RevertReason.OrderUnfillable); + } + } +} diff --git a/packages/order-utils/src/remaining_fillable_calculator.ts b/packages/order-utils/src/remaining_fillable_calculator.ts new file mode 100644 index 000000000..7022aa979 --- /dev/null +++ b/packages/order-utils/src/remaining_fillable_calculator.ts @@ -0,0 +1,86 @@ +import { BigNumber } from '@0xproject/utils'; + +export class RemainingFillableCalculator { + private readonly _isTraderAssetZRX: boolean; + // Transferrable Amount is the minimum of Approval and Balance + private readonly _transferrableAssetAmount: BigNumber; + private readonly _transferrableFeeAmount: BigNumber; + private readonly _remainingOrderAssetAmount: BigNumber; + private readonly _remainingOrderFeeAmount: BigNumber; + private readonly _orderFee: BigNumber; + private readonly _orderAssetAmount: BigNumber; + constructor( + orderFee: BigNumber, + orderAssetAmount: BigNumber, + isTraderAssetZRX: boolean, + transferrableAssetAmount: BigNumber, + transferrableFeeAmount: BigNumber, + remainingOrderAssetAmount: BigNumber, + ) { + this._orderFee = orderFee; + this._orderAssetAmount = orderAssetAmount; + this._isTraderAssetZRX = isTraderAssetZRX; + this._transferrableAssetAmount = transferrableAssetAmount; + this._transferrableFeeAmount = transferrableFeeAmount; + this._remainingOrderAssetAmount = remainingOrderAssetAmount; + this._remainingOrderFeeAmount = orderAssetAmount.eq(0) + ? new BigNumber(0) + : remainingOrderAssetAmount.times(orderFee).dividedToIntegerBy(orderAssetAmount); + } + public computeRemainingFillable(): BigNumber { + if (this._hasSufficientFundsForFeeAndTransferAmount()) { + return this._remainingOrderAssetAmount; + } + if (this._orderFee.isZero()) { + return BigNumber.min(this._remainingOrderAssetAmount, this._transferrableAssetAmount); + } + return this._calculatePartiallyFillableAssetAmount(); + } + private _hasSufficientFundsForFeeAndTransferAmount(): boolean { + if (this._isTraderAssetZRX) { + const totalZRXTransferAmountRequired = this._remainingOrderAssetAmount.plus(this._remainingOrderFeeAmount); + const hasSufficientFunds = this._transferrableAssetAmount.greaterThanOrEqualTo( + totalZRXTransferAmountRequired, + ); + return hasSufficientFunds; + } else { + const hasSufficientFundsForTransferAmount = this._transferrableAssetAmount.greaterThanOrEqualTo( + this._remainingOrderAssetAmount, + ); + const hasSufficientFundsForFeeAmount = this._transferrableFeeAmount.greaterThanOrEqualTo( + this._remainingOrderFeeAmount, + ); + const hasSufficientFunds = hasSufficientFundsForTransferAmount && hasSufficientFundsForFeeAmount; + return hasSufficientFunds; + } + } + private _calculatePartiallyFillableAssetAmount(): BigNumber { + // Given an order for 200 wei for 2 ZRXwei fee, find 100 wei for 1 ZRXwei. Order ratio is then 100:1 + const orderToFeeRatio = this._orderAssetAmount.dividedBy(this._orderFee); + // The number of times the trader (maker or taker) can fill the order, if each fill only required the transfer of a single + // baseUnit of fee tokens. + // Given 2 ZRXwei, the maximum amount of times trader can fill this order, in terms of fees, is 2 + const fillableTimesInFeeBaseUnits = BigNumber.min(this._transferrableFeeAmount, this._remainingOrderFeeAmount); + // The number of times the trader can fill the order, given the traders asset Balance + // Assuming a balance of 150 wei, and an orderToFeeRatio of 100:1, trader can fill this order 1 time. + let fillableTimesInAssetUnits = this._transferrableAssetAmount.dividedBy(orderToFeeRatio); + if (this._isTraderAssetZRX) { + // If ZRX is the trader asset, the Fee and the trader fill amount need to be removed from the same pool; + // 200 ZRXwei for 2ZRXwei fee can only be filled once (need 202 ZRXwei) + const totalZRXTokenPooled = this._transferrableAssetAmount; + // The purchasing power here is less as the tokens are taken from the same Pool + // For every one number of fills, we have to take an extra ZRX out of the pool + fillableTimesInAssetUnits = totalZRXTokenPooled.dividedBy(orderToFeeRatio.plus(new BigNumber(1))); + } + // When Ratio is not fully divisible there can be remainders which cannot be represented, so they are floored. + // This can result in a RoundingError being thrown by the Exchange Contract. + const partiallyFillableAssetAmount = fillableTimesInAssetUnits + .times(this._orderAssetAmount) + .dividedToIntegerBy(this._orderFee); + const partiallyFillableFeeAmount = fillableTimesInFeeBaseUnits + .times(this._orderAssetAmount) + .dividedToIntegerBy(this._orderFee); + const partiallyFillableAmount = BigNumber.min(partiallyFillableAssetAmount, partiallyFillableFeeAmount); + return partiallyFillableAmount; + } +} diff --git a/packages/order-utils/src/salt.ts b/packages/order-utils/src/salt.ts new file mode 100644 index 000000000..90a4197c0 --- /dev/null +++ b/packages/order-utils/src/salt.ts @@ -0,0 +1,18 @@ +import { BigNumber } from '@0xproject/utils'; + +const MAX_DIGITS_IN_UNSIGNED_256_INT = 78; + +/** + * Generates a pseudo-random 256-bit salt. + * The salt can be included in a 0x order, ensuring that the order generates a unique orderHash + * and will not collide with other outstanding orders that are identical in all other parameters. + * @return A pseudo-random 256-bit number that can be used as a salt. + */ +export function generatePseudoRandomSalt(): BigNumber { + // BigNumber.random returns a pseudo-random number between 0 & 1 with a passed in number of decimal places. + // Source: https://mikemcl.github.io/bignumber.js/#random + const randomNumber = BigNumber.random(MAX_DIGITS_IN_UNSIGNED_256_INT); + const factor = new BigNumber(10).pow(MAX_DIGITS_IN_UNSIGNED_256_INT - 1); + const salt = randomNumber.times(factor).round(); + return salt; +} diff --git a/packages/order-utils/src/signature_utils.ts b/packages/order-utils/src/signature_utils.ts new file mode 100644 index 000000000..870aef2ed --- /dev/null +++ b/packages/order-utils/src/signature_utils.ts @@ -0,0 +1,388 @@ +import { schemas } from '@0xproject/json-schemas'; +import { ECSignature, SignatureType, SignerType, ValidatorSignature } from '@0xproject/types'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import { Provider } from 'ethereum-types'; +import * as ethUtil from 'ethereumjs-util'; +import * as _ from 'lodash'; + +import { artifacts } from './artifacts'; +import { assert } from './assert'; +import { ExchangeContract } from './generated_contract_wrappers/exchange'; +import { IValidatorContract } from './generated_contract_wrappers/i_validator'; +import { IWalletContract } from './generated_contract_wrappers/i_wallet'; +import { OrderError } from './types'; +import { utils } from './utils'; + +/** + * Verifies that the provided signature is valid according to the 0x Protocol smart contracts + * @param data The hex encoded data signed by the supplied signature. + * @param signature A hex encoded 0x Protocol signature made up of: [TypeSpecificData][SignatureType]. + * E.g [vrs][SignatureType.EIP712] + * @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. + */ +export async function isValidSignatureAsync( + provider: Provider, + data: string, + signature: string, + signerAddress: string, +): Promise<boolean> { + assert.isWeb3Provider('provider', provider); + assert.isHexString('data', data); + assert.isHexString('signature', signature); + assert.isETHAddressHex('signerAddress', signerAddress); + const signatureTypeIndexIfExists = utils.getSignatureTypeIndexIfExists(signature); + if (_.isUndefined(signatureTypeIndexIfExists)) { + throw new Error(`Unrecognized signatureType in signature: ${signature}`); + } + + switch (signatureTypeIndexIfExists) { + case SignatureType.Illegal: + case SignatureType.Invalid: + return false; + + case SignatureType.EIP712: { + const ecSignature = parseECSignature(signature); + return isValidECSignature(data, ecSignature, signerAddress); + } + + case SignatureType.EthSign: { + const ecSignature = parseECSignature(signature); + const prefixedMessageHex = addSignedMessagePrefix(data, SignerType.Default); + return isValidECSignature(prefixedMessageHex, ecSignature, signerAddress); + } + + case SignatureType.Caller: + // HACK: We currently do not "validate" the caller signature type. + // It can only be validated during Exchange contract execution. + throw new Error('Caller signature type cannot be validated off-chain'); + + case SignatureType.Wallet: { + const isValid = await isValidWalletSignatureAsync(provider, data, signature, signerAddress); + return isValid; + } + + case SignatureType.Validator: { + const isValid = await isValidValidatorSignatureAsync(provider, data, signature, signerAddress); + return isValid; + } + + case SignatureType.PreSigned: { + return isValidPresignedSignatureAsync(provider, data, signerAddress); + } + + case SignatureType.Trezor: { + const prefixedMessageHex = addSignedMessagePrefix(data, SignerType.Trezor); + const ecSignature = parseECSignature(signature); + return isValidECSignature(prefixedMessageHex, ecSignature, signerAddress); + } + + default: + throw new Error(`Unhandled SignatureType: ${signatureTypeIndexIfExists}`); + } +} + +/** + * Verifies that the provided presigned signature is valid according to the 0x Protocol smart contracts + * @param data The hex encoded data signed by the supplied signature. + * @param signature A hex encoded presigned 0x Protocol signature made up of: [SignatureType.Presigned] + * @param signerAddress The hex encoded address that signed the data, producing the supplied signature. + * @return Whether the data was preSigned by the supplied signerAddress. + */ +export async function isValidPresignedSignatureAsync( + provider: Provider, + data: string, + signerAddress: string, +): Promise<boolean> { + assert.isWeb3Provider('provider', provider); + assert.isHexString('data', data); + assert.isETHAddressHex('signerAddress', signerAddress); + const exchangeContract = new ExchangeContract(artifacts.Exchange.compilerOutput.abi, signerAddress, provider); + const isValid = await exchangeContract.preSigned.callAsync(data, signerAddress); + return isValid; +} + +/** + * Verifies that the provided wallet signature is valid according to the 0x Protocol smart contracts + * @param data The hex encoded data signed by the supplied signature. + * @param signature A hex encoded presigned 0x Protocol signature made up of: [SignatureType.Presigned] + * @param signerAddress The hex encoded address that signed the data, producing the supplied signature. + * @return Whether the data was preSigned by the supplied signerAddress. + */ +export async function isValidWalletSignatureAsync( + provider: Provider, + data: string, + signature: string, + signerAddress: string, +): Promise<boolean> { + assert.isWeb3Provider('provider', provider); + assert.isHexString('data', data); + assert.isHexString('signature', signature); + assert.isETHAddressHex('signerAddress', signerAddress); + // tslint:disable-next-line:custom-no-magic-numbers + const signatureWithoutType = signature.slice(-2); + const walletContract = new IWalletContract(artifacts.IWallet.compilerOutput.abi, signerAddress, provider); + const isValid = await walletContract.isValidSignature.callAsync(data, signatureWithoutType); + return isValid; +} + +/** + * Verifies that the provided validator signature is valid according to the 0x Protocol smart contracts + * @param data The hex encoded data signed by the supplied signature. + * @param signature A hex encoded presigned 0x Protocol signature made up of: [SignatureType.Presigned] + * @param signerAddress The hex encoded address that signed the data, producing the supplied signature. + * @return Whether the data was preSigned by the supplied signerAddress. + */ +export async function isValidValidatorSignatureAsync( + provider: Provider, + data: string, + signature: string, + signerAddress: string, +): Promise<boolean> { + assert.isWeb3Provider('provider', provider); + assert.isHexString('data', data); + assert.isHexString('signature', signature); + assert.isETHAddressHex('signerAddress', signerAddress); + const validatorSignature = parseValidatorSignature(signature); + const exchangeContract = new ExchangeContract(artifacts.Exchange.compilerOutput.abi, signerAddress, provider); + const isValidatorApproved = await exchangeContract.allowedValidators.callAsync( + signerAddress, + validatorSignature.validatorAddress, + ); + if (!isValidatorApproved) { + throw new Error(`Validator ${validatorSignature.validatorAddress} was not pre-approved by ${signerAddress}.`); + } + + const validatorContract = new IValidatorContract(artifacts.IValidator.compilerOutput.abi, signerAddress, provider); + const isValid = await validatorContract.isValidSignature.callAsync( + data, + signerAddress, + validatorSignature.signature, + ); + return isValid; +} + +/** + * Checks if the supplied elliptic curve signature corresponds to signing `data` with + * the private key corresponding to `signerAddress` + * @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 ECSignature is valid. + */ +export function isValidECSignature(data: string, signature: ECSignature, signerAddress: string): boolean { + assert.isHexString('data', data); + assert.doesConformToSchema('signature', signature, schemas.ecSignatureSchema); + assert.isETHAddressHex('signerAddress', signerAddress); + + const msgHashBuff = ethUtil.toBuffer(data); + try { + const pubKey = ethUtil.ecrecover( + msgHashBuff, + signature.v, + ethUtil.toBuffer(signature.r), + ethUtil.toBuffer(signature.s), + ); + const retrievedAddress = ethUtil.bufferToHex(ethUtil.pubToAddress(pubKey)); + return retrievedAddress === signerAddress; + } catch (err) { + return false; + } +} + +/** + * Signs an orderHash and returns it's elliptic curve signature and signature type. + * This method currently supports TestRPC, Geth and Parity above and below V1.6.6 + * @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 signerType Different signers add/require different prefixes to be prepended to the message being signed. + * Since we cannot know ahead of time which signer you are using, you must supply a SignerType. + * @return A hex encoded string containing the Elliptic curve signature generated by signing the orderHash and the Signature Type. + */ +export async function ecSignOrderHashAsync( + provider: Provider, + orderHash: string, + signerAddress: string, + signerType: SignerType, +): Promise<string> { + assert.isWeb3Provider('provider', provider); + assert.isHexString('orderHash', orderHash); + assert.isETHAddressHex('signerAddress', signerAddress); + const web3Wrapper = new Web3Wrapper(provider); + await assert.isSenderAddressAsync('signerAddress', signerAddress, web3Wrapper); + const normalizedSignerAddress = signerAddress.toLowerCase(); + + let msgHashHex = orderHash; + const prefixedMsgHashHex = addSignedMessagePrefix(orderHash, signerType); + // Metamask incorrectly implements eth_sign and does not prefix the message as per the spec + // Source: https://github.com/MetaMask/metamask-extension/commit/a9d36860bec424dcee8db043d3e7da6a5ff5672e + if (signerType === SignerType.Metamask) { + msgHashHex = prefixedMsgHashHex; + } + const signature = await web3Wrapper.signMessageAsync(normalizedSignerAddress, msgHashHex); + + // HACK: There is no consensus on whether the signatureHex string should be formatted as + // v + r + s OR r + s + v, and different clients (even different versions of the same client) + // return the signature params in different orders. In order to support all client implementations, + // we parse the signature in both ways, and evaluate if either one is a valid signature. + // r + s + v is the most prevalent format from eth_sign, so we attempt this first. + // tslint:disable-next-line:custom-no-magic-numbers + const validVParamValues = [27, 28]; + const ecSignatureRSV = parseSignatureHexAsRSV(signature); + if (_.includes(validVParamValues, ecSignatureRSV.v)) { + const isValidRSVSignature = isValidECSignature(prefixedMsgHashHex, ecSignatureRSV, normalizedSignerAddress); + if (isValidRSVSignature) { + const convertedSignatureHex = convertECSignatureToSignatureHex(ecSignatureRSV, signerType); + return convertedSignatureHex; + } + } + const ecSignatureVRS = parseSignatureHexAsVRS(signature); + if (_.includes(validVParamValues, ecSignatureVRS.v)) { + const isValidVRSSignature = isValidECSignature(prefixedMsgHashHex, ecSignatureVRS, normalizedSignerAddress); + if (isValidVRSSignature) { + const convertedSignatureHex = convertECSignatureToSignatureHex(ecSignatureVRS, signerType); + return convertedSignatureHex; + } + } + + throw new Error(OrderError.InvalidSignature); +} +/** + * Combines ECSignature with V,R,S and the relevant signature type for use in 0x protocol + * @param ecSignature The ECSignature of the signed data + * @param signerType The SignerType of the signed data + * @return Hex encoded string of signature (v,r,s) with Signature Type + */ +export function convertECSignatureToSignatureHex(ecSignature: ECSignature, signerType: SignerType): string { + const signatureBuffer = Buffer.concat([ + ethUtil.toBuffer(ecSignature.v), + ethUtil.toBuffer(ecSignature.r), + ethUtil.toBuffer(ecSignature.s), + ]); + const signatureHex = `0x${signatureBuffer.toString('hex')}`; + let signatureType; + switch (signerType) { + case SignerType.Metamask: + case SignerType.Ledger: + case SignerType.Default: { + signatureType = SignatureType.EthSign; + break; + } + case SignerType.Trezor: { + signatureType = SignatureType.Trezor; + break; + } + default: + throw new Error(`Unrecognized SignerType: ${signerType}`); + } + const signatureWithType = convertToSignatureWithType(signatureHex, signatureType); + return signatureWithType; +} +/** + * Combines the signature proof and the Signature Type. + * @param signature The hex encoded signature proof + * @param signatureType The signature type, i.e EthSign, Trezor, Wallet etc. + * @return Hex encoded string of signature proof with Signature Type + */ +export function convertToSignatureWithType(signature: string, signatureType: SignatureType): string { + const signatureBuffer = Buffer.concat([ethUtil.toBuffer(signature), ethUtil.toBuffer(signatureType)]); + const signatureHex = `0x${signatureBuffer.toString('hex')}`; + return signatureHex; +} +/** + * Adds the relevant prefix to the message being signed. + * @param message Message to sign + * @param signerType The type of message prefix to add for a given SignerType. Different signers expect + * specific message prefixes. + * @return Prefixed message + */ +export function addSignedMessagePrefix(message: string, signerType: SignerType = SignerType.Default): string { + assert.isString('message', message); + assert.doesBelongToStringEnum('signerType', signerType, SignerType); + switch (signerType) { + case SignerType.Metamask: + case SignerType.Ledger: + case SignerType.Default: { + const msgBuff = ethUtil.toBuffer(message); + const prefixedMsgBuff = ethUtil.hashPersonalMessage(msgBuff); + const prefixedMsgHex = ethUtil.bufferToHex(prefixedMsgBuff); + return prefixedMsgHex; + } + case SignerType.Trezor: { + const msgBuff = ethUtil.toBuffer(message); + const prefixedMsgBuff = hashTrezorPersonalMessage(msgBuff); + const prefixedMsgHex = ethUtil.bufferToHex(prefixedMsgBuff); + return prefixedMsgHex; + } + default: + throw new Error(`Unrecognized SignerType: ${signerType}`); + } +} + +/** + * Parse a 0x protocol hex-encoded signature string into it's ECSignature components + * @param signature A hex encoded ecSignature 0x Protocol signature + * @return An ECSignature object with r,s,v parameters + */ +export function parseECSignature(signature: string): ECSignature { + assert.isHexString('signature', signature); + const ecSignatureTypes = [SignatureType.EthSign, SignatureType.EIP712, SignatureType.Trezor]; + assert.isOneOfExpectedSignatureTypes(signature, ecSignatureTypes); + + // tslint:disable-next-line:custom-no-magic-numbers + const vrsHex = signature.slice(0, -2); + const ecSignature = parseSignatureHexAsVRS(vrsHex); + + return ecSignature; +} + +function hashTrezorPersonalMessage(message: Buffer): Buffer { + const prefix = ethUtil.toBuffer('\x19Ethereum Signed Message:\n' + String.fromCharCode(message.byteLength)); + return ethUtil.sha3(Buffer.concat([prefix, message])); +} + +function parseValidatorSignature(signature: string): ValidatorSignature { + assert.isOneOfExpectedSignatureTypes(signature, [SignatureType.Validator]); + // tslint:disable:custom-no-magic-numbers + const validatorSignature = { + validatorAddress: signature.slice(-22, -2), + signature: signature.slice(0, -22), + }; + // tslint:enable:custom-no-magic-numbers + return validatorSignature; +} + +function parseSignatureHexAsVRS(signatureHex: string): ECSignature { + const signatureBuffer = ethUtil.toBuffer(signatureHex); + let v = signatureBuffer[0]; + // HACK: Sometimes v is returned as [0, 1] and sometimes as [27, 28] + // If it is returned as [0, 1], add 27 to both so it becomes [27, 28] + const lowestValidV = 27; + const isProperlyFormattedV = v >= lowestValidV; + if (!isProperlyFormattedV) { + v += lowestValidV; + } + // signatureBuffer contains vrs + const vEndIndex = 1; + const rsIndex = 33; + const r = signatureBuffer.slice(vEndIndex, rsIndex); + const sEndIndex = 65; + const s = signatureBuffer.slice(rsIndex, sEndIndex); + const ecSignature: ECSignature = { + v, + r: ethUtil.bufferToHex(r), + s: ethUtil.bufferToHex(s), + }; + return ecSignature; +} + +function parseSignatureHexAsRSV(signatureHex: string): ECSignature { + const { v, r, s } = ethUtil.fromRpcSig(signatureHex); + const ecSignature: ECSignature = { + v, + r: ethUtil.bufferToHex(r), + s: ethUtil.bufferToHex(s), + }; + return ecSignature; +} diff --git a/packages/order-utils/src/store/balance_and_proxy_allowance_lazy_store.ts b/packages/order-utils/src/store/balance_and_proxy_allowance_lazy_store.ts new file mode 100644 index 000000000..5a2c1d7ff --- /dev/null +++ b/packages/order-utils/src/store/balance_and_proxy_allowance_lazy_store.ts @@ -0,0 +1,97 @@ +import { AssetProxyId } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; +import * as _ from 'lodash'; + +import { AbstractBalanceAndProxyAllowanceFetcher } from '../abstract/abstract_balance_and_proxy_allowance_fetcher'; +import { AbstractBalanceAndProxyAllowanceLazyStore } from '../abstract/abstract_balance_and_proxy_allowance_lazy_store'; +import { assetDataUtils } from '../asset_data_utils'; + +/** + * Copy on read store for balances/proxyAllowances of tokens/accounts + */ +export class BalanceAndProxyAllowanceLazyStore implements AbstractBalanceAndProxyAllowanceLazyStore { + private readonly _balanceAndProxyAllowanceFetcher: AbstractBalanceAndProxyAllowanceFetcher; + private _balance: { + [assetData: string]: { + [userAddress: string]: BigNumber; + }; + }; + private _proxyAllowance: { + [assetData: string]: { + [userAddress: string]: BigNumber; + }; + }; + constructor(balanceAndProxyAllowanceFetcher: AbstractBalanceAndProxyAllowanceFetcher) { + this._balanceAndProxyAllowanceFetcher = balanceAndProxyAllowanceFetcher; + this._balance = {}; + this._proxyAllowance = {}; + } + public async getBalanceAsync(assetData: string, userAddress: string): Promise<BigNumber> { + if (_.isUndefined(this._balance[assetData]) || _.isUndefined(this._balance[assetData][userAddress])) { + const balance = await this._balanceAndProxyAllowanceFetcher.getBalanceAsync(assetData, userAddress); + this.setBalance(assetData, userAddress, balance); + } + const cachedBalance = this._balance[assetData][userAddress]; + return cachedBalance; + } + public setBalance(assetData: string, userAddress: string, balance: BigNumber): void { + if (_.isUndefined(this._balance[assetData])) { + this._balance[assetData] = {}; + } + this._balance[assetData][userAddress] = balance; + } + public deleteBalance(assetData: string, userAddress: string): void { + if (!_.isUndefined(this._balance[assetData])) { + delete this._balance[assetData][userAddress]; + if (_.isEmpty(this._balance[assetData])) { + delete this._balance[assetData]; + } + } + } + public async getProxyAllowanceAsync(assetData: string, userAddress: string): Promise<BigNumber> { + if ( + _.isUndefined(this._proxyAllowance[assetData]) || + _.isUndefined(this._proxyAllowance[assetData][userAddress]) + ) { + const proxyAllowance = await this._balanceAndProxyAllowanceFetcher.getProxyAllowanceAsync( + assetData, + userAddress, + ); + this.setProxyAllowance(assetData, userAddress, proxyAllowance); + } + const cachedProxyAllowance = this._proxyAllowance[assetData][userAddress]; + return cachedProxyAllowance; + } + public setProxyAllowance(assetData: string, userAddress: string, proxyAllowance: BigNumber): void { + if (_.isUndefined(this._proxyAllowance[assetData])) { + this._proxyAllowance[assetData] = {}; + } + this._proxyAllowance[assetData][userAddress] = proxyAllowance; + } + public deleteProxyAllowance(assetData: string, userAddress: string): void { + if (!_.isUndefined(this._proxyAllowance[assetData])) { + delete this._proxyAllowance[assetData][userAddress]; + if (_.isEmpty(this._proxyAllowance[assetData])) { + delete this._proxyAllowance[assetData]; + } + } + } + public deleteAllERC721ProxyAllowance(tokenAddress: string, userAddress: string): void { + for (const assetData in this._proxyAllowance) { + if (this._proxyAllowance.hasOwnProperty(assetData)) { + const decodedAssetData = assetDataUtils.decodeAssetDataOrThrow(assetData); + if ( + decodedAssetData.assetProxyId === AssetProxyId.ERC721 && + decodedAssetData.tokenAddress === tokenAddress && + !_.isUndefined(this._proxyAllowance[assetData][userAddress]) + ) { + delete this._proxyAllowance[assetData][userAddress]; + } + } + } + } + public deleteAll(): void { + this._balance = {}; + this._proxyAllowance = {}; + } +} diff --git a/packages/order-utils/src/store/order_filled_cancelled_lazy_store.ts b/packages/order-utils/src/store/order_filled_cancelled_lazy_store.ts new file mode 100644 index 000000000..336c6d0ba --- /dev/null +++ b/packages/order-utils/src/store/order_filled_cancelled_lazy_store.ts @@ -0,0 +1,65 @@ +import { BigNumber } from '@0xproject/utils'; +import * as _ from 'lodash'; + +import { AbstractOrderFilledCancelledFetcher } from '../abstract/abstract_order_filled_cancelled_fetcher'; +import { AbstractOrderFilledCancelledLazyStore } from '../abstract/abstract_order_filled_cancelled_lazy_store'; + +/** + * Copy on read store for balances/proxyAllowances of tokens/accounts + */ +export class OrderFilledCancelledLazyStore implements AbstractOrderFilledCancelledLazyStore { + private readonly _orderFilledCancelledFetcher: AbstractOrderFilledCancelledFetcher; + private _filledTakerAmount: { + [orderHash: string]: BigNumber; + }; + private _isCancelled: { + [orderHash: string]: boolean; + }; + constructor(orderFilledCancelledFetcher: AbstractOrderFilledCancelledFetcher) { + this._orderFilledCancelledFetcher = orderFilledCancelledFetcher; + this._filledTakerAmount = {}; + this._isCancelled = {}; + } + public async getFilledTakerAmountAsync(orderHash: string): Promise<BigNumber> { + if (_.isUndefined(this._filledTakerAmount[orderHash])) { + const filledTakerAmount = await this._orderFilledCancelledFetcher.getFilledTakerAmountAsync(orderHash); + this.setFilledTakerAmount(orderHash, filledTakerAmount); + } + const cachedFilledTakerAmount = this._filledTakerAmount[orderHash]; + return cachedFilledTakerAmount; + } + public setFilledTakerAmount(orderHash: string, filledTakerAmount: BigNumber): void { + this._filledTakerAmount[orderHash] = filledTakerAmount; + } + public deleteFilledTakerAmount(orderHash: string): void { + delete this._filledTakerAmount[orderHash]; + } + public async getIsCancelledAsync(orderHash: string): Promise<boolean> { + if (_.isUndefined(this._isCancelled[orderHash])) { + const isCancelled = await this._orderFilledCancelledFetcher.isOrderCancelledAsync(orderHash); + this.setIsCancelled(orderHash, isCancelled); + } + const cachedIsCancelled = this._isCancelled[orderHash]; // tslint:disable-line:boolean-naming + return cachedIsCancelled; + } + public setIsCancelled(orderHash: string, isCancelled: boolean): void { + this._isCancelled[orderHash] = isCancelled; + } + public deleteIsCancelled(orderHash: string): void { + delete this._isCancelled[orderHash]; + } + public deleteAll(): void { + this.deleteAllFilled(); + this.deleteAllIsCancelled(); + } + public deleteAllIsCancelled(): void { + this._isCancelled = {}; + } + public deleteAllFilled(): void { + this._filledTakerAmount = {}; + } + public getZRXAssetData(): string { + const zrxAssetData = this._orderFilledCancelledFetcher.getZRXAssetData(); + return zrxAssetData; + } +} diff --git a/packages/order-utils/src/types.ts b/packages/order-utils/src/types.ts new file mode 100644 index 000000000..1fbd8cf7b --- /dev/null +++ b/packages/order-utils/src/types.ts @@ -0,0 +1,43 @@ +import { BigNumber } from '@0xproject/utils'; + +export enum OrderError { + InvalidSignature = 'INVALID_SIGNATURE', +} + +export enum TradeSide { + Maker = 'maker', + Taker = 'taker', +} + +export enum TransferType { + Trade = 'trade', + Fee = 'fee', +} + +export interface EIP712Parameter { + name: string; + type: EIP712Types; +} + +export interface EIP712Schema { + name: string; + parameters: EIP712Parameter[]; +} + +export enum EIP712Types { + Address = 'address', + Bytes = 'bytes', + Bytes32 = 'bytes32', + String = 'string', + Uint256 = 'uint256', +} + +export interface CreateOrderOpts { + takerAddress?: string; + senderAddress?: string; + makerFee?: BigNumber; + takerFee?: BigNumber; + feeRecipientAddress?: string; + salt?: BigNumber; + expirationTimeSeconds?: BigNumber; +} diff --git a/packages/order-utils/src/utils.ts b/packages/order-utils/src/utils.ts new file mode 100644 index 000000000..7aaaf0609 --- /dev/null +++ b/packages/order-utils/src/utils.ts @@ -0,0 +1,22 @@ +import { BigNumber } from '@0xproject/utils'; + +export const utils = { + getSignatureTypeIndexIfExists(signature: string): number { + // tslint:disable-next-line:custom-no-magic-numbers + const signatureTypeHex = signature.slice(-2); + const base = 16; + const signatureTypeInt = parseInt(signatureTypeHex, base); + return signatureTypeInt; + }, + getCurrentUnixTimestampSec(): BigNumber { + const milisecondsInSecond = 1000; + return new BigNumber(Date.now() / milisecondsInSecond).round(); + }, + getPartialAmount(numerator: BigNumber, denominator: BigNumber, target: BigNumber): BigNumber { + const fillMakerTokenAmount = numerator + .mul(target) + .div(denominator) + .round(0); + return fillMakerTokenAmount; + }, +}; |