Wagmi is not available on React Native. Use Ethers.js or Viem for EVM operations.
Read Contract Data
- Ethers.js
- Viem
import { ethers } from "ethers";
const CONTRACT_ABI = [
"function balanceOf(address owner) view returns (uint256)",
"function totalSupply() view returns (uint256)",
"function name() view returns (string)",
"function symbol() view returns (string)",
"function decimals() view returns (uint8)"
];
async function readContractData(
provider: ethers.Provider,
contractAddress: string,
userAddress: string
) {
const contract = new ethers.Contract(
contractAddress,
CONTRACT_ABI,
provider
);
const [balance, totalSupply, name, symbol, decimals] = await Promise.all([
contract.balanceOf(userAddress),
contract.totalSupply(),
contract.name(),
contract.symbol(),
contract.decimals()
]);
return {
balance: ethers.formatUnits(balance, decimals),
totalSupply: ethers.formatUnits(totalSupply, decimals),
name,
symbol,
decimals
};
}
import { formatUnits } from "viem";
const CONTRACT_ABI = [
{
name: "balanceOf",
type: "function",
stateMutability: "view",
inputs: [{ name: "owner", type: "address" }],
outputs: [{ type: "uint256" }]
},
{
name: "totalSupply",
type: "function",
stateMutability: "view",
inputs: [],
outputs: [{ type: "uint256" }]
},
{
name: "name",
type: "function",
stateMutability: "view",
inputs: [],
outputs: [{ type: "string" }]
},
{
name: "symbol",
type: "function",
stateMutability: "view",
inputs: [],
outputs: [{ type: "string" }]
},
{
name: "decimals",
type: "function",
stateMutability: "view",
inputs: [],
outputs: [{ type: "uint8" }]
}
] as const;
async function readContractData(
publicClient: any,
contractAddress: `0x${string}`,
userAddress: `0x${string}`
) {
const results = await publicClient.multicall({
contracts: [
{
address: contractAddress,
abi: CONTRACT_ABI,
functionName: "balanceOf",
args: [userAddress]
},
{
address: contractAddress,
abi: CONTRACT_ABI,
functionName: "totalSupply"
},
{
address: contractAddress,
abi: CONTRACT_ABI,
functionName: "name"
},
{
address: contractAddress,
abi: CONTRACT_ABI,
functionName: "symbol"
},
{
address: contractAddress,
abi: CONTRACT_ABI,
functionName: "decimals"
}
]
});
const [balance, totalSupply, name, symbol, decimals] = results.map(r => r.result);
return {
balance: formatUnits(balance, decimals),
totalSupply: formatUnits(totalSupply, decimals),
name,
symbol,
decimals
};
}
Write Contract Data
- Ethers.js
- Viem
const STAKING_ABI = [
"function stake(uint256 amount) payable",
"function unstake(uint256 amount)",
"function getStakedBalance(address user) view returns (uint256)",
"event Staked(address indexed user, uint256 amount)",
"event Unstaked(address indexed user, uint256 amount)"
];
async function stakeTokens(
signer: any,
contractAddress: string,
amount: string,
decimals: number
) {
const contract = new ethers.Contract(contractAddress, STAKING_ABI, signer);
const parsedAmount = ethers.parseUnits(amount, decimals);
const tx = await contract.stake(parsedAmount);
await tx.wait();
const newBalance = await contract.getStakedBalance(await signer.getAddress());
return {
hash: tx.hash,
stakedAmount: ethers.formatUnits(newBalance, decimals)
};
}
import { parseUnits, formatUnits, parseEventLogs } from "viem";
const STAKING_ABI = [
{
name: "stake",
type: "function",
stateMutability: "payable",
inputs: [{ name: "amount", type: "uint256" }],
outputs: []
},
{
name: "unstake",
type: "function",
stateMutability: "nonpayable",
inputs: [{ name: "amount", type: "uint256" }],
outputs: []
},
{
name: "getStakedBalance",
type: "function",
stateMutability: "view",
inputs: [{ name: "user", type: "address" }],
outputs: [{ type: "uint256" }]
},
{
name: "Staked",
type: "event",
inputs: [
{ name: "user", type: "address", indexed: true },
{ name: "amount", type: "uint256" }
]
},
{
name: "Unstaked",
type: "event",
inputs: [
{ name: "user", type: "address", indexed: true },
{ name: "amount", type: "uint256" }
]
}
] as const;
async function stakeTokens(
walletClient: any,
publicClient: any,
account: `0x${string}`,
contractAddress: `0x${string}`,
amount: string,
decimals: number
) {
const parsedAmount = parseUnits(amount, decimals);
const hash = await walletClient.writeContract({
address: contractAddress,
abi: STAKING_ABI,
functionName: "stake",
args: [parsedAmount]
});
await publicClient.waitForTransactionReceipt({ hash });
const newBalance = await publicClient.readContract({
address: contractAddress,
abi: STAKING_ABI,
functionName: "getStakedBalance",
args: [account]
});
return {
hash,
stakedAmount: formatUnits(newBalance, decimals)
};
}