Skip to content

Instantly share code, notes, and snippets.

@SametSahin10
Created December 5, 2023 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SametSahin10/78a3bc50356c9f82a62c97586e07a41f to your computer and use it in GitHub Desktop.
Save SametSahin10/78a3bc50356c9f82a62c97586e07a41f to your computer and use it in GitHub Desktop.
import { FuseSDK } from '@fuseio/fusebox-web-sdk';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Signer, Wallet, utils } from 'ethers';
import { ISendUserOperationResponse } from 'userop';
@Injectable()
export class FuseboxService {
private fuseSDK: FuseSDK | null = null;
constructor(private readonly configService: ConfigService) {}
async ensureInitialized(): Promise<void> {
if (!this.fuseSDK) {
await this.init();
}
}
private async init() {
const publicKey = this.configService.getOrThrow('CHARGE_PUBLIC_API_KEY');
const privateKey = this.configService.get('PRIVATE_KEY');
const wallet = new Wallet(privateKey) as Signer;
try {
this.fuseSDK = await FuseSDK.init(publicKey, wallet);
console.log('FuseSDK initialized successfully:', this.fuseSDK);
} catch (error) {
console.error('Error initializing FuseSDK:', error);
throw new Error('Failed to initialize FuseSDK.');
}
}
async transferNativeToken(
recipientAddress: string,
amount: number,
options?: TxOptions,
): Promise<ISendUserOperationResponse> {
await this.ensureInitialized();
return this.fuseSDK.callContract(
recipientAddress,
utils.parseEther(amount.toString()),
new Uint8Array(0),
options,
);
}
}
interface TxOptions {
feePerGas: string;
feeIncrementPercentage: number;
withRetry: boolean;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment