Contract <Abi>
Hierarchy
- Web3Context<EthExecutionAPI, typeof contractSubscriptions>
- Contract
Implements
- Web3EventEmitter<ContractEventEmitterInterface<Abi>>
Index
Constructors
Properties
- options
- providers
- blockHeaderTimeout
- defaultAccount
- defaultBlock
- defaultCommon
- defaultHardfork
- givenProvider
- handleRevert
- providers
- transactionBlockTimeout
- transactionConfirmationBlocks
- transactionConfirmationPollingInterval
- transactionPollingInterval
- transactionPollingTimeout
- transactionReceiptPollingInterval
- transactionSendTimeout
Accessors
- BatchRequest
- accountProvider
- blockHeaderTimeout
- currentProvider
- defaultAccount
- defaultBlock
- defaultChain
- defaultCommon
- defaultHardfork
- defaultMaxPriorityFeePerGas
- defaultNetworkId
- defaultTransactionType
- enableExperimentalFeatures
- events
- givenProvider
- handleRevert
- maxListenersWarningThreshold
- methods
- provider
- requestManager
- subscriptionManager
- transactionBlockTimeout
- transactionBuilder
- transactionConfirmationBlocks
- transactionConfirmationPollingInterval
- transactionPollingInterval
- transactionPollingTimeout
- transactionReceiptPollingInterval
- transactionSendTimeout
- transactionTypeParser
- wallet
Methods
Constructors
publicconstructor
Creates a new contract instance with all its methods and events defined in its json interface object.
new web3.eth.Contract(jsonInterface[, address][, options])
Type parameters
- Abi: readonly AbiFragment[]
Parameters
jsonInterface: Abi
The JSON interface for the contract to instantiate.
optionalcontext: Partial<Web3ContextInitOptions<EthExecutionAPI, { logs: typeof LogsSubscription; newBlockHeaders: typeof NewHeadsSubscription; newHeads: typeof NewHeadsSubscription }>>
The context of the contract used for customizing the behavior of the contract.
optionalreturnFormat: DataFormat
Returns Contract<Abi>
- The contract instance with all its methods and events.
var myContract = new web3.eth.Contract([...], '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe', {
from: '0x1234567890123456789012345678901234567891', // default from address
gasPrice: '20000000000' // default gas price in wei, 20 gwei in this case
});To use the type safe interface for these contracts you have to include the ABI definitions in your Typescript project and then declare these as
const
.const myContractAbi = [....] as const; // ABI definitions
const myContract = new web3.eth.Contract(myContractAbi, '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe');
Properties
publicreadonlyoptions
The options object
for the contract instance. from
, gas
and gasPrice
are used as fallback values when sending transactions.
myContract.options;
> {
address: '0x1234567890123456789012345678901234567891',
jsonInterface: [...],
from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe',
gasPrice: '10000000000000',
gas: 1000000
}
myContract.options.from = '0x1234567890123456789012345678901234567891'; // default from address
myContract.options.gasPrice = '20000000000000'; // default gas price in wei
myContract.options.gas = 5000000; // provide as fallback always 5M gas
readonlyproviders
Type declaration
HttpProvider: Web3BaseProviderConstructor
IpcProvider: Web3BaseProviderConstructor
WebsocketProvider: Web3BaseProviderConstructor
publicstaticoptionalblockHeaderTimeout
Can be used to set Contract.blockHeaderTimeout for all contracts.
publicstaticoptionaldefaultAccount
Can be used to set Contract.defaultAccount for all contracts.
publicstaticoptionaldefaultBlock
Can be used to set Contract.defaultBlock for all contracts.
publicstaticoptionaldefaultCommon
Can be used to set Contract.defaultCommon for all contracts.
publicstaticoptionaldefaultHardfork
Can be used to set Contract.defaultHardfork for all contracts.
staticoptionalgivenProvider
publicstaticoptionalhandleRevert
Can be used to set Contract.handleRevert for all contracts.
staticreadonlyproviders
Type declaration
HttpProvider: Web3BaseProviderConstructor
IpcProvider: Web3BaseProviderConstructor
WebsocketProvider: Web3BaseProviderConstructor
publicstaticoptionaltransactionBlockTimeout
Can be used to set Contract.transactionBlockTimeout for all contracts.
publicstaticoptionaltransactionConfirmationBlocks
Can be used to set Contract.transactionConfirmationBlocks for all contracts.
publicstaticoptionaltransactionConfirmationPollingInterval
Can be used to set Contract.transactionConfirmationPollingInterval for all contracts.
publicstaticoptionaltransactionPollingInterval
Can be used to set Contract.transactionPollingInterval for all contracts.
publicstaticoptionaltransactionPollingTimeout
Can be used to set Contract.transactionPollingTimeout for all contracts.
publicstaticoptionaltransactionReceiptPollingInterval
Can be used to set Contract.transactionReceiptPollingInterval for all contracts.
publicstaticoptionaltransactionSendTimeout
Can be used to set Contract.transactionSendTimeout for all contracts.
Accessors
BatchRequest
Returns new () => Web3BatchRequest
Returns Web3BatchRequest
accountProvider
Returns undefined | Web3AccountProvider<Web3BaseWalletAccount>
publicblockHeaderTimeout
Returns number
Parameters
value: number
Returns void
currentProvider
Returns undefined | string | SupportedProviders<API>
Parameters
provider: undefined | string | SupportedProviders<API>
Returns void
publicdefaultAccount
Returns undefined | string
Parameters
value: undefined | string
Returns void
publicdefaultBlock
Returns BlockNumberOrTag
Parameters
value: BlockNumberOrTag
Returns void
defaultChain
Returns string
Parameters
val: string
Returns void
publicdefaultCommon
Returns undefined | Common
Parameters
value: undefined | Common
Returns void
publicdefaultHardfork
Returns string
Parameters
value: string
Returns void
defaultMaxPriorityFeePerGas
Returns Numbers
Parameters
val: Numbers
Returns void
defaultNetworkId
Returns undefined | Numbers
Parameters
val: undefined | Numbers
Returns void
defaultTransactionType
Returns Numbers
Parameters
val: Numbers
Returns void
enableExperimentalFeatures
Returns { useRpcCallSpecification: boolean; useSubscriptionWhenCheckingBlockTimeout: boolean }
useRpcCallSpecification: boolean
useSubscriptionWhenCheckingBlockTimeout: boolean
Parameters
val: { useRpcCallSpecification: boolean; useSubscriptionWhenCheckingBlockTimeout: boolean }
Returns void
publicevents
Subscribe to an event.
await myContract.events.MyEvent([options])
There is a special event
allEvents
that can be used to subscribe all events.await myContract.events.allEvents([options])
Returns ContractEventsInterface<Abi, ContractEvents<Abi>>
- When individual event is accessed will returns ContractBoundEvent object
givenProvider
Returns undefined | SupportedProviders<never>
publichandleRevert
Returns boolean
Parameters
value: boolean
Returns void
maxListenersWarningThreshold
Returns number
Parameters
val: number
Returns void
publicmethods
Creates a transaction object for that method, which then can be
called
,send
,estimated
,createAccessList
, orABI encoded
.The methods of this smart contract are available through:
The name:
myContract.methods.myMethod(123)
The name with parameters:myContract.methods['myMethod(uint256)'](123)
The signaturemyContract.methods['0x58cf5f10'](123)
This allows calling functions with same name but different parameters from the JavaScript contract object.
> The method signature does not provide a type safe interface, so we recommend to use method
name
instead.// calling a method
const result = await myContract.methods.myMethod(123).call({from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'});
// or sending and using a promise
const receipt = await myContract.methods.myMethod(123).send({from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'});
// or sending and using the events
const sendObject = myContract.methods.myMethod(123).send({from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'});
sendObject.on('transactionHash', function(hash){
...
});
sendObject.on('receipt', function(receipt){
...
});
sendObject.on('confirmation', function(confirmationNumber, receipt){
...
});
sendObject.on('error', function(error, receipt) {
...
});Returns ContractMethodsInterface<Abi>
- Either returns PayableMethodObject or NonPayableMethodObject based on the definitions of the json interface of that contract.
provider
Returns undefined | string | SupportedProviders<API>
Parameters
provider: undefined | string | SupportedProviders<API>
Returns void
requestManager
Returns Web3RequestManager<API>
subscriptionManager
Returns undefined | Web3SubscriptionManager<API, RegisteredSubs>
publictransactionBlockTimeout
Returns number
Parameters
value: number
Returns void
transactionBuilder
Returns undefined | TransactionBuilder<unknown>
Parameters
val: undefined | TransactionBuilder<unknown>
Returns void
publictransactionConfirmationBlocks
Returns number
Parameters
value: number
Returns void
publictransactionConfirmationPollingInterval
Returns undefined | number
Parameters
value: undefined | number
Returns void
publictransactionPollingInterval
Returns number
Parameters
value: number
Returns void
publictransactionPollingTimeout
Returns number
Parameters
value: number
Returns void
publictransactionReceiptPollingInterval
Returns undefined | number
Parameters
value: undefined | number
Returns void
publictransactionSendTimeout
Returns number
Parameters
value: number
Returns void
transactionTypeParser
Returns undefined | TransactionTypeParser
Parameters
val: undefined | TransactionTypeParser
Returns void
wallet
Returns undefined | Web3BaseWallet<Web3BaseWalletAccount>
Methods
publicclone
Clones the current contract instance. This doesn’t deploy contract on blockchain and only creates a local clone.
Returns Contract<Abi>
- The new contract instance.
const contract1 = new eth.Contract(abi, address, {gasPrice: '12345678', from: fromAddress});
const contract2 = contract1.clone();
contract2.options.address = address2;
(contract1.options.address !== contract2.options.address);
> true
publicdeploy
Call this function to deploy the contract to the blockchain. After successful deployment the promise will resolve with a new contract instance.
myContract.deploy({
data: '0x12345...',
arguments: [123, 'My String']
})
.send({
from: '0x1234567890123456789012345678901234567891',
gas: 1500000,
gasPrice: '30000000000000'
}, function(error, transactionHash){ ... })
.on('error', function(error){ ... })
.on('transactionHash', function(transactionHash){ ... })
.on('receipt', function(receipt){
console.log(receipt.contractAddress) // contains the new contract address
})
.on('confirmation', function(confirmationNumber, receipt){ ... })
.then(function(newContractInstance){
console.log(newContractInstance.options.address) // instance with the new contract address
});
// When the data is already set as an option to the contract itself
myContract.options.data = '0x12345...';
myContract.deploy({
arguments: [123, 'My String']
})
.send({
from: '0x1234567890123456789012345678901234567891',
gas: 1500000,
gasPrice: '30000000000000'
})
.then(function(newContractInstance){
console.log(newContractInstance.options.address) // instance with the new contract address
});
// Simply encoding
myContract.deploy({
data: '0x12345...',
arguments: [123, 'My String']
})
.encodeABI();
> '0x12345...0000012345678765432'
// Gas estimation
myContract.deploy({
data: '0x12345...',
arguments: [123, 'My String']
})
.estimateGas(function(err, gas){
console.log(gas);
});Parameters
optionaldeployOptions: { arguments?: ContractConstructorArgs<Abi>; data?: string }
Returns { arguments: []; encodeABI: () => string; estimateGas: <ReturnFormat>(options?: PayableCallOptions, returnFormat?: ReturnFormat) => Promise<NumberTypes[ReturnFormat[number]]>; send: (options?: PayableCallOptions) => Web3PromiEvent<Contract<Abi>, SendTransactionEvents<{ bytes: HEX; number: BIGINT }>> }
- The transaction object
arguments: []
encodeABI: () => string
Returns string
estimateGas: <ReturnFormat>(options?: PayableCallOptions, returnFormat?: ReturnFormat) => Promise<NumberTypes[ReturnFormat[number]]>
Type parameters
- ReturnFormat: DataFormat = { bytes: HEX; number: BIGINT }
Parameters
optionaloptions: PayableCallOptions
returnFormat: ReturnFormat = ...
Returns Promise<NumberTypes[ReturnFormat[number]]>
send: (options?: PayableCallOptions) => Web3PromiEvent<Contract<Abi>, SendTransactionEvents<{ bytes: HEX; number: BIGINT }>>
Parameters
optionaloptions: PayableCallOptions
Returns Web3PromiEvent<Contract<Abi>, SendTransactionEvents<{ bytes: HEX; number: BIGINT }>>
emit
Type parameters
- K: CONFIG_CHANGE
Parameters
eventName: K
params: { CONFIG_CHANGE: { name: handleRevert; newValue: boolean; oldValue: boolean } | { name: defaultAccount; newValue: undefined | string; oldValue: undefined | string } | { name: defaultBlock; newValue: BlockNumberOrTag; oldValue: BlockNumberOrTag } | { name: transactionSendTimeout; newValue: number; oldValue: number } | { name: transactionBlockTimeout; newValue: number; oldValue: number } | { name: transactionConfirmationBlocks; newValue: number; oldValue: number } | { name: transactionPollingInterval; newValue: number; oldValue: number } | { name: transactionPollingTimeout; newValue: number; oldValue: number } | { name: transactionReceiptPollingInterval; newValue: undefined | number; oldValue: undefined | number } | { name: transactionConfirmationPollingInterval; newValue: undefined | number; oldValue: undefined | number } | { name: blockHeaderTimeout; newValue: number; oldValue: number } | { name: maxListenersWarningThreshold; newValue: number; oldValue: number } | { name: defaultNetworkId; newValue: undefined | Numbers; oldValue: undefined | Numbers } | { name: defaultChain; newValue: string; oldValue: string } | { name: defaultHardfork; newValue: string; oldValue: string } | { name: defaultCommon; newValue: undefined | Common; oldValue: undefined | Common } | { name: defaultTransactionType; newValue: Numbers; oldValue: Numbers } | { name: defaultMaxPriorityFeePerGas; newValue: Numbers; oldValue: Numbers } | { name: enableExperimentalFeatures; newValue: { useRpcCallSpecification: boolean; useSubscriptionWhenCheckingBlockTimeout: boolean }; oldValue: { useRpcCallSpecification: boolean; useSubscriptionWhenCheckingBlockTimeout: boolean } } | { name: transactionBuilder; newValue: undefined | TransactionBuilder<unknown>; oldValue: undefined | TransactionBuilder<unknown> } | { name: transactionTypeParser; newValue: undefined | TransactionTypeParser; oldValue: undefined | TransactionTypeParser } }[K]
Returns void
eventNames
Returns (string | symbol)[]
getConfig
Returns Web3ConfigOptions
getContextObject
Returns Web3ContextObject<EthExecutionAPI, { logs: typeof LogsSubscription; newBlockHeaders: typeof NewHeadsSubscription; newHeads: typeof NewHeadsSubscription }>
getMaxListeners
Returns number
publicgetPastEvents
Gets past events for this contract.
const events = await myContract.getPastEvents('MyEvent', {
filter: {myIndexedParam: [20,23], myOtherIndexedParam: '0x123456789...'}, // Using an array means OR: e.g. 20 or 23
fromBlock: 0,
toBlock: 'latest'
});
> [{
returnValues: {
myIndexedParam: 20,
myOtherIndexedParam: '0x123456789...',
myNonIndexParam: 'My String'
},
raw: {
data: '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385',
topics: ['0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7', '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385']
},
event: 'MyEvent',
signature: '0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7',
logIndex: 0,
transactionIndex: 0,
transactionHash: '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385',
blockHash: '0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7',
blockNumber: 1234,
address: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'
},{
...
}]Type parameters
- ReturnFormat: DataFormat = { bytes: HEX; number: BIGINT }
Parameters
optionalreturnFormat: ReturnFormat
Return format
Returns Promise<(string | EventLog)[]>
- An array with the past event
Objects
, matching the given event name and filter.
link
Type parameters
- T: Web3Context<unknown, any, T>
Parameters
parentContext: T
Returns void
listenerCount
Type parameters
- K: CONFIG_CHANGE
Parameters
eventName: K
Returns number
listeners
Type parameters
- K: CONFIG_CHANGE
Parameters
eventName: K
Returns Function[]
off
Type parameters
- K: CONFIG_CHANGE
Parameters
eventName: K
fn: Web3EventCallback<{ CONFIG_CHANGE: { name: handleRevert; newValue: boolean; oldValue: boolean } | { name: defaultAccount; newValue: undefined | string; oldValue: undefined | string } | { name: defaultBlock; newValue: BlockNumberOrTag; oldValue: BlockNumberOrTag } | { name: transactionSendTimeout; newValue: number; oldValue: number } | { name: transactionBlockTimeout; newValue: number; oldValue: number } | { name: transactionConfirmationBlocks; newValue: number; oldValue: number } | { name: transactionPollingInterval; newValue: number; oldValue: number } | { name: transactionPollingTimeout; newValue: number; oldValue: number } | { name: transactionReceiptPollingInterval; newValue: undefined | number; oldValue: undefined | number } | { name: transactionConfirmationPollingInterval; newValue: undefined | number; oldValue: undefined | number } | { name: blockHeaderTimeout; newValue: number; oldValue: number } | { name: maxListenersWarningThreshold; newValue: number; oldValue: number } | { name: defaultNetworkId; newValue: undefined | Numbers; oldValue: undefined | Numbers } | { name: defaultChain; newValue: string; oldValue: string } | { name: defaultHardfork; newValue: string; oldValue: string } | { name: defaultCommon; newValue: undefined | Common; oldValue: undefined | Common } | { name: defaultTransactionType; newValue: Numbers; oldValue: Numbers } | { name: defaultMaxPriorityFeePerGas; newValue: Numbers; oldValue: Numbers } | { name: enableExperimentalFeatures; newValue: { useRpcCallSpecification: boolean; useSubscriptionWhenCheckingBlockTimeout: boolean }; oldValue: { useRpcCallSpecification: boolean; useSubscriptionWhenCheckingBlockTimeout: boolean } } | { name: transactionBuilder; newValue: undefined | TransactionBuilder<unknown>; oldValue: undefined | TransactionBuilder<unknown> } | { name: transactionTypeParser; newValue: undefined | TransactionTypeParser; oldValue: undefined | TransactionTypeParser } }[K]>
Returns void
on
Type parameters
- K: CONFIG_CHANGE
Parameters
eventName: K
fn: Web3EventCallback<{ CONFIG_CHANGE: { name: handleRevert; newValue: boolean; oldValue: boolean } | { name: defaultAccount; newValue: undefined | string; oldValue: undefined | string } | { name: defaultBlock; newValue: BlockNumberOrTag; oldValue: BlockNumberOrTag } | { name: transactionSendTimeout; newValue: number; oldValue: number } | { name: transactionBlockTimeout; newValue: number; oldValue: number } | { name: transactionConfirmationBlocks; newValue: number; oldValue: number } | { name: transactionPollingInterval; newValue: number; oldValue: number } | { name: transactionPollingTimeout; newValue: number; oldValue: number } | { name: transactionReceiptPollingInterval; newValue: undefined | number; oldValue: undefined | number } | { name: transactionConfirmationPollingInterval; newValue: undefined | number; oldValue: undefined | number } | { name: blockHeaderTimeout; newValue: number; oldValue: number } | { name: maxListenersWarningThreshold; newValue: number; oldValue: number } | { name: defaultNetworkId; newValue: undefined | Numbers; oldValue: undefined | Numbers } | { name: defaultChain; newValue: string; oldValue: string } | { name: defaultHardfork; newValue: string; oldValue: string } | { name: defaultCommon; newValue: undefined | Common; oldValue: undefined | Common } | { name: defaultTransactionType; newValue: Numbers; oldValue: Numbers } | { name: defaultMaxPriorityFeePerGas; newValue: Numbers; oldValue: Numbers } | { name: enableExperimentalFeatures; newValue: { useRpcCallSpecification: boolean; useSubscriptionWhenCheckingBlockTimeout: boolean }; oldValue: { useRpcCallSpecification: boolean; useSubscriptionWhenCheckingBlockTimeout: boolean } } | { name: transactionBuilder; newValue: undefined | TransactionBuilder<unknown>; oldValue: undefined | TransactionBuilder<unknown> } | { name: transactionTypeParser; newValue: undefined | TransactionTypeParser; oldValue: undefined | TransactionTypeParser } }[K]>
Returns void
once
Type parameters
- K: CONFIG_CHANGE
Parameters
eventName: K
fn: Web3EventCallback<{ CONFIG_CHANGE: { name: handleRevert; newValue: boolean; oldValue: boolean } | { name: defaultAccount; newValue: undefined | string; oldValue: undefined | string } | { name: defaultBlock; newValue: BlockNumberOrTag; oldValue: BlockNumberOrTag } | { name: transactionSendTimeout; newValue: number; oldValue: number } | { name: transactionBlockTimeout; newValue: number; oldValue: number } | { name: transactionConfirmationBlocks; newValue: number; oldValue: number } | { name: transactionPollingInterval; newValue: number; oldValue: number } | { name: transactionPollingTimeout; newValue: number; oldValue: number } | { name: transactionReceiptPollingInterval; newValue: undefined | number; oldValue: undefined | number } | { name: transactionConfirmationPollingInterval; newValue: undefined | number; oldValue: undefined | number } | { name: blockHeaderTimeout; newValue: number; oldValue: number } | { name: maxListenersWarningThreshold; newValue: number; oldValue: number } | { name: defaultNetworkId; newValue: undefined | Numbers; oldValue: undefined | Numbers } | { name: defaultChain; newValue: string; oldValue: string } | { name: defaultHardfork; newValue: string; oldValue: string } | { name: defaultCommon; newValue: undefined | Common; oldValue: undefined | Common } | { name: defaultTransactionType; newValue: Numbers; oldValue: Numbers } | { name: defaultMaxPriorityFeePerGas; newValue: Numbers; oldValue: Numbers } | { name: enableExperimentalFeatures; newValue: { useRpcCallSpecification: boolean; useSubscriptionWhenCheckingBlockTimeout: boolean }; oldValue: { useRpcCallSpecification: boolean; useSubscriptionWhenCheckingBlockTimeout: boolean } } | { name: transactionBuilder; newValue: undefined | TransactionBuilder<unknown>; oldValue: undefined | TransactionBuilder<unknown> } | { name: transactionTypeParser; newValue: undefined | TransactionTypeParser; oldValue: undefined | TransactionTypeParser } }[K]>
Returns void
registerPlugin
Parameters
plugin: Web3PluginBase<unknown>
Returns void
removeAllListeners
Returns EventEmitter
setConfig
Parameters
options: Partial<Web3ConfigOptions>
Returns void
setMaxListenerWarningThreshold
Parameters
maxListenersWarningThreshold: number
Returns void
setProvider
Parameters
optionalprovider: string | SupportedProviders<EthExecutionAPI>
Returns boolean
use
Type parameters
- T: Web3Context<unknown, any, T>
- T2: unknown[]
Parameters
ContextRef: Web3ContextConstructor<T, T2>
rest...args: [...T2[]]
Returns T
staticfromContextObject
Type parameters
- T: Web3Context<unknown, any, T>
- T3: unknown[]
Parameters
this: Web3ContextConstructor<T, T3>
rest...args: [Web3ContextObject<unknown, any>, ...T3[]]
Returns T
The class designed to interact with smart contracts on the Ethereum blockchain.