UNPKG

@sega-so/sega-sdk

Version:

An SDK for building applications on top of SEGA.

2 lines 14.6 kB
"use strict";var Pe=Object.create;var N=Object.defineProperty,Se=Object.defineProperties,Ie=Object.getOwnPropertyDescriptor,Ce=Object.getOwnPropertyDescriptors,Ae=Object.getOwnPropertyNames,F=Object.getOwnPropertySymbols,ke=Object.getPrototypeOf,U=Object.prototype.hasOwnProperty,ne=Object.prototype.propertyIsEnumerable;var te=(t,e,n)=>e in t?N(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,d=(t,e)=>{for(var n in e||(e={}))U.call(e,n)&&te(t,n,e[n]);if(F)for(var n of F(e))ne.call(e,n)&&te(t,n,e[n]);return t},T=(t,e)=>Se(t,Ce(e));var G=(t,e)=>{var n={};for(var r in t)U.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(t!=null&&F)for(var r of F(t))e.indexOf(r)<0&&ne.call(t,r)&&(n[r]=t[r]);return n};var Be=(t,e)=>{for(var n in e)N(t,n,{get:e[n],enumerable:!0})},re=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of Ae(e))!U.call(t,o)&&o!==n&&N(t,o,{get:()=>e[o],enumerable:!(r=Ie(e,o))||r.enumerable});return t};var p=(t,e,n)=>(n=t!=null?Pe(ke(t)):{},re(e||!t||!t.__esModule?N(n,"default",{value:t,enumerable:!0}):n,t)),Ne=t=>re(N({},"__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 tt={};Be(tt,{Api:()=>ee,endlessRetry:()=>et});module.exports=Ne(tt);var Q=p(require("axios"));var Le=require("@solana/web3.js");var R=require("lodash");var H=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}},ie={},Re={};function f(t){let e=(0,R.get)(ie,t);if(!e){let n=(0,R.get)(Re,t);e=new H({name:t,logLevel:n}),(0,R.set)(ie,t,e)}return e}var D=require("@solana/spl-token");var ut=f("Sega_accountInfo_util");var h=p(require("bn.js")),qe=p(require("decimal.js"));var ue=p(require("big.js")),Fe=p(require("bn.js"));var oe=p(require("toformat")),Oe=oe.default,L=Oe;var O=p(require("big.js")),ae=p(require("bn.js")),ce=p(require("decimal.js-light"));var S=p(require("bn.js"));var se=9007199254740991;function b(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>=se||t<=-se)&&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"),Y=L(O.default),E=L(ce.default),Me={0:E.ROUND_DOWN,1:E.ROUND_HALF_UP,2:E.ROUND_UP},_e={0:O.default.roundDown,1:O.default.roundHalfUp,2:O.default.roundUp},y=class t{constructor(e,n=new ae.default(1)){this.numerator=b(e),this.denominator=b(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(b(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(b(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(b(e));return new t(this.numerator.mul(n.numerator),this.denominator.mul(n.denominator))}div(e){let n=e instanceof t?e:new t(b(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.`),E.set({precision:e+1,rounding:Me[r]});let o=new E(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.`),Y.DP=e,Y.RM=_e[r]||1,new Y(this.numerator.toString()).div(this.denominator.toString()).toFormat(e,n)}isZero(){return this.numerator.isZero()}};var Rt=f("Sega_amount"),Lt=L(ue.default);var le=require("@solana/web3.js"),z=require("@solana/spl-token"),me={chainId:101,address:le.PublicKey.default.toBase58(),programId:z.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:z.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 V=require("@solana/web3.js");var i=require("@solana/web3.js"),pe=require("@solana/spl-token");function $({pubkey:t,isSigner:e=!1,isWritable:n=!0}){return{pubkey:t,isWritable:n,isSigner:e}}var _t=[$({pubkey:pe.TOKEN_PROGRAM_ID,isWritable:!1}),$({pubkey:i.SystemProgram.programId,isWritable:!1}),$({pubkey:i.SYSVAR_RENT_PUBKEY,isWritable:!1})];function fe({publicKey:t,transformSol:e}){let n=de(t.toString());if(n instanceof i.PublicKey)return e&&n.equals(M)?j:n;if(e&&n.toString()===M.toBase58())return j;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 de(t){try{return new i.PublicKey(t)}catch(e){return t}}var Ft=new i.PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),Dt=new i.PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),Kt=new i.PublicKey("SysvarRent111111111111111111111111111111111"),Vt=new i.PublicKey("SysvarC1ock11111111111111111111111111111111"),Ke=new i.PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"),Wt=new i.PublicKey("Sysvar1nstructions1111111111111111111111111"),vt=i.SystemProgram.programId,qt=new i.PublicKey("4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R"),Ut=new i.PublicKey("Ea5SjE2Y6yvCeW5dYTn7PYMuW5ikXkvbGdcmSnXeaLjS"),Gt=new i.PublicKey("SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt"),Ht=new i.PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"),jt=new i.PublicKey("Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB"),Yt=new i.PublicKey("mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So"),zt=new i.PublicKey("7dHbWXmci3dT8UFYWYZweBLXgycu7Y3iL6trKn1Y7ARj"),$t=new i.PublicKey("USDH1SM1ojwWUga67PGrgFWUHibbjqMvuMaDkRJTgkX"),Jt=new i.PublicKey("NRVwhjBQiUPYtfDT5zRBVJajzFQHaBUNtC7SNVvqRFa"),Zt=new i.PublicKey("ANAxByE6G2WjFp7A4NqtWYXb3mgruyzZYg3spfxe6Lbo"),Xt=new i.PublicKey("7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs"),j=new i.PublicKey("So11111111111111111111111111111111111111112"),M=i.PublicKey.default;var W=class W{constructor({mint:e,decimals:n,symbol:r,name:o,skipMint:c=!1,isToken2022:s=!1}){if(e===M.toBase58()||e instanceof V.PublicKey&&M.equals(e)){this.decimals=w.decimals,this.symbol=w.symbol,this.name=w.name,this.mint=new V.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?V.PublicKey.default:fe({publicKey:e}),this.isToken2022=s}equals(e){return this===e?!0:this.mint.equals(e.mint)}};W.WSOL=new W(T(d({},w),{mint:w.address}));var _=W;var v=class v{constructor({decimals:e,symbol:n="UNKNOWN",name:r="UNKNOWN"}){this.decimals=e,this.symbol=n,this.name=r}equals(e){return this===e}};v.SOL=new v(me);var J=v;var ge=p(require("bn.js"));var ln=new y(new ge.default(100));var hn=f("Sega_price");var We=require("@solana/web3.js"),ve=p(require("bn.js"));function ye(t){return u(this,null,function*(){new Promise(e=>setTimeout(e,t))})}var Ve=new h.default(0),$n=new h.default(1),Jn=new h.default(2),Zn=new h.default(3),Xn=new h.default(5),De=new h.default(10),Qn=new h.default(100),er=new h.default(1e3),tr=new h.default(1e4);var He=require("@solana/web3.js"),je=require("@solana/spl-token");var I=require("@solana/web3.js");var Ge=require("@solana/spl-token");var gr=f("Sega_txUtil");var x=require("@solana/web3.js"),Pr=new x.PublicKey("SegazTQwbYWknDZkJ6j2Kgvm5gw3MrHGKtWstZdoNKZ"),Sr=new x.PublicKey("Ajk8d9bERSaFdeoT1d8JUVfaDayrYLnAdgeB47TFMUaG"),Ir=new x.PublicKey("2HbjxVVKJ7Ct72Rcd8WK4VTqmwTXL5aAggkH1CHGFGmh"),Ye=new x.PublicKey("auetiVUVJkRxvNPLYEvVnz8UJSqQ1VgoA83PcicHVD1"),ze=new x.PublicKey("GzqQwxPSmUZnTyCfX9YWqi32MJg43ujP786ZoacL559Y"),$e=new x.PublicKey("GzqQwxPSmUZnTyCfX9YWqi32MJg43ujP786ZoacL559Y");var Cr={SERUM_MARKET:x.PublicKey.default,UTIL1216:x.PublicKey.default,CREATE_CPMM_POOL_PROGRAM:Ye,CREATE_CPMM_POOL_AUTH:ze,CREATE_CPMM_POOL_FEE_ACC:$e};var Je=p(require("bn.js"));var xe=require("@solana/web3.js");var C=require("@solana/web3.js"),Ze=p(require("axios"));var Xe=require("@solana/web3.js");var g={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"},xi=d({},g);var we="ray_tab_hash",Z="ray_req_hash",Qe=()=>{if(typeof window===void 0)return"";let t=sessionStorage.getItem(we);return t||(t=`ray-${Date.now()}`,sessionStorage.setItem(we,t)),t},q=r=>u(void 0,null,function*(){var o=r,{logCount:t=1e3,removeLastLog:e}=o,n=G(o,["logCount","removeLastLog"]);if(typeof window===void 0)return new Promise(s=>s());let c=JSON.parse(localStorage.getItem(Z)||"[]").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(T(d({},n),{time:Date.now(),session:Qe()}));try{localStorage.setItem(Z,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(Z,JSON.stringify(c)),a=!0}catch(P){a=!1}}return new Promise(m=>m())}return q(T(d({},n),{logCount:t,removeLastLog:!0}))}});var A=f("Sega_Api"),X=new Map;function et(t,e,n=1e3){return u(this,null,function*(){let r;for(;r==null;)try{A.debug(`Request ${t} through endlessRetry`),r=yield e()}catch(o){A.error(`Request ${t} failed, retry after ${n} ms`,o),yield ye(n)}return r})}var ee=class{constructor({cluster:e,timeout:n,logRequests:r,logCount:o,urlConfigs:c}){this.cluster=e,this.urlConfigs=c||{},this.logCount=o||1e3,this.api=Q.default.create({baseURL:this.urlConfigs.BASE_HOST||g.BASE_HOST,timeout:n}),this.api.interceptors.request.use(s=>{let{method:a,baseURL:l,url:m}=s;return A.debug(`${a==null?void 0:a.toUpperCase()} ${l}${m}`),s},s=>(A.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&&q({status:m,url:`${k}${B}`,params:a.params,data:l,logCount:this.logCount}),A.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&&q({status:m,url:`${k}${B}`,params:a.params,data:s.message,logCount:this.logCount}),A.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||g.CPMM_CONFIG)).data})}getBlockSlotCountForSecond(e){return u(this,null,function*(){if(!e)return 2;let r=(yield Q.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||g.CHAIN_TIME)).data})}fetchPoolKeysById(e){return u(this,null,function*(){let{idList:n}=e,r=[],o=n.filter(s=>X.has(s)?(r.push(X.get(s)),!1):!0),c=[];return o.length&&(c=(yield this.api.get((this.urlConfigs.POOL_KEY_BY_ID||g.POOL_KEY_BY_ID)+`?ids=${o.join(",")}`)).data.filter(Boolean),c.forEach(a=>{X.set(a.id,a)})),r.concat(c)})}getRpcs(){return u(this,null,function*(){return this.api.get(this.urlConfigs.RPCS||g.RPCS)})}getTokenList(){return u(this,null,function*(){return(yield this.api.get(this.urlConfigs.TOKEN_LIST||g.TOKEN_LIST)).data})}getJupTokenList(){return u(this,null,function*(){return this.api.get("",{baseURL:this.urlConfigs.JUP_TOKEN_LIST||g.JUP_TOKEN_LIST})})}getTokenInfo(e){return u(this,null,function*(){return(yield this.api.get((this.urlConfigs.MINT_INFO_ID||g.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||g.CHECK_AVAILABILITY)).data})}};0&&(module.exports={Api,endlessRetry}); //# sourceMappingURL=api.js.map