1029 lines
43 KiB
TypeScript
Generated
1029 lines
43 KiB
TypeScript
Generated
// 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,
|
|
SubscriptionManager,
|
|
PromiseWithTransactionHash,
|
|
methodAbiToFunctionSignature,
|
|
linkLibrariesInBytecode,
|
|
} from '@0x/base-contract';
|
|
import { schemas } from '@0x/json-schemas';
|
|
import {
|
|
BlockParam,
|
|
BlockParamLiteral,
|
|
BlockRange,
|
|
CallData,
|
|
ContractAbi,
|
|
ContractArtifact,
|
|
DecodedLogArgs,
|
|
LogWithDecodedArgs,
|
|
MethodAbi,
|
|
TransactionReceiptWithDecodedLogs,
|
|
TxData,
|
|
TxDataPayable,
|
|
SupportedProvider,
|
|
} from 'ethereum-types';
|
|
import { BigNumber, classUtils, hexUtils, 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
|
|
|
|
export type ERC721TokenEventArgs =
|
|
| ERC721TokenApprovalEventArgs
|
|
| ERC721TokenApprovalForAllEventArgs
|
|
| ERC721TokenTransferEventArgs;
|
|
|
|
export enum ERC721TokenEvents {
|
|
Approval = 'Approval',
|
|
ApprovalForAll = 'ApprovalForAll',
|
|
Transfer = 'Transfer',
|
|
}
|
|
|
|
export interface ERC721TokenApprovalEventArgs extends DecodedLogArgs {
|
|
_owner: string;
|
|
_approved: string;
|
|
_tokenId: BigNumber;
|
|
}
|
|
|
|
export interface ERC721TokenApprovalForAllEventArgs extends DecodedLogArgs {
|
|
_owner: string;
|
|
_operator: string;
|
|
_approved: boolean;
|
|
}
|
|
|
|
export interface ERC721TokenTransferEventArgs extends DecodedLogArgs {
|
|
_from: string;
|
|
_to: string;
|
|
_tokenId: BigNumber;
|
|
}
|
|
|
|
/* istanbul ignore next */
|
|
// tslint:disable:no-parameter-reassignment
|
|
// tslint:disable-next-line:class-name
|
|
export class ERC721TokenContract extends BaseContract {
|
|
/**
|
|
* @ignore
|
|
*/
|
|
public static deployedBytecode: string | undefined;
|
|
public static contractName = 'ERC721Token';
|
|
private readonly _methodABIIndex: { [name: string]: number } = {};
|
|
private readonly _subscriptionManager: SubscriptionManager<ERC721TokenEventArgs, ERC721TokenEvents>;
|
|
public static async deployFrom0xArtifactAsync(
|
|
artifact: ContractArtifact | SimpleContractArtifact,
|
|
supportedProvider: SupportedProvider,
|
|
txDefaults: Partial<TxData>,
|
|
logDecodeDependencies: { [contractName: string]: ContractArtifact | SimpleContractArtifact },
|
|
): Promise<ERC721TokenContract> {
|
|
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 ERC721TokenContract.deployAsync(bytecode, abi, provider, txDefaults, logDecodeDependenciesAbiOnly);
|
|
}
|
|
|
|
public static async deployWithLibrariesFrom0xArtifactAsync(
|
|
artifact: ContractArtifact,
|
|
libraryArtifacts: { [libraryName: string]: ContractArtifact },
|
|
supportedProvider: SupportedProvider,
|
|
txDefaults: Partial<TxData>,
|
|
logDecodeDependencies: { [contractName: string]: ContractArtifact | SimpleContractArtifact },
|
|
): Promise<ERC721TokenContract> {
|
|
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 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;
|
|
}
|
|
}
|
|
const libraryAddresses = await ERC721TokenContract._deployLibrariesAsync(
|
|
artifact,
|
|
libraryArtifacts,
|
|
new Web3Wrapper(provider),
|
|
txDefaults,
|
|
);
|
|
const bytecode = linkLibrariesInBytecode(artifact, libraryAddresses);
|
|
return ERC721TokenContract.deployAsync(bytecode, abi, provider, txDefaults, logDecodeDependenciesAbiOnly);
|
|
}
|
|
|
|
public static async deployAsync(
|
|
bytecode: string,
|
|
abi: ContractAbi,
|
|
supportedProvider: SupportedProvider,
|
|
txDefaults: Partial<TxData>,
|
|
logDecodeDependencies: { [contractName: string]: ContractAbi },
|
|
): Promise<ERC721TokenContract> {
|
|
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);
|
|
[] = BaseContract._formatABIDataItemList(constructorAbi.inputs, [], BaseContract._bigNumberToString);
|
|
const iface = new ethers.utils.Interface(abi);
|
|
const deployInfo = iface.deployFunction;
|
|
const txData = deployInfo.encode(bytecode, []);
|
|
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(`ERC721Token successfully deployed at ${txReceipt.contractAddress}`);
|
|
const contractInstance = new ERC721TokenContract(
|
|
txReceipt.contractAddress as string,
|
|
provider,
|
|
txDefaults,
|
|
logDecodeDependencies,
|
|
);
|
|
contractInstance.constructorArgs = [];
|
|
return contractInstance;
|
|
}
|
|
|
|
/**
|
|
* @returns The contract ABI
|
|
*/
|
|
public static ABI(): ContractAbi {
|
|
const abi = [
|
|
{
|
|
anonymous: false,
|
|
inputs: [
|
|
{
|
|
name: '_owner',
|
|
type: 'address',
|
|
indexed: true,
|
|
},
|
|
{
|
|
name: '_approved',
|
|
type: 'address',
|
|
indexed: true,
|
|
},
|
|
{
|
|
name: '_tokenId',
|
|
type: 'uint256',
|
|
indexed: true,
|
|
},
|
|
],
|
|
name: 'Approval',
|
|
outputs: [],
|
|
type: 'event',
|
|
},
|
|
{
|
|
anonymous: false,
|
|
inputs: [
|
|
{
|
|
name: '_owner',
|
|
type: 'address',
|
|
indexed: true,
|
|
},
|
|
{
|
|
name: '_operator',
|
|
type: 'address',
|
|
indexed: true,
|
|
},
|
|
{
|
|
name: '_approved',
|
|
type: 'bool',
|
|
indexed: false,
|
|
},
|
|
],
|
|
name: 'ApprovalForAll',
|
|
outputs: [],
|
|
type: 'event',
|
|
},
|
|
{
|
|
anonymous: false,
|
|
inputs: [
|
|
{
|
|
name: '_from',
|
|
type: 'address',
|
|
indexed: true,
|
|
},
|
|
{
|
|
name: '_to',
|
|
type: 'address',
|
|
indexed: true,
|
|
},
|
|
{
|
|
name: '_tokenId',
|
|
type: 'uint256',
|
|
indexed: true,
|
|
},
|
|
],
|
|
name: 'Transfer',
|
|
outputs: [],
|
|
type: 'event',
|
|
},
|
|
{
|
|
constant: false,
|
|
inputs: [
|
|
{
|
|
name: '_approved',
|
|
type: 'address',
|
|
},
|
|
{
|
|
name: '_tokenId',
|
|
type: 'uint256',
|
|
},
|
|
],
|
|
name: 'approve',
|
|
outputs: [],
|
|
payable: false,
|
|
stateMutability: 'nonpayable',
|
|
type: 'function',
|
|
},
|
|
{
|
|
constant: true,
|
|
inputs: [
|
|
{
|
|
name: '_owner',
|
|
type: 'address',
|
|
},
|
|
],
|
|
name: 'balanceOf',
|
|
outputs: [
|
|
{
|
|
name: '',
|
|
type: 'uint256',
|
|
},
|
|
],
|
|
payable: false,
|
|
stateMutability: 'view',
|
|
type: 'function',
|
|
},
|
|
{
|
|
constant: true,
|
|
inputs: [
|
|
{
|
|
name: '_tokenId',
|
|
type: 'uint256',
|
|
},
|
|
],
|
|
name: 'getApproved',
|
|
outputs: [
|
|
{
|
|
name: '',
|
|
type: 'address',
|
|
},
|
|
],
|
|
payable: false,
|
|
stateMutability: 'view',
|
|
type: 'function',
|
|
},
|
|
{
|
|
constant: true,
|
|
inputs: [
|
|
{
|
|
name: '_owner',
|
|
type: 'address',
|
|
},
|
|
{
|
|
name: '_operator',
|
|
type: 'address',
|
|
},
|
|
],
|
|
name: 'isApprovedForAll',
|
|
outputs: [
|
|
{
|
|
name: '',
|
|
type: 'bool',
|
|
},
|
|
],
|
|
payable: false,
|
|
stateMutability: 'view',
|
|
type: 'function',
|
|
},
|
|
{
|
|
constant: true,
|
|
inputs: [
|
|
{
|
|
name: '_tokenId',
|
|
type: 'uint256',
|
|
},
|
|
],
|
|
name: 'ownerOf',
|
|
outputs: [
|
|
{
|
|
name: '',
|
|
type: 'address',
|
|
},
|
|
],
|
|
payable: false,
|
|
stateMutability: 'view',
|
|
type: 'function',
|
|
},
|
|
{
|
|
constant: false,
|
|
inputs: [
|
|
{
|
|
name: '_from',
|
|
type: 'address',
|
|
},
|
|
{
|
|
name: '_to',
|
|
type: 'address',
|
|
},
|
|
{
|
|
name: '_tokenId',
|
|
type: 'uint256',
|
|
},
|
|
],
|
|
name: 'safeTransferFrom',
|
|
outputs: [],
|
|
payable: false,
|
|
stateMutability: 'nonpayable',
|
|
type: 'function',
|
|
},
|
|
{
|
|
constant: false,
|
|
inputs: [
|
|
{
|
|
name: '_from',
|
|
type: 'address',
|
|
},
|
|
{
|
|
name: '_to',
|
|
type: 'address',
|
|
},
|
|
{
|
|
name: '_tokenId',
|
|
type: 'uint256',
|
|
},
|
|
{
|
|
name: '_data',
|
|
type: 'bytes',
|
|
},
|
|
],
|
|
name: 'safeTransferFrom',
|
|
outputs: [],
|
|
payable: false,
|
|
stateMutability: 'nonpayable',
|
|
type: 'function',
|
|
},
|
|
{
|
|
constant: false,
|
|
inputs: [
|
|
{
|
|
name: '_operator',
|
|
type: 'address',
|
|
},
|
|
{
|
|
name: '_approved',
|
|
type: 'bool',
|
|
},
|
|
],
|
|
name: 'setApprovalForAll',
|
|
outputs: [],
|
|
payable: false,
|
|
stateMutability: 'nonpayable',
|
|
type: 'function',
|
|
},
|
|
{
|
|
constant: false,
|
|
inputs: [
|
|
{
|
|
name: '_from',
|
|
type: 'address',
|
|
},
|
|
{
|
|
name: '_to',
|
|
type: 'address',
|
|
},
|
|
{
|
|
name: '_tokenId',
|
|
type: 'uint256',
|
|
},
|
|
],
|
|
name: 'transferFrom',
|
|
outputs: [],
|
|
payable: false,
|
|
stateMutability: 'nonpayable',
|
|
type: 'function',
|
|
},
|
|
] as ContractAbi;
|
|
return abi;
|
|
}
|
|
|
|
protected static async _deployLibrariesAsync(
|
|
artifact: ContractArtifact,
|
|
libraryArtifacts: { [libraryName: string]: ContractArtifact },
|
|
web3Wrapper: Web3Wrapper,
|
|
txDefaults: Partial<TxData>,
|
|
libraryAddresses: { [libraryName: string]: string } = {},
|
|
): Promise<{ [libraryName: string]: string }> {
|
|
const links = artifact.compilerOutput.evm.bytecode.linkReferences;
|
|
// Go through all linked libraries, recursively deploying them if necessary.
|
|
for (const link of Object.values(links)) {
|
|
for (const libraryName of Object.keys(link)) {
|
|
if (!libraryAddresses[libraryName]) {
|
|
// Library not yet deployed.
|
|
const libraryArtifact = libraryArtifacts[libraryName];
|
|
if (!libraryArtifact) {
|
|
throw new Error(`Missing artifact for linked library "${libraryName}"`);
|
|
}
|
|
// Deploy any dependent libraries used by this library.
|
|
await ERC721TokenContract._deployLibrariesAsync(
|
|
libraryArtifact,
|
|
libraryArtifacts,
|
|
web3Wrapper,
|
|
txDefaults,
|
|
libraryAddresses,
|
|
);
|
|
// Deploy this library.
|
|
const linkedLibraryBytecode = linkLibrariesInBytecode(libraryArtifact, libraryAddresses);
|
|
const txDataWithDefaults = await BaseContract._applyDefaultsToContractTxDataAsync(
|
|
{
|
|
data: linkedLibraryBytecode,
|
|
...txDefaults,
|
|
},
|
|
web3Wrapper.estimateGasAsync.bind(web3Wrapper),
|
|
);
|
|
const txHash = await web3Wrapper.sendTransactionAsync(txDataWithDefaults);
|
|
logUtils.log(`transactionHash: ${txHash}`);
|
|
const { contractAddress } = await web3Wrapper.awaitTransactionSuccessAsync(txHash);
|
|
logUtils.log(`${libraryArtifact.contractName} successfully deployed at ${contractAddress}`);
|
|
libraryAddresses[libraryArtifact.contractName] = contractAddress as string;
|
|
}
|
|
}
|
|
}
|
|
return libraryAddresses;
|
|
}
|
|
|
|
public getFunctionSignature(methodName: string): string {
|
|
const index = this._methodABIIndex[methodName];
|
|
const methodAbi = ERC721TokenContract.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 ERC721TokenContract;
|
|
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 ERC721TokenContract;
|
|
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 ERC721TokenContract;
|
|
const abiEncoder = self._lookupAbiEncoder(functionSignature);
|
|
return abiEncoder.getSelector();
|
|
}
|
|
|
|
/**
|
|
* The zero address indicates there is no approved address.
|
|
* Throws unless `msg.sender` is the current NFT owner, or an authorized
|
|
* operator of the current owner.
|
|
* @param _approved The new approved NFT controller
|
|
* @param _tokenId The NFT to approve
|
|
*/
|
|
public approve(_approved: string, _tokenId: BigNumber): ContractTxFunctionObj<void> {
|
|
const self = (this as any) as ERC721TokenContract;
|
|
assert.isString('_approved', _approved);
|
|
assert.isBigNumber('_tokenId', _tokenId);
|
|
const functionSignature = 'approve(address,uint256)';
|
|
|
|
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, [_approved.toLowerCase(), _tokenId]);
|
|
},
|
|
};
|
|
}
|
|
/**
|
|
* NFTs assigned to the zero address are considered invalid, and this
|
|
* function throws for queries about the zero address.
|
|
* @param _owner An address for whom to query the balance
|
|
* @returns The number of NFTs owned by `_owner`, possibly zero
|
|
*/
|
|
public balanceOf(_owner: string): ContractFunctionObj<BigNumber> {
|
|
const self = (this as any) as ERC721TokenContract;
|
|
assert.isString('_owner', _owner);
|
|
const functionSignature = 'balanceOf(address)';
|
|
|
|
return {
|
|
async callAsync(callData: Partial<CallData> = {}, defaultBlock?: BlockParam): Promise<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<BigNumber>(rawCallResult);
|
|
},
|
|
getABIEncodedTransactionData(): string {
|
|
return self._strictEncodeArguments(functionSignature, [_owner.toLowerCase()]);
|
|
},
|
|
};
|
|
}
|
|
/**
|
|
* Throws if `_tokenId` is not a valid NFT.
|
|
* @param _tokenId The NFT to find the approved address for
|
|
* @returns The approved address for this NFT, or the zero address if there is none
|
|
*/
|
|
public getApproved(_tokenId: BigNumber): ContractFunctionObj<string> {
|
|
const self = (this as any) as ERC721TokenContract;
|
|
assert.isBigNumber('_tokenId', _tokenId);
|
|
const functionSignature = 'getApproved(uint256)';
|
|
|
|
return {
|
|
async callAsync(callData: Partial<CallData> = {}, defaultBlock?: BlockParam): Promise<string> {
|
|
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<string>(rawCallResult);
|
|
},
|
|
getABIEncodedTransactionData(): string {
|
|
return self._strictEncodeArguments(functionSignature, [_tokenId]);
|
|
},
|
|
};
|
|
}
|
|
public isApprovedForAll(_owner: string, _operator: string): ContractFunctionObj<boolean> {
|
|
const self = (this as any) as ERC721TokenContract;
|
|
assert.isString('_owner', _owner);
|
|
assert.isString('_operator', _operator);
|
|
const functionSignature = 'isApprovedForAll(address,address)';
|
|
|
|
return {
|
|
async callAsync(callData: Partial<CallData> = {}, defaultBlock?: BlockParam): Promise<boolean> {
|
|
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<boolean>(rawCallResult);
|
|
},
|
|
getABIEncodedTransactionData(): string {
|
|
return self._strictEncodeArguments(functionSignature, [_owner.toLowerCase(), _operator.toLowerCase()]);
|
|
},
|
|
};
|
|
}
|
|
/**
|
|
* NFTs assigned to zero address are considered invalid, and queries
|
|
* about them do throw.
|
|
* @param _tokenId The identifier for an NFT
|
|
* @returns The address of the owner of the NFT
|
|
*/
|
|
public ownerOf(_tokenId: BigNumber): ContractFunctionObj<string> {
|
|
const self = (this as any) as ERC721TokenContract;
|
|
assert.isBigNumber('_tokenId', _tokenId);
|
|
const functionSignature = 'ownerOf(uint256)';
|
|
|
|
return {
|
|
async callAsync(callData: Partial<CallData> = {}, defaultBlock?: BlockParam): Promise<string> {
|
|
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<string>(rawCallResult);
|
|
},
|
|
getABIEncodedTransactionData(): string {
|
|
return self._strictEncodeArguments(functionSignature, [_tokenId]);
|
|
},
|
|
};
|
|
}
|
|
/**
|
|
* This works identically to the other function with an extra data parameter,
|
|
* except this function just sets data to "".
|
|
* @param _from The current owner of the NFT
|
|
* @param _to The new owner
|
|
* @param _tokenId The NFT to transfer
|
|
*/
|
|
public safeTransferFrom1(_from: string, _to: string, _tokenId: BigNumber): ContractTxFunctionObj<void> {
|
|
const self = (this as any) as ERC721TokenContract;
|
|
assert.isString('_from', _from);
|
|
assert.isString('_to', _to);
|
|
assert.isBigNumber('_tokenId', _tokenId);
|
|
const functionSignature = 'safeTransferFrom(address,address,uint256)';
|
|
|
|
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(),
|
|
_tokenId,
|
|
]);
|
|
},
|
|
};
|
|
}
|
|
/**
|
|
* Throws unless `msg.sender` is the current owner, an authorized
|
|
* operator, or the approved address for this NFT. Throws if `_from` is
|
|
* not the current owner. Throws if `_to` is the zero address. Throws if
|
|
* `_tokenId` is not a valid NFT. When transfer is complete, this function
|
|
* checks if `_to` is a smart contract (code size > 0). If so, it calls
|
|
* `onERC721Received` on `_to` and throws if the return value is not
|
|
* `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`.
|
|
* @param _from The current owner of the NFT
|
|
* @param _to The new owner
|
|
* @param _tokenId The NFT to transfer
|
|
* @param _data Additional data with no specified format, sent in call to `_to`
|
|
*/
|
|
public safeTransferFrom2(
|
|
_from: string,
|
|
_to: string,
|
|
_tokenId: BigNumber,
|
|
_data: string,
|
|
): ContractTxFunctionObj<void> {
|
|
const self = (this as any) as ERC721TokenContract;
|
|
assert.isString('_from', _from);
|
|
assert.isString('_to', _to);
|
|
assert.isBigNumber('_tokenId', _tokenId);
|
|
assert.isString('_data', _data);
|
|
const functionSignature = 'safeTransferFrom(address,address,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(),
|
|
_tokenId,
|
|
_data,
|
|
]);
|
|
},
|
|
};
|
|
}
|
|
/**
|
|
* Emits the ApprovalForAll event. The contract MUST allow
|
|
* multiple operators per owner.
|
|
* @param _operator Address to add to the set of authorized operators
|
|
* @param _approved True if the operator is approved, false to revoke approval
|
|
*/
|
|
public setApprovalForAll(_operator: string, _approved: boolean): ContractTxFunctionObj<void> {
|
|
const self = (this as any) as ERC721TokenContract;
|
|
assert.isString('_operator', _operator);
|
|
assert.isBoolean('_approved', _approved);
|
|
const functionSignature = 'setApprovalForAll(address,bool)';
|
|
|
|
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, [_operator.toLowerCase(), _approved]);
|
|
},
|
|
};
|
|
}
|
|
/**
|
|
* Throws unless `msg.sender` is the current owner, an authorized
|
|
* operator, or the approved address for this NFT. Throws if `_from` is
|
|
* not the current owner. Throws if `_to` is the zero address. Throws if
|
|
* `_tokenId` is not a valid NFT.
|
|
* @param _from The current owner of the NFT
|
|
* @param _to The new owner
|
|
* @param _tokenId The NFT to transfer
|
|
*/
|
|
public transferFrom(_from: string, _to: string, _tokenId: BigNumber): ContractTxFunctionObj<void> {
|
|
const self = (this as any) as ERC721TokenContract;
|
|
assert.isString('_from', _from);
|
|
assert.isString('_to', _to);
|
|
assert.isBigNumber('_tokenId', _tokenId);
|
|
const functionSignature = 'transferFrom(address,address,uint256)';
|
|
|
|
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(),
|
|
_tokenId,
|
|
]);
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Subscribe to an event type emitted by the ERC721Token contract.
|
|
* @param eventName The ERC721Token contract event you would like to subscribe to.
|
|
* @param indexFilterValues An object where the keys are indexed args returned by the event and
|
|
* the value is the value you are interested in. E.g `{maker: aUserAddressHex}`
|
|
* @param callback Callback that gets called when a log is added/removed
|
|
* @param isVerbose Enable verbose subscription warnings (e.g recoverable network issues encountered)
|
|
* @return Subscription token used later to unsubscribe
|
|
*/
|
|
public subscribe<ArgsType extends ERC721TokenEventArgs>(
|
|
eventName: ERC721TokenEvents,
|
|
indexFilterValues: IndexedFilterValues,
|
|
callback: EventCallback<ArgsType>,
|
|
isVerbose: boolean = false,
|
|
blockPollingIntervalMs?: number,
|
|
): string {
|
|
assert.doesBelongToStringEnum('eventName', eventName, ERC721TokenEvents);
|
|
assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema);
|
|
assert.isFunction('callback', callback);
|
|
const subscriptionToken = this._subscriptionManager.subscribe<ArgsType>(
|
|
this.address,
|
|
eventName,
|
|
indexFilterValues,
|
|
ERC721TokenContract.ABI(),
|
|
callback,
|
|
isVerbose,
|
|
blockPollingIntervalMs,
|
|
);
|
|
return subscriptionToken;
|
|
}
|
|
|
|
/**
|
|
* Cancel a subscription
|
|
* @param subscriptionToken Subscription token returned by `subscribe()`
|
|
*/
|
|
public unsubscribe(subscriptionToken: string): void {
|
|
this._subscriptionManager.unsubscribe(subscriptionToken);
|
|
}
|
|
|
|
/**
|
|
* Cancels all existing subscriptions
|
|
*/
|
|
public unsubscribeAll(): void {
|
|
this._subscriptionManager.unsubscribeAll();
|
|
}
|
|
|
|
/**
|
|
* Gets historical logs without creating a subscription
|
|
* @param eventName The ERC721Token contract event you would like to subscribe to.
|
|
* @param blockRange Block range to get logs from.
|
|
* @param indexFilterValues An object where the keys are indexed args returned by the event and
|
|
* the value is the value you are interested in. E.g `{_from: aUserAddressHex}`
|
|
* @return Array of logs that match the parameters
|
|
*/
|
|
public async getLogsAsync<ArgsType extends ERC721TokenEventArgs>(
|
|
eventName: ERC721TokenEvents,
|
|
blockRange: BlockRange,
|
|
indexFilterValues: IndexedFilterValues,
|
|
): Promise<Array<LogWithDecodedArgs<ArgsType>>> {
|
|
assert.doesBelongToStringEnum('eventName', eventName, ERC721TokenEvents);
|
|
assert.doesConformToSchema('blockRange', blockRange, schemas.blockRangeSchema);
|
|
assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema);
|
|
const logs = await this._subscriptionManager.getLogsAsync<ArgsType>(
|
|
this.address,
|
|
eventName,
|
|
blockRange,
|
|
indexFilterValues,
|
|
ERC721TokenContract.ABI(),
|
|
);
|
|
return logs;
|
|
}
|
|
|
|
constructor(
|
|
address: string,
|
|
supportedProvider: SupportedProvider,
|
|
txDefaults?: Partial<TxData>,
|
|
logDecodeDependencies?: { [contractName: string]: ContractAbi },
|
|
deployedBytecode: string | undefined = ERC721TokenContract.deployedBytecode,
|
|
) {
|
|
super(
|
|
'ERC721Token',
|
|
ERC721TokenContract.ABI(),
|
|
address,
|
|
supportedProvider,
|
|
txDefaults,
|
|
logDecodeDependencies,
|
|
deployedBytecode,
|
|
);
|
|
classUtils.bindAll(this, ['_abiEncoderByFunctionSignature', 'address', '_web3Wrapper']);
|
|
this._subscriptionManager = new SubscriptionManager<ERC721TokenEventArgs, ERC721TokenEvents>(
|
|
ERC721TokenContract.ABI(),
|
|
this._web3Wrapper,
|
|
);
|
|
ERC721TokenContract.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
|