@0x/asset-swaper: Use transformer utils from @0x/order-utils.

This commit is contained in:
Lawrence Forman 2020-08-19 12:48:16 -04:00 committed by Lawrence Forman
parent d31d646ecc
commit 14d6330b40
2 changed files with 3 additions and 35 deletions

View File

@ -9,11 +9,11 @@ import {
ERC20AssetData, ERC20AssetData,
ETH_TOKEN_ADDRESS, ETH_TOKEN_ADDRESS,
FillQuoteTransformerSide, FillQuoteTransformerSide,
findTransformerNonce,
} from '@0x/order-utils'; } from '@0x/order-utils';
import { AssetProxyId } from '@0x/types'; import { AssetProxyId } from '@0x/types';
import { BigNumber, providerUtils } from '@0x/utils'; import { BigNumber, providerUtils } from '@0x/utils';
import { SupportedProvider, ZeroExProvider } from '@0x/web3-wrapper'; import { SupportedProvider, ZeroExProvider } from '@0x/web3-wrapper';
import * as ethjs from 'ethereumjs-util';
import * as _ from 'lodash'; import * as _ from 'lodash';
import { constants } from '../constants'; import { constants } from '../constants';
@ -34,7 +34,6 @@ import { assert } from '../utils/assert';
// tslint:disable-next-line:custom-no-magic-numbers // tslint:disable-next-line:custom-no-magic-numbers
const MAX_UINT256 = new BigNumber(2).pow(256).minus(1); const MAX_UINT256 = new BigNumber(2).pow(256).minus(1);
const { NULL_ADDRESS } = constants; const { NULL_ADDRESS } = constants;
const MAX_NONCE_GUESSES = 2048;
export class ExchangeProxySwapQuoteConsumer implements SwapQuoteConsumerBase { export class ExchangeProxySwapQuoteConsumer implements SwapQuoteConsumerBase {
public readonly provider: ZeroExProvider; public readonly provider: ZeroExProvider;
@ -210,32 +209,3 @@ function getTokenFromAssetData(assetData: string): string {
// tslint:disable-next-line:no-unnecessary-type-assertion // tslint:disable-next-line:no-unnecessary-type-assertion
return (data as ERC20AssetData).tokenAddress; return (data as ERC20AssetData).tokenAddress;
} }
/**
* Find the nonce for a transformer given its deployer.
* If `deployer` is the null address, zero will always be returned.
*/
export function findTransformerNonce(transformer: string, deployer: string = NULL_ADDRESS): number {
if (deployer === NULL_ADDRESS) {
return 0;
}
const lowercaseTransformer = transformer.toLowerCase();
// Try to guess the nonce.
for (let nonce = 0; nonce < MAX_NONCE_GUESSES; ++nonce) {
const deployedAddress = getTransformerAddress(deployer, nonce);
if (deployedAddress === lowercaseTransformer) {
return nonce;
}
}
throw new Error(`${deployer} did not deploy ${transformer}!`);
}
/**
* Compute the deployed address for a transformer given a deployer and nonce.
*/
export function getTransformerAddress(deployer: string, nonce: number): string {
return ethjs.bufferToHex(
// tslint:disable-next-line: custom-no-magic-numbers
ethjs.rlphash([deployer, nonce] as any).slice(12),
);
}

View File

@ -8,6 +8,7 @@ import {
decodeWethTransformerData, decodeWethTransformerData,
ETH_TOKEN_ADDRESS, ETH_TOKEN_ADDRESS,
FillQuoteTransformerSide, FillQuoteTransformerSide,
getTransformerAddress,
} from '@0x/order-utils'; } from '@0x/order-utils';
import { Order } from '@0x/types'; import { Order } from '@0x/types';
import { AbiEncoder, BigNumber, hexUtils } from '@0x/utils'; import { AbiEncoder, BigNumber, hexUtils } from '@0x/utils';
@ -16,10 +17,7 @@ import * as _ from 'lodash';
import 'mocha'; import 'mocha';
import { constants } from '../src/constants'; import { constants } from '../src/constants';
import { import { ExchangeProxySwapQuoteConsumer } from '../src/quote_consumers/exchange_proxy_swap_quote_consumer';
ExchangeProxySwapQuoteConsumer,
getTransformerAddress,
} from '../src/quote_consumers/exchange_proxy_swap_quote_consumer';
import { MarketBuySwapQuote, MarketOperation, MarketSellSwapQuote } from '../src/types'; import { MarketBuySwapQuote, MarketOperation, MarketSellSwapQuote } from '../src/types';
import { OptimizedMarketOrder } from '../src/utils/market_operation_utils/types'; import { OptimizedMarketOrder } from '../src/utils/market_operation_utils/types';