Created
December 5, 2023 09:51
-
-
Save SametSahin10/78a3bc50356c9f82a62c97586e07a41f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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