Skip to main content
Version: 5.14.1

Contract

Contracts can do data transformations in JavaScript based on an ABI. They can also call and invoke to Starknet through a provided Signer.

Contracts allow you to transform Cairo values, like Uint256 to BigNumber. It could also allow users to pass their own transformers, similar to JSON.parse.

Creating an instance​

new starknet.Contract(abi, address, providerOrAccount)

contract.attach(address) for changing the address of the connected contract

contract.connect(providerOrAccount) for changing the provider or account

Properties​

contract.abi => Abi

The ABI the contract was constructed with.

contract.address => string

The address the contract was constructed/connected with.

contract.providerOrAccount => ProviderInterface | AccountInterface

Provider or account that are used to interact with the network.

contract.deployTransactionHash => string | null

If the Contract object is the result of a ContractFactory deployment, this is the transaction which was used to deploy the contract.

Methods​

attach()​

contract.attach(address) => void

Saves the address of the contract deployed on network that will be used for interaction.

address - address of the contract.


connect()​

contract.connect(providerOrAccount) => void

Attaches to new Provider or Account


deployed()​

contract.deployed() => Promise < Contract >

If the Contract object is the result of a ContractFactory deployment, this method will wait for the transaction to be resolved.


call()​

contract.call(method, args, options) => Promise < Result >

Calls a method on a contract.


invoke()​

contract.invoke(method, args, options) => Promise < InvokeFunctionResponse >

Invokes a method on a contract.


estimate()​

contract.estimate(method, args, options) => Promise < any >

Estimates a method on a contract.


populate()​

contract.populate(method, args, options) => Invocation

Meta-Class​

A Meta-Class is a Class which has any of its properties determined at run-time. The Contract object uses a Contract's ABI to determine what methods are available, so the following sections describe the generic ways to interact with the properties added at run-time during the Contract constructor.

Read-Only Methods(constant)​

A constant method (denoted view in Cairo) is read-only and evaluates a small amount of Cairo code against the current blockchain state. It is therefore free and does not require any fee, but cannot make changes to the blockchain state...

contract.METHOD_NAME(...args [ , overrides ]) => Promise < Result >

The type of the result depends on the ABI. Result object will be returned with each parameter available positionally and if the parameter is named, it will also be available by its name.

The overrides object for a read-only method may include:

  • overrides.blockIdentifier

Write Methods (non-constant)​

A non-constant method requires a transaction to be signed and requires payment in the form of a fee to be paid.

contract.METHOD_NAME(...args [ , overrides ]) => Promise < AddTransactionResponse >

Returns a AddTransactionResponse for the transaction after it is sent to the network. This requires that Contract has a signer.

The overrides object for write methods may include any of:

  • overrides.signature - Signature that will be used for the transaction
  • overrides.maxFee - Max Fee for the transaction
  • overrides.nonce - Nonce for the transaction

Write Methods Analysis​

There are several options to analyze properties and results of a write method without actually executing it.

contract.estimateFee.METHOD_NAME( ...args ) => Promise < EstimateFeeResponse >

Returns the estimate units of gas that would be required to execute the METHOD_NAME with args and overrides.

contract.populateTransaction.METHOD_NAME( ...args [ , overrides ] ) ⇒ Call

Returns an Call object which represents the transaction that would need to be signed and submitted to the network to execute METHOD_NAME with args and overrides.

The overrides are identical to the overrides above for write methods.