Examples
Interacting with Wireshape Smart Contracts using Ethers.js
This example demonstrates how to interact with a smart contract deployed on the Wireshape blockchain using the Ethers.js library. It includes connecting to the Wireshape RPC network, interacting with a smart contract to read and write data, and handling transactions.
Prerequisites
-
Node.js and npm installed.
-
Ethers.js library installed (npm install ethers).
-
Wireshape Floripa Testnet RPC endpoint.
-
A deployed smart contract on Wireshape and its ABI and address.
You can fund your wallet with testnet WIRE tokens for free on Wireshape Faucet.
Example: Interacting with a Smart Contract
Consider a simple smart contract deployed on Wireshape that stores and retrieves a greeting message. The contract might look something like this in Solidity:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Greeter {
string private greeting;
constructor(string memory _greeting) {
greeting = _greeting;
}
function setGreeting(string memory _greeting) public {
greeting = _greeting;
}
function getGreeting() public view returns (string memory) {
return greeting;
}
}
Script to Interact with the Greeter Contract
const { ethers } = require("ethers");
// Wireshape Floripa Testnet RPC endpoint URL
const rpcURL = "https://rpc-floripa.wireshape.org";
// Creating a custom provider using the RPC URL
const provider = new ethers.providers.JsonRpcProvider(rpcURL);
// Greeter contract ABI
const greeterABI = [
"function getGreeting() view returns (string memory)",
"function setGreeting(string memory _greeting)",
];
// Greeter contract address (replace with your contract's address)
const greeterAddress = "0x...";
// Creating a contract instance
const greeterContract = new ethers.Contract(greeterAddress, greeterABI, provider);
// Function to get the current greeting
async function getCurrentGreeting() {
try {
const greeting = await greeterContract.getGreeting();
console.log("Current Greeting:", greeting);
} catch (error) {
console.error("An error occurred while fetching the greeting:", error);
}
}
// Function to set a new greeting
async function setNewGreeting(newGreeting) {
// Creating a signer from the provider with your wallet
const signer = provider.getSigner();
// Connecting the contract to the signer to enable transactions
const greeterWithSigner = greeterContract.connect(signer);
try {
// Sending a transaction to set a new greeting
const tx = await greeterWithSigner.setGreeting(newGreeting);
console.log("Setting new greeting with transaction:", tx);
// Wait for the transaction to be validated
await tx.wait();
console.log("New greeting set successfully.");
} catch (error) {
console.error("An error occurred while setting the greeting:", error);
}
}
// Example usage
getCurrentGreeting();
setNewGreeting("Hello, Wireshape!");
Ensure you replace '0x...' with your Greeter contract's address and configure your wallet with the provider for transactions.
This script demonstrates reading the current greeting from the Greeter contract and updating it with a new message. By connecting to the Wireshape Floripa Testnet through the specified RPC endpoint and using the Ethers.js library, developers can easily interact with smart contracts on the Wireshape blockchain, showcasing the flexibility and power of EVM compatibility for blockchain integrations.
Interacting with a Token Contract using Web3.js
This example demonstrates how to interact with a standard ERC-20 token contract deployed on the Wireshape Floripa Testnet blockchain using the Web3.js library. It includes connecting to the Wireshape RPC network, querying token balances, and initiating token transfers.
Prerequisites
-
Node.js and npm installed.
-
Web3.js library installed (npm install web3).
-
Wireshape Floripa Testnet RPC endpoint.
-
An ERC-20 token contract deployed on Wireshape and its ABI and address.
You can fund your wallet with testnet WIRE tokens for free on Wireshape Faucet.
Example: Querying and Transferring ERC-20 Tokens
Assuming we have a simple ERC-20 token contract deployed on Wireshape Floripa Testnet, the contract might implement standard functions like balanceOf to check token balances and transfer to send tokens to another address.
Script to Interact with the ERC-20 Token Contract using Web3.js
const Web3 = require("web3");
// Wireshape Floripa Testnet RPC endpoint URL
const rpcURL = "https://rpc-floripa.wireshape.org";
// Connect to the Wireshape network
const web3 = new Web3(rpcURL);
// ERC-20 Token contract ABI
const tokenABI = [
{
constant: true,
inputs: [{ name: "_owner", type: "address" }],
name: "balanceOf",
outputs: [{ name: "balance", type: "uint256" }],
payable: false,
stateMutability: "view",
type: "function",
},
{
constant: false,
inputs: [
{ name: "_to", type: "address" },
{ name: "_value", type: "uint256" },
],
name: "transfer",
outputs: [{ name: "", type: "bool" }],
payable: false,
stateMutability: "nonpayable",
type: "function",
},
];
// ERC-20 Token contract address (replace with your contract's address)
const tokenAddress = "0x...";
// Creating a contract instance
const tokenContract = new web3.eth.Contract(tokenABI, tokenAddress);
// Function to get the token balance of an address
async function getTokenBalance(address) {
try {
const balance = await tokenContract.methods.balanceOf(address).call();
console.log(`Token Balance for ${address}:`, balance);
} catch (error) {
console.error("An error occurred while fetching the token balance:", error);
}
}
// Function to transfer tokens
// Note: This function assumes you have set up your account and provided a private key or similar authentication method to sign transactions.
async function transferTokens(toAddress, amount, senderAddress) {
try {
// Estimate Gas
const gas = await tokenContract.methods.transfer(toAddress, amount).estimateGas({ from: senderAddress });
// Sending a transaction to transfer tokens
const tx = await tokenContract.methods.transfer(toAddress, amount).send({ from: senderAddress, gas: gas });
console.log("Token transfer transaction:", tx);
} catch (error) {
console.error("An error occurred while transferring tokens:", error);
}
}
// Example usage (Make sure to replace the addresses and amount accordingly)
const senderAddress = "0x..."; // Replace with your Ethereum address
const recipientAddress = "0x..."; // Replace with the recipient's address
const amount = web3.utils.toWei("100", "ether"); // Specify the amount to transfer
getTokenBalance(senderAddress);
transferTokens(recipientAddress, amount, senderAddress);
Ensure you replace '0x...' with your token contract's address, senderAddress with your actual Ethereum address, and recipientAddress with the recipient's address. For transactions, you will need to authenticate and sign transactions, which might involve using your private key securely or integrating with a wallet provider.
This script provides a practical example of querying ERC-20 token balances and executing token transfers using Web3.js. By connecting to the Wireshape Floripa Testnet and utilizing the Web3.js library, developers can efficiently interact with token contracts on the Wireshape blockchain, demonstrating the versatility and developer-friendly nature of EVM-compatible blockchain integrations.