UNPKG

@sega-so/sega-sdk

Version:

An SDK for building applications on top of SEGA.

2 lines 11.2 kB
var ye=Object.defineProperty,we=Object.defineProperties;var xe=Object.getOwnPropertyDescriptors;var X=Object.getOwnPropertySymbols;var Se=Object.prototype.hasOwnProperty,Pe=Object.prototype.propertyIsEnumerable;var g=Math.pow,$=(n,e,t)=>e in n?ye(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,h=(n,e)=>{for(var t in e||(e={}))Se.call(e,t)&&$(n,t,e[t]);if(X)for(var t of X(e))Pe.call(e,t)&&$(n,t,e[t]);return n},b=(n,e)=>we(n,xe(e));import Y from"bn.js";import O from"decimal.js-light";import{PublicKey as nt}from"@solana/web3.js";import{get as Q,set as Be}from"lodash";var v=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 t=e.map(r=>typeof r=="object"?JSON.stringify(r):r).join(", ");throw new Error(t)}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}},ee={},Ae={};function u(n){let e=Q(ee,n);if(!e){let t=Q(Ae,n);e=new v({name:n,logLevel:t}),Be(ee,n,e)}return e}import{MINT_SIZE as st,TOKEN_PROGRAM_ID as at,getTransferFeeConfig as ct,unpackMint as ut}from"@solana/spl-token";var lt=u("Sega_accountInfo_util");import d from"bn.js";import sr from"decimal.js";import De from"big.js";import Rt from"bn.js";import ke from"toformat";var Ce=ke,w=Ce;import N from"big.js";import Ne from"bn.js";import Re from"decimal.js-light";import x from"bn.js";var te=9007199254740991;function m(n){let e=u("Sega_parseBigNumberish");if(n instanceof x)return n;if(typeof n=="string"){if(n.match(/^-?[0-9]+$/))return new x(n);e.logWithError(`invalid BigNumberish string: ${n}`)}return typeof n=="number"?(n%1&&e.logWithError(`BigNumberish number underflow: ${n}`),(n>=te||n<=-te)&&e.logWithError(`BigNumberish number overflow: ${n}`),new x(String(n))):typeof n=="bigint"?new x(n.toString()):(e.error(`invalid BigNumberish value: ${n}`),new x(0))}var I=u("module/fraction"),q=w(N),S=w(Re),Me={0:S.ROUND_DOWN,1:S.ROUND_HALF_UP,2:S.ROUND_UP},Fe={0:N.roundDown,1:N.roundHalfUp,2:N.roundUp},p=class n{constructor(e,t=new Ne(1)){this.numerator=m(e),this.denominator=m(t)}get quotient(){return this.numerator.div(this.denominator)}invert(){return new n(this.denominator,this.numerator)}add(e){let t=e instanceof n?e:new n(m(e));return this.denominator.eq(t.denominator)?new n(this.numerator.add(t.numerator),this.denominator):new n(this.numerator.mul(t.denominator).add(t.numerator.mul(this.denominator)),this.denominator.mul(t.denominator))}sub(e){let t=e instanceof n?e:new n(m(e));return this.denominator.eq(t.denominator)?new n(this.numerator.sub(t.numerator),this.denominator):new n(this.numerator.mul(t.denominator).sub(t.numerator.mul(this.denominator)),this.denominator.mul(t.denominator))}mul(e){let t=e instanceof n?e:new n(m(e));return new n(this.numerator.mul(t.numerator),this.denominator.mul(t.denominator))}div(e){let t=e instanceof n?e:new n(m(e));return new n(this.numerator.mul(t.denominator),this.denominator.mul(t.numerator))}toSignificant(e,t={groupSeparator:""},r=1){Number.isInteger(e)||I.logWithError(`${e} is not an integer.`),e<=0&&I.logWithError(`${e} is not positive.`),S.set({precision:e+1,rounding:Me[r]});let o=new S(this.numerator.toString()).div(this.denominator.toString()).toSignificantDigits(e);return o.toFormat(o.decimalPlaces(),t)}toFixed(e,t={groupSeparator:""},r=1){return Number.isInteger(e)||I.logWithError(`${e} is not an integer.`),e<0&&I.logWithError(`${e} is negative.`),q.DP=e,q.RM=Fe[r]||1,new q(this.numerator.toString()).div(this.denominator.toString()).toFormat(e,t)}isZero(){return this.numerator.isZero()}};var Wt=u("Sega_amount"),vt=w(De);import{PublicKey as Oe}from"@solana/web3.js";import{TOKEN_PROGRAM_ID as ne}from"@solana/spl-token";var re={chainId:101,address:Oe.default.toBase58(),programId:ne.toBase58(),decimals:9,symbol:"SOL",name:"solana",logoURI:"https://img-v1.raydium.io/icon/So11111111111111111111111111111111111111112.png",tags:[],priority:2,type:"sega",extensions:{coingeckoId:"solana"}},f={chainId:101,address:"So11111111111111111111111111111111111111112",programId:ne.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"}};import{PublicKey as G}from"@solana/web3.js";import{PublicKey as i,SystemProgram as ie,SYSVAR_RENT_PUBKEY as Le}from"@solana/web3.js";import{TOKEN_PROGRAM_ID as _e}from"@solana/spl-token";function U({pubkey:n,isSigner:e=!1,isWritable:t=!0}){return{pubkey:n,isWritable:t,isSigner:e}}var Yt=[U({pubkey:_e,isWritable:!1}),U({pubkey:ie.programId,isWritable:!1}),U({pubkey:Le,isWritable:!1})];function oe({publicKey:n,transformSol:e}){let t=se(n.toString());if(t instanceof i)return e&&t.equals(P)?K:t;if(e&&t.toString()===P.toBase58())return K;if(typeof t=="string"){if(t===i.default.toBase58())return i.default;try{return new i(t)}catch(r){throw new Error("invalid public key")}}throw new Error("invalid public key")}function se(n){try{return new i(n)}catch(e){return n}}var Zt=new i("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),Jt=new i("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),Xt=new i("SysvarRent111111111111111111111111111111111"),$t=new i("SysvarC1ock11111111111111111111111111111111"),Ve=new i("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"),Qt=new i("Sysvar1nstructions1111111111111111111111111"),en=ie.programId,tn=new i("4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R"),nn=new i("Ea5SjE2Y6yvCeW5dYTn7PYMuW5ikXkvbGdcmSnXeaLjS"),rn=new i("SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt"),on=new i("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"),sn=new i("Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB"),an=new i("mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So"),cn=new i("7dHbWXmci3dT8UFYWYZweBLXgycu7Y3iL6trKn1Y7ARj"),un=new i("USDH1SM1ojwWUga67PGrgFWUHibbjqMvuMaDkRJTgkX"),ln=new i("NRVwhjBQiUPYtfDT5zRBVJajzFQHaBUNtC7SNVvqRFa"),mn=new i("ANAxByE6G2WjFp7A4NqtWYXb3mgruyzZYg3spfxe6Lbo"),pn=new i("7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs"),K=new i("So11111111111111111111111111111111111111112"),P=i.default;var R=class R{constructor({mint:e,decimals:t,symbol:r,name:o,skipMint:c=!1,isToken2022:a=!1}){if(e===P.toBase58()||e instanceof G&&P.equals(e)){this.decimals=f.decimals,this.symbol=f.symbol,this.name=f.name,this.mint=new G(f.address),this.isToken2022=!1;return}this.decimals=t,this.symbol=r||e.toString().substring(0,6),this.name=o||e.toString().substring(0,6),this.mint=c?G.default:oe({publicKey:e}),this.isToken2022=a}equals(e){return this===e?!0:this.mint.equals(e.mint)}};R.WSOL=new R(b(h({},f),{mint:f.address}));var B=R;var M=class M{constructor({decimals:e,symbol:t="UNKNOWN",name:r="UNKNOWN"}){this.decimals=e,this.symbol=t,this.name=r}equals(e){return this===e}};M.SOL=new M(re);var j=M;import We from"bn.js";var An=new p(new We(100));var Mn=u("Sega_price");import{PublicKey as Yn}from"@solana/web3.js";import Jn from"bn.js";var ve=new d(0),hr=new d(1),br=new d(2),Tr=new d(3),yr=new d(5),Ee=new d(10),wr=new d(100),xr=new d(1e3),Sr=new d(1e4);import{PublicKey as Jr}from"@solana/web3.js";import{TOKEN_PROGRAM_ID as $r}from"@solana/spl-token";import{ComputeBudgetProgram as Mr,Keypair as Er,PublicKey as Or,Transaction as _r,TransactionMessage as Wr,VersionedTransaction as vr}from"@solana/web3.js";import{TOKEN_PROGRAM_ID as Gr}from"@solana/spl-token";var jr=u("Sega_txUtil");import{PublicKey as l}from"@solana/web3.js";var ri=new l("SegazTQwbYWknDZkJ6j2Kgvm5gw3MrHGKtWstZdoNKZ"),ii=new l("Ajk8d9bERSaFdeoT1d8JUVfaDayrYLnAdgeB47TFMUaG"),oi=new l("2HbjxVVKJ7Ct72Rcd8WK4VTqmwTXL5aAggkH1CHGFGmh"),qe=new l("auetiVUVJkRxvNPLYEvVnz8UJSqQ1VgoA83PcicHVD1"),Ue=new l("GzqQwxPSmUZnTyCfX9YWqi32MJg43ujP786ZoacL559Y"),Ge=new l("GzqQwxPSmUZnTyCfX9YWqi32MJg43ujP786ZoacL559Y"),si=new l("SegaXNnoXYTZiqUt9Xn2XqGcL56b25yzXLuJSpadcMu"),ai=new l("2t1hV2YqnDCPuJZqr7Fy8oAygGwZE5Q9uaQzpLC1QryJ"),ci=new l("BLxynZeHE123MmQgNJZiV1pwW4VYh46oZYyC66xwmtTY");var je={SERUM_MARKET:l.default,UTIL1216:l.default,CREATE_CPMM_POOL_PROGRAM:qe,CREATE_CPMM_POOL_AUTH:Ue,CREATE_CPMM_POOL_FEE_ACC:Ge};import le from"bn.js";function z(n,e){let{div:t,mod:r}=n.divmod(e);return r.gt(new le(0))?t.add(new le(1)):t}import{PublicKey as hi,AddressLookupTableAccount as bi}from"@solana/web3.js";import{PublicKey as Ei,sendAndConfirmTransaction as Oi,SystemProgram as Vi,Transaction as Wi,TransactionMessage as Ki,VersionedTransaction as qi}from"@solana/web3.js";import Gi from"axios";import{Connection as eo}from"@solana/web3.js";import A from"bn.js";function F(n,e){if(e.isZero())throw Error("divisor is zero");return n.mod(e)}function ze(n,e){if(e.isZero())throw Error("rhs is zero");let t=n.div(e);if(t.isZero())throw Error("quotient is zero");let r=F(n,e);return r.gt(T)&&(t=t.add(new A(1)),e=n.div(t),r=F(n,t),r.gt(T)&&(e=e.add(new A(1)))),[t,e]}var T=new A(0),D=class{static swapWithoutFees(e,t,r){let o=t.mul(r),c=t.add(e),[a]=ze(o,c),s=r.sub(a);if(s.isZero())throw Error("destinationAmountSwapped is zero");return{destinationAmountSwapped:s}}static lpTokensToTradingTokens(e,t,r,o,c){let a=e.mul(r).div(t),s=e.mul(o).div(t);if(c===0)return{tokenAmount0:a,tokenAmount1:s};if(c===1)return F(e.mul(r),t).gt(T)&&a.gt(T)&&(a=a.add(new A(1))),F(e.mul(o),t).gt(T)&&s.gt(T)&&(s=s.add(new A(1))),{tokenAmount0:a,tokenAmount1:s};throw Error("roundDirection value error")}};import de from"bn.js";var H=new de(1e6);function He(n,e,t){return n.mul(e).add(t).sub(new de(1)).div(t)}function pe(n,e,t){return n.mul(e).div(t)}var E=class{static tradingFee(e,t){return He(e,t,H)}static protocolFee(e,t){return pe(e,t,H)}static fundFee(e,t){return pe(e,t,H)}};var me=(t=>(t[t.Floor=0]="Floor",t[t.Ceiling=1]="Ceiling",t))(me||{}),fe=class{static validate_supply(e,t){if(e.isZero())throw Error("tokenAmount0 is zero");if(t.isZero())throw Error("tokenAmount1 is zero")}static swap(e,t,r,o){let c=E.tradingFee(e,o),a=e.sub(c),{destinationAmountSwapped:s}=D.swapWithoutFees(a,t,r);return{newSwapDestinationAmount:r.sub(s),sourceAmountSwapped:e,destinationAmountSwapped:s,tradeFee:c}}static swapBaseOut({poolMintA:e,poolMintB:t,tradeFeeRate:r,baseReserve:o,quoteReserve:c,outputMint:a,outputAmount:s}){let[L,y,Z,J,Ye]=t.address===a.toString()?[o,c,e.decimals,t.decimals,e.address]:[c,o,t.decimals,e.decimals,t.address],_=new O(y.toString()).div(g(10,J)).div(new O(L.toString()).div(g(10,Z))),k=s.gte(y)?y.sub(new Y(1)):s,ge=y.sub(k),V=z(L.mul(k),ge),W=z(V.mul(new Y(1e6)),new Y(1e6).sub(r)),he=W.sub(V),be=new O(k.toString()).div(g(10,J)).div(new O(W.toString()).div(g(10,Z))),Te=_.isZero()?0:be.sub(_).div(_).abs().toNumber();return{amountRealOut:k,amountIn:W,amountInWithoutFee:V,tradeFee:he,priceImpact:Te}}};export{fe as CurveCalculator,me as RoundDirection}; //# sourceMappingURL=calculator.mjs.map