@solutiofi/sdk
Version:
This SDK provides a convenient way to interact with the SolutioFi API.
2 lines (1 loc) • 2.22 kB
JavaScript
import t from"axios";import{VersionedTransaction as a}from"@solana/web3.js";function e(){return e=Object.assign?Object.assign.bind():function(t){for(var a=1;a<arguments.length;a++){var e=arguments[a];for(var r in e)({}).hasOwnProperty.call(e,r)&&(t[r]=e[r])}return t},e.apply(null,arguments)}function r(t){const e=[];for(const r of t){const t=Uint8Array.from(Buffer.from(r,"base64"));e.push(a.deserialize(t))}return e}class n{constructor(a){var e=this;this.client=void 0,this.apiKey=void 0,this.bearerToken=null,this.apiKey=a.apiKey,this.client=t.create({baseURL:a.baseUrl||"https://api.solutiofi.io",headers:{"Content-Type":"application/json"}}),this.client.interceptors.response.use(t=>t,async function(t){if(t.response&&(401===t.response.status||403===t.response.status)){await e.authenticate();const a=t.config;return a.headers.Authorization=`Bearer ${e.bearerToken}`,e.client.request(a)}return Promise.reject(t)})}async authenticate(){const t=await this.client.get("/auth/token",{headers:{"x-api-key":this.apiKey}});this.bearerToken=t.data.token,this.client.defaults.headers.common.Authorization=`Bearer ${this.bearerToken}`}async close(t,a){const e=await this.client.post("/v1/close/create",{owner:t,mints:a});if(e.data.error)throw new Error(`SolutfioFi API error: ${e.data.error}`);return r(e.data.transactions)}async burn(t,a){const e=await this.client.post("/v1/burn/create",{owner:t,mints:a});if(e.data.error)throw new Error(`SolutfioFi API error: ${e.data.error}`);return r(e.data.transactions)}async merge(t,a,n,s){const i=await this.client.post("/v1/swap/merge",{owner:t,inputAssets:a,outputMint:n,priorityFee:s});return i.data.transactions&&(i.data.transactions=i.data.transactions.map(t=>e({},t,{transaction:r([t.transaction])[0]}))),i.data}async spread(t,a,n,s){const i=await this.client.post("/v1/swap/spread",{owner:t,inputAsset:a,targetTokens:n,priorityFee:s});return i.data.transactions&&(i.data.transactions=i.data.transactions.map(t=>e({},t,{transaction:r([t.transaction])[0]}))),i.data}async getTokenPrices(t){return(await this.client.post("/v1/tokens/prices",{mints:t})).data}async getUserTokens(t,a){return(await this.client.post("/v1/usertokens",{owner:t,type:a})).data}}export{n as default};