Skip to main content

Sending a Transaction

This guide walks you through the process of sending a transaction using WalletConnect, specifically for interactions with OP_NET smart contracts.


Steps to Send a Transaction

1. Get Required Parameters

Before sending a transaction, ensure you have the following parameters:

  • account: Wallet account for transaction signing.
import { useWallet } from "@btc-vision/walletconnect";

const { account } = useWallet();

2. Define the Contract Instance

Use the getContract method to define the contract you want to interact with. This step ensures you have a properly typed contract instance that can encode calldata for the desired function.

const contractAddress = "bcrt1qexampleaddress"; // Replace with a valid contract address

const contractInstance = getContract<IOP_20Contract>(
contractAddress, // Contract address
OP_20_ABI, // Contract ABI
provider: account.provider, // Provider instance
network: account.network, // Target network
address: account.address // Sender's address
);
Contract Interaction

Ensure the contract instance matches the contract you want to interact with, including the correct address and ABI.

Learn more about Interacting with Smart Contracts.


3. Simulate the Transaction

Before sending the transaction, simulate it to ensure it will succeed. This step is crucial for avoiding failed transactions due to incorrect parameters.

const resApprove = await contractInstance.approve(account.address, 100n);
if (!resApprove) throw new Error("Approve call failed.");

4. Signing and Sending the Transaction

Use the sendTransaction method to sign and send the transaction.

const tx = await resApprove.sendTransaction({
signer: account.signer!, // OP_WALLET doesn't need a signer
maximumAllowedSatToSpend: 100_000n,
network: account.network,
refundTo: account.addressTyped,
});
if (!tx.transactionId) throw new Error("Failed to send transaction");

console.log("Transaction:", tx);

Full Example

import {
IInteractionParameters,
TransactionFactory,
} from "@btc-vision/transaction";
import { SupportedWallets, useWallet } from "@btc-vision/walletconnect";
import { getContract, IOP_20Contract, OP_20_ABI } from "opnet";

function BuildAnApproveTransaction() {
const { account, connect, disconnect } = useWallet();

async function approveRandomToken() {
if (!account) throw new Error("Connect wallet first");

const contractAddress = "bcrt1qexampleaddress"; // Replace with a valid contract address

const contractInstance = getContract<IOP_20Contract>(
contractAddress, // Contract address
OP_20_ABI, // Contract ABI
provider: account.provider, // Provider instance
network: account.network, // Target network
address: account.address // Sender's address
);

const resApprove = await contractInstance.approve(account.address, 100n);
if (!resApprove)
throw new Error("Approve call failed.");

const tx = await resApprove.sendTransaction({
signer: account.signer!, // OP_WALLET doesn't need a signer
maximumAllowedSatToSpend: 100_000n,
network: account.network,
refundTo: account.addressTyped,
});
if (!tx.transactionId) throw new Error("Failed to send transaction");

console.log("Transaction:", tx);
}

return (
<div>
{account ? (
<div>
<p>Connected Address: {account.addressTyped}</p>
<button onClick={approveRandomToken}>Approve a random token</button>
<button onClick={disconnect}>Disconnect</button>
</div>
) : (
<button onClick={() => connect(SupportedWallets.OP_WALLET)}>
Connect Wallet
</button>
)}
</div>
);
}

export default BuildAnApproveTransaction;

Best Practices

  • Always simulate the transaction before sending it to ensure it will succeed.
  • Use the maximumAllowedSatToSpend parameter to limit the amount of satoshis that can be spent in the transaction.
  • Monitor the transaction status after sending it to confirm its success or failure.
  • Handle errors gracefully to provide a better user experience.
  • Ensure you have the correct contract address and ABI to avoid transaction failures.