Skip to main content
Version: Next

Class: Contract

Implements

Indexable

[key: string]: AsyncContractFunction | any

Constructors

constructor

new Contract(options): Contract

Parameters

NameTypeDescription
optionsContractOptionsabi: Abi of the contract object (required) - address: address to connect to (required) - providerOrAccount?: Provider or Account to attach to (fallback to defaultProvider) - parseRequest?: compile and validate arguments (optional, default true) - parseResponse?: Parse elements of the response array and structuring them into response object (optional, default true)

Returns

Contract

Defined in

src/contract/default.ts:135

Properties

abi

abi: Abi

Implementation of

ContractInterface.abi

Defined in

src/contract/default.ts:101


address

address: string

Implementation of

ContractInterface.address

Defined in

src/contract/default.ts:103


providerOrAccount

providerOrAccount: ProviderOrAccount

Implementation of

ContractInterface.providerOrAccount

Defined in

src/contract/default.ts:105


classHash

Optional classHash: string

Implementation of

ContractInterface.classHash

Defined in

src/contract/default.ts:107


structs

Private structs: Object

Index signature

[name: string]: AbiStruct

Defined in

src/contract/default.ts:109


events

Private events: AbiEvents

Defined in

src/contract/default.ts:111


functions

Readonly functions: Object

Index signature

[name: string]: AsyncContractFunction

Implementation of

ContractInterface.functions

Defined in

src/contract/default.ts:113


callStatic

Readonly callStatic: Object

Index signature

[name: string]: AsyncContractFunction

Implementation of

ContractInterface.callStatic

Defined in

src/contract/default.ts:115


populateTransaction

Readonly populateTransaction: Object

Index signature

[name: string]: ContractFunction

Implementation of

ContractInterface.populateTransaction

Defined in

src/contract/default.ts:117


estimateFee

Readonly estimateFee: Object

Index signature

[name: string]: ContractFunction

Implementation of

ContractInterface.estimateFee

Defined in

src/contract/default.ts:119


callData

Private callData: CallData

Defined in

src/contract/default.ts:123


withOptionsProps

Optional withOptionsProps: WithOptions

Defined in

src/contract/default.ts:125

Methods

factory

factory(params, details?): Promise<Contract>

Factory method to declare and deploy a contract creating a new Contract instance

It handles the entire lifecycle: compiles constructor calldata, declares the contract class, deploys an instance, and returns a ready-to-use Contract object.

Parameters

NameTypeDescription
paramsFactoryParamsFactory parameters containing Contract Class details and deployment options
detailsUniversalDetails-

Returns

Promise<Contract>

Promise that resolves to a deployed Contract instance with address and transaction hash

Throws

Error if deployment fails or contract_address is not returned

Example

// Deploy an ERC20 contract
const contract = await Contract.factory({
compiledContract: erc20CompiledContract,
account: myAccount,
casm: erc20Casm,
constructorArguments: {
name: 'MyToken',
symbol: 'MTK',
decimals: 18,
initial_supply: { low: 1000000, high: 0 },
recipient: myAccount.address
}
});

console.log('Contract deployed at:', contract.address);
```\

#### Defined in

[src/contract/default.ts:241](https://github.com/starknet-io/starknet.js/blob/develop/src/contract/default.ts#L241)

___

### withOptions

▸ **withOptions**(`options`): [`Contract`](/docs/next/API/classes/Contract)

#### Parameters

| Name | Type |
| :------ | :------ |
| `options` | [`WithOptions`](/docs/next/API/modules#withoptions) |

#### Returns

[`Contract`](/docs/next/API/classes/Contract)

#### Defined in

[src/contract/default.ts:197](https://github.com/starknet-io/starknet.js/blob/develop/src/contract/default.ts#L197)

___

### attach

▸ **attach**(`address`, `abi?`): `void`

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

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `address` | `string` | address of the contract |
| `abi?` | [`Abi`](/docs/next/API/modules#abi) | optional new abi to use with the contract |

#### Returns

`void`

#### Implementation of

[ContractInterface](/docs/next/API/classes/ContractInterface).[attach](/docs/next/API/classes/ContractInterface#attach)

#### Defined in

[src/contract/default.ts:202](https://github.com/starknet-io/starknet.js/blob/develop/src/contract/default.ts#L202)

___

### isDeployed

▸ **isDeployed**(): `Promise`<[`Contract`](/docs/next/API/classes/Contract)\>

Verifies that the contract is deployed at the configured address

#### Returns

`Promise`<[`Contract`](/docs/next/API/classes/Contract)\>

Promise that resolves when contract is confirmed to exist at the address

**`Throws`**

Error if the contract is not deployed at the address

#### Implementation of

[ContractInterface](/docs/next/API/classes/ContractInterface).[isDeployed](/docs/next/API/classes/ContractInterface#isdeployed)

#### Defined in

[src/contract/default.ts:266](https://github.com/starknet-io/starknet.js/blob/develop/src/contract/default.ts#L266)

___

### call

▸ **call**(`method`, `args?`, `«destructured»?`): `Promise`<[`CallResult`](/docs/next/API/modules#callresult)\>

Calls a method on a contract

#### Parameters

| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
| `method` | `string` | `undefined` | name of the method |
| `args` | [`ArgsOrCalldata`](/docs/next/API/modules#argsorcalldata) | `[]` | Array of the arguments for the call |
| `«destructured»` | [`CallOptions`](/docs/next/API/modules#calloptions) | `{}` | optional blockIdentifier |

#### Returns

`Promise`<[`CallResult`](/docs/next/API/modules#callresult)\>

Result of the call as an array with key value pars

#### Implementation of

[ContractInterface](/docs/next/API/classes/ContractInterface).[call](/docs/next/API/classes/ContractInterface#call)

#### Defined in

[src/contract/default.ts:277](https://github.com/starknet-io/starknet.js/blob/develop/src/contract/default.ts#L277)

___

### invoke

▸ **invoke**(`method`, `args?`, `«destructured»?`): `Promise`<\{ `transaction_hash`: `string` }\>

Invokes a method on a contract

#### Parameters

| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
| `method` | `string` | `undefined` | name of the method |
| `args` | [`ArgsOrCalldata`](/docs/next/API/modules#argsorcalldata) | `[]` | Array of the arguments for the invoke or Calldata |
| `«destructured»` | [`ExecuteOptions`](/docs/next/API/modules#executeoptions) | `{}` | |

#### Returns

`Promise`<\{ `transaction_hash`: `string` }\>

Add Transaction Response

#### Implementation of

[ContractInterface](/docs/next/API/classes/ContractInterface).[invoke](/docs/next/API/classes/ContractInterface#invoke)

#### Defined in

[src/contract/default.ts:318](https://github.com/starknet-io/starknet.js/blob/develop/src/contract/default.ts#L318)

___

### estimate

▸ **estimate**(`method`, `args?`): `Promise`<[`EstimateFeeResponseOverhead`](/docs/next/API/modules#estimatefeeresponseoverhead)\>

Estimates a method on a contract

#### Parameters

| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
| `method` | `string` | `undefined` | name of the method |
| `args` | [`ArgsOrCalldata`](/docs/next/API/modules#argsorcalldata) | `[]` | Array of the arguments for the call or Calldata |

#### Returns

`Promise`<[`EstimateFeeResponseOverhead`](/docs/next/API/modules#estimatefeeresponseoverhead)\>

#### Implementation of

[ContractInterface](/docs/next/API/classes/ContractInterface).[estimate](/docs/next/API/classes/ContractInterface#estimate)

#### Defined in

[src/contract/default.ts:361](https://github.com/starknet-io/starknet.js/blob/develop/src/contract/default.ts#L361)

___

### populate

▸ **populate**(`method`, `args?`): [`Call`](/docs/next/API/modules#call)

Calls a method on a contract

#### Parameters

| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
| `method` | `string` | `undefined` | name of the method |
| `args` | [`RawArgs`](/docs/next/API/modules#rawargs) | `[]` | Array of the arguments for the call or Calldata |

#### Returns

[`Call`](/docs/next/API/modules#call)

Invocation object

#### Implementation of

[ContractInterface](/docs/next/API/classes/ContractInterface).[populate](/docs/next/API/classes/ContractInterface#populate)

#### Defined in

[src/contract/default.ts:378](https://github.com/starknet-io/starknet.js/blob/develop/src/contract/default.ts#L378)

___

### parseEvents

▸ **parseEvents**(`receipt`): [`ParsedEvents`](/docs/next/API/modules#parsedevents)

Parse contract events of a GetTransactionReceiptResponse received from waitForTransaction. Based on contract's abi

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `receipt` | [`GetTransactionReceiptResponse`](/docs/next/API/modules#gettransactionreceiptresponse) | transaction receipt |

#### Returns

[`ParsedEvents`](/docs/next/API/modules#parsedevents)

Events parsed

#### Implementation of

[ContractInterface](/docs/next/API/classes/ContractInterface).[parseEvents](/docs/next/API/classes/ContractInterface#parseevents)

#### Defined in

[src/contract/default.ts:389](https://github.com/starknet-io/starknet.js/blob/develop/src/contract/default.ts#L389)

___

### isCairo1

▸ **isCairo1**(): `boolean`

tells if the contract comes from a Cairo 1 contract

#### Returns

`boolean`

TRUE if the contract comes from a Cairo1 contract

**`Example`**

```typescript
const isCairo1: boolean = myContract.isCairo1();

Implementation of

ContractInterface.isCairo1

Defined in

src/contract/default.ts:420


getVersion

getVersion(): Promise<ContractVersion>

Retrieves the version of the contract (cairo version & compiler version)

Returns

Promise<ContractVersion>

Implementation of

ContractInterface.getVersion

Defined in

src/contract/default.ts:424


typedv2

typedv2<TAbi>(tAbi): TypedContractV2<TAbi>

Returns a typed instance of ContractV2 based on the supplied ABI.

Type parameters

NameType
TAbiextends readonly (AbiImpl | AbiFunction | AbiInterface | AbiConstructor | AbiEvent | AbiStruct | AbiEnum)[]

Parameters

NameTypeDescription
tAbiTAbiThe ABI (Abstract Binary Interface) of the ContractV2.

Returns

TypedContractV2<TAbi>

  • A typed instance of ContractV2.

Implementation of

ContractInterface.typedv2

Defined in

src/contract/default.ts:428