metal-presale
Version:
SDK for Metal Presales - Client and Server utilities
106 lines (105 loc) • 3.24 kB
JavaScript
import { g as a, c as n, a as i } from "./shared/utils-Crf4vSn8.js";
class h {
constructor({ publicApiKey: e, apiBasePath: o = "/api/metal" }) {
this.publicApiKey = e, this.apiBasePath = o;
}
/**
* Public API methods - these work with just the public key
*/
async getHolder(e) {
return a(e, this.publicApiKey);
}
async getHolderWithPortfolio(e) {
const o = await a(e, this.publicApiKey), r = n(o);
return { holder: o, portfolio: r };
}
async getPresale(e) {
return i(e, this.publicApiKey);
}
/**
* Server-proxied methods - these call your API routes
*/
async createUser(e) {
const o = await fetch(`${this.apiBasePath}/users/create`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ userId: e })
});
if (!o.ok) {
const t = await o.json();
throw new Error(t.error || "Failed to create user");
}
return (await o.json()).holder;
}
async getActivePresales() {
const e = await fetch(`${this.apiBasePath}/presales/active`);
if (!e.ok) {
const o = await e.json();
throw new Error(o.error || "Failed to fetch active presales");
}
return e.json();
}
async buyPresale(e, o, r) {
const t = await fetch(`${this.apiBasePath}/presales/buy`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ userId: e, presaleId: o, usdcAmount: r })
});
if (!t.ok) {
const s = await t.json();
throw new Error(s.error || "Failed to buy presale");
}
return t.json();
}
async buyTokens(e, o, r) {
const t = await fetch(`${this.apiBasePath}/tokens/buy`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ userId: e, tokenAddress: o, usdcAmount: r })
});
if (!t.ok) {
const s = await t.json();
throw new Error(s.error || "Failed to buy tokens");
}
return t.json();
}
async sellTokens(e, o, r) {
const t = await fetch(`${this.apiBasePath}/tokens/sell`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ userId: e, tokenAddress: o, tokenAmount: r })
});
if (!t.ok) {
const s = await t.json();
throw new Error(s.error || "Failed to sell tokens");
}
return t.json();
}
async quoteBuyTokens(e, o, r) {
const t = await fetch(`${this.apiBasePath}/tokens/quote-buy`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ userId: e, tokenAddress: o, usdcAmount: r })
});
if (!t.ok) {
const s = await t.json();
throw new Error(s.error || "Failed to get buy quote");
}
return t.json();
}
async quoteSellTokens(e, o, r) {
const t = await fetch(`${this.apiBasePath}/tokens/quote-sell`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ userId: e, tokenAddress: o, tokenAmount: r })
});
if (!t.ok) {
const s = await t.json();
throw new Error(s.error || "Failed to get sell quote");
}
return t.json();
}
}
export {
h as MetalPresalesClient
};