Skip to main content

How to Build a Smart Contract Account using the FuseBox Web SDK

Introduction

In this tutorial, we'll walk through the process of creating a Smart Contract Account using the FuseBox Web SDK. The provided code example demonstrates how to set up and interact with the SDK to create a Smart Contract Wallet. This Smart Contract Wallet will be capable of executing User Operations (transactions) according to the ERC-4337 Standard on the Fuse Blockchain.

Prerequisites

Before we begin, ensure you have the following:

  1. Node.js installed on your machine. You can download it from here.
  2. Code Editor: Use your preferred code editor; VS Code is recommended.
  3. An EOA wallet with a private key. You can use an existing one or create a new wallet.
  4. 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/fuse-sdk

Step 2: Write the Code

Create a new file createSmartAccountWallet.js and copy the provided code into it. This code initializes the Fuse Web SDK, allowing you to interact with the Fuse Network.

// createSmartAccountWallet.mjs

// Import necessary libraries
import { ethers } from 'ethers';
import { FuseSDK } from '@fuseio/fuse-web-sdk';

// Main function to create a Smart Account Wallet
const main = async () => {
// Load your private key from the environment variables
const credentials = new ethers.Wallet(process.env.PRIVATE_KEY as string);

// Retrieve your public API key from the environment variables
const publicApiKey = process.env.PUBLIC_API_KEY as string;

// Initialize the Fuse SDK with your public API key and wallet credentials
const fuseSDK = await FuseSDK.init(publicApiKey, credentials);

// Log the sender address of the Smart Wallet
console.log(`Sender Address is ${fuseSDK.wallet.getSender()}`);
};

// Execute the main function
main();

Step 3: Configure Environment Variables

Set up your environment variables. Open your terminal and export your private key and public API key:

export PRIVATE_KEY="your_private_key_here"
export PUBLIC_API_KEY="your_public_api_key_here"

Replace your_private_key_here and your_public_api_key_here with your actual private key and public API key.

Step 4: Run Your Script

Execute your script using the following command:

node createSmartAccountWallet.mjs

If everything is set up correctly, you should see the sender address of your newly created Smart Wallet logged to the console.

Conclusion

Congratulations! You've successfully created a Smart Contract Account using the Fuse Web SDK. This Smart Wallet can be used to execute transactions on the Fuse network. Feel free to explore additional features and functionalities offered by the Fuse SDK to enhance your Smart Wallet further.