Docs
Starknet.js 101
Account

Account

The Account API extends the primary features of the Provider API to also include the Signer API, which can be used to create and verify signatures.

This API unlike the Provider, can make both reads and write calls to the blockchain, and as such is the primary way to interact with StarkNet contracts.

Instantiation

In order to create an account instance, an account contract must first be deployed in advance. The provider instance and the keypair for the account should be passed into the constructor of the account. To instantiate an Account:

new starknet.Account(Provider, address, starkKeyPair)

Properties

  1. account.address => string

This returns the address of the account contract.

Methods

  1. 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.

  1. 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

Response​

{
  overall_fee: BN;  
  gas_consumed?: BN;  
  gas_price?: BN;
}
  1. 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

Response​

{
  overall_fee: BN;  
  gas_consumed?: BN;  
  gas_price?: BN;
}
  1. 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

Response​

{
  overall_fee: BN;  
  gas_consumed?: BN;  
  gas_price?: BN;
}
  1. 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 will be used to execute the call(s)
  • transactionsDetail.nonce - Nonce for the transaction
  • transactionsDetail.version - Version for the transaction (default is 1)

Response​

{
  transaction_hash: string;
};
  1. 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 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',
});
Response​
{
  transaction_hash: string;  
  class_hash: string;
};
  1. 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 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.

Response​
{
  contract_address: string;  
  transaction_hash: string;
};
  1. account.signMessage(typedData) => Promise < Signature >

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

typedData - JSON object to be signed

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

Hash a JSON object with Pedersen hash and return the hash. This adds a message prefix so it can't be interchanged with transactions.

typedData - JSON object to be signed. Returns the hash of the JSON object.

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

Verify a signature of a given hash.

WARNING This method is not recommended, use verifyMessage instead

  1. 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

  1. 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