@faktoryfun/styx-sdk
Version:
Bitcoin deposit SDK for Stacks applications, enabling trustless Bitcoin-to-sBTC deposits
116 lines (115 loc) • 4.03 kB
JavaScript
// src/api.ts in SDK
import axios from "axios";
export class BitcoinDepositAPI {
constructor(baseUrl, apiKey) {
this.baseUrl = baseUrl;
// Use provided API key or fall back to a hardcoded one
this.apiKey =
apiKey ||
"jc_e4d2e10396eef95215a7afd492f42d743a3325739d29200c2a28b256f778be01";
// Create an axios instance with the authorization header
this.axiosInstance = axios.create({
baseURL: baseUrl,
headers: {
Authorization: `Bearer ${this.apiKey}`,
},
});
}
// In src/api.ts in your SDK
async getFeeEstimates() {
try {
const response = await this.axiosInstance.get("/deposits/sdk/bitcoin-fees");
return response.data;
}
catch (error) {
console.error("Error fetching fee rates:", error);
// Default fallback values with proper separation
return { low: 1, medium: 2, high: 5 };
}
}
// In src/api.ts of your SDK
async createDeposit(data) {
try {
// Use the same endpoint as the frontend hook
const response = await this.axiosInstance.post("/deposits", data);
// Handle different response formats, matching the logic in your hook
const responseData = response.data.data || response.data;
if (responseData &&
typeof responseData === "object" &&
"id" in responseData) {
return responseData.id;
}
if (typeof responseData === "string") {
return responseData;
}
console.error("Unexpected response format:", responseData);
throw new Error("Invalid response format from server");
}
catch (error) {
console.error("Error creating deposit:", error);
throw error;
}
}
async updateDeposit(data) {
try {
const response = await this.axiosInstance.patch(`/deposits/sdk/${data.id}`, data.data);
return response.data;
}
catch (error) {
console.error("Error updating deposit:", error);
throw error;
}
}
async getDepositHistory(userAddress) {
try {
const response = await this.axiosInstance.get(`/deposits/sdk/user/${userAddress}`);
return response.data;
}
catch (error) {
console.error("Error fetching deposit history:", error);
return [];
}
}
async getAllDepositsHistory() {
try {
const response = await this.axiosInstance.get("/deposits/sdk/list");
return response.data;
}
catch (error) {
console.error("Error fetching all deposits:", error);
return [];
}
}
async prepareTransaction(params) {
try {
const response = await this.axiosInstance.post("/deposits/sdk/prepare-transaction", params);
return response.data;
}
catch (error) {
console.error("Error preparing transaction:", error);
throw error;
}
}
async updateDepositStatus(params) {
try {
// Use PUT instead of PATCH to match the frontend hook
const response = await this.axiosInstance.put(`/deposits/${params.id}`, params.data);
// Return the processed response data
return response.data.data || response.data;
}
catch (error) {
console.error("Error updating deposit status:", error);
throw error;
}
}
async executeTransaction(params) {
try {
const response = await this.axiosInstance.post("/deposits/sdk/execute-transaction", params);
return response.data;
}
catch (error) {
console.error("Error executing transaction:", error);
throw error;
}
}
}