Skip to main content

Send Starknet transactions

You can send Starknet transactions using the get-starknet library or the wallet_invokeSnap JSON-RPC method.

Prerequisites

Connect to Starknet from your dapp.

Send a transaction

Send a transaction using the starknet.account.execute() function (with get-starknet) or the starknet_executeTxn method (with wallet_invokeSnap):

const sendStarknetTransaction = async (wallet, contractAddress, entrypoint, calldata) => {
try {
if(wallet?.isConnected !== true){
throw("Wallet not connected");
}

// Send the transaction.
const result = await wallet?.account?.execute({
contractAddress: contractAddress, // The address of the contract.
entrypoint: entrypoint, // The function to call in the contract.
calldata: calldata // The parameters to pass to the function.
});
console.log("Transaction successful:", result);
return result;
} catch (error) {
console.error("Error sending transaction:", error);
}
};

Simplified example

The following is a full, simplified example of connecting to a Starknet account and sending a transaction:

  import { connect } from "get-starknet";

const connectStarknetAccount = async () => {
const starknet = await connect();
await starknet.enable(); // Prompts the user to connect their Starknet account using MetaMask.
return starknet;
};

const sendStarknetTransaction = async (contractAddress, entrypoint, calldata) => {
try {
const starknet = await connectStarknetAccount(); // Ensure the account is connected.

// Send the transaction.
const result = await starknet.account.execute({
contractAddress: contractAddress,
entrypoint: entrypoint,
calldata: calldata
});

console.log("Transaction successful:", result);
return result;
} catch (error) {
console.error("Error sending transaction:", error);
}
};

const contractAddress = "0xYourContractAddress";
const entrypoint = "your_function_name";
const calldata = [/* your function arguments */];

sendStarknetTransaction(contractAddress, entrypoint, calldata);