Interacting with Storage Variables
Interacting with the Contract
Setup MetaMask:
Ensure MetaMask is configured for the Arbitrum network. Detailed instructions are available in the Arbitrum documentation.
Interacting with the Contract using Ethers.js:
const { ethers } = require("ethers"); const provider = new ethers.providers.JsonRpcProvider("https://sepolia-rollup.arbitrum.io/rpc"); const contractAddress = "<YOUR_CONTRACT_ADDRESS>"; const abi = [ "function setValue(uint256 new_value) external", "function getValue() view returns (uint256)", "function mulValue(uint256 multiplier) external", "function addValue(uint256 increment) external", "function increment() external" ]; const privateKey = "<YOUR_PRIVATE_KEY>"; const wallet = new ethers.Wallet(privateKey, provider); const contract = new ethers.Contract(contractAddress, abi, wallet); async function setValue(newValue) { const tx = await contract.setValue(newValue); await tx.wait(); console.log("Value set to:", newValue); } async function getValue() { const value = await contract.getValue(); console.log("Current value is:", value.toString()); } async function mulValue(multiplier) { const tx = await contract.mulValue(multiplier); await tx.wait(); console.log("Value multiplied by:", multiplier); } async function addValue(increment) { const tx = await contract.addValue(increment); await tx.wait(); console.log("Value incremented by:", increment); } async function increment() { const tx = await contract.increment(); await tx.wait(); console.log("Value incremented by 1"); } async function main() { await setValue(42); await getValue(); await mulValue(2); await getValue(); await addValue(10); await getValue(); await increment(); await getValue(); } main().catch(console.error);
This script connects to the Arbitrum network, interacts with your deployed contract, sets a new value, multiplies it, increments it, and retrieves the current value from the contract.
Last updated