protocol/contracts/test-utils/src/transaction_factory.ts
F. Eugene Aumson f11d8a5bd8
@0x/order-utils refactors for v3: orderParsingUtils, signatureUtils, orderHashUtils, RevertErrors, transactionHashUtils (#2321)
* move orderParsingUtils from order-utils to connect

* Remove many functions from signatureUtils

Removed from the exported object, that is.  All of them are used in
other existing code, so they were all moved to be as local to their
usage as possible.

* remove orderHashUtils.isValidOrderHash()

* Move all *RevertErrors from order-utils...

...into their respective @0x/contracts- packages.

* Refactor @0x/order-utils' orderHashUtils away

- Move existing routines into @0x/contracts-test-utils

- Migrate non-contract-test callers to a newly-exposed getOrderHash()
method in DevUtils.

* Move all *RevertErrors from @0x/utils...

...into their respective @0x/contracts- packages.

* rm transactionHashUtils.isValidTransactionHash()

* DevUtils.sol: Fail yarn test if too big to deploy

* Refactor @0x/order-utils transactionHashUtils away

- Move existing routines into @0x/contracts-test-utils

- Migrate non-contract-test callers to a newly-exposed
getTransactionHash() method in DevUtils.

* Consolidate `Removed export...` CHANGELOG entries

* Rm EthBalanceChecker from devutils wrapper exports

* Stop importing from '.' or '.../src'

* fix builds

* fix prettier; dangling promise

* increase max bundle size
2019-11-14 17:14:24 -05:00

57 lines
2.3 KiB
TypeScript

import { generatePseudoRandomSalt } from '@0x/order-utils';
import { SignatureType, SignedZeroExTransaction, ZeroExTransaction } from '@0x/types';
import { BigNumber } from '@0x/utils';
import * as ethUtil from 'ethereumjs-util';
import { transactionHashUtils } from '../src';
import { getLatestBlockTimestampAsync } from './block_timestamp';
import { constants } from './constants';
import { signingUtils } from './signing_utils';
export class TransactionFactory {
private readonly _signerBuff: Buffer;
private readonly _exchangeAddress: string;
private readonly _privateKey: Buffer;
private readonly _chainId: number;
constructor(privateKey: Buffer, exchangeAddress: string, chainId: number) {
this._privateKey = privateKey;
this._exchangeAddress = exchangeAddress;
this._chainId = chainId;
this._signerBuff = ethUtil.privateToAddress(this._privateKey);
}
public async newSignedTransactionAsync(
customTransactionParams: Partial<ZeroExTransaction>,
signatureType: SignatureType = SignatureType.EthSign,
): Promise<SignedZeroExTransaction> {
if (customTransactionParams.data === undefined) {
throw new Error('Error: ZeroExTransaction data field must be supplied');
}
const tenMinutesInSeconds = 10 * 60;
const currentBlockTimestamp = await getLatestBlockTimestampAsync();
const salt = generatePseudoRandomSalt();
const signerAddress = `0x${this._signerBuff.toString('hex')}`;
const transaction = {
salt,
signerAddress,
data: customTransactionParams.data,
expirationTimeSeconds: new BigNumber(currentBlockTimestamp).plus(tenMinutesInSeconds),
gasPrice: new BigNumber(constants.DEFAULT_GAS_PRICE),
domain: {
verifyingContract: this._exchangeAddress,
chainId: this._chainId,
},
...customTransactionParams,
};
const transactionHashBuffer = transactionHashUtils.getTransactionHashBuffer(transaction);
const signature = signingUtils.signMessage(transactionHashBuffer, this._privateKey, signatureType);
const signedTransaction = {
...transaction,
signature: `0x${signature.toString('hex')}`,
};
return signedTransaction;
}
}