Interact with Smart Contracts
Learn how to interact with deployed smart contracts on Tetreum Testnet using different tools and methods.
There are several ways to interact with smart contracts on Tetreum:
Block Explorer
Use the Tetreum Explorer to read contract data and call functions directly from the web interface.
Web3 Libraries
Use ethers.js, web3.js, or viem to interact programmatically from your dApp.
MetaMask
Connect your wallet to dApps and sign transactions directly through MetaMask.
Command Line
Use cast, foundry, or hardhat console for developer interactions and testing.
The easiest way to interact with contracts is through the Tetreum Explorer:
- 1
Find the Contract
Navigate to the contract address on the explorer or search for verified contracts.
- 2
Read Contract Data
Use the "Read Contract" tab to call view functions and see contract state.
- 3
Write to Contract
Connect your wallet and use "Write Contract" tab to execute state-changing functions.
Note: The contract must be verified on the explorer to use the Read/Write interface.
Interact with contracts programmatically using popular Web3 libraries:
Using ethers.js
import { ethers } from 'ethers';
// Connect to Tetreum
const provider = new ethers.JsonRpcProvider('https://testrpc.tetreum.com');
const wallet = new ethers.Wallet(privateKey, provider);
// Contract instance
const contractAddress = '0x...';
const abi = [...]; // Contract ABI
const contract = new ethers.Contract(contractAddress, abi, wallet);
// Read from contract
const balance = await contract.balanceOf(address);
// Write to contract
const tx = await contract.transfer(toAddress, amount);
await tx.wait(); // Wait for confirmationUsing viem
import { createPublicClient, createWalletClient, http } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';
// Create clients
const publicClient = createPublicClient({
transport: http('https://testrpc.tetreum.com')
});
const account = privateKeyToAccount('0x...');
const walletClient = createWalletClient({
account,
transport: http('https://testrpc.tetreum.com')
});
// Read contract
const balance = await publicClient.readContract({
address: '0x...',
abi: contractAbi,
functionName: 'balanceOf',
args: [address]
});
// Write contract
const hash = await walletClient.writeContract({
address: '0x...',
abi: contractAbi,
functionName: 'transfer',
args: [toAddress, amount]
});Integrate contract interactions into your dApp frontend:
React Hook Example
import { useState, useEffect } from 'react';
import { ethers } from 'ethers';
function useContract(contractAddress, abi) {
const [contract, setContract] = useState(null);
const [signer, setSigner] = useState(null);
useEffect(() => {
async function init() {
if (window.ethereum) {
await window.ethereum.request({
method: 'eth_requestAccounts'
});
const provider = new ethers.BrowserProvider(window.ethereum);
const signer = await provider.getSigner();
const contract = new ethers.Contract(contractAddress, abi, signer);
setSigner(signer);
setContract(contract);
}
}
init();
}, [contractAddress, abi]);
return { contract, signer };
}
// Usage in component
function TokenTransfer() {
const { contract } = useContract(tokenAddress, tokenAbi);
const transfer = async (to, amount) => {
if (!contract) return;
try {
const tx = await contract.transfer(to, amount);
await tx.wait();
console.log('Transfer successful!');
} catch (error) {
console.error('Transfer failed:', error);
}
};
return (
<button onClick={() => transfer(recipient, parseEther('1.0'))}>
Send 1 Token
</button>
);
}Error Handling
try {
const tx = await contract.someFunction(params);
await tx.wait();
} catch (error) {
if (error.code === 'INSUFFICIENT_FUNDS') {
alert('Insufficient balance for transaction');
} else if (error.code === 'USER_REJECTED') {
alert('Transaction rejected by user');
} else if (error.reason) {
alert(`Contract error: ${error.reason}`);
} else {
alert('Transaction failed');
}
}Always Validate Inputs
- • Check address formats before sending transactions
- • Validate amounts and prevent overflow
- • Implement proper error handling
- • Show transaction status to users
Gas Optimization
- • Estimate gas before sending transactions
- • Allow users to adjust gas price
- • Batch operations when possible
- • Use view functions for read operations
Security Considerations
- • Always verify contract addresses
- • Use verified contracts when possible
- • Implement proper access controls
- • Test on testnet before mainnet
Start Interacting!
Choose your preferred method and start building powerful dApps on Tetreum.