* update abi-gen with new method interfaces * wip: get all packages to build * wip: get all packages to build * Fix two contract wrapper calls * Export necessary types part of the contract wrapper public interfaces * Revive and fix wrapper_unit_tests * Remove duplicate type * Fix lib_exchange_rich_error_decoder tests * Fix remaining test failures in contracts-* packages * Prettier fixes * remove transactionHelper * lint and update changelogs * Fix prettier * Revert changes to reference docs * Add back changelog already published and add revert changelog entry * Add missing CHANGELOG entries * Add missing comma * Update mesh-rpc-client dep * Update Mesh RPC logic in @0x/orderbook to v6.0.1-beta * Align package versions
108 lines
4.0 KiB
TypeScript
108 lines
4.0 KiB
TypeScript
import { DummyERC20TokenContract, WETH9Contract } from '@0x/contracts-erc20';
|
|
import { DummyERC721TokenContract } from '@0x/contracts-erc721';
|
|
import { constants, getRandomInteger, TransactionFactory } from '@0x/contracts-test-utils';
|
|
import { SignatureType, SignedZeroExTransaction, ZeroExTransaction } from '@0x/types';
|
|
import { BigNumber } from '@0x/utils';
|
|
import * as _ from 'lodash';
|
|
|
|
import { AssertionResult } from '../../src/function_assertions';
|
|
import { DeploymentManager } from '../deployment_manager';
|
|
import { SimulationEnvironment } from '../simulation/simulation';
|
|
|
|
export type Constructor<T = {}> = new (...args: any[]) => T;
|
|
|
|
export interface ActorConfig {
|
|
name?: string;
|
|
deployment: DeploymentManager;
|
|
simulationEnvironment?: SimulationEnvironment;
|
|
[mixinProperty: string]: any;
|
|
}
|
|
|
|
export class Actor {
|
|
public static count: number = 0;
|
|
public readonly address: string;
|
|
public readonly name: string;
|
|
public readonly privateKey: Buffer;
|
|
public readonly deployment: DeploymentManager;
|
|
public readonly simulationEnvironment?: SimulationEnvironment;
|
|
public simulationActions: {
|
|
[action: string]: AsyncIterableIterator<AssertionResult | void>;
|
|
} = {};
|
|
protected readonly _transactionFactory: TransactionFactory;
|
|
|
|
constructor(config: ActorConfig) {
|
|
Actor.count++;
|
|
this.address = config.deployment.accounts[Actor.count];
|
|
this.name = config.name || this.address;
|
|
this.deployment = config.deployment;
|
|
this.privateKey = constants.TESTRPC_PRIVATE_KEYS[config.deployment.accounts.indexOf(this.address)];
|
|
this.simulationEnvironment = config.simulationEnvironment;
|
|
this._transactionFactory = new TransactionFactory(
|
|
this.privateKey,
|
|
config.deployment.exchange.address,
|
|
config.deployment.chainId,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Sets a balance for an ERC20 token and approves a spender (defaults to the ERC20 asset proxy)
|
|
* to transfer the token.
|
|
*/
|
|
public async configureERC20TokenAsync(
|
|
token: DummyERC20TokenContract | WETH9Contract,
|
|
spender?: string,
|
|
amount?: BigNumber,
|
|
): Promise<void> {
|
|
if (token instanceof DummyERC20TokenContract) {
|
|
await token
|
|
.setBalance(this.address, amount || constants.INITIAL_ERC20_BALANCE)
|
|
.awaitTransactionSuccessAsync();
|
|
} else {
|
|
await token.deposit().awaitTransactionSuccessAsync({
|
|
from: this.address,
|
|
value: amount || constants.ONE_ETHER,
|
|
});
|
|
}
|
|
|
|
await token
|
|
.approve(spender || this.deployment.assetProxies.erc20Proxy.address, constants.MAX_UINT256)
|
|
.awaitTransactionSuccessAsync({ from: this.address });
|
|
}
|
|
|
|
/**
|
|
* Mints some number of ERC721 NFTs and approves a spender (defaults to the ERC721 asset proxy)
|
|
* to transfer the token.
|
|
*/
|
|
public async configureERC721TokenAsync(
|
|
token: DummyERC721TokenContract,
|
|
spender?: string,
|
|
numToMint: number = 1,
|
|
): Promise<BigNumber[]> {
|
|
const tokenIds: BigNumber[] = [];
|
|
_.times(numToMint, async () => {
|
|
const tokenId = getRandomInteger(constants.ZERO_AMOUNT, constants.MAX_UINT256);
|
|
await token.mint(this.address, tokenId).awaitTransactionSuccessAsync({
|
|
from: this.address,
|
|
});
|
|
tokenIds.push(tokenId);
|
|
});
|
|
|
|
await token
|
|
.setApprovalForAll(spender || this.deployment.assetProxies.erc721Proxy.address, true)
|
|
.awaitTransactionSuccessAsync({
|
|
from: this.address,
|
|
});
|
|
return tokenIds;
|
|
}
|
|
|
|
/**
|
|
* Signs a transaction.
|
|
*/
|
|
public async signTransactionAsync(
|
|
customTransactionParams: Partial<ZeroExTransaction>,
|
|
signatureType: SignatureType = SignatureType.EthSign,
|
|
): Promise<SignedZeroExTransaction> {
|
|
return this._transactionFactory.newSignedTransactionAsync(customTransactionParams, signatureType);
|
|
}
|
|
}
|