@zebec-network/exchange-card-sdk
Version:
An sdk for purchasing silver card in zebec
63 lines (62 loc) • 2.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OctaService = void 0;
const ethers_1 = require("ethers");
const constants_1 = require("../constants");
const apiHelpers_1 = require("../helpers/apiHelpers");
class OctaService {
signer;
apiConfig;
apiService;
constructor(signer, apiConfig, sdkOptions) {
this.signer = signer;
this.apiConfig = apiConfig;
this.apiService = new apiHelpers_1.ZebecCardAPIService(apiConfig, sdkOptions?.sandbox || false);
}
/**
* Fetches a quote for Bitcoin transfer.
*
* @returns {Promise<Quote>} A promise that resolves to a Quote object.
*/
async fetchQuote(symbol = "OCTA") {
const res = await this.apiService.fetchQuote(symbol);
return res;
}
/**
* 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 = (0, ethers_1.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 = {
...params.overrides,
gasLimit: constants_1.DEFAULT_EVM_GAS_LIMIT,
};
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;
}
}
exports.OctaService = OctaService;