Skip to content

fuseio/fusebox-web-sdk

Repository files navigation

MIT License

Introduction

The FuseBox TS SDK is a set of tools for creating, managing, and engaging with FuseBox TS SDK in client applications. FuseBox TS SDK lets users create smart contract wallets(Based on ERC-4337) associated with each user's Externally Owned Account (EOA) that provide added security compared to traditional EOAs with a single private key. With FuseBox TS SDK, users can deposit funds that no one else can control and withdraw at any time.

Benefits of using FuseBox TS SDK

Using FuseBox TS SDK provides several benefits, including:

  • Enhanced security: FuseBox TS SDK are non-custodial accounts that allow users to deposit funds that no one else can control and withdraw at any time. Each Fuse Smart Wallet is a smart contract associated with the user's EOA and can only be controlled by that user.
  • Enhanced UX: FuseBox TS SDK support gasless transactions, improving the user experience and making it more seamless to interact with the blockchain.
  • Better developer experience: The FuseBox TS SDK abstracts away the complexities of web3 development, such as cryptography, wallet management, and smart contract interactions, making it easier for developers to build blockchain-based applications

Purpose of the SDK

The SDK is designed to make it easy for developers to create, manage, and engage with FuseBox TS SDK in their applications. The SDK provides pre-built functions and utilities, allowing developers to interact with FuseBox TS SDK securely and efficiently.

Instantiation

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

// Create a project: https://console.fuse.io/build
const apiKey = 'YOUR_PUBLIC_API_KEY';
const credentials = new ethers.Wallet("PRIVATE_KEY");
const fuseSDK = await FuseSDK.init(apiKey, credentials);

Examples

Features

The FuseBox TS SDK provides several features that allow developers to create, manage, and engage with FuseBox TS SDK in their applications. Some of the key features include:

Get Address

Gets the address of the wallet created.

console.log(`Smart contract wallet address: ${fuseSDK.wallet.getSender()}`);

Send transactions

Send transactions, including ERC20 and NFT transfers and interaction with arbitrary smart contracts.

const tokenAddress = "YOUR_TOKEN";
const to = "RECEIVER_ADDRESS";
const amount = parseUnits('10000', DECIMAL); //Amount should be set in WEI. `DECIMAL` should be a numeric value
const data = Uint8Array.from([]);
const res = await fuseSDK.transferToken(
  tokenAddress,
  to,
  amount,
  data
);
console.log(`UserOpHash: ${res.userOpHash}`);
console.log(`Waiting for transaction...`);
const ev = await res.wait();
console.log(`Transaction hash: https://explorer.fuse.io/tx/${ev?.transactionHash}`);

Send batch transactions

The process of grouping multiple transactions into a single batch to be processed together. This is often done to optimize processing time and reduce transaction fees.

// Approve and transfer in a single batch
const approveCallData = ContractUtils.encodeERC20ApproveCall(
  spender,
  amount
) as unknown as Uint8Array;

const calls = [
  {
    to: tokenAddress,
    value: BigInt(0),
    data: approveCallData,
  },
  {
    to: spender,
    value: BigInt(0),
    data: callData,
  },
];

const res = await fuseSDK.executeBatch(calls, txOptions);

console.log(`UserOpHash: ${res.userOpHash}`);

console.log(`Waiting for transaction...`);
const ev = await res.wait();
console.log(`Transaction hash: https://explorer.fuse.io/tx/${ev?.transactionHash}`);

Sponsored Transactions

Sponsored transactions are the ability to pay for another user’s transaction fees. To do this, the Fuse operator must enable the sponsored feature in his project and deposit some funds into the paymaster contract. The SDK provides a middleware to check if the project is sponsored and the amount of funds available for sponsoring.

To use this feature, you must first initialize the SDK with the withPaymaster parameter set to true.

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

const apiKey = 'YOUR_PUBLIC_API_KEY';
const credentials = new ethers.Wallet("PRIVATE_KEY");
const fuseSDK = await FuseSDK.init(apiKey, credentials, { withPaymaster: true });

Staking

The SDK provides a module for staking. This module allows users to stake their tokens and earn rewards.

Currently, the SDK supports staking for the following tokens: Native Fuse & VoltToken

const stakingOptions = await fuseSDK.stakingModule.getStakingOptions(); // Get staking options

const nativeTokenAddress = Variables.NATIVE_TOKEN_ADDRESS;
const res = await fuseSDK.stakeToken(
  new StakeRequestBody({
    accountAddress: fuseSDK.wallet.getSender(),
    tokenAmount: '0.01',
    tokenAddress: nativeTokenAddress,
  })
);

console.log(`UserOpHash: ${res?.userOpHash}`);
console.log('Waiting for transaction...');
const ev = await res?.wait();
console.log(`Transaction hash: https://explorer.fuse.io/tx/${ev?.transactionHash}`);

Trading

Smart Wallet can buy and sell popular cryptocurrencies like Bitcoin and Ethereum, Stable-coins. Behind the scenes, it uses voltage.finance decentralized exchange.

const nativeTokenAddress = Variables.NATIVE_TOKEN_ADDRESS;
const usdcTokenAddress = '0x28C3d1cD466Ba22f6cae51b1a4692a831696391A';
const res = await fuseSDK.swapTokens(
  new TradeRequest(
    nativeTokenAddress,
    usdcTokenAddress,
    parseUnits('1', 18),
    true,
  ),
);

console.log(`UserOpHash: ${res?.userOpHash}`);
console.log('Waiting for transaction...');
const ev = await res?.wait();
console.log(`Transaction hash: https://explorer.fuse.io/tx/${ev?.transactionHash}`);

Troubleshooting

  1. User op cannot be replaced: fee too low.

    If you're getting the User op cannot be replaced: fee too low error, it means that the gas price you set is too low. You can increase the gas price by setting the TxOptions parameter when sending a transaction. To replace an user operation, a new user operation must have at least 10% higher maxPriorityFeePerGas and 10% higher maxPriorityFeePerGas than the one in the user operation mempool.

To replace the user operation, the new gas price must be at least 10% higher.

const tokenAddress = "YOUR_TOKEN";
const to = "RECEIVER_ADDRESS";
const amount = parseUnits('10000', DECIMAL); //Amount should be set in WEI. `DECIMAL` should be a numeric value
const data = Uint8Array.from([]);

const res = await fuseSDK.transferToken(
  tokenAddress,
  to,
  amount,
  data,
  {
    withRetry: true,
    feeIncrementPercentage: 11,
  }
);
console.log(`UserOpHash: ${res.userOpHash}`);
console.log(`Waiting for transaction...`);
const ev = await res.wait();
console.log(`Transaction hash: https://explorer.fuse.io/tx/${ev?.transactionHash}`);

Limitations

The FuseBox TS SDK works only on the Fuse & Fuse Sparknet networks, an EVM based chain L1 blockchain. Support for other blockchains is planned for the future.

If you have any questions or feedback, please get in touch with our support team at [email protected].

About

JS implementation of Fusebox SDK

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published