@zebec-network/exchange-card-sdk
Version:
An sdk for purchasing silver card in zebec
73 lines (72 loc) • 2.87 kB
JavaScript
import { ethers } from "ethers";
import { ERC20__factory } from "../artifacts";
import { BOBA_CHAIN_ID, DEFAULT_EVM_GAS_LIMIT } from "../constants";
import { ZebecCardAPIService } from "../helpers/apiHelpers";
export class BobaService {
signer;
network;
chainId;
apiService;
constructor(signer, sdkOptions) {
this.signer = signer;
this.network = sdkOptions?.sandbox ? "testnet" : "mainnet";
this.chainId = BOBA_CHAIN_ID[this.network];
this.apiService = new ZebecCardAPIService(sdkOptions?.sandbox || false);
}
/**
* Fetches the Bitcoin vault address.
*
* @returns {Promise<{ address: string }>} A promise that resolves to the vault address.
*/
async fetchVault(symbol) {
const data = await this.apiService.fetchVault(symbol);
return data;
}
async transferBobaEth(params) {
const parsedAmount = ethers.parseEther(params.amount.toString());
const provider = this.signer.provider;
const vault = await this.fetchVault("BOBA-ETH");
const recipientAddress = vault.address;
if (!provider) {
throw new Error("There is no provider in signer instance.");
}
const senderBalance = await provider.getBalance(this.signer);
if (senderBalance < parsedAmount) {
throw new Error("Insufficient balance for transaction.");
}
const overides = {
gasLimit: DEFAULT_EVM_GAS_LIMIT,
...params.overrides,
};
const response = await this.signer.sendTransaction({
...overides,
to: recipientAddress,
value: parsedAmount,
from: this.signer,
chainId: this.chainId,
});
console.debug("Boba Transaction Hash:", response.hash);
const receipt = await response.wait();
return receipt;
}
async transferToken(params) {
const tokenContract = ERC20__factory.connect(params.tokenAddress, this.signer);
const tokenDecimals = await tokenContract.decimals();
const parsedAmount = ethers.parseUnits(params.amount.toString(), tokenDecimals);
const vault = await this.fetchVault(params.symbol);
const recipientAddress = vault.address;
const senderBalance = await tokenContract.balanceOf(this.signer);
if (senderBalance < parsedAmount) {
throw new Error("Insufficient balance for transaction.");
}
const overides = {
gasLimit: DEFAULT_EVM_GAS_LIMIT,
...params.overrides,
chainId: this.chainId,
};
const response = await tokenContract.transfer(recipientAddress, parsedAmount, overides);
console.debug("Boba Transaction Hash:", response.hash);
const receipt = await response.wait();
return receipt;
}
}