UNPKG

@sega-so/sega-sdk

Version:

An SDK for building applications on top of SEGA.

2 lines 14.9 kB
"use strict";var Ae=Object.create;var R=Object.defineProperty,Ie=Object.defineProperties,Ce=Object.getOwnPropertyDescriptor,ke=Object.getOwnPropertyDescriptors,Be=Object.getOwnPropertyNames,D=Object.getOwnPropertySymbols,Re=Object.getPrototypeOf,G=Object.prototype.hasOwnProperty,re=Object.prototype.propertyIsEnumerable;var ne=(t,e,n)=>e in t?R(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,g=(t,e)=>{for(var n in e||(e={}))G.call(e,n)&&ne(t,n,e[n]);if(D)for(var n of D(e))re.call(e,n)&&ne(t,n,e[n]);return t},b=(t,e)=>Ie(t,ke(e));var H=(t,e)=>{var n={};for(var r in t)G.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(t!=null&&D)for(var r of D(t))e.indexOf(r)<0&&re.call(t,r)&&(n[r]=t[r]);return n};var Ne=(t,e)=>{for(var n in e)R(t,n,{get:e[n],enumerable:!0})},ie=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of Be(e))!G.call(t,o)&&o!==n&&R(t,o,{get:()=>e[o],enumerable:!(r=Ce(e,o))||r.enumerable});return t};var p=(t,e,n)=>(n=t!=null?Ae(Re(t)):{},ie(e||!t||!t.__esModule?R(n,"default",{value:t,enumerable:!0}):n,t)),Le=t=>ie(R({},"__esModule",{value:!0}),t);var u=(t,e,n)=>new Promise((r,o)=>{var c=l=>{try{a(n.next(l))}catch(m){o(m)}},s=l=>{try{a(n.throw(l))}catch(m){o(m)}},a=l=>l.done?r(l.value):Promise.resolve(l.value).then(c,s);a((n=n.apply(t,e)).next())});var rt={};Ne(rt,{API_URLS:()=>d,Api:()=>te,DEV_API_URLS:()=>tt,JupTokenType:()=>Se,SESSION_KEY:()=>X,STORAGE_KEY:()=>U,endlessRetry:()=>nt,getSessionKey:()=>Pe,updateReqHistory:()=>_});module.exports=Le(rt);var ee=p(require("axios"));var Fe=require("@solana/web3.js");var N=require("lodash");var j=class{constructor(e){this.logLevel=e.logLevel!==void 0?e.logLevel:0,this.name=e.name}set level(e){this.logLevel=e}get time(){return Date.now().toString()}get moduleName(){return this.name}isLogLevel(e){return e<=this.logLevel}error(...e){return this.isLogLevel(0)?(console.error(this.time,this.name,"sdk logger error",...e),this):this}logWithError(...e){let n=e.map(r=>typeof r=="object"?JSON.stringify(r):r).join(", ");throw new Error(n)}warning(...e){return this.isLogLevel(1)?(console.warn(this.time,this.name,"sdk logger warning",...e),this):this}info(...e){return this.isLogLevel(2)?(console.info(this.time,this.name,"sdk logger info",...e),this):this}debug(...e){return this.isLogLevel(3)?(console.debug(this.time,this.name,"sdk logger debug",...e),this):this}},oe={},Oe={};function f(t){let e=(0,N.get)(oe,t);if(!e){let n=(0,N.get)(Oe,t);e=new j({name:t,logLevel:n}),(0,N.set)(oe,t,e)}return e}var V=require("@solana/spl-token");var mt=f("Sega_accountInfo_util");var h=p(require("bn.js")),Ge=p(require("decimal.js"));var le=p(require("big.js")),Ve=p(require("bn.js"));var se=p(require("toformat")),Ee=se.default,L=Ee;var O=p(require("big.js")),ce=p(require("bn.js")),ue=p(require("decimal.js-light"));var S=p(require("bn.js"));var ae=9007199254740991;function T(t){let e=f("Sega_parseBigNumberish");if(t instanceof S.default)return t;if(typeof t=="string"){if(t.match(/^-?[0-9]+$/))return new S.default(t);e.logWithError(`invalid BigNumberish string: ${t}`)}return typeof t=="number"?(t%1&&e.logWithError(`BigNumberish number underflow: ${t}`),(t>=ae||t<=-ae)&&e.logWithError(`BigNumberish number overflow: ${t}`),new S.default(String(t))):typeof t=="bigint"?new S.default(t.toString()):(e.error(`invalid BigNumberish value: ${t}`),new S.default(0))}var K=f("module/fraction"),z=L(O.default),F=L(ue.default),_e={0:F.ROUND_DOWN,1:F.ROUND_HALF_UP,2:F.ROUND_UP},De={0:O.default.roundDown,1:O.default.roundHalfUp,2:O.default.roundUp},y=class t{constructor(e,n=new ce.default(1)){this.numerator=T(e),this.denominator=T(n)}get quotient(){return this.numerator.div(this.denominator)}invert(){return new t(this.denominator,this.numerator)}add(e){let n=e instanceof t?e:new t(T(e));return this.denominator.eq(n.denominator)?new t(this.numerator.add(n.numerator),this.denominator):new t(this.numerator.mul(n.denominator).add(n.numerator.mul(this.denominator)),this.denominator.mul(n.denominator))}sub(e){let n=e instanceof t?e:new t(T(e));return this.denominator.eq(n.denominator)?new t(this.numerator.sub(n.numerator),this.denominator):new t(this.numerator.mul(n.denominator).sub(n.numerator.mul(this.denominator)),this.denominator.mul(n.denominator))}mul(e){let n=e instanceof t?e:new t(T(e));return new t(this.numerator.mul(n.numerator),this.denominator.mul(n.denominator))}div(e){let n=e instanceof t?e:new t(T(e));return new t(this.numerator.mul(n.denominator),this.denominator.mul(n.numerator))}toSignificant(e,n={groupSeparator:""},r=1){Number.isInteger(e)||K.logWithError(`${e} is not an integer.`),e<=0&&K.logWithError(`${e} is not positive.`),F.set({precision:e+1,rounding:_e[r]});let o=new F(this.numerator.toString()).div(this.denominator.toString()).toSignificantDigits(e);return o.toFormat(o.decimalPlaces(),n)}toFixed(e,n={groupSeparator:""},r=1){return Number.isInteger(e)||K.logWithError(`${e} is not an integer.`),e<0&&K.logWithError(`${e} is negative.`),z.DP=e,z.RM=De[r]||1,new z(this.numerator.toString()).div(this.denominator.toString()).toFormat(e,n)}isZero(){return this.numerator.isZero()}};var Ot=f("Sega_amount"),Ft=L(le.default);var me=require("@solana/web3.js"),$=require("@solana/spl-token"),pe={chainId:101,address:me.PublicKey.default.toBase58(),programId:$.TOKEN_PROGRAM_ID.toBase58(),decimals:9,symbol:"SOL",name:"solana",logoURI:"https://img-v1.raydium.io/icon/So11111111111111111111111111111111111111112.png",tags:[],priority:2,type:"sega",extensions:{coingeckoId:"solana"}},w={chainId:101,address:"So11111111111111111111111111111111111111112",programId:$.TOKEN_PROGRAM_ID.toBase58(),decimals:9,symbol:"WSOL",name:"Wrapped SOL",logoURI:"https://img-v1.raydium.io/icon/So11111111111111111111111111111111111111112.png",tags:[],priority:2,type:"sega",extensions:{coingeckoId:"solana"}};var W=require("@solana/web3.js");var i=require("@solana/web3.js"),fe=require("@solana/spl-token");function J({pubkey:t,isSigner:e=!1,isWritable:n=!0}){return{pubkey:t,isWritable:n,isSigner:e}}var Dt=[J({pubkey:fe.TOKEN_PROGRAM_ID,isWritable:!1}),J({pubkey:i.SystemProgram.programId,isWritable:!1}),J({pubkey:i.SYSVAR_RENT_PUBKEY,isWritable:!1})];function de({publicKey:t,transformSol:e}){let n=ge(t.toString());if(n instanceof i.PublicKey)return e&&n.equals(E)?Y:n;if(e&&n.toString()===E.toBase58())return Y;if(typeof n=="string"){if(n===i.PublicKey.default.toBase58())return i.PublicKey.default;try{return new i.PublicKey(n)}catch(r){throw new Error("invalid public key")}}throw new Error("invalid public key")}function ge(t){try{return new i.PublicKey(t)}catch(e){return t}}var Vt=new i.PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),Kt=new i.PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),Wt=new i.PublicKey("SysvarRent111111111111111111111111111111111"),vt=new i.PublicKey("SysvarC1ock11111111111111111111111111111111"),We=new i.PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"),qt=new i.PublicKey("Sysvar1nstructions1111111111111111111111111"),Ut=i.SystemProgram.programId,Gt=new i.PublicKey("4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R"),Ht=new i.PublicKey("Ea5SjE2Y6yvCeW5dYTn7PYMuW5ikXkvbGdcmSnXeaLjS"),jt=new i.PublicKey("SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt"),Yt=new i.PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"),zt=new i.PublicKey("Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB"),$t=new i.PublicKey("mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So"),Jt=new i.PublicKey("7dHbWXmci3dT8UFYWYZweBLXgycu7Y3iL6trKn1Y7ARj"),Zt=new i.PublicKey("USDH1SM1ojwWUga67PGrgFWUHibbjqMvuMaDkRJTgkX"),Xt=new i.PublicKey("NRVwhjBQiUPYtfDT5zRBVJajzFQHaBUNtC7SNVvqRFa"),Qt=new i.PublicKey("ANAxByE6G2WjFp7A4NqtWYXb3mgruyzZYg3spfxe6Lbo"),en=new i.PublicKey("7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs"),Y=new i.PublicKey("So11111111111111111111111111111111111111112"),E=i.PublicKey.default;var v=class v{constructor({mint:e,decimals:n,symbol:r,name:o,skipMint:c=!1,isToken2022:s=!1}){if(e===E.toBase58()||e instanceof W.PublicKey&&E.equals(e)){this.decimals=w.decimals,this.symbol=w.symbol,this.name=w.name,this.mint=new W.PublicKey(w.address),this.isToken2022=!1;return}this.decimals=n,this.symbol=r||e.toString().substring(0,6),this.name=o||e.toString().substring(0,6),this.mint=c?W.PublicKey.default:de({publicKey:e}),this.isToken2022=s}equals(e){return this===e?!0:this.mint.equals(e.mint)}};v.WSOL=new v(b(g({},w),{mint:w.address}));var M=v;var q=class q{constructor({decimals:e,symbol:n="UNKNOWN",name:r="UNKNOWN"}){this.decimals=e,this.symbol=n,this.name=r}equals(e){return this===e}};q.SOL=new q(pe);var Z=q;var he=p(require("bn.js"));var pn=new y(new he.default(100));var Tn=f("Sega_price");var qe=require("@solana/web3.js"),Ue=p(require("bn.js"));function xe(t){return u(this,null,function*(){new Promise(e=>setTimeout(e,t))})}var ve=new h.default(0),Zn=new h.default(1),Xn=new h.default(2),Qn=new h.default(3),er=new h.default(5),Ke=new h.default(10),tr=new h.default(100),nr=new h.default(1e3),rr=new h.default(1e4);var Ye=require("@solana/web3.js"),ze=require("@solana/spl-token");var A=require("@solana/web3.js");var je=require("@solana/spl-token");var br=f("Sega_txUtil");var x=require("@solana/web3.js"),Ar=new x.PublicKey("SegazTQwbYWknDZkJ6j2Kgvm5gw3MrHGKtWstZdoNKZ"),Ir=new x.PublicKey("Ajk8d9bERSaFdeoT1d8JUVfaDayrYLnAdgeB47TFMUaG"),Cr=new x.PublicKey("2HbjxVVKJ7Ct72Rcd8WK4VTqmwTXL5aAggkH1CHGFGmh"),$e=new x.PublicKey("auetiVUVJkRxvNPLYEvVnz8UJSqQ1VgoA83PcicHVD1"),Je=new x.PublicKey("GzqQwxPSmUZnTyCfX9YWqi32MJg43ujP786ZoacL559Y"),Ze=new x.PublicKey("GzqQwxPSmUZnTyCfX9YWqi32MJg43ujP786ZoacL559Y");var kr={SERUM_MARKET:x.PublicKey.default,UTIL1216:x.PublicKey.default,CREATE_CPMM_POOL_PROGRAM:$e,CREATE_CPMM_POOL_AUTH:Je,CREATE_CPMM_POOL_FEE_ACC:Ze};var Xe=p(require("bn.js"));var we=require("@solana/web3.js");var I=require("@solana/web3.js"),Qe=p(require("axios"));var et=require("@solana/web3.js");var d={BASE_HOST:"https://api.sega.so/api",SEND_TRANSACTION:"/send-transaction",FARM_ARP:"/main/farm/info",FARM_ARP_LINE:"/main/farm-apr-tv",CLMM_CONFIG:"/main/clmm-config",CPMM_CONFIG:"/main/cpmm-config",VERSION:"/main/version",CHECK_AVAILABILITY:"/v3/main/AvailabilityCheckAPI",RPCS:"/main/rpcs",INFO:"/main/info",STAKE_POOLS:"/main/stake-pools",CHAIN_TIME:"/main/chain-time",TOKEN_LIST:"/mint/list",MINT_INFO_ID:"/mint/ids",JUP_TOKEN_LIST:"https://tokens.jup.ag/tokens?tags=lst,community",POOL_LIST:"/pools/info/list",POOL_SEARCH_BY_ID:"/pools/info/ids",POOL_SEARCH_MINT:"/pools/info/mint",POOL_SEARCH_LP:"/pools/info/lps",POOL_KEY_BY_ID:"/pools/key/ids",POOL_LIQUIDITY_LINE:"/pools/line/liquidity",POOL_POSITION_LINE:"/pools/line/position",FARM_INFO:"/farms/info/ids",FARM_LP_INFO:"/farms/info/lp",FARM_KEYS:"/farms/key/ids",OWNER_CREATED_FARM:"/create-pool/{owner}",OWNER_IDO:"/main/ido/{owner}",OWNER_STAKE_FARMS:"/position/stake/{owner}",OWNER_LOCK_POSITION:"/position/clmm-lock/{owner}",IDO_KEYS:"/ido/key/ids",SWAP_COMPUTE:"/compute/",SWAP_TX:"/transaction/",MINT_PRICE:"/mint/price",MIGRATE_CONFIG:"/main/migrate-lp",PRIORITY_FEE:"/main/auto-fee"},tt=g({},d);var X="ray_tab_hash",U="ray_req_hash",Pe=()=>{if(typeof window===void 0)return"";let t=sessionStorage.getItem(X);return t||(t=`ray-${Date.now()}`,sessionStorage.setItem(X,t)),t},_=r=>u(void 0,null,function*(){var o=r,{logCount:t=1e3,removeLastLog:e}=o,n=H(o,["logCount","removeLastLog"]);if(typeof window===void 0)return new Promise(s=>s());let c=JSON.parse(localStorage.getItem(U)||"[]").slice(0,t-1);e&&c.pop(),new Blob([JSON.stringify(n.data)]).size>1024&&(n.data=JSON.stringify(n.data).substring(0,200)+"..."),c.unshift(b(g({},n),{time:Date.now(),session:Pe()}));try{localStorage.setItem(U,JSON.stringify(c))}catch(s){if(e){let a=!1,l=JSON.stringify(n.data).substring(0,100);for(c[0].data=l+(l.length>100?"...":"");!a;){c.pop();let m=JSON.stringify(n.data).substring(0,100);c[0].data=m+(m.length>100?"...":"");try{localStorage.setItem(U,JSON.stringify(c)),a=!0}catch(P){a=!1}}return new Promise(m=>m())}return _(b(g({},n),{logCount:t,removeLastLog:!0}))}});var C=f("Sega_Api"),Q=new Map;function nt(t,e,n=1e3){return u(this,null,function*(){let r;for(;r==null;)try{C.debug(`Request ${t} through endlessRetry`),r=yield e()}catch(o){C.error(`Request ${t} failed, retry after ${n} ms`,o),yield xe(n)}return r})}var te=class{constructor({cluster:e,timeout:n,logRequests:r,logCount:o,urlConfigs:c}){this.cluster=e,this.urlConfigs=c||{},this.logCount=o||1e3,this.api=ee.default.create({baseURL:this.urlConfigs.BASE_HOST||d.BASE_HOST,timeout:n}),this.api.interceptors.request.use(s=>{let{method:a,baseURL:l,url:m}=s;return C.debug(`${a==null?void 0:a.toUpperCase()} ${l}${m}`),s},s=>(C.error("Request failed"),Promise.reject(s))),this.api.interceptors.response.use(s=>{let{config:a,data:l,status:m}=s,{method:P,baseURL:k,url:B}=a;return r&&_({status:m,url:`${k}${B}`,params:a.params,data:l,logCount:this.logCount}),C.debug(`${P==null?void 0:P.toUpperCase()} ${k}${B} ${m}`),l},s=>{let{config:a,response:l={}}=s,{status:m}=l,{method:P,baseURL:k,url:B}=a;return r&&_({status:m,url:`${k}${B}`,params:a.params,data:s.message,logCount:this.logCount}),C.error(`${P.toUpperCase()} ${k}${B} ${m||s.message}`),Promise.reject(s)})}getCpmmConfigs(){return u(this,null,function*(){return(yield this.api.get(this.urlConfigs.CPMM_CONFIG||d.CPMM_CONFIG)).data})}getBlockSlotCountForSecond(e){return u(this,null,function*(){if(!e)return 2;let r=(yield ee.default.post(e,{id:"getRecentPerformanceSamples",jsonrpc:"2.0",method:"getRecentPerformanceSamples",params:[4]})).result.map(o=>o.numSlots);return r.reduce((o,c)=>o+c,0)/r.length/60})}getChainTimeOffset(){return u(this,null,function*(){return(yield this.api.get(this.urlConfigs.CHAIN_TIME||d.CHAIN_TIME)).data})}fetchPoolKeysById(e){return u(this,null,function*(){let{idList:n}=e,r=[],o=n.filter(s=>Q.has(s)?(r.push(Q.get(s)),!1):!0),c=[];return o.length&&(c=(yield this.api.get((this.urlConfigs.POOL_KEY_BY_ID||d.POOL_KEY_BY_ID)+`?ids=${o.join(",")}`)).data.filter(Boolean),c.forEach(a=>{Q.set(a.id,a)})),r.concat(c)})}getRpcs(){return u(this,null,function*(){return this.api.get(this.urlConfigs.RPCS||d.RPCS)})}getTokenList(){return u(this,null,function*(){return(yield this.api.get(this.urlConfigs.TOKEN_LIST||d.TOKEN_LIST)).data})}getJupTokenList(){return u(this,null,function*(){return this.api.get("",{baseURL:this.urlConfigs.JUP_TOKEN_LIST||d.JUP_TOKEN_LIST})})}getTokenInfo(e){return u(this,null,function*(){return(yield this.api.get((this.urlConfigs.MINT_INFO_ID||d.MINT_INFO_ID)+`?mints=${e.map(r=>r.toString()).join(",")}`)).data})}fetchAvailabilityStatus(){return u(this,null,function*(){return(yield this.api.get(this.urlConfigs.CHECK_AVAILABILITY||d.CHECK_AVAILABILITY)).data})}};var Se=(n=>(n.ALL="all",n.Strict="strict",n))(Se||{});0&&(module.exports={API_URLS,Api,DEV_API_URLS,JupTokenType,SESSION_KEY,STORAGE_KEY,endlessRetry,getSessionKey,updateReqHistory}); //# sourceMappingURL=index.js.map