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
  • Step 3: Interacting with the Contract
  • Conclusion
  1. 5. Creating Your Own Token: Token Contract

Deploying and Testing Your Token

Step 3: Interacting with the Contract

Setting Up MetaMask

  1. Install MetaMask: Install the MetaMask browser extension from metamask.io.

  2. Configure MetaMask for Arbitrum:

    • Click on the network dropdown in MetaMask and select "Custom RPC".

    • Enter the following details:

      • Network Name: Arbitrum Sepolia Testnet

      • New RPC URL: https://sepolia-rollup.arbitrum.io/rpc

      • Chain ID: 421613 (for Sepolia)

      • Symbol: ETH

      • Block Explorer URL: https://explorer.arbitrum.io

    • Save the network configuration.

Acquiring Testnet ETH

  1. Visit a Sepolia Faucet: Use one of the following links to get Sepolia ETH for testing:

    • BwareLabs Sepolia Faucet

    • QuickNode Sepolia Faucet

    • Sepolia Faucet

  2. Enter your MetaMask wallet address: Click "Claim" to receive Sepolia ETH.

Interacting with the Contract using Ethers.js

Create a script.js file with the following content:

const { ethers } = require("ethers");

const provider = new ethers.providers.JsonRpcProvider("https://sepolia-rollup.arbitrum.io/rpc");
const privateKey = "<YOUR_PRIVATE_KEY>"; // Replace with your private key
const wallet = new ethers.Wallet(privateKey, provider);

const abi = [
    "function name() view returns (string)",
    "function symbol() view returns (string)",
    "function decimals() view returns (uint8)",
    "function totalSupply() view returns (uint256)",
    "function balanceOf(address account) view returns (uint256)",
    "function transfer(address recipient, uint256 amount) returns (bool)",
    "function transferFrom(address sender, address recipient, uint256 amount) returns (bool)",
    "function approve(address spender, uint256 amount) returns (bool)",
    "function allowance(address owner, address spender) view returns (uint256)"
];

const contractAddress = "<YOUR_CONTRACT_ADDRESS>"; // Replace with your contract address

const erc20 = new ethers.Contract(contractAddress, abi, wallet);

async function main() {
    const name = await erc20.name();
    console.log("Token Name:", name);

    const symbol = await erc20.symbol();
    console.log("Token Symbol:", symbol);

    const decimals = await erc20.decimals();
    console.log("Token Decimals:", decimals);

    const totalSupply = await erc20.totalSupply();
    console.log("Total Supply:", totalSupply.toString());

    const balance = await erc20.balanceOf(wallet.address);
    console.log("Balance:", balance.toString());

    const recipient = "<RECIPIENT_ADDRESS>"; // Replace with a recipient address
    const amount = ethers.utils.parseUnits("10", decimals); // Sending 10 tokens
    const tx = await erc20.transfer(recipient, amount);
    await tx.wait();
    console.log("Transfer successful!");
}

main().catch(console.error);

Run the script:

node script.js

This script connects to your deployed ERC-20 contract, retrieves token information, checks balances, and performs a token transfer.

Conclusion

You have successfully created and deployed an ERC-20 token contract using Rust and Arbitrum Stylus. You also learned how to interact with your contract using Ethers.js. This example demonstrates the power and flexibility of combining Rust with blockchain development on the Arbitrum network. Happy coding!

PreviousWriting the Token ContractNext6. Additional Resources and Next Steps

Last updated 10 months ago