UNPKG

@sega-so/sega-sdk

Version:

An SDK for building applications on top of SEGA.

2 lines 27 kB
"use strict";var At=Object.create;var Z=Object.defineProperty,It=Object.defineProperties,Lt=Object.getOwnPropertyDescriptor,Ct=Object.getOwnPropertyDescriptors,Kt=Object.getOwnPropertyNames,Ve=Object.getOwnPropertySymbols,Nt=Object.getPrototypeOf,Ue=Object.prototype.hasOwnProperty,_t=Object.prototype.propertyIsEnumerable;var v=Math.pow,ve=(n,e,t)=>e in n?Z(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,W=(n,e)=>{for(var t in e||(e={}))Ue.call(e,t)&&ve(n,t,e[t]);if(Ve)for(var t of Ve(e))_t.call(e,t)&&ve(n,t,e[t]);return n},U=(n,e)=>It(n,Ct(e));var Mt=(n,e)=>{for(var t in e)Z(n,t,{get:e[t],enumerable:!0})},qe=(n,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of Kt(e))!Ue.call(n,o)&&o!==t&&Z(n,o,{get:()=>e[o],enumerable:!(r=Lt(e,o))||r.enumerable});return n};var x=(n,e,t)=>(t=n!=null?At(Nt(n)):{},qe(e||!n||!n.__esModule?Z(t,"default",{value:n,enumerable:!0}):t,n)),Rt=n=>qe(Z({},"__esModule",{value:!0}),n);var E=(n,e,t)=>new Promise((r,o)=>{var u=l=>{try{a(t.next(l))}catch(c){o(c)}},s=l=>{try{a(t.throw(l))}catch(c){o(c)}},a=l=>l.done?r(l.value):Promise.resolve(l.value).then(u,s);a((t=t.apply(n,e)).next())});var Sn={};Mt(Sn,{ConstantProductCurve:()=>re,CpmmConfigInfoLayout:()=>Dt,CpmmFee:()=>ie,CpmmPoolInfoLayout:()=>Ot,CurveCalculator:()=>Oe,FEE_RATE_DENOMINATOR_VALUE:()=>ye,LOCK_LIQUIDITY_SEED:()=>xt,RoundDirection:()=>We,ceilDiv:()=>St,collectCpFeeInstruction:()=>xn,cpmmLockPositionInstruction:()=>wt,floorDiv:()=>Ee,getCpLockPda:()=>Re,getCpmmPdaAmmConfigId:()=>mn,getCpmmPdaPoolId:()=>ge,getCreatePoolKeys:()=>fn,getPdaLpMint:()=>Pt,getPdaObservationId:()=>Tt,getPdaPoolAuthority:()=>ht,getPdaVault:()=>Me,makeCpmmLockInstruction:()=>Tn,makeCreateCpmmPoolInInstruction:()=>gn,makeDepositCpmmInInstruction:()=>bn,makeSwapCpmmBaseInInstruction:()=>hn,makeSwapCpmmBaseOutInstruction:()=>Pn,makeWithdrawCpmmInInstruction:()=>yn});module.exports=Rt(Sn);var Ze=require("@solana/web3.js"),G=x(require("bn.js"));var i=require("@solana/buffer-layout"),Pe=i.Layout,Ge=i.Structure;var ze=i.UInt;var je=i.u8,He=i.u16;var Ye=i.seq;var q=i.blob;var Te=class extends Pe{constructor(e,t,r){super(e,r),this.blob=q(e),this.signed=t}decode(e,t=0){let r=new G.default(this.blob.decode(e,t),10,"le");return this.signed?r.fromTwos(this.span*8).clone():r}encode(e,t,r=0){return typeof e=="number"&&(e=new G.default(e)),this.signed&&(e=e.toTwos(this.span*8)),this.blob.encode(e.toArrayLike(Buffer,"le",this.span),t,r)}};function D(n){return new ze(1,n)}function p(n){return new Te(8,!1,n)}var se=class extends Pe{constructor(e,t,r,o){super(e.span,o),this.layout=e,this.decoder=t,this.encoder=r}decode(e,t){return this.decoder(this.layout.decode(e,t))}encode(e,t,r){return this.layout.encode(this.encoder(e),t,r)}getSpan(e,t){return this.layout.getSpan(e,t)}};function A(n){return new se(q(32),e=>new Ze.PublicKey(e),e=>e.toBuffer(),n)}function ae(n){return new se(je(),Wt,Et,n)}function Wt(n){if(n===0)return!1;if(n===1)return!0;throw new Error("Invalid bool: "+n)}function Et(n){return n?1:0}var xe=class extends Ge{decode(e,t){return super.decode(e,t)}};function L(n,e,t){return new xe(n,e,t)}function we(n,e,t){let r,o=typeof e=="number"?e:(0,G.isBN)(e)?e.toNumber():new Proxy(e,{get(u,s){if(!r){let a=Reflect.get(u,"count");r=(0,G.isBN)(a)?a.toNumber():a,Reflect.set(u,"count",r)}return Reflect.get(u,s)},set(u,s,a){return s==="count"&&(r=a),Reflect.set(u,s,a)}});return Ye(n,o,t)}var Dt=L([q(8),D("bump"),ae("disableCreatePool"),He("index"),p("tradeFeeRate"),p("protocolFeeRate"),p("fundFeeRate"),p("createPoolFee"),A("protocolOwner"),A("fundOwner"),we(p(),16)]),Ot=L([q(8),A("configId"),A("poolCreator"),A("vaultA"),A("vaultB"),A("mintLp"),A("mintA"),A("mintB"),A("mintProgramA"),A("mintProgramB"),A("observationId"),D("bump"),D("status"),D("lpDecimals"),D("mintDecimalA"),D("mintDecimalB"),p("lpAmount"),p("protocolFeesMintA"),p("protocolFeesMintB"),p("fundFeesMintA"),p("fundFeesMintB"),p("openTime"),we(p(),32)]);var w=require("@solana/web3.js"),S=require("@solana/spl-token");var Vt=require("@solana/web3.js");var J=require("lodash");var Se=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}},Je={},Ft={};function B(n){let e=(0,J.get)(Je,n);if(!e){let t=(0,J.get)(Ft,n);e=new Se({name:n,logLevel:t}),(0,J.set)(Je,n,e)}return e}var ue=require("@solana/spl-token");var zn=B("Sega_accountInfo_util");var K=x(require("bn.js")),Jt=x(require("decimal.js"));var tt=x(require("big.js")),zt=x(require("bn.js"));var Xe=x(require("toformat")),vt=Xe.default,X=vt;var $=x(require("big.js")),Qe=x(require("bn.js")),et=x(require("decimal.js-light"));var z=x(require("bn.js"));var $e=9007199254740991;function _(n){let e=B("Sega_parseBigNumberish");if(n instanceof z.default)return n;if(typeof n=="string"){if(n.match(/^-?[0-9]+$/))return new z.default(n);e.logWithError(`invalid BigNumberish string: ${n}`)}return typeof n=="number"?(n%1&&e.logWithError(`BigNumberish number underflow: ${n}`),(n>=$e||n<=-$e)&&e.logWithError(`BigNumberish number overflow: ${n}`),new z.default(String(n))):typeof n=="bigint"?new z.default(n.toString()):(e.error(`invalid BigNumberish value: ${n}`),new z.default(0))}var ce=B("module/fraction"),Be=X($.default),Q=X(et.default),qt={0:Q.ROUND_DOWN,1:Q.ROUND_HALF_UP,2:Q.ROUND_UP},Gt={0:$.default.roundDown,1:$.default.roundHalfUp,2:$.default.roundUp},M=class n{constructor(e,t=new Qe.default(1)){this.numerator=_(e),this.denominator=_(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(_(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(_(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(_(e));return new n(this.numerator.mul(t.numerator),this.denominator.mul(t.denominator))}div(e){let t=e instanceof n?e:new n(_(e));return new n(this.numerator.mul(t.denominator),this.denominator.mul(t.numerator))}toSignificant(e,t={groupSeparator:""},r=1){Number.isInteger(e)||ce.logWithError(`${e} is not an integer.`),e<=0&&ce.logWithError(`${e} is not positive.`),Q.set({precision:e+1,rounding:qt[r]});let o=new Q(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)||ce.logWithError(`${e} is not an integer.`),e<0&&ce.logWithError(`${e} is negative.`),Be.DP=e,Be.RM=Gt[r]||1,new Be(this.numerator.toString()).div(this.denominator.toString()).toFormat(e,t)}isZero(){return this.numerator.isZero()}};var pr=B("Sega_amount"),fr=X(tt.default);var nt=require("@solana/web3.js"),Ae=require("@solana/spl-token"),rt={chainId:101,address:nt.PublicKey.default.toBase58(),programId:Ae.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"}},O={chainId:101,address:"So11111111111111111111111111111111111111112",programId:Ae.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 le=require("@solana/web3.js");var m=require("@solana/web3.js"),it=require("@solana/spl-token");function Ie({pubkey:n,isSigner:e=!1,isWritable:t=!0}){return{pubkey:n,isWritable:t,isSigner:e}}var yr=[Ie({pubkey:it.TOKEN_PROGRAM_ID,isWritable:!1}),Ie({pubkey:m.SystemProgram.programId,isWritable:!1}),Ie({pubkey:m.SYSVAR_RENT_PUBKEY,isWritable:!1})];function ot({publicKey:n,transformSol:e}){let t=st(n.toString());if(t instanceof m.PublicKey)return e&&t.equals(ee)?ke:t;if(e&&t.toString()===ee.toBase58())return ke;if(typeof t=="string"){if(t===m.PublicKey.default.toBase58())return m.PublicKey.default;try{return new m.PublicKey(t)}catch(r){throw new Error("invalid public key")}}throw new Error("invalid public key")}function st(n){try{return new m.PublicKey(n)}catch(e){return n}}var hr=new m.PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),Le=new m.PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),Ce=new m.PublicKey("SysvarRent111111111111111111111111111111111"),Pr=new m.PublicKey("SysvarC1ock11111111111111111111111111111111"),te=new m.PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"),Tr=new m.PublicKey("Sysvar1nstructions1111111111111111111111111"),at=m.SystemProgram.programId,xr=new m.PublicKey("4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R"),wr=new m.PublicKey("Ea5SjE2Y6yvCeW5dYTn7PYMuW5ikXkvbGdcmSnXeaLjS"),Sr=new m.PublicKey("SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt"),kr=new m.PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"),Br=new m.PublicKey("Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB"),Ar=new m.PublicKey("mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So"),Ir=new m.PublicKey("7dHbWXmci3dT8UFYWYZweBLXgycu7Y3iL6trKn1Y7ARj"),Lr=new m.PublicKey("USDH1SM1ojwWUga67PGrgFWUHibbjqMvuMaDkRJTgkX"),Cr=new m.PublicKey("NRVwhjBQiUPYtfDT5zRBVJajzFQHaBUNtC7SNVvqRFa"),Kr=new m.PublicKey("ANAxByE6G2WjFp7A4NqtWYXb3mgruyzZYg3spfxe6Lbo"),Nr=new m.PublicKey("7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs"),ke=new m.PublicKey("So11111111111111111111111111111111111111112"),ee=m.PublicKey.default;var me=class me{constructor({mint:e,decimals:t,symbol:r,name:o,skipMint:u=!1,isToken2022:s=!1}){if(e===ee.toBase58()||e instanceof le.PublicKey&&ee.equals(e)){this.decimals=O.decimals,this.symbol=O.symbol,this.name=O.name,this.mint=new le.PublicKey(O.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=u?le.PublicKey.default:ot({publicKey:e}),this.isToken2022=s}equals(e){return this===e?!0:this.mint.equals(e.mint)}};me.WSOL=new me(U(W({},O),{mint:O.address}));var ne=me;var pe=class pe{constructor({decimals:e,symbol:t="UNKNOWN",name:r="UNKNOWN"}){this.decimals=e,this.symbol=t,this.name=r}equals(e){return this===e}};pe.SOL=new pe(rt);var Ke=pe;var ut=x(require("bn.js"));var Ur=new M(new ut.default(100));var Yr=B("Sega_price");var Yt=require("@solana/web3.js"),Zt=x(require("bn.js"));var Ht=new K.default(0),Ai=new K.default(1),Ii=new K.default(2),Li=new K.default(3),Ci=new K.default(5),jt=new K.default(10),Ki=new K.default(100),Ni=new K.default(1e3),_i=new K.default(1e4);var pt=require("@solana/web3.js"),ft=require("@solana/spl-token");var F=require("@solana/web3.js");var fe={CreateAccount:"CreateAccount",InitAccount:"InitAccount",CreateATA:"CreateATA",CloseAccount:"CloseAccount",TransferAmount:"TransferAmount",InitMint:"InitMint",MintTo:"MintTo",InitMarket:"InitMarket",Util1216OwnerClaim:"Util1216OwnerClaim",SetComputeUnitPrice:"SetComputeUnitPrice",SetComputeUnitLimit:"SetComputeUnitLimit",ClmmCreatePool:"ClmmCreatePool",ClmmOpenPosition:"ClmmOpenPosition",ClmmIncreasePosition:"ClmmIncreasePosition",ClmmDecreasePosition:"ClmmDecreasePosition",ClmmClosePosition:"ClmmClosePosition",ClmmSwapBaseIn:"ClmmSwapBaseIn",ClmmSwapBaseOut:"ClmmSwapBaseOut",ClmmInitReward:"ClmmInitReward",ClmmSetReward:"ClmmSetReward",ClmmCollectReward:"ClmmCollectReward",ClmmLockPosition:"ClmmLockPosition",ClmmHarvestLockPosition:"ClmmHarvestLockPosition",AmmV4Swap:"AmmV4Swap",AmmV4AddLiquidity:"AmmV4AddLiquidity",AmmV4RemoveLiquidity:"AmmV4RemoveLiquidity",AmmV4SimulatePoolInfo:"AmmV4SimulatePoolInfo",AmmV4SwapBaseIn:"AmmV4SwapBaseIn",AmmV4SwapBaseOut:"AmmV4SwapBaseOut",AmmV4CreatePool:"AmmV4CreatePool",AmmV4InitPool:"AmmV4InitPool",AmmV5AddLiquidity:"AmmV5AddLiquidity",AmmV5RemoveLiquidity:"AmmV5RemoveLiquidity",AmmV5SimulatePoolInfo:"AmmV5SimulatePoolInfo",AmmV5SwapBaseIn:"AmmV5SwapBaseIn",AmmV5SwapBaseOut:"AmmV5SwapBaseOut",RouteSwap:"RouteSwap",RouteSwap1:"RouteSwap1",RouteSwap2:"RouteSwap2",FarmV3Deposit:"FarmV3Deposit",FarmV3Withdraw:"FarmV3Withdraw",FarmV3CreateLedger:"FarmV3CreateLedger",FarmV4Withdraw:"FarmV4Withdraw",FarmV5Deposit:"FarmV5Deposit",FarmV5Withdraw:"FarmV5Withdraw",FarmV5CreateLedger:"FarmV5CreateLedger",FarmV6Deposit:"FarmV6Deposit",FarmV6Withdraw:"FarmV6Withdraw",FarmV6Create:"FarmV6Create",FarmV6Restart:"FarmV6Restart",FarmV6CreatorAddReward:"FarmV6CreatorAddReward",FarmV6CreatorWithdraw:"FarmV6CreatorWithdraw",CpmmCreatePool:"CpmmCreatePool",CpmmAddLiquidity:"CpmmAddLiquidity",CpmmWithdrawLiquidity:"CpmmWithdrawLiquidity",CpmmSwapBaseIn:"CpmmSwapBaseIn",CpmmSwapBaseOut:"CpmmSwapBaseOut",CpmmLockLp:"CpmmLockLp",CpmmCollectLockFee:"CpmmCollectLockFee",TransferTip:"TransferTip"};var Xt=require("@solana/spl-token");var ji=B("Sega_txUtil");function C(n,e){let[t,r]=F.PublicKey.findProgramAddressSync(n,e);return{publicKey:t,nonce:r}}function de(n,e,t){return C([n.toBuffer(),(t!=null?t:ft.TOKEN_PROGRAM_ID).toBuffer(),e.toBuffer()],new pt.PublicKey("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"))}function dt(n){return C([Buffer.from("metadata","utf8"),te.toBuffer(),n.toBuffer()],te)}var I=require("@solana/web3.js"),gt=new I.PublicKey("SegazTQwbYWknDZkJ6j2Kgvm5gw3MrHGKtWstZdoNKZ"),bt=new I.PublicKey("Ajk8d9bERSaFdeoT1d8JUVfaDayrYLnAdgeB47TFMUaG"),$i=new I.PublicKey("2HbjxVVKJ7Ct72Rcd8WK4VTqmwTXL5aAggkH1CHGFGmh"),$t=new I.PublicKey("auetiVUVJkRxvNPLYEvVnz8UJSqQ1VgoA83PcicHVD1"),Qt=new I.PublicKey("GzqQwxPSmUZnTyCfX9YWqi32MJg43ujP786ZoacL559Y"),en=new I.PublicKey("GzqQwxPSmUZnTyCfX9YWqi32MJg43ujP786ZoacL559Y"),Qi=new I.PublicKey("SegaXNnoXYTZiqUt9Xn2XqGcL56b25yzXLuJSpadcMu"),eo=new I.PublicKey("2t1hV2YqnDCPuJZqr7Fy8oAygGwZE5Q9uaQzpLC1QryJ"),to=new I.PublicKey("BLxynZeHE123MmQgNJZiV1pwW4VYh46oZYyC66xwmtTY");var tn={SERUM_MARKET:I.PublicKey.default,UTIL1216:I.PublicKey.default,CREATE_CPMM_POOL_PROGRAM:$t,CREATE_CPMM_POOL_AUTH:Qt,CREATE_CPMM_POOL_FEE_ACC:en};var Ne=x(require("bn.js"));function _e(n,e){let{div:t,mod:r}=n.divmod(e);return r.gt(new Ne.default(0))?t.add(new Ne.default(1)):t}var yt=require("@solana/web3.js");var j=require("@solana/web3.js"),nn=x(require("axios"));var rn=require("@solana/web3.js");var on=Buffer.from("vault_and_lp_mint_auth_seed","utf8"),sn=Buffer.from("amm_config","utf8"),an=Buffer.from("pool","utf8"),un=Buffer.from("pool_lp_mint","utf8"),cn=Buffer.from("pool_vault","utf8"),ln=Buffer.from("observation","utf8");function ht(n){return C([on],n)}function mn(n,e){return C([sn,pn(e)],n)}function ge(n,e,t,r){return C([an,e.toBuffer(),t.toBuffer(),r.toBuffer()],n)}function Pt(n,e){return C([un,e.toBuffer()],n)}function Me(n,e,t){return C([cn,e.toBuffer(),t.toBuffer()],n)}function Tt(n,e){return C([ln,e.toBuffer()],n)}function pn(n){let e=new ArrayBuffer(2);return new DataView(e).setUint16(0,n,!1),new Uint8Array(e)}function fn({poolId:n,programId:e,configId:t,mintA:r,mintB:o}){let u=ht(e).publicKey,s=n||ge(e,t,r,o).publicKey,a=Pt(e,s).publicKey,l=Me(e,s,r).publicKey,c=Me(e,s,o).publicKey,b=Tt(e,s).publicKey;return{poolId:s,configId:t,authority:u,lpMint:a,vaultA:l,vaultB:c,observationId:b}}var xt=Buffer.from("locked_liquidity","utf8");function Re(n,e){return C([xt,e.toBuffer()],n)}var dn=B("Sega_cpmm"),V={initialize:[175,175,109,31,13,152,155,237],deposit:[242,35,198,137,82,225,242,182],withdraw:[183,18,70,156,148,109,161,34],swapBaseInput:[143,190,90,218,196,30,51,222],swapBaseOutput:[55,217,98,86,163,74,180,173],lockCpLiquidity:[216,157,29,78,38,51,31,26],collectCpFee:[8,30,51,199,209,184,247,133]};function gn(n,e,t,r,o,u,s,a,l,c,b,P,T,y,d,h,f,g,k,N){let R=L([p("amountMaxA"),p("amountMaxB"),p("openTime")]),kt=ge(n,t,u,s).publicKey,Bt=[{pubkey:e,isSigner:!0,isWritable:!1},{pubkey:t,isSigner:!1,isWritable:!1},{pubkey:r,isSigner:!1,isWritable:!1},{pubkey:o,isSigner:!o.equals(kt),isWritable:!0},{pubkey:u,isSigner:!1,isWritable:!1},{pubkey:s,isSigner:!1,isWritable:!1},{pubkey:a,isSigner:!1,isWritable:!0},{pubkey:l,isSigner:!1,isWritable:!0},{pubkey:c,isSigner:!1,isWritable:!0},{pubkey:b,isSigner:!1,isWritable:!0},{pubkey:P,isSigner:!1,isWritable:!0},{pubkey:T,isSigner:!1,isWritable:!0},{pubkey:y,isSigner:!1,isWritable:!0},{pubkey:f,isSigner:!1,isWritable:!0},{pubkey:S.TOKEN_PROGRAM_ID,isSigner:!1,isWritable:!1},{pubkey:d,isSigner:!1,isWritable:!1},{pubkey:h,isSigner:!1,isWritable:!1},{pubkey:S.ASSOCIATED_TOKEN_PROGRAM_ID,isSigner:!1,isWritable:!1},{pubkey:at,isSigner:!1,isWritable:!1},{pubkey:Ce,isSigner:!1,isWritable:!1}],Fe=Buffer.alloc(R.span);return R.encode({amountMaxA:g,amountMaxB:k,openTime:N},Fe),new w.TransactionInstruction({keys:Bt,programId:n,data:Buffer.from([...V.initialize,...Fe])})}function bn(n,e,t,r,o,u,s,a,l,c,b,P,T,y,d){let h=L([p("lpAmount"),p("amountMaxA"),p("amountMaxB")]),f=[{pubkey:e,isSigner:!0,isWritable:!1},{pubkey:t,isSigner:!1,isWritable:!1},{pubkey:r,isSigner:!1,isWritable:!0},{pubkey:o,isSigner:!1,isWritable:!0},{pubkey:u,isSigner:!1,isWritable:!0},{pubkey:s,isSigner:!1,isWritable:!0},{pubkey:a,isSigner:!1,isWritable:!0},{pubkey:l,isSigner:!1,isWritable:!0},{pubkey:S.TOKEN_PROGRAM_ID,isSigner:!1,isWritable:!1},{pubkey:S.TOKEN_2022_PROGRAM_ID,isSigner:!1,isWritable:!1},{pubkey:c,isSigner:!1,isWritable:!1},{pubkey:b,isSigner:!1,isWritable:!1},{pubkey:P,isSigner:!1,isWritable:!0}],g=Buffer.alloc(h.span);return dn.debug("cpmm deposit data",{lpAmount:T.toString(),amountMaxA:y.toString(),amountMaxB:d.toString()}),h.encode({lpAmount:T,amountMaxA:y,amountMaxB:d},g),new w.TransactionInstruction({keys:f,programId:n,data:Buffer.from([...V.deposit,...g])})}function yn(n,e,t,r,o,u,s,a,l,c,b,P,T,y,d){let h=L([p("lpAmount"),p("amountMinA"),p("amountMinB")]),f=[{pubkey:e,isSigner:!0,isWritable:!1},{pubkey:t,isSigner:!1,isWritable:!1},{pubkey:r,isSigner:!1,isWritable:!0},{pubkey:o,isSigner:!1,isWritable:!0},{pubkey:u,isSigner:!1,isWritable:!0},{pubkey:s,isSigner:!1,isWritable:!0},{pubkey:a,isSigner:!1,isWritable:!0},{pubkey:l,isSigner:!1,isWritable:!0},{pubkey:S.TOKEN_PROGRAM_ID,isSigner:!1,isWritable:!1},{pubkey:S.TOKEN_2022_PROGRAM_ID,isSigner:!1,isWritable:!1},{pubkey:c,isSigner:!1,isWritable:!1},{pubkey:b,isSigner:!1,isWritable:!1},{pubkey:P,isSigner:!1,isWritable:!0},{pubkey:Le,isSigner:!1,isWritable:!1}],g=Buffer.alloc(h.span);return h.encode({lpAmount:T,amountMinA:y,amountMinB:d},g),new w.TransactionInstruction({keys:f,programId:n,data:Buffer.from([...V.withdraw,...g])})}function hn(n,e,t,r,o,u,s,a,l,c,b,P,T,y,d,h){let f=L([p("amountIn"),p("amounOutMin")]),g=[{pubkey:e,isSigner:!0,isWritable:!1},{pubkey:t,isSigner:!1,isWritable:!1},{pubkey:r,isSigner:!1,isWritable:!1},{pubkey:o,isSigner:!1,isWritable:!0},{pubkey:u,isSigner:!1,isWritable:!0},{pubkey:s,isSigner:!1,isWritable:!0},{pubkey:a,isSigner:!1,isWritable:!0},{pubkey:l,isSigner:!1,isWritable:!0},{pubkey:c,isSigner:!1,isWritable:!1},{pubkey:b,isSigner:!1,isWritable:!1},{pubkey:P,isSigner:!1,isWritable:!1},{pubkey:T,isSigner:!1,isWritable:!1},{pubkey:y,isSigner:!1,isWritable:!0}],k=Buffer.alloc(f.span);return f.encode({amountIn:d,amounOutMin:h},k),new w.TransactionInstruction({keys:g,programId:n,data:Buffer.from([...V.swapBaseInput,...k])})}function Pn(n,e,t,r,o,u,s,a,l,c,b,P,T,y,d,h){let f=L([p("amountInMax"),p("amountOut")]),g=[{pubkey:e,isSigner:!0,isWritable:!1},{pubkey:t,isSigner:!1,isWritable:!1},{pubkey:r,isSigner:!1,isWritable:!1},{pubkey:o,isSigner:!1,isWritable:!0},{pubkey:u,isSigner:!1,isWritable:!0},{pubkey:s,isSigner:!1,isWritable:!0},{pubkey:a,isSigner:!1,isWritable:!0},{pubkey:l,isSigner:!1,isWritable:!0},{pubkey:c,isSigner:!1,isWritable:!1},{pubkey:b,isSigner:!1,isWritable:!1},{pubkey:P,isSigner:!1,isWritable:!1},{pubkey:T,isSigner:!1,isWritable:!1},{pubkey:y,isSigner:!1,isWritable:!0}],k=Buffer.alloc(f.span);return f.encode({amountInMax:d,amountOut:h},k),new w.TransactionInstruction({keys:g,programId:n,data:Buffer.from([...V.swapBaseOutput,...k])})}function Tn(n){return E(this,null,function*(){var f;let{ownerInfo:e,poolInfo:t,poolKeys:r,feeNftOwner:o,getEphemeralSigners:u}=n,s=[],[a,l]=[new w.PublicKey(t.id),new w.PublicKey(t.lpMint.address)],c;if(u)c=new w.PublicKey((yield u(1))[0]);else{let g=w.Keypair.generate();s.push(g),c=g.publicKey}let{publicKey:b}=de(o,c,S.TOKEN_PROGRAM_ID),{publicKey:P}=dt(c),{publicKey:T}=Re(n.lockProgram,c),{publicKey:y}=de(e.wallet,l,S.TOKEN_PROGRAM_ID),{publicKey:d}=de(n.lockAuthProgram,l,S.TOKEN_PROGRAM_ID),h=wt({programId:n.lockProgram,auth:n.lockAuthProgram,payer:e.feePayer,liquidityOwner:e.wallet,nftOwner:o,nftMint:c,nftAccount:b,poolId:a,lockPda:T,mintLp:l,userLpVault:y,lockLpVault:d,poolVaultA:new w.PublicKey(r.vault.A),poolVaultB:new w.PublicKey(r.vault.B),metadataAccount:P,lpAmount:n.lpAmount,withMetadata:(f=n.withMetadata)!=null?f:!0});return{address:{nftMint:c,nftAccount:b,metadataAccount:P,lockPda:T,userLpVault:y,lockLpVault:d},instructions:[h],signers:s,instructionTypes:[fe.CpmmLockLp],lookupTableAddress:[]}})}function wt({programId:n,auth:e,payer:t,liquidityOwner:r,nftOwner:o,nftMint:u,nftAccount:s,poolId:a,lockPda:l,mintLp:c,userLpVault:b,lockLpVault:P,poolVaultA:T,poolVaultB:y,metadataAccount:d,lpAmount:h,withMetadata:f}){let g=[{pubkey:e,isSigner:!1,isWritable:!1},{pubkey:t,isSigner:!0,isWritable:!0},{pubkey:r,isSigner:!0,isWritable:!1},{pubkey:o,isSigner:!1,isWritable:!1},{pubkey:u,isSigner:!0,isWritable:!0},{pubkey:s,isSigner:!1,isWritable:!0},{pubkey:a,isSigner:!1,isWritable:!1},{pubkey:l,isSigner:!1,isWritable:!0},{pubkey:c,isSigner:!1,isWritable:!1},{pubkey:b,isSigner:!1,isWritable:!0},{pubkey:P,isSigner:!1,isWritable:!0},{pubkey:T,isSigner:!1,isWritable:!0},{pubkey:y,isSigner:!1,isWritable:!0},{pubkey:d,isSigner:!1,isWritable:!0},{pubkey:Ce,isSigner:!1,isWritable:!1},{pubkey:w.SystemProgram.programId,isSigner:!1,isWritable:!1},{pubkey:S.TOKEN_PROGRAM_ID,isSigner:!1,isWritable:!1},{pubkey:S.ASSOCIATED_TOKEN_PROGRAM_ID,isSigner:!1,isWritable:!1},{pubkey:te,isSigner:!1,isWritable:!1}],k=L([p("lpAmount"),ae("withMetadata")]),N=Buffer.alloc(k.span);k.encode({lpAmount:h,withMetadata:f},N);let R=Buffer.from([...V.lockCpLiquidity,...N]);return new w.TransactionInstruction({keys:g,programId:n,data:R})}function xn({programId:n,nftOwner:e,auth:t,nftAccount:r,lockPda:o,poolId:u,mintLp:s,userVaultA:a,userVaultB:l,poolVaultA:c,poolVaultB:b,mintA:P,mintB:T,lockLpVault:y,lpFeeAmount:d,cpmmProgram:h,cpmmAuthProgram:f}){let g=[{pubkey:t,isSigner:!1,isWritable:!1},{pubkey:e,isSigner:!0,isWritable:!1},{pubkey:r,isSigner:!1,isWritable:!0},{pubkey:o,isSigner:!1,isWritable:!0},{pubkey:h!=null?h:gt,isSigner:!1,isWritable:!1},{pubkey:f!=null?f:bt,isSigner:!1,isWritable:!1},{pubkey:u,isSigner:!1,isWritable:!0},{pubkey:s,isSigner:!1,isWritable:!0},{pubkey:a,isSigner:!1,isWritable:!0},{pubkey:l,isSigner:!1,isWritable:!0},{pubkey:c,isSigner:!1,isWritable:!0},{pubkey:b,isSigner:!1,isWritable:!0},{pubkey:P,isSigner:!1,isWritable:!1},{pubkey:T,isSigner:!1,isWritable:!1},{pubkey:y,isSigner:!1,isWritable:!0},{pubkey:S.TOKEN_PROGRAM_ID,isSigner:!1,isWritable:!1},{pubkey:S.TOKEN_2022_PROGRAM_ID,isSigner:!1,isWritable:!1},{pubkey:Le,isSigner:!1,isWritable:!1}],k=L([p("lpFeeAmount")]),N=Buffer.alloc(k.span);k.encode({lpFeeAmount:d},N);let R=Buffer.from([...V.collectCpFee,...N]);return new w.TransactionInstruction({keys:g,programId:n,data:R})}var he=x(require("bn.js")),oe=x(require("decimal.js-light"));var Y=x(require("bn.js"));function be(n,e){if(e.isZero())throw Error("divisor is zero");return n.mod(e)}function wn(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=be(n,e);return r.gt(H)&&(t=t.add(new Y.default(1)),e=n.div(t),r=be(n,t),r.gt(H)&&(e=e.add(new Y.default(1)))),[t,e]}var H=new Y.default(0),re=class{static swapWithoutFees(e,t,r){let o=t.mul(r),u=t.add(e),[s]=wn(o,u),a=r.sub(s);if(a.isZero())throw Error("destinationAmountSwapped is zero");return{destinationAmountSwapped:a}}static lpTokensToTradingTokens(e,t,r,o,u){let s=e.mul(r).div(t),a=e.mul(o).div(t);if(u===0)return{tokenAmount0:s,tokenAmount1:a};if(u===1)return be(e.mul(r),t).gt(H)&&s.gt(H)&&(s=s.add(new Y.default(1))),be(e.mul(o),t).gt(H)&&a.gt(H)&&(a=a.add(new Y.default(1))),{tokenAmount0:s,tokenAmount1:a};throw Error("roundDirection value error")}};var De=x(require("bn.js")),ye=new De.default(1e6);function St(n,e,t){return n.mul(e).add(t).sub(new De.default(1)).div(t)}function Ee(n,e,t){return n.mul(e).div(t)}var ie=class{static tradingFee(e,t){return St(e,t,ye)}static protocolFee(e,t){return Ee(e,t,ye)}static fundFee(e,t){return Ee(e,t,ye)}};var We=(t=>(t[t.Floor=0]="Floor",t[t.Ceiling=1]="Ceiling",t))(We||{}),Oe=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 u=ie.tradingFee(e,o),s=e.sub(u),{destinationAmountSwapped:a}=re.swapWithoutFees(s,t,r);return{newSwapDestinationAmount:r.sub(a),sourceAmountSwapped:e,destinationAmountSwapped:a,tradeFee:u}}static swapBaseOut({poolMintA:e,poolMintB:t,tradeFeeRate:r,baseReserve:o,quoteReserve:u,outputMint:s,outputAmount:a}){let[l,c,b,P,T]=t.address===s.toString()?[o,u,e.decimals,t.decimals,e.address]:[u,o,t.decimals,e.decimals,t.address],y=new oe.default(c.toString()).div(v(10,P)).div(new oe.default(l.toString()).div(v(10,b))),d=a.gte(c)?c.sub(new he.default(1)):a,h=c.sub(d),f=_e(l.mul(d),h),g=_e(f.mul(new he.default(1e6)),new he.default(1e6).sub(r)),k=g.sub(f),N=new oe.default(d.toString()).div(v(10,P)).div(new oe.default(g.toString()).div(v(10,b))),R=y.isZero()?0:N.sub(y).div(y).abs().toNumber();return{amountRealOut:d,amountIn:g,amountInWithoutFee:f,tradeFee:k,priceImpact:R}}};0&&(module.exports={ConstantProductCurve,CpmmConfigInfoLayout,CpmmFee,CpmmPoolInfoLayout,CurveCalculator,FEE_RATE_DENOMINATOR_VALUE,LOCK_LIQUIDITY_SEED,RoundDirection,ceilDiv,collectCpFeeInstruction,cpmmLockPositionInstruction,floorDiv,getCpLockPda,getCpmmPdaAmmConfigId,getCpmmPdaPoolId,getCreatePoolKeys,getPdaLpMint,getPdaObservationId,getPdaPoolAuthority,getPdaVault,makeCpmmLockInstruction,makeCreateCpmmPoolInInstruction,makeDepositCpmmInInstruction,makeSwapCpmmBaseInInstruction,makeSwapCpmmBaseOutInstruction,makeWithdrawCpmmInInstruction}); //# sourceMappingURL=index.js.map