@zebec-network/exchange-card-sdk
Version:
An sdk for purchasing silver card in zebec
48 lines (47 loc) • 1.68 kB
JavaScript
import { parseEther } from "ethers";
import { DEFAULT_EVM_GAS_LIMIT } from "../constants";
import { ZebecCardAPIService } from "../helpers/apiHelpers";
export class OctaService {
signer;
apiService;
constructor(signer, sdkOptions) {
this.signer = signer;
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 = "OCTA") {
const data = await this.apiService.fetchVault(symbol);
return data;
}
async transferOcta(params) {
const parsedAmount = parseEther(params.amount.toString());
const provider = this.signer.provider;
const vault = await this.fetchVault();
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: 800001,
});
console.debug("Octa Transaction Hash:", response.hash);
const receipt = await response.wait();
return receipt;
}
}