UNPKG

@raydium-io/raydium-sdk-v2

Version:

An SDK for building applications on top of Raydium.

2 lines 7.34 kB
import{PublicKey as b,ComputeBudgetProgram as V,Transaction as p,TransactionMessage as E,Keypair as B,VersionedTransaction as C}from"@solana/web3.js";import{TOKEN_PROGRAM_ID as k}from"@solana/spl-token";import{get as y,set as v}from"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 r=e.map(n=>typeof n=="object"?JSON.stringify(n):n).join(", ");throw new Error(r)}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}},w={},x={};function A(t){let e=y(w,t);if(!e){let r=y(x,t);e=new h({name:t,logLevel:r}),v(w,t,e)}return e}var d={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",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",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"};var s=A("Raydium_txUtil"),P=1644;function J(t){let e=[],r=[];return t.microLamports&&(e.push(V.setComputeUnitPrice({microLamports:t.microLamports})),r.push(d.SetComputeUnitPrice)),t.units&&(e.push(V.setComputeUnitLimit({units:t.units})),r.push(d.SetComputeUnitLimit)),{instructions:e,instructionTypes:r}}async function X(t,e){var n,o;let r=e!=null?e:"confirmed";try{return((o=await((n=t.getLatestBlockhash)==null?void 0:n.call(t,{commitment:r})))==null?void 0:o.blockhash)||(await t.getRecentBlockhash(r)).blockhash}catch{return(await t.getRecentBlockhash(r)).blockhash}}function I(t,e){t.length<1&&s.logWithError(`no instructions provided: ${t.toString()}`),e.length<1&&s.logWithError(`no signers provided:, ${e.toString()}`);let r=new p;r.recentBlockhash="11111111111111111111111111111111",r.feePayer=e[0],r.add(...t);try{return Buffer.from(r.serialize({verifySignatures:!1})).toString("base64").length<P}catch{return!1}}async function Y(t,e,r,n=!0){let o=new b("RaydiumSimuLateTransaction11111111111111111"),m=[],a=new p;a.feePayer=o;for(let i of e)I([...a.instructions,i],[o])||(m.push(a),a=new p,a.feePayer=o),a.add(i);a.instructions.length>0&&m.push(a);let c=[];try{if(c=await F(t,m,n),c.find(i=>i.err!==null))throw Error("rpc simulateTransaction error")}catch(i){i instanceof Error&&s.logWithError("failed to simulate for instructions","RPC_ERROR",{message:i.message})}let l=[];for(let i of c)if(s.debug("simulate result:",i),i.logs){let u=i.logs.filter(g=>g&&g.includes(r));s.debug("filteredLog:",l),u.length||s.logWithError("simulate log not match keyword","keyword",r),l.push(...u)}return l}function Z(t,e){let r=t.match(/{["\w:,]+}/g);return!r||r.length!==1?s.logWithError(`simulate log fail to match json, keyword: ${e}`):r[0]}function Q(t,e){let n=new RegExp(`"${e}":(\\d+)`,"g").exec(t);return!n||n.length!==2?s.logWithError(`simulate log fail to match key", key: ${e}`):n[1]}function ee(t,e){let[r,n]=b.findProgramAddressSync(t,e);return{publicKey:r,nonce:n}}async function F(t,e,r){let n=[];if(r){let o=await t.getLatestBlockhash(),m=[];for(let i of e){i.recentBlockhash=o.blockhash,i.lastValidBlockHeight=o.lastValidBlockHeight;let g=i._compile().serialize(),R=i._serialize(g).toString("base64");m.push(R)}let a=m.map(i=>{let u=t._buildArgs([i],void 0,"base64");return{methodName:"simulateTransaction",args:u}}),c=[],l=20;for(let i=0;i<Math.ceil(a.length/l);i++)c.push(a.slice(i*l,(i+1)*l));n=await(await Promise.all(c.map(async i=>(await t._rpcBatchRequest(i)).map(u=>u.result.value)))).flat()}else try{n=await Promise.all(e.map(async o=>await(await t.simulateTransaction(o)).value))}catch(o){o instanceof Error&&s.logWithError("failed to get info for multiple accounts","RPC_ERROR",{message:o.message})}return n}function te({instructions:t,payer:e,signers:r}){return I(t,[e,...r])}function re({instructions:t,payer:e,lookupTableAddressAccount:r,recentBlockhash:n=B.generate().publicKey.toString()}){let m=new E({payerKey:e,recentBlockhash:n,instructions:t}).compileToV0Message(Object.values(r!=null?r:{}));try{return Buffer.from(new C(m).serialize()).toString("base64").length<P}catch{return!1}}var f={time:0,data:void 0};async function ie(t){if(!f.data||(Date.now()-f.time)/1e3>30){let e=await t.getEpochInfo();return f={time:Date.now(),data:e},e}else return f.data}var T=t=>Buffer.isBuffer(t)?t:t instanceof Uint8Array?Buffer.from(t.buffer,t.byteOffset,t.byteLength):Buffer.from(t),W=t=>{let e=t.serialize({requireAllSignatures:!1,verifySignatures:!1});t instanceof C&&(e=T(e));try{return e instanceof Buffer?e.toString("base64"):Buffer.from(e).toString("base64")}catch{return e.toString("base64")}};function ne(t){let e=[];return t.forEach(r=>{r instanceof p&&(r.recentBlockhash||(r.recentBlockhash=k.toBase58()),r.feePayer||(r.feePayer=B.generate().publicKey)),e.push(W(r))}),console.log("simulate tx string:",e),e}function oe(t){let e=t.serialize({requireAllSignatures:!1,verifySignatures:!1});return t instanceof C&&(e=T(e)),e.toString("base64")}export{P as MAX_BASE64_SIZE,J as addComputeBudget,te as checkLegacyTxSize,re as checkV0TxSize,ee as findProgramAddress,I as forecastTransactionSize,ie as getEpochInfo,X as getRecentBlockHash,Z as parseSimulateLogToJson,Q as parseSimulateValue,ne as printSimulate,Y as simulateMultipleInstruction,F as simulateTransaction,T as toBuffer,oe as transformTxToBase64,W as txToBase64}; //# sourceMappingURL=txUtils.mjs.map