stylusguide
  • 1. Welcome to Arbitrum Stylus and Rust
    • Brief Introduction to Arbitrum Stylus
    • Importance of Rust in Blockchain Development
    • What You'll Learn in This Tutorial Series
  • 2. Setting Up Your Development Environment
    • Installing Rust
    • Setting Up Arbitrum Stylus
    • Basic Commands
  • 3. Your First Smart Contract: Hello World
    • Introduction to Smart Contracts
    • Writing the Hello World Contract
    • Deploying and Testing Your Contract
  • 4. Understanding Smart Contract Storage: Simple Storage
    • Basics of Contract Storage
    • Writing the Simple Storage Contract
    • Interacting with Storage Variables
  • 5. Creating Your Own Token: Token Contract
    • Introduction to Tokens and Standards
    • Writing the Token Contract
    • Deploying and Testing Your Token
  • 6. Additional Resources and Next Steps
Powered by GitBook
On this page
  1. 4. Understanding Smart Contract Storage: Simple Storage

Interacting with Storage Variables

Interacting with the Contract

  1. Setup MetaMask:

    Ensure MetaMask is configured for the Arbitrum network. Detailed instructions are available in the Arbitrum documentation.

  2. Acquire Testnet ETH:

    Use faucets like BwareLabs or QuickNode to get Sepolia ETH for testing.

  3. 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.

PreviousWriting the Simple Storage ContractNext5. Creating Your Own Token: Token Contract

Last updated 10 months ago