Provider
The Provider is an API from Starknet.js, that enables you to interact with StarkNet, without necessarily signing messages, e.g when performing read calls. You can read states without signing messages, but you cannot alter or modify the contract’s state.
Default Provider
Instantiation
Starknet.js library provides several options which should cover the use case of a vast majority but also includes a robust function for a more customized configuration if necessary. Instantiating a new Provider can be done with this single line of code:
new starknet.Provider(optionsOrProvider)
The options for the provider depends on the network. The structure of the options object is:
- options.sequencer - Options for sequencer provider
- options.rpc - Options for RPC provider
You could choose to instantiate using the default means, which automatically creates a Provider instance on StarkNet's alpha-goerli:
const provider = new starknet.Provider()
Or you could explicitly specify a network using the network keyword:
const provider = new starknet.Provider({
sequencer: {
network: 'mainnet-alpha' // or 'goerli-alpha'
}
})
Since using the network keyword is limited to just mainnet-alpha and goerli-alpha, Starknet.js also provides a means to connect to any custom network on StarkNet using the baseurl:
const provider = new starknet.Provider({
sequencer: {
baseUrl: 'https://alpha4.starknet.io',
feederGatewayUrl: 'feeder_gateway',
gatewayUrl: 'gateway',
}
})
Methods
Starknet.js provides you with a plethora of method calls when using the default provider:
- provider.getChainId() => Promise < StarknetChainId >
Returns the chain Id for the current network.
- provider.callContract(call [ , blockIdentifier ]) => Promise < CallContractResponse >
Calls a function on the StarkNet contract.
Response
{
result: string[];
}
- provider.getBlock(blockIdentifier) => Promise < GetBlockResponse >
Gets the block information.
Response
{
accepted_time: number;
block_hash: string;
block_number: number;
gas_price: string;
new_root: string;
old_root?: string;
parent_hash: string;
sequencer: string;
status: 'NOT_RECEIVED' | 'RECEIVED' | 'PENDING' | 'ACCEPTED_ON_L2' | 'ACCEPTED_ON_L1' | 'REJECTED';
transactions: Array<string>;
starknet_version?: string;
}
- provider.getClassAt(contractAddress, blockIdentifier) => Promise < ContractClass >
Gets the contract class of the deployed contract.
- provider.getInvokeEstimateFee(invocationWithTxType, invocationDetails, blockIdentifier) => Promise < EstimateFeeResponse >
Estimates fee for invoke transaction.
Response
{
overall_fee: BN;
gas_consumed?: BN;
gas_price?: BN;
}
- provider.getTransactionReceipt(txHash) => Promise < GetTransactionReceiptResponse >
Gets the status of a transaction.
Response
{
transaction_hash: string;
status: 'NOT_RECEIVED' | 'RECEIVED' | 'PENDING' | 'ACCEPTED_ON_L2' | 'ACCEPTED_ON_L1' | 'REJECTED';
actual_fee?: string;
status_data?: string;
messages_sent?: Array<MessageToL1>;
events?: Array<Event>;
l1_origin_message?: MessageToL2;
}
- provider.getTransaction(txHash) => Promise < GetTransactionResponse >
Gets the transaction information from a tx hash.
Response
{
transaction_hash: string;
version?: string;
signature?: Signature;
max_fee?: string;
nonce?: string;
contract_address?: string;
entry_point_selector?: string;
calldata?: RawCalldata;
contract_class?: ContractClass;
sender_address?: string;
}
- provider.getNonce(contractAddress, blockIdentifier) => Promise < BigNumberish >
Gets the nonce of the provided contractAddress.
- provider.getStorageAt(contractAddress, key, blockIdentifier) => Promise < string >
Gets the contract's storage variable at a specific key.
- provider.deployContract(payload [ , abi ]) => Promise < DeployContractResponse >
Deploys a contract on Starknet.
Response
{
transaction_hash: string;
contract_address: string;
};
- provider.declareContract(transaction, details) => Promise < DeclareContractResponse >
Declare a contract on Starknet.
Response
{
transaction_hash: string;
class_hash: string;
};
- provider.getDeclareEstimateFee(transaction, details, blockIdentifier) => Promise < EstimateFeeResponse >
Estimate fee for declare transaction.
Response
{
overall_fee: BN;
gas_consumed?: BN;
gas_price?: BN;
};
Sequencer Provider
The default provider from above is also a sequencer provider, but the sequencer provider extends the methods available in the default provider.
Instantiation
To instantiate a sequencer provider:
new starknet.SequencerProvider(optionsOrProvider)
Where the options just like the default provider, take in either the network keyword (if the network is mainnet-alpha or goerli-alpha) else a custom setup containing:
- options.baseUrl - Base URL of the network
- options.feederGatewayUrl - Feeder Gateway Endpoint of the network
- options.gatewayUrl - Gateway Endpoint
- options.headers - [Optional] custom fetch headers
Example Here's a setup for goerli testnet-2:
const provider = new starknet.SequencerProvider({
baseUrl: 'https://alpha4-2.starknet.io',
feederGatewayUrl: 'feeder_gateway',
gatewayUrl: 'gateway',
})
Methods
- provider.getContractAddresses() => Promise < GetContractAddressesResponse >
Gets the smart contract address on the network.
Response
{
Starknet: string;
GpsStatementVerifier: string;
}
- provider.getCode(contractAddress, blockIdentifier) => Promise < GetCodeResponse >
Gets the smart contract code.
Response
{
bytecode: string[];
abi: Abi;
}
- provider.estimateMessageFee(CallL1Handler, blockIdentifier) => Promise < EstimateFeeResponse >
Estimate fee for sending a message to L1.
CallL1Handler
type CallL1Handler = {
from_address: getDecimalString(from_address),
to_address: getHexString(to_address),
entry_point_selector: getSelector(entry_point_selector),
payload: getHexStringArray(payload),
};
EstimateFeeResponse
{
overall_fee: number;
gas_price: number;
gas_usage: number;
unit: string;
}
- provider.getTransactionStatus(txHash) => Promise < GetTransactionStatusResponse >
Gets the status of a transaction.
Response
{
tx_status: 'NOT_RECEIVED' | 'RECEIVED' | 'PENDING' | 'ACCEPTED_ON_L2' | 'ACCEPTED_ON_L1' | 'REJECTED';
block_hash: string;
tx_failure_reason?: {
tx_id: number;
code: string;
error_message: string;
}
}
- provider.getTransactionTrace(txHash) => Promise < GetTransactionTraceResponse >
Gets the transaction trace from a tx hash.
{
validate_invocation?: FunctionInvocation;
function_invocation?: FunctionInvocation;
fee_transfer_invocation?: FunctionInvocation;
signature: Signature;
}
{
FunctionInvocation: {
caller_address: string;
contract_address: string;
calldata: {
[inputName: string]: string | string[] | { type: 'struct'; [k: string]: BigNumberish };
};
call_type?: string;
class_hash?: string;
selector?: string;
entry_point_type?: EntryPointType;
result: Array<any>;
execution_resources: ExecutionResources;
internal_calls: Array<FunctionInvocation>;
events: Array<any>;
messages: Array<any>;
};
}
RPC Provider
Instantiation
An RPC (Remote procedure call) node enables users to make read and write calls to the blockchain. To instantiate an RPC provider: new starknet.RpcProvider(options) Where options can contain: options.nodeUrl - Starknet RPC node url options.headers - [Optional] custom fetch headers options.retries - [Optional] wait for transaction max retries
Example
const provider = new starknet.RpcProvider({
nodeUrl: 'URL_TO_STARKNET_RPC_NODE',
})
Methods
- provider.fetch(method: any, params: any) => Promise < any >
Generic method for users to be able to experiment with RPC methods.
-
provider.getChainId() => Promise < any >
-
provider.getBlock(blockIdentifier) => Promise < GetBlockResponse >
-
provider.getBlockHashAndNumber() => Promise < BlockHashAndNumber >
Response
{
block_hash: BLOCK_HASH;
block_number: BLOCK_NUMBER;
}
- provider.getBlockWithTxHashes(blockIdentifier) => Promise < GetBlockWithTxHashesResponse >
Response
OPENRPC.BlockWithTxHashes
-
provider.getBlockWithTxs(blockIdentifier) => Promise < GetBlockWithTxs >
-
provider.getClassHashAt(blockIdentifier) => Promise < ContractAddress >
-
provider.getTransactionCount(blockIdentifier) => Promise < number >
Gets the transaction count from a block.
- provider.getBlockNumber() => Promise < number >
Gets the latest block number.
- provider.getNonce(contractAddress, blockIdentifier) => Promise < BigNumberish >
Gets the nonce of the provided contractAddress
-
provider.getPendingTransactions() => Promise < PendingTransactions >
-
provider.getStateUpdate(blockIdentifier) => Promise < StateUpdate >
-
provider.getStorageAt(contractAddress, key, blockIdentifier) => Promise < BigNumberish >
-
provider.getTransaction(txHash) => Promise < GetTransactionResponse >
-
provider.getTransactionByHash(txHash) => Promise < GetTransactionByHashResponse >
Response
OPENRPC.Transaction;
-
provider.getTransactionByBlockIdAndIndex(blockIdentifier, index) => Promise < GetTransactionByBlockIdAndIndex >
-
provider.getTransactionReceipt(txHash) => Promise < GetTransactionReceiptResponse >
-
provider.getClass(classHash) => Promise < ContractClass >
-
provider.getInvokeEstimateFee(invocation, invocationDetails, blockIdentifier) => Promise < EstimateFeeResponse >
Response
overall_fee: BN;
gas_consumed?: BN;
gas_price?: BN;
- provider.getDeclareEstimateFee(DeclareContractTransaction, details, blockIdentifier) => Promise < EstimateFeeResponse >
Response
overall_fee: BN;
gas_consumed?: BN;
gas_price?: BN;
- provider.declareContract(DeclareContractTransaction, details) => Promise < DeclareContractResponse >
Response
transaction_hash: string;
class_hash: string;
- provider.deployContract(contract, constructorCalldata, addressSalt) => Promise < DeployContractResponse >
Response
contract_address: string; transaction_hash: string;
-
provider.callContract(call, blockIdentifier) => Promise < CallContractResponse >
-
provider.traceTransaction(transactionHash) => Promise < Trace >
-
provider.traceBlockTransactions(blockHash) => Promise < Traces >
-
provider.getSyncingStats() => Promise < GetSyncingStatsResponse >
Gets the syncing status of the node.
Response
boolean |
{
starting_block_hash: string;
starting_block_num: string;
current_block_hash: string;
current_block_num: string;
highest_block_hash: string;
highest_block_num: string;
}
- provider.getEvents(eventFilter) => Promise < GetEventsResponse >
Gets all the events filtered
EventFilter
type EventFilter = {
fromBlock: string;
toBlock: string;
address: string;
keys: string[];
page_size: number;
page_number: number;
};
Response
{
events: StarknetEmittedEvent[];
page_number: number;
is_last_page: number;
}