@0x/order-utils: Add findTransformerNonce() and getTransformerAddress() functions.

This commit is contained in:
Lawrence Forman 2020-08-18 16:39:59 -04:00
parent e27e6baabe
commit 8e73ae3614
3 changed files with 45 additions and 1 deletions

View File

@ -13,6 +13,10 @@
{
"note": "Add `refundReceiver` field to `FillQuoteTransformer.TransformData`.",
"pr": 2657
},
{
"note": "Add `findTransformerNonce()` and `getTransformerAddress()` functions.",
"pr": 2657
}
]
},

View File

@ -77,7 +77,9 @@ export {
AffiliateFeeTransformerData,
encodeAffiliateFeeTransformerData,
decodeAffiliateFeeTransformerData,
} from './transformer_data_encoders';
findTransformerNonce,
getTransformerAddress,
} from './transformer_utils';
export { getOrderHash, getExchangeMetaTransactionHash, getExchangeProxyMetaTransactionHash } from './hash_utils';

View File

@ -1,5 +1,10 @@
import { Order } from '@0x/types';
import { AbiEncoder, BigNumber } from '@0x/utils';
import * as ethjs from 'ethereumjs-util';
import { constants } from './constants';
const { NULL_ADDRESS } = constants;
const ORDER_ABI_COMPONENTS = [
{ name: 'makerAddress', type: 'address' },
@ -187,3 +192,36 @@ export function encodeAffiliateFeeTransformerData(data: AffiliateFeeTransformerD
export function decodeAffiliateFeeTransformerData(encoded: string): AffiliateFeeTransformerData {
return affiliateFeeTransformerDataEncoder.decode(encoded);
}
/**
* 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,
maxGuesses: number = 1024,
): number {
if (deployer === NULL_ADDRESS) {
return 0;
}
const lowercaseTransformer = transformer.toLowerCase();
// Try to guess the nonce.
for (let nonce = 0; nonce < maxGuesses; ++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),
);
}