Skip to main content

Account

An Account extends Provider and inherits all of its methods.

It also introduces new methods that allow Accounts to create and verify signatures with a custom Signer.

This API is the primary way to interact with an account contract on StarkNet.

Creating an instance

To create a new instance of the Account, first an account contract must be deployed. Also there needs to be a Provider instance that will be passed in the constructor and key pair for the account.

new starknet.Account(Provider, address, starkKeyPair)

Properties

account.address => string

The address of the account contract.

Methods

account.getNonce(blockIdentifier) => Promise < BigNumberish >

Gets the nonce of the account with respect to a specific block.

blockIdentifier - optional blockIdentifier. Defaults to 'pending'.

Returns the nonce of the account.


account.estimateInvokeFee(calls [ , estimateFeeDetails ]) => Promise < EstimateFeeResponse >

Estimate Fee for executing an INVOKE transaction on starknet.

The calls object structure:

  • calls.contractAddress - Address of the contract
  • calls.entrypoint - Entrypoint of the call (method name)
  • calls.calldata - Payload for the invoking method

The estimateFeeDetails object may include any of:

  • estimateFeeDetails.blockIdentifier - Block Identifier for the transaction
  • estimateFeeDetails.nonce - Nonce for the transaction
EstimateFeeResponse
{
overall_fee: BN;
gas_consumed?: BN;
gas_price?: BN;
}

account.estimateDeclareFee(contractPayload [ , estimateFeeDetails ]) => Promise < EstimateFeeResponse >

Estimate Fee for executing a DECLARE transaction on starknet.

The contractPayload object structure:

  • contractPayload.contract - The compiled contract
  • contractPayload.classHash - This can be obtained by using starknet-cli. Once the classHash is included in CompiledContract, this can be removed

The estimateFeeDetails object may include any of:

  • estimateFeeDetails.blockIdentifier - Block Identifier for the transaction
  • estimateFeeDetails.nonce - Nonce for the transaction
EstimateFeeResponse
{
overall_fee: BN;
gas_consumed?: BN;
gas_price?: BN;
}

account.estimateAccountDeployFee(contractPayload [ , estimateFeeDetails ]) => Promise < EstimateFeeResponse >

Estimate Fee for executing a DEPLOY_ACCOUNT transaction on starknet

The contractPayload object structure:

  • contractPayload.contract - The compiled contract to be declared
  • contractPayload.classHash - This can be obtained by using starknet-cli. Once the classHash is included in CompiledContract, this can be removed

The estimateFeeDetails object may include any of:

  • estimateFeeDetails.blockIdentifier - Block Identifier for the transaction
  • estimateFeeDetails.nonce - Nonce for the transaction
EstimateFeeResponse
{
overall_fee: BN;
gas_consumed?: BN;
gas_price?: BN;
}

account.execute(transactions [ , abi , transactionsDetail ]) => Promise < InvokeFunctionResponse >

Executes one or multiple calls using the account contract.

The transactions object structure:

  • contractPayload.contractAddress - the address of the contract
  • contractPayload.entrypoint - the entrypoint of the contract
  • contractPayload.calldata - (defaults to []) the calldata
  • contractPayload.signature - (defaults to []) the signature

abi - (optional) the abi of the contract for better displaying

The transactionsDetail object may include any of:

  • transactionsDetail.maxFee - Max Fee that that will be used to execute the call(s)
  • transactionsDetail.nonce - Nonce for the transaction
  • transactionsDetail.version - Version for the transaction (default is 1)
InvokeFunctionResponse
{
transaction_hash: string;
};

account.declare(contractPayload [ , transactionsDetail ]) => Promise < DeclareContractResponse >

Declares a given compiled contract (json) to starknet.

The contractPayload object consists of:

  • contractPayload.contract - The compiled contract
  • contractPayload.classHash - Hash of the compiled contract

The transactionsDetail object may include any of:

  • transactionsDetail.maxFee - Max Fee that that will be used to execute the call(s)
  • transactionsDetail.nonce - Nonce for the transaction
  • transactionsDetail.version - Version for the transaction (default is 1)

Note: Once the classHash is included in CompiledContract, this parameter can be removed. Currently it can be pre-computed from starknet-cli.

Example:

const declareTx = await account.declare({
contract: compiledErc20,
// classHash is pre-computed from starknet-cli
classHash: '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a',
});
DeclareContractResponse
{
transaction_hash: string;
class_hash: string;
};

account.deployAccount(contractPayload [ , transactionsDetail ]) => Promise < DeployContractResponse >

Declares a given compiled contract (json) to starknet.

The contractPayload object consists of:

  • contractPayload.classHash - Hash of the compiled contract
  • contractPayload.constructorCalldata - optional
  • contractPayload.addressSalt - optional
  • contractPayload.contractAddress - optional

The transactionsDetail object may include any of:

  • transactionsDetail.maxFee - Max Fee that that will be used to execute the call(s)
  • transactionsDetail.nonce - Nonce for the transaction
  • transactionsDetail.version - Version for the transaction (default is 1)

Note: Once the classHash is included in CompiledContract, this parameter can be removed. Currently it can be pre-computed from starknet-cli.

DeployContractResponse
{
contract_address: string;
transaction_hash: string;
};

account.signMessage(typedData) => Promise < Signature >

Sign an JSON object for off-chain usage with the starknet private key and return the signature. This adds a message prefix so it cant be interchanged with transactions.

typedData - JSON object to be signed

Signature
string[];

account.hashMessage(typedData) => Promise < string >

Hash a JSON object with pederson hash and return the hash. This adds a message prefix so it cant be interchanged with transactions.

typedData - JSON object to be signed

Returns the hash of the JSON object.


account.verifyMessageHash(hash, signature) => Promise < boolean >

Verify a signature of a given hash.

WARNING

This method is not recommended, use verifyMessage instead


account.verifyMessage(typedData, signature) => Promise < boolean >

Verify a signature of a JSON object.

typedData - JSON object to be verified signature - signature of the JSON object

Returns true if the signature is valid, false otherwise


account.getSuggestedMaxFee(estimateFeeAction, details) => Promise < BigNumberish >

Gets Suggested Max Fee based on the transaction type.

The details object may include any of:

  • details.blockIdentifier
  • details.nonce