Add Broker and GodsUnchainedValidator to contract-wrappers

This commit is contained in:
Michael Zhu 2020-02-04 16:33:28 -08:00
parent a9b1ea9690
commit e3e0d00e21
4 changed files with 1084 additions and 1 deletions

View File

@ -1,4 +1,13 @@
[
{
"version": "13.6.0",
"changes": [
{
"note": "Add Broker and GodsUnchainedValidator wrappers",
"pr": 2469
}
]
},
{
"version": "13.5.0",
"changes": [

View File

@ -31,7 +31,7 @@
"wrappers:generate": "abi-gen --abis ${npm_package_config_abis} --output src/generated-wrappers --backend ethers"
},
"config": {
"abis": "../contract-artifacts/artifacts/@(DevUtils|ERC20Token|ERC721Token|Exchange|Forwarder|IAssetData|LibTransactionDecoder|WETH9|Coordinator|Staking|StakingProxy|IERC20BridgeSampler).json"
"abis": "../contract-artifacts/artifacts/@(DevUtils|ERC20Token|ERC721Token|Exchange|Forwarder|IAssetData|LibTransactionDecoder|WETH9|Coordinator|Staking|StakingProxy|IERC20BridgeSampler|GodsUnchainedValidator|Broker).json"
},
"repository": {
"type": "git",

View File

@ -0,0 +1,826 @@
// tslint:disable:no-consecutive-blank-lines ordered-imports align trailing-comma enum-naming
// tslint:disable:whitespace no-unbound-method no-trailing-whitespace
// tslint:disable:no-unused-variable
import {
AwaitTransactionSuccessOpts,
ContractFunctionObj,
ContractTxFunctionObj,
SendTransactionOpts,
BaseContract,
PromiseWithTransactionHash,
methodAbiToFunctionSignature,
} from '@0x/base-contract';
import { schemas } from '@0x/json-schemas';
import {
BlockParam,
BlockParamLiteral,
BlockRange,
CallData,
ContractAbi,
ContractArtifact,
DecodedLogArgs,
MethodAbi,
TransactionReceiptWithDecodedLogs,
TxData,
TxDataPayable,
SupportedProvider,
} from 'ethereum-types';
import { BigNumber, classUtils, logUtils, providerUtils } from '@0x/utils';
import { EventCallback, IndexedFilterValues, SimpleContractArtifact } from '@0x/types';
import { Web3Wrapper } from '@0x/web3-wrapper';
import { assert } from '@0x/assert';
import * as ethers from 'ethers';
// tslint:enable:no-unused-variable
/* istanbul ignore next */
// tslint:disable:no-parameter-reassignment
// tslint:disable-next-line:class-name
export class BrokerContract extends BaseContract {
/**
* @ignore
*/
public static deployedBytecode: string | undefined;
public static contractName = 'Broker';
private readonly _methodABIIndex: { [name: string]: number } = {};
public static async deployFrom0xArtifactAsync(
artifact: ContractArtifact | SimpleContractArtifact,
supportedProvider: SupportedProvider,
txDefaults: Partial<TxData>,
logDecodeDependencies: { [contractName: string]: ContractArtifact | SimpleContractArtifact },
exchange: string,
weth: string,
): Promise<BrokerContract> {
assert.doesConformToSchema('txDefaults', txDefaults, schemas.txDataSchema, [
schemas.addressSchema,
schemas.numberSchema,
schemas.jsNumber,
]);
if (artifact.compilerOutput === undefined) {
throw new Error('Compiler output not found in the artifact file');
}
const provider = providerUtils.standardizeOrThrow(supportedProvider);
const bytecode = artifact.compilerOutput.evm.bytecode.object;
const abi = artifact.compilerOutput.abi;
const logDecodeDependenciesAbiOnly: { [contractName: string]: ContractAbi } = {};
if (Object.keys(logDecodeDependencies) !== undefined) {
for (const key of Object.keys(logDecodeDependencies)) {
logDecodeDependenciesAbiOnly[key] = logDecodeDependencies[key].compilerOutput.abi;
}
}
return BrokerContract.deployAsync(
bytecode,
abi,
provider,
txDefaults,
logDecodeDependenciesAbiOnly,
exchange,
weth,
);
}
public static async deployAsync(
bytecode: string,
abi: ContractAbi,
supportedProvider: SupportedProvider,
txDefaults: Partial<TxData>,
logDecodeDependencies: { [contractName: string]: ContractAbi },
exchange: string,
weth: string,
): Promise<BrokerContract> {
assert.isHexString('bytecode', bytecode);
assert.doesConformToSchema('txDefaults', txDefaults, schemas.txDataSchema, [
schemas.addressSchema,
schemas.numberSchema,
schemas.jsNumber,
]);
const provider = providerUtils.standardizeOrThrow(supportedProvider);
const constructorAbi = BaseContract._lookupConstructorAbi(abi);
[exchange, weth] = BaseContract._formatABIDataItemList(
constructorAbi.inputs,
[exchange, weth],
BaseContract._bigNumberToString,
);
const iface = new ethers.utils.Interface(abi);
const deployInfo = iface.deployFunction;
const txData = deployInfo.encode(bytecode, [exchange, weth]);
const web3Wrapper = new Web3Wrapper(provider);
const txDataWithDefaults = await BaseContract._applyDefaultsToContractTxDataAsync(
{
data: txData,
...txDefaults,
},
web3Wrapper.estimateGasAsync.bind(web3Wrapper),
);
const txHash = await web3Wrapper.sendTransactionAsync(txDataWithDefaults);
logUtils.log(`transactionHash: ${txHash}`);
const txReceipt = await web3Wrapper.awaitTransactionSuccessAsync(txHash);
logUtils.log(`Broker successfully deployed at ${txReceipt.contractAddress}`);
const contractInstance = new BrokerContract(
txReceipt.contractAddress as string,
provider,
txDefaults,
logDecodeDependencies,
);
contractInstance.constructorArgs = [exchange, weth];
return contractInstance;
}
/**
* @returns The contract ABI
*/
public static ABI(): ContractAbi {
const abi = [
{
inputs: [
{
name: 'exchange',
type: 'address',
},
{
name: 'weth',
type: 'address',
},
],
outputs: [],
payable: false,
stateMutability: 'nonpayable',
type: 'constructor',
},
{
inputs: [],
outputs: [],
payable: true,
stateMutability: 'payable',
type: 'fallback',
},
{
constant: false,
inputs: [
{
name: 'brokeredTokenIds',
type: 'uint256[]',
},
{
name: 'orders',
type: 'tuple[]',
components: [
{
name: 'makerAddress',
type: 'address',
},
{
name: 'takerAddress',
type: 'address',
},
{
name: 'feeRecipientAddress',
type: 'address',
},
{
name: 'senderAddress',
type: 'address',
},
{
name: 'makerAssetAmount',
type: 'uint256',
},
{
name: 'takerAssetAmount',
type: 'uint256',
},
{
name: 'makerFee',
type: 'uint256',
},
{
name: 'takerFee',
type: 'uint256',
},
{
name: 'expirationTimeSeconds',
type: 'uint256',
},
{
name: 'salt',
type: 'uint256',
},
{
name: 'makerAssetData',
type: 'bytes',
},
{
name: 'takerAssetData',
type: 'bytes',
},
{
name: 'makerFeeAssetData',
type: 'bytes',
},
{
name: 'takerFeeAssetData',
type: 'bytes',
},
],
},
{
name: 'takerAssetFillAmounts',
type: 'uint256[]',
},
{
name: 'signatures',
type: 'bytes[]',
},
{
name: 'batchFillFunctionSelector',
type: 'bytes4',
},
{
name: 'ethFeeAmounts',
type: 'uint256[]',
},
{
name: 'feeRecipients',
type: 'address[]',
},
],
name: 'batchBrokerTrade',
outputs: [
{
name: 'fillResults',
type: 'tuple[]',
components: [
{
name: 'makerAssetFilledAmount',
type: 'uint256',
},
{
name: 'takerAssetFilledAmount',
type: 'uint256',
},
{
name: 'makerFeePaid',
type: 'uint256',
},
{
name: 'takerFeePaid',
type: 'uint256',
},
{
name: 'protocolFeePaid',
type: 'uint256',
},
],
},
],
payable: true,
stateMutability: 'payable',
type: 'function',
},
{
constant: false,
inputs: [
{
name: 'brokeredTokenIds',
type: 'uint256[]',
},
{
name: 'order',
type: 'tuple',
components: [
{
name: 'makerAddress',
type: 'address',
},
{
name: 'takerAddress',
type: 'address',
},
{
name: 'feeRecipientAddress',
type: 'address',
},
{
name: 'senderAddress',
type: 'address',
},
{
name: 'makerAssetAmount',
type: 'uint256',
},
{
name: 'takerAssetAmount',
type: 'uint256',
},
{
name: 'makerFee',
type: 'uint256',
},
{
name: 'takerFee',
type: 'uint256',
},
{
name: 'expirationTimeSeconds',
type: 'uint256',
},
{
name: 'salt',
type: 'uint256',
},
{
name: 'makerAssetData',
type: 'bytes',
},
{
name: 'takerAssetData',
type: 'bytes',
},
{
name: 'makerFeeAssetData',
type: 'bytes',
},
{
name: 'takerFeeAssetData',
type: 'bytes',
},
],
},
{
name: 'takerAssetFillAmount',
type: 'uint256',
},
{
name: 'signature',
type: 'bytes',
},
{
name: 'fillFunctionSelector',
type: 'bytes4',
},
{
name: 'ethFeeAmounts',
type: 'uint256[]',
},
{
name: 'feeRecipients',
type: 'address[]',
},
],
name: 'brokerTrade',
outputs: [
{
name: 'fillResults',
type: 'tuple',
components: [
{
name: 'makerAssetFilledAmount',
type: 'uint256',
},
{
name: 'takerAssetFilledAmount',
type: 'uint256',
},
{
name: 'makerFeePaid',
type: 'uint256',
},
{
name: 'takerFeePaid',
type: 'uint256',
},
{
name: 'protocolFeePaid',
type: 'uint256',
},
],
},
],
payable: true,
stateMutability: 'payable',
type: 'function',
},
{
constant: false,
inputs: [
{
name: 'from',
type: 'address',
},
{
name: 'to',
type: 'address',
},
{
name: 'index_2',
type: 'uint256[]',
},
{
name: 'amounts',
type: 'uint256[]',
},
{
name: 'data',
type: 'bytes',
},
],
name: 'safeBatchTransferFrom',
outputs: [],
payable: false,
stateMutability: 'nonpayable',
type: 'function',
},
] as ContractAbi;
return abi;
}
public getFunctionSignature(methodName: string): string {
const index = this._methodABIIndex[methodName];
const methodAbi = BrokerContract.ABI()[index] as MethodAbi; // tslint:disable-line:no-unnecessary-type-assertion
const functionSignature = methodAbiToFunctionSignature(methodAbi);
return functionSignature;
}
public getABIDecodedTransactionData<T>(methodName: string, callData: string): T {
const functionSignature = this.getFunctionSignature(methodName);
const self = (this as any) as BrokerContract;
const abiEncoder = self._lookupAbiEncoder(functionSignature);
const abiDecodedCallData = abiEncoder.strictDecode<T>(callData);
return abiDecodedCallData;
}
public getABIDecodedReturnData<T>(methodName: string, callData: string): T {
const functionSignature = this.getFunctionSignature(methodName);
const self = (this as any) as BrokerContract;
const abiEncoder = self._lookupAbiEncoder(functionSignature);
const abiDecodedCallData = abiEncoder.strictDecodeReturnValue<T>(callData);
return abiDecodedCallData;
}
public getSelector(methodName: string): string {
const functionSignature = this.getFunctionSignature(methodName);
const self = (this as any) as BrokerContract;
const abiEncoder = self._lookupAbiEncoder(functionSignature);
return abiEncoder.getSelector();
}
/**
* Fills multiple property-based orders by the given amounts using the given assets.
* Pays protocol fees using either the ETH supplied by the taker to the transaction or
* WETH acquired from the maker during settlement. The final WETH balance is sent to the taker.
* @param brokeredTokenIds Token IDs specified by the taker to be used to fill
* the orders.
* @param orders The property-based orders to fill. The format of a property-
* based order is the same as that of a normal order, except the
* takerAssetData. Instaed of specifying a specific ERC721 asset,
* the takerAssetData should be ERC1155 assetData where the
* underlying tokenAddress is this contract's address and the desired
* properties are encoded in the extra data field. Also note that
* takerFees must be denominated in WETH (or zero).
* @param takerAssetFillAmounts The amounts to fill the orders by.
* @param signatures The makers' signatures for the given orders.
* @param batchFillFunctionSelector The selector for either `batchFillOrders`,
* `batchFillOrKillOrders`, or `batchFillOrdersNoThrow`.
* @param ethFeeAmounts Amounts of ETH, denominated in Wei, that are paid to
* corresponding feeRecipients.
* @param feeRecipients Addresses that will receive ETH when orders are filled.
* @returns fillResults Amounts filled and fees paid by the makers and taker.
*/
public batchBrokerTrade(
brokeredTokenIds: BigNumber[],
orders: Array<{
makerAddress: string;
takerAddress: string;
feeRecipientAddress: string;
senderAddress: string;
makerAssetAmount: BigNumber;
takerAssetAmount: BigNumber;
makerFee: BigNumber;
takerFee: BigNumber;
expirationTimeSeconds: BigNumber;
salt: BigNumber;
makerAssetData: string;
takerAssetData: string;
makerFeeAssetData: string;
takerFeeAssetData: string;
}>,
takerAssetFillAmounts: BigNumber[],
signatures: string[],
batchFillFunctionSelector: string,
ethFeeAmounts: BigNumber[],
feeRecipients: string[],
): ContractTxFunctionObj<
Array<{
makerAssetFilledAmount: BigNumber;
takerAssetFilledAmount: BigNumber;
makerFeePaid: BigNumber;
takerFeePaid: BigNumber;
protocolFeePaid: BigNumber;
}>
> {
const self = (this as any) as BrokerContract;
assert.isArray('brokeredTokenIds', brokeredTokenIds);
assert.isArray('orders', orders);
assert.isArray('takerAssetFillAmounts', takerAssetFillAmounts);
assert.isArray('signatures', signatures);
assert.isString('batchFillFunctionSelector', batchFillFunctionSelector);
assert.isArray('ethFeeAmounts', ethFeeAmounts);
assert.isArray('feeRecipients', feeRecipients);
const functionSignature =
'batchBrokerTrade(uint256[],(address,address,address,address,uint256,uint256,uint256,uint256,uint256,uint256,bytes,bytes,bytes,bytes)[],uint256[],bytes[],bytes4,uint256[],address[])';
return {
async sendTransactionAsync(
txData?: Partial<TxData> | undefined,
opts: SendTransactionOpts = { shouldValidate: true },
): Promise<string> {
const txDataWithDefaults = await self._applyDefaultsToTxDataAsync(
{ ...txData, data: this.getABIEncodedTransactionData() },
this.estimateGasAsync.bind(this),
);
if (opts.shouldValidate !== false) {
await this.callAsync(txDataWithDefaults);
}
return self._web3Wrapper.sendTransactionAsync(txDataWithDefaults);
},
awaitTransactionSuccessAsync(
txData?: Partial<TxData>,
opts: AwaitTransactionSuccessOpts = { shouldValidate: true },
): PromiseWithTransactionHash<TransactionReceiptWithDecodedLogs> {
return self._promiseWithTransactionHash(this.sendTransactionAsync(txData, opts), opts);
},
async estimateGasAsync(txData?: Partial<TxData> | undefined): Promise<number> {
const txDataWithDefaults = await self._applyDefaultsToTxDataAsync({
...txData,
data: this.getABIEncodedTransactionData(),
});
return self._web3Wrapper.estimateGasAsync(txDataWithDefaults);
},
async callAsync(
callData: Partial<CallData> = {},
defaultBlock?: BlockParam,
): Promise<
Array<{
makerAssetFilledAmount: BigNumber;
takerAssetFilledAmount: BigNumber;
makerFeePaid: BigNumber;
takerFeePaid: BigNumber;
protocolFeePaid: BigNumber;
}>
> {
BaseContract._assertCallParams(callData, defaultBlock);
const rawCallResult = await self._performCallAsync(
{ ...callData, data: this.getABIEncodedTransactionData() },
defaultBlock,
);
const abiEncoder = self._lookupAbiEncoder(functionSignature);
BaseContract._throwIfUnexpectedEmptyCallResult(rawCallResult, abiEncoder);
return abiEncoder.strictDecodeReturnValue<
Array<{
makerAssetFilledAmount: BigNumber;
takerAssetFilledAmount: BigNumber;
makerFeePaid: BigNumber;
takerFeePaid: BigNumber;
protocolFeePaid: BigNumber;
}>
>(rawCallResult);
},
getABIEncodedTransactionData(): string {
return self._strictEncodeArguments(functionSignature, [
brokeredTokenIds,
orders,
takerAssetFillAmounts,
signatures,
batchFillFunctionSelector,
ethFeeAmounts,
feeRecipients,
]);
},
};
}
/**
* Fills a single property-based order by the given amount using the given assets.
* Pays protocol fees using either the ETH supplied by the taker to the transaction or
* WETH acquired from the maker during settlement. The final WETH balance is sent to the taker.
* @param brokeredTokenIds Token IDs specified by the taker to be used to fill
* the orders.
* @param order The property-based order to fill. The format of a property-
* based order is the same as that of a normal order, except the
* takerAssetData. Instaed of specifying a specific ERC721 asset,
* the takerAssetData should be ERC1155 assetData where the
* underlying tokenAddress is this contract's address and the desired
* properties are encoded in the extra data field. Also note that
* takerFees must be denominated in WETH (or zero).
* @param takerAssetFillAmount The amount to fill the order by.
* @param signature The maker's signature of the given order.
* @param fillFunctionSelector The selector for either `fillOrder` or
* `fillOrKillOrder`.
* @param ethFeeAmounts Amounts of ETH, denominated in Wei, that are paid to
* corresponding feeRecipients.
* @param feeRecipients Addresses that will receive ETH when orders are filled.
* @returns fillResults Amounts filled and fees paid by the maker and taker.
*/
public brokerTrade(
brokeredTokenIds: BigNumber[],
order: {
makerAddress: string;
takerAddress: string;
feeRecipientAddress: string;
senderAddress: string;
makerAssetAmount: BigNumber;
takerAssetAmount: BigNumber;
makerFee: BigNumber;
takerFee: BigNumber;
expirationTimeSeconds: BigNumber;
salt: BigNumber;
makerAssetData: string;
takerAssetData: string;
makerFeeAssetData: string;
takerFeeAssetData: string;
},
takerAssetFillAmount: BigNumber,
signature: string,
fillFunctionSelector: string,
ethFeeAmounts: BigNumber[],
feeRecipients: string[],
): ContractTxFunctionObj<{
makerAssetFilledAmount: BigNumber;
takerAssetFilledAmount: BigNumber;
makerFeePaid: BigNumber;
takerFeePaid: BigNumber;
protocolFeePaid: BigNumber;
}> {
const self = (this as any) as BrokerContract;
assert.isArray('brokeredTokenIds', brokeredTokenIds);
assert.isBigNumber('takerAssetFillAmount', takerAssetFillAmount);
assert.isString('signature', signature);
assert.isString('fillFunctionSelector', fillFunctionSelector);
assert.isArray('ethFeeAmounts', ethFeeAmounts);
assert.isArray('feeRecipients', feeRecipients);
const functionSignature =
'brokerTrade(uint256[],(address,address,address,address,uint256,uint256,uint256,uint256,uint256,uint256,bytes,bytes,bytes,bytes),uint256,bytes,bytes4,uint256[],address[])';
return {
async sendTransactionAsync(
txData?: Partial<TxData> | undefined,
opts: SendTransactionOpts = { shouldValidate: true },
): Promise<string> {
const txDataWithDefaults = await self._applyDefaultsToTxDataAsync(
{ ...txData, data: this.getABIEncodedTransactionData() },
this.estimateGasAsync.bind(this),
);
if (opts.shouldValidate !== false) {
await this.callAsync(txDataWithDefaults);
}
return self._web3Wrapper.sendTransactionAsync(txDataWithDefaults);
},
awaitTransactionSuccessAsync(
txData?: Partial<TxData>,
opts: AwaitTransactionSuccessOpts = { shouldValidate: true },
): PromiseWithTransactionHash<TransactionReceiptWithDecodedLogs> {
return self._promiseWithTransactionHash(this.sendTransactionAsync(txData, opts), opts);
},
async estimateGasAsync(txData?: Partial<TxData> | undefined): Promise<number> {
const txDataWithDefaults = await self._applyDefaultsToTxDataAsync({
...txData,
data: this.getABIEncodedTransactionData(),
});
return self._web3Wrapper.estimateGasAsync(txDataWithDefaults);
},
async callAsync(
callData: Partial<CallData> = {},
defaultBlock?: BlockParam,
): Promise<{
makerAssetFilledAmount: BigNumber;
takerAssetFilledAmount: BigNumber;
makerFeePaid: BigNumber;
takerFeePaid: BigNumber;
protocolFeePaid: BigNumber;
}> {
BaseContract._assertCallParams(callData, defaultBlock);
const rawCallResult = await self._performCallAsync(
{ ...callData, data: this.getABIEncodedTransactionData() },
defaultBlock,
);
const abiEncoder = self._lookupAbiEncoder(functionSignature);
BaseContract._throwIfUnexpectedEmptyCallResult(rawCallResult, abiEncoder);
return abiEncoder.strictDecodeReturnValue<{
makerAssetFilledAmount: BigNumber;
takerAssetFilledAmount: BigNumber;
makerFeePaid: BigNumber;
takerFeePaid: BigNumber;
protocolFeePaid: BigNumber;
}>(rawCallResult);
},
getABIEncodedTransactionData(): string {
return self._strictEncodeArguments(functionSignature, [
brokeredTokenIds,
order,
takerAssetFillAmount,
signature,
fillFunctionSelector,
ethFeeAmounts,
feeRecipients,
]);
},
};
}
/**
* The Broker implements the ERC1155 transfer function to be compatible with the ERC1155 asset proxy
* @param from Since the Broker serves as the taker of the order, this should
* equal `address(this)`
* @param to This should be the maker of the order.
* @param amounts Should be an array of just one `uint256`, specifying the
* amount of the brokered assets to transfer.
* @param data Encodes the validator contract address and any auxiliary data it
* needs for property validation.
*/
public safeBatchTransferFrom(
from: string,
to: string,
index_2: BigNumber[],
amounts: BigNumber[],
data: string,
): ContractTxFunctionObj<void> {
const self = (this as any) as BrokerContract;
assert.isString('from', from);
assert.isString('to', to);
assert.isArray('index_2', index_2);
assert.isArray('amounts', amounts);
assert.isString('data', data);
const functionSignature = 'safeBatchTransferFrom(address,address,uint256[],uint256[],bytes)';
return {
async sendTransactionAsync(
txData?: Partial<TxData> | undefined,
opts: SendTransactionOpts = { shouldValidate: true },
): Promise<string> {
const txDataWithDefaults = await self._applyDefaultsToTxDataAsync(
{ ...txData, data: this.getABIEncodedTransactionData() },
this.estimateGasAsync.bind(this),
);
if (opts.shouldValidate !== false) {
await this.callAsync(txDataWithDefaults);
}
return self._web3Wrapper.sendTransactionAsync(txDataWithDefaults);
},
awaitTransactionSuccessAsync(
txData?: Partial<TxData>,
opts: AwaitTransactionSuccessOpts = { shouldValidate: true },
): PromiseWithTransactionHash<TransactionReceiptWithDecodedLogs> {
return self._promiseWithTransactionHash(this.sendTransactionAsync(txData, opts), opts);
},
async estimateGasAsync(txData?: Partial<TxData> | undefined): Promise<number> {
const txDataWithDefaults = await self._applyDefaultsToTxDataAsync({
...txData,
data: this.getABIEncodedTransactionData(),
});
return self._web3Wrapper.estimateGasAsync(txDataWithDefaults);
},
async callAsync(callData: Partial<CallData> = {}, defaultBlock?: BlockParam): Promise<void> {
BaseContract._assertCallParams(callData, defaultBlock);
const rawCallResult = await self._performCallAsync(
{ ...callData, data: this.getABIEncodedTransactionData() },
defaultBlock,
);
const abiEncoder = self._lookupAbiEncoder(functionSignature);
BaseContract._throwIfUnexpectedEmptyCallResult(rawCallResult, abiEncoder);
return abiEncoder.strictDecodeReturnValue<void>(rawCallResult);
},
getABIEncodedTransactionData(): string {
return self._strictEncodeArguments(functionSignature, [
from.toLowerCase(),
to.toLowerCase(),
index_2,
amounts,
data,
]);
},
};
}
constructor(
address: string,
supportedProvider: SupportedProvider,
txDefaults?: Partial<TxData>,
logDecodeDependencies?: { [contractName: string]: ContractAbi },
deployedBytecode: string | undefined = BrokerContract.deployedBytecode,
) {
super(
'Broker',
BrokerContract.ABI(),
address,
supportedProvider,
txDefaults,
logDecodeDependencies,
deployedBytecode,
);
classUtils.bindAll(this, ['_abiEncoderByFunctionSignature', 'address', '_web3Wrapper']);
BrokerContract.ABI().forEach((item, index) => {
if (item.type === 'function') {
const methodAbi = item as MethodAbi;
this._methodABIIndex[methodAbi.name] = index;
}
});
}
}
// tslint:disable:max-file-line-count
// tslint:enable:no-unbound-method no-parameter-reassignment no-consecutive-blank-lines ordered-imports align
// tslint:enable:trailing-comma whitespace no-trailing-whitespace

View File

@ -0,0 +1,248 @@
// tslint:disable:no-consecutive-blank-lines ordered-imports align trailing-comma enum-naming
// tslint:disable:whitespace no-unbound-method no-trailing-whitespace
// tslint:disable:no-unused-variable
import {
AwaitTransactionSuccessOpts,
ContractFunctionObj,
ContractTxFunctionObj,
SendTransactionOpts,
BaseContract,
PromiseWithTransactionHash,
methodAbiToFunctionSignature,
} from '@0x/base-contract';
import { schemas } from '@0x/json-schemas';
import {
BlockParam,
BlockParamLiteral,
BlockRange,
CallData,
ContractAbi,
ContractArtifact,
DecodedLogArgs,
MethodAbi,
TransactionReceiptWithDecodedLogs,
TxData,
TxDataPayable,
SupportedProvider,
} from 'ethereum-types';
import { BigNumber, classUtils, logUtils, providerUtils } from '@0x/utils';
import { EventCallback, IndexedFilterValues, SimpleContractArtifact } from '@0x/types';
import { Web3Wrapper } from '@0x/web3-wrapper';
import { assert } from '@0x/assert';
import * as ethers from 'ethers';
// tslint:enable:no-unused-variable
/* istanbul ignore next */
// tslint:disable:no-parameter-reassignment
// tslint:disable-next-line:class-name
export class GodsUnchainedValidatorContract extends BaseContract {
/**
* @ignore
*/
public static deployedBytecode: string | undefined;
public static contractName = 'GodsUnchainedValidator';
private readonly _methodABIIndex: { [name: string]: number } = {};
public static async deployFrom0xArtifactAsync(
artifact: ContractArtifact | SimpleContractArtifact,
supportedProvider: SupportedProvider,
txDefaults: Partial<TxData>,
logDecodeDependencies: { [contractName: string]: ContractArtifact | SimpleContractArtifact },
_godsUnchained: string,
): Promise<GodsUnchainedValidatorContract> {
assert.doesConformToSchema('txDefaults', txDefaults, schemas.txDataSchema, [
schemas.addressSchema,
schemas.numberSchema,
schemas.jsNumber,
]);
if (artifact.compilerOutput === undefined) {
throw new Error('Compiler output not found in the artifact file');
}
const provider = providerUtils.standardizeOrThrow(supportedProvider);
const bytecode = artifact.compilerOutput.evm.bytecode.object;
const abi = artifact.compilerOutput.abi;
const logDecodeDependenciesAbiOnly: { [contractName: string]: ContractAbi } = {};
if (Object.keys(logDecodeDependencies) !== undefined) {
for (const key of Object.keys(logDecodeDependencies)) {
logDecodeDependenciesAbiOnly[key] = logDecodeDependencies[key].compilerOutput.abi;
}
}
return GodsUnchainedValidatorContract.deployAsync(
bytecode,
abi,
provider,
txDefaults,
logDecodeDependenciesAbiOnly,
_godsUnchained,
);
}
public static async deployAsync(
bytecode: string,
abi: ContractAbi,
supportedProvider: SupportedProvider,
txDefaults: Partial<TxData>,
logDecodeDependencies: { [contractName: string]: ContractAbi },
_godsUnchained: string,
): Promise<GodsUnchainedValidatorContract> {
assert.isHexString('bytecode', bytecode);
assert.doesConformToSchema('txDefaults', txDefaults, schemas.txDataSchema, [
schemas.addressSchema,
schemas.numberSchema,
schemas.jsNumber,
]);
const provider = providerUtils.standardizeOrThrow(supportedProvider);
const constructorAbi = BaseContract._lookupConstructorAbi(abi);
[_godsUnchained] = BaseContract._formatABIDataItemList(
constructorAbi.inputs,
[_godsUnchained],
BaseContract._bigNumberToString,
);
const iface = new ethers.utils.Interface(abi);
const deployInfo = iface.deployFunction;
const txData = deployInfo.encode(bytecode, [_godsUnchained]);
const web3Wrapper = new Web3Wrapper(provider);
const txDataWithDefaults = await BaseContract._applyDefaultsToContractTxDataAsync(
{
data: txData,
...txDefaults,
},
web3Wrapper.estimateGasAsync.bind(web3Wrapper),
);
const txHash = await web3Wrapper.sendTransactionAsync(txDataWithDefaults);
logUtils.log(`transactionHash: ${txHash}`);
const txReceipt = await web3Wrapper.awaitTransactionSuccessAsync(txHash);
logUtils.log(`GodsUnchainedValidator successfully deployed at ${txReceipt.contractAddress}`);
const contractInstance = new GodsUnchainedValidatorContract(
txReceipt.contractAddress as string,
provider,
txDefaults,
logDecodeDependencies,
);
contractInstance.constructorArgs = [_godsUnchained];
return contractInstance;
}
/**
* @returns The contract ABI
*/
public static ABI(): ContractAbi {
const abi = [
{
inputs: [
{
name: '_godsUnchained',
type: 'address',
},
],
outputs: [],
payable: false,
stateMutability: 'nonpayable',
type: 'constructor',
},
{
constant: true,
inputs: [
{
name: 'tokenId',
type: 'uint256',
},
{
name: 'propertyData',
type: 'bytes',
},
],
name: 'checkBrokerAsset',
outputs: [],
payable: false,
stateMutability: 'view',
type: 'function',
},
] as ContractAbi;
return abi;
}
public getFunctionSignature(methodName: string): string {
const index = this._methodABIIndex[methodName];
const methodAbi = GodsUnchainedValidatorContract.ABI()[index] as MethodAbi; // tslint:disable-line:no-unnecessary-type-assertion
const functionSignature = methodAbiToFunctionSignature(methodAbi);
return functionSignature;
}
public getABIDecodedTransactionData<T>(methodName: string, callData: string): T {
const functionSignature = this.getFunctionSignature(methodName);
const self = (this as any) as GodsUnchainedValidatorContract;
const abiEncoder = self._lookupAbiEncoder(functionSignature);
const abiDecodedCallData = abiEncoder.strictDecode<T>(callData);
return abiDecodedCallData;
}
public getABIDecodedReturnData<T>(methodName: string, callData: string): T {
const functionSignature = this.getFunctionSignature(methodName);
const self = (this as any) as GodsUnchainedValidatorContract;
const abiEncoder = self._lookupAbiEncoder(functionSignature);
const abiDecodedCallData = abiEncoder.strictDecodeReturnValue<T>(callData);
return abiDecodedCallData;
}
public getSelector(methodName: string): string {
const functionSignature = this.getFunctionSignature(methodName);
const self = (this as any) as GodsUnchainedValidatorContract;
const abiEncoder = self._lookupAbiEncoder(functionSignature);
return abiEncoder.getSelector();
}
/**
* Checks that the given card (encoded as assetData) has the proto and quality encoded in `propertyData`.
* Reverts if the card doesn't match the specified proto and quality.
* @param tokenId The ERC721 tokenId of the card to check.
* @param propertyData Encoded proto and quality that the card is expected to
* have.
*/
public checkBrokerAsset(tokenId: BigNumber, propertyData: string): ContractFunctionObj<void> {
const self = (this as any) as GodsUnchainedValidatorContract;
assert.isBigNumber('tokenId', tokenId);
assert.isString('propertyData', propertyData);
const functionSignature = 'checkBrokerAsset(uint256,bytes)';
return {
async callAsync(callData: Partial<CallData> = {}, defaultBlock?: BlockParam): Promise<void> {
BaseContract._assertCallParams(callData, defaultBlock);
const rawCallResult = await self._performCallAsync(
{ ...callData, data: this.getABIEncodedTransactionData() },
defaultBlock,
);
const abiEncoder = self._lookupAbiEncoder(functionSignature);
BaseContract._throwIfUnexpectedEmptyCallResult(rawCallResult, abiEncoder);
return abiEncoder.strictDecodeReturnValue<void>(rawCallResult);
},
getABIEncodedTransactionData(): string {
return self._strictEncodeArguments(functionSignature, [tokenId, propertyData]);
},
};
}
constructor(
address: string,
supportedProvider: SupportedProvider,
txDefaults?: Partial<TxData>,
logDecodeDependencies?: { [contractName: string]: ContractAbi },
deployedBytecode: string | undefined = GodsUnchainedValidatorContract.deployedBytecode,
) {
super(
'GodsUnchainedValidator',
GodsUnchainedValidatorContract.ABI(),
address,
supportedProvider,
txDefaults,
logDecodeDependencies,
deployedBytecode,
);
classUtils.bindAll(this, ['_abiEncoderByFunctionSignature', 'address', '_web3Wrapper']);
GodsUnchainedValidatorContract.ABI().forEach((item, index) => {
if (item.type === 'function') {
const methodAbi = item as MethodAbi;
this._methodABIIndex[methodAbi.name] = index;
}
});
}
}
// tslint:disable:max-file-line-count
// tslint:enable:no-unbound-method no-parameter-reassignment no-consecutive-blank-lines ordered-imports align
// tslint:enable:trailing-comma whitespace no-trailing-whitespace