UNPKG

@sega-so/sega-sdk

Version:

An SDK for building applications on top of SEGA.

2 lines 11 kB
var ye=Object.defineProperty,we=Object.defineProperties;var xe=Object.getOwnPropertyDescriptors;var X=Object.getOwnPropertySymbols;var Se=Object.prototype.hasOwnProperty,Be=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))Be.call(e,t)&&$(n,t,e[t]);return n},b=(n,e)=>we(n,xe(e));import Y from"bn.js";import L from"decimal.js-light";import{PublicKey as tt}from"@solana/web3.js";import{get as Q,set as Pe}from"lodash";var K=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={},ke={};function u(n){let e=Q(ee,n);if(!e){let t=Q(ke,n);e=new K({name:n,logLevel:t}),Pe(ee,n,e)}return e}import{MINT_SIZE as ot,TOKEN_PROGRAM_ID as st,getTransferFeeConfig as at,unpackMint as ct}from"@solana/spl-token";var ut=u("Sega_accountInfo_util");import p from"bn.js";import or from"decimal.js";import Me from"big.js";import Nt from"bn.js";import Ae from"toformat";var Ce=Ae,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 l(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),Fe={0:S.ROUND_DOWN,1:S.ROUND_HALF_UP,2:S.ROUND_UP},De={0:N.roundDown,1:N.roundHalfUp,2:N.roundUp},m=class n{constructor(e,t=new Ne(1)){this.numerator=l(e),this.denominator=l(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(l(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(l(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(l(e));return new n(this.numerator.mul(t.numerator),this.denominator.mul(t.denominator))}div(e){let t=e instanceof n?e:new n(l(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:Fe[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=De[r]||1,new q(this.numerator.toString()).div(this.denominator.toString()).toFormat(e,t)}isZero(){return this.numerator.isZero()}};var Vt=u("Sega_amount"),vt=w(Me);import{PublicKey as Le}from"@solana/web3.js";import{TOKEN_PROGRAM_ID as ne}from"@solana/spl-token";var re={chainId:101,address:Le.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 Oe}from"@solana/web3.js";import{TOKEN_PROGRAM_ID as We}from"@solana/spl-token";function U({pubkey:n,isSigner:e=!1,isWritable:t=!0}){return{pubkey:n,isWritable:t,isSigner:e}}var Ht=[U({pubkey:We,isWritable:!1}),U({pubkey:ie.programId,isWritable:!1}),U({pubkey:Oe,isWritable:!1})];function oe({publicKey:n,transformSol:e}){let t=se(n.toString());if(t instanceof i)return e&&t.equals(B)?_:t;if(e&&t.toString()===B.toBase58())return _;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 Yt=new i("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),Zt=new i("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),Jt=new i("SysvarRent111111111111111111111111111111111"),Xt=new i("SysvarC1ock11111111111111111111111111111111"),Ve=new i("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"),$t=new i("Sysvar1nstructions1111111111111111111111111"),Qt=ie.programId,en=new i("4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R"),tn=new i("Ea5SjE2Y6yvCeW5dYTn7PYMuW5ikXkvbGdcmSnXeaLjS"),nn=new i("SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt"),rn=new i("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"),on=new i("Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB"),sn=new i("mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So"),an=new i("7dHbWXmci3dT8UFYWYZweBLXgycu7Y3iL6trKn1Y7ARj"),cn=new i("USDH1SM1ojwWUga67PGrgFWUHibbjqMvuMaDkRJTgkX"),un=new i("NRVwhjBQiUPYtfDT5zRBVJajzFQHaBUNtC7SNVvqRFa"),ln=new i("ANAxByE6G2WjFp7A4NqtWYXb3mgruyzZYg3spfxe6Lbo"),mn=new i("7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs"),_=new i("So11111111111111111111111111111111111111112"),B=i.default;var R=class R{constructor({mint:e,decimals:t,symbol:r,name:o,skipMint:c=!1,isToken2022:a=!1}){if(e===B.toBase58()||e instanceof G&&B.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 P=R;var F=class F{constructor({decimals:e,symbol:t="UNKNOWN",name:r="UNKNOWN"}){this.decimals=e,this.symbol=t,this.name=r}equals(e){return this===e}};F.SOL=new F(re);var j=F;import ve from"bn.js";var Pn=new m(new ve(100));var Rn=u("Sega_price");import{PublicKey as Hn}from"@solana/web3.js";import Zn from"bn.js";var Ke=new p(0),gr=new p(1),hr=new p(2),br=new p(3),Tr=new p(5),Ee=new p(10),yr=new p(100),wr=new p(1e3),xr=new p(1e4);import{PublicKey as Zr}from"@solana/web3.js";import{TOKEN_PROGRAM_ID as Xr}from"@solana/spl-token";import{ComputeBudgetProgram as Rr,Keypair as Mr,PublicKey as Er,Transaction as Or,TransactionMessage as Vr,VersionedTransaction as vr}from"@solana/web3.js";import{TOKEN_PROGRAM_ID as Ur}from"@solana/spl-token";var Gr=u("Sega_txUtil");import{PublicKey as d}from"@solana/web3.js";var ni=new d("SegazTQwbYWknDZkJ6j2Kgvm5gw3MrHGKtWstZdoNKZ"),ri=new d("Ajk8d9bERSaFdeoT1d8JUVfaDayrYLnAdgeB47TFMUaG"),ii=new d("2HbjxVVKJ7Ct72Rcd8WK4VTqmwTXL5aAggkH1CHGFGmh"),qe=new d("auetiVUVJkRxvNPLYEvVnz8UJSqQ1VgoA83PcicHVD1"),Ue=new d("GzqQwxPSmUZnTyCfX9YWqi32MJg43ujP786ZoacL559Y"),Ge=new d("GzqQwxPSmUZnTyCfX9YWqi32MJg43ujP786ZoacL559Y");var oi={SERUM_MARKET:d.default,UTIL1216:d.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 di,AddressLookupTableAccount as fi}from"@solana/web3.js";import{PublicKey as Fi,sendAndConfirmTransaction as Di,SystemProgram as Li,Transaction as Oi,TransactionMessage as Vi,VersionedTransaction as vi}from"@solana/web3.js";import _i from"axios";import{Connection as Zi}from"@solana/web3.js";import k from"bn.js";function D(n,e){if(e.isZero())throw Error("divisor is zero");return n.mod(e)}function je(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=D(n,e);return r.gt(T)&&(t=t.add(new k(1)),e=n.div(t),r=D(n,t),r.gt(T)&&(e=e.add(new k(1)))),[t,e]}var T=new k(0),M=class{static swapWithoutFees(e,t,r){let o=t.mul(r),c=t.add(e),[a]=je(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 D(e.mul(r),t).gt(T)&&a.gt(T)&&(a=a.add(new k(1))),D(e.mul(o),t).gt(T)&&s.gt(T)&&(s=s.add(new k(1))),{tokenAmount0:a,tokenAmount1:s};throw Error("roundDirection value error")}};import de from"bn.js";var H=new de(1e6);function ze(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 ze(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}=M.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[O,y,Z,J,He]=t.address===a.toString()?[o,c,e.decimals,t.decimals,e.address]:[c,o,t.decimals,e.decimals,t.address],W=new L(y.toString()).div(g(10,J)).div(new L(O.toString()).div(g(10,Z))),A=s.gte(y)?y.sub(new Y(1)):s,ge=y.sub(A),V=z(O.mul(A),ge),v=z(V.mul(new Y(1e6)),new Y(1e6).sub(r)),he=v.sub(V),be=new L(A.toString()).div(g(10,J)).div(new L(v.toString()).div(g(10,Z))),Te=W.isZero()?0:be.sub(W).div(W).abs().toNumber();return{amountRealOut:A,amountIn:v,amountInWithoutFee:V,tradeFee:he,priceImpact:Te}}};export{fe as CurveCalculator,me as RoundDirection}; //# sourceMappingURL=calculator.mjs.map