Skip to main content

Sending a State-Changing UserOp for a Smart Contract Wallet using Fusebox Web SDK

Introduction:

In this tutorial, we will explore how to use the Fuse Web SDK to send a Fuse Blockchain state-changing UserOp for a Smart Contract Wallet. The provided code example demonstrates how to interact with a smart contract on the Fuse blockchain using a gasless transaction.

Prerequisites:

  1. Node.js Installed: You can download it from here.
  2. Code Editor: Use your preferred code editor; VS Code is recommended.
  3. Fuse Wallet and API Key: Obtain a Fuse Wallet and API Key from the Fuse Console Dashboard.
  4. A Fuse Console Gas Tank set up for the Paymaster.
  5. Basic Understanding of JavaScript: Familiarity with JavaScript will be helpful.

Step 1: Set up your Project

Create a new project folder and initialize it using Node.js:

mkdir new-project && cc new-project
npm init -y

Install the required packages:

npm install ethers@5.7.2 @fuseio/fusebox-web-sdk

Step 2: Write the Code

Open your code editor and create a new file, e.g. sendUserOp.mjs.

Import Libraries and Initialize FuseSDK: Import the necessary libraries, including ethers for web3 access functionalities and FuseSDK for Fuse Network interactions.

import { ethers } from "ethers";
import { FuseSDK } from "@fuseio/fusebox-web-sdk";

Step 3: Set Up Wallet Credential

const credentials = new ethers.Wallet(process.env.<YOUR_PRIVATE_KEY>);
const publicApiKey = process.env.<YOUR_PUBLIC_API_KEY> ;

Create a wallet using your private key and set up the public API key required for FuseSDK initialization. Make sure to keep your private key secure and never share it.

Step 4: Initialize FuseSDK:

const fuseSDK = await FuseSDK.init(publicApiKey, credentials, {
withPaymaster: true,
});

Initialize the FuseSDK with your public API key, and wallet credentials, and enable the Paymaster for gasless transactions.

Step 5: Specify Contract Information

const contractAddress = "0xb8D4BD32d0c8C9012cF5E90D2acF37091a73B6F6"; // MTK Token Contract
const amount = ethers.utils.parseEther("0");

Specify the contract address and the amount (in Wei) for the transaction. In this example, we set the amount to 0, indicating a state-changing operation that does not involve transferring funds.

Step 6: Encode Contract Call

const contractCall = new ethers.utils.Interface([
"function mint(uint256 amount)",
]);

const data = contractCall.encodeFunctionData("mint", [1]);

Create an interface for the smart contract function you want to call (in this case, mint), and encode the function data with the specified parameters. In this example, we pass the value 1 as the amount to mint.

Step 7: Send Contract Call

const res = await fuseSDK.callContract(contractAddress, amount, data);

Use the FuseSDK to send the contract call. The callContract function handles the gasless transaction and returns a response object containing the UserOpHash.

Step 8: Monitor Transaction

console.log(`UserOpHash: ${res?.userOpHash}`);
console.log("Waiting for transaction...");

const receipt = await res?.wait();
console.log("Transaction Hash:", receipt?.transactionHash);

Output the UserOpHash and wait for the transaction receipt. The UserOpHash uniquely identifies the gasless transaction, and the transaction hash is printed once the transaction is confirmed.

Step 9: Running the Code

Save the file and open your terminal. Navigate to your project folder and run:

node sendUserOp.mjs

This will execute the script, and you should see output similar to:

UserOpHash: 0x1234567890abcdef...
Waiting for transaction...
Transaction Hash: 0x9876543210fedcba...

Congratulations! You've just sent a UserOp on the Fuse Blockchain using the Fuse Web SDK.

Complete Code

const main = async () => {
const credentials = new ethers.Wallet(process.env.NEXT_PUBLIC_PRIVATE_KEY);
const publicApiKey = process.env.NEXT_PUBLIC_PUBLIC_API_KEY;
const fuseSDK = await FuseSDK.init(publicApiKey, credentials, {
withPaymaster: true,
});

// You can use any other "to" address and any other "value"
// MTK Token
const contractAddress = "0xb8D4BD32d0c8C9012cF5E90D2acF37091a73B6F6"; Contract
const amount = parseEther("0");

const contractCall = new ethers.utils.Interface([
"function mint(uint256 amount)",
]);

const data = contractCall.encodeFunctionData("mint", [1]);

const res = await fuseSDK.callContract(contractAddress, amount, data);

console.log(`UserOpHash: ${res?.userOpHash}`);
console.log("Waiting for transaction...");

const receipt = await res?.wait();
console.log("Transaction Hash:", receipt?.transactionHash);
};

// Execute the main function
main();

Conclusion:

Congratulations! You have successfully sent a Fuse blockchain state-changing UserOp for a Smart Account Wallet using the Fuse Web SDK. This example demonstrates a gasless transaction with a smart contract on the Fuse blockchain. Feel free to explore and modify the code for your specific use case.