@raydium-io/raydium-sdk-v2
Version:
An SDK for building applications on top of Raydium.
2 lines • 8.55 kB
JavaScript
var h=Object.defineProperty;var v=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var k=Object.prototype.hasOwnProperty;var E=(t,e)=>{for(var r in e)h(t,r,{get:e[r],enumerable:!0})},F=(t,e,r,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of R(e))!k.call(t,n)&&n!==r&&h(t,n,{get:()=>e[n],enumerable:!(i=v(e,n))||i.enumerable});return t};var x=t=>F(h({},"__esModule",{value:!0}),t);var G={};E(G,{MAX_BASE64_SIZE:()=>y,addComputeBudget:()=>O,checkLegacyTxSize:()=>_,checkV0TxSize:()=>H,confirmTransaction:()=>M,findProgramAddress:()=>z,forecastTransactionSize:()=>w,getEpochInfo:()=>$,getRecentBlockHash:()=>q,parseSimulateLogToJson:()=>K,parseSimulateValue:()=>U,printSimulate:()=>j,simulateMultipleInstruction:()=>D,simulateTransaction:()=>T,toBuffer:()=>S,transformTxToBase64:()=>N,txToBase64:()=>B});module.exports=x(G);var a=require("@solana/web3.js");var f=require("lodash");var C=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(i=>typeof i=="object"?JSON.stringify(i):i).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}},V={},W={};function P(t){let e=(0,f.get)(V,t);if(!e){let r=(0,f.get)(W,t);e=new C({name:t,logLevel:r}),(0,f.set)(V,t,e)}return e}var L={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 b=require("@solana/spl-token"),l=P("Raydium_txUtil"),y=1644;function O(t){let e=[],r=[];return t.microLamports&&(e.push(a.ComputeBudgetProgram.setComputeUnitPrice({microLamports:t.microLamports})),r.push(L.SetComputeUnitPrice)),t.units&&(e.push(a.ComputeBudgetProgram.setComputeUnitLimit({units:t.units})),r.push(L.SetComputeUnitLimit)),{instructions:e,instructionTypes:r}}async function q(t,e){var i,n;let r=e!=null?e:"confirmed";return(n=await((i=t.getLatestBlockhash)==null?void 0:i.call(t,{commitment:r})))==null?void 0:n.blockhash}async function M(t,e){return t.getSignatureStatuses([e]),new Promise((r,i)=>{let n=setTimeout(i,6e4);t.onSignature(e,m=>{if(clearTimeout(n),!m.err){r("");return}i(Object.assign(m.err,{txId:e}))},"confirmed")})}function w(t,e){t.length<1&&l.logWithError(`no instructions provided: ${t.toString()}`),e.length<1&&l.logWithError(`no signers provided:, ${e.toString()}`);let r=new a.Transaction;r.recentBlockhash="11111111111111111111111111111111",r.feePayer=e[0],r.add(...t);try{return Buffer.from(r.serialize({verifySignatures:!1})).toString("base64").length<y}catch{return!1}}async function D(t,e,r,i=!0){let n=new a.PublicKey("RaydiumSimuLateTransaction11111111111111111"),m=[],s=new a.Transaction;s.feePayer=n;for(let o of e)w([...s.instructions,o],[n])||(m.push(s),s=new a.Transaction,s.feePayer=n),s.add(o);s.instructions.length>0&&m.push(s);let g=[];try{if(g=await T(t,m,i),g.find(o=>o.err!==null))throw Error("rpc simulateTransaction error")}catch(o){o instanceof Error&&l.logWithError("failed to simulate for instructions","RPC_ERROR",{message:o.message})}let c=[];for(let o of g)if(l.debug("simulate result:",o),o.logs){let u=o.logs.filter(p=>p&&p.includes(r));l.debug("filteredLog:",c),u.length||l.logWithError("simulate log not match keyword","keyword",r),c.push(...u)}return c}function K(t,e){let r=t.match(/{["\w:,]+}/g);return!r||r.length!==1?l.logWithError(`simulate log fail to match json, keyword: ${e}`):r[0]}function U(t,e){let i=new RegExp(`"${e}":(\\d+)`,"g").exec(t);return!i||i.length!==2?l.logWithError(`simulate log fail to match key", key: ${e}`):i[1]}function z(t,e){let[r,i]=a.PublicKey.findProgramAddressSync(t,e);return{publicKey:r,nonce:i}}async function T(t,e,r){let i=[];if(r){let n=await t.getLatestBlockhash(),m=[];for(let o of e){o.recentBlockhash=n.blockhash,o.lastValidBlockHeight=n.lastValidBlockHeight;let p=o._compile().serialize(),I=o._serialize(p).toString("base64");m.push(I)}let s=m.map(o=>{let u=t._buildArgs([o],void 0,"base64");return{methodName:"simulateTransaction",args:u}}),g=[],c=20;for(let o=0;o<Math.ceil(s.length/c);o++)g.push(s.slice(o*c,(o+1)*c));i=await(await Promise.all(g.map(async o=>(await t._rpcBatchRequest(o)).map(u=>u.result.value)))).flat()}else try{i=await Promise.all(e.map(async n=>await(await t.simulateTransaction(n)).value))}catch(n){n instanceof Error&&l.logWithError("failed to get info for multiple accounts","RPC_ERROR",{message:n.message})}return i}function _({instructions:t,payer:e,signers:r}){return w(t,[e,...r])}function H({instructions:t,payer:e,lookupTableAddressAccount:r,recentBlockhash:i=a.Keypair.generate().publicKey.toString()}){let m=new a.TransactionMessage({payerKey:e,recentBlockhash:i,instructions:t}).compileToV0Message(Object.values(r!=null?r:{}));try{return Buffer.from(new a.VersionedTransaction(m).serialize()).toString("base64").length<y}catch{return!1}}var d={time:0,data:void 0};async function $(t){if(!d.data||(Date.now()-d.time)/1e3>30){let e=await t.getEpochInfo();return d={time:Date.now(),data:e},e}else return d.data}var S=t=>Buffer.isBuffer(t)?t:t instanceof Uint8Array?Buffer.from(t.buffer,t.byteOffset,t.byteLength):Buffer.from(t),B=t=>{let e=t.serialize({requireAllSignatures:!1,verifySignatures:!1});t instanceof a.VersionedTransaction&&(e=S(e));try{return e instanceof Buffer?e.toString("base64"):Buffer.from(e).toString("base64")}catch{return e.toString("base64")}};function j(t){let e=[];return t.forEach(r=>{r instanceof a.Transaction&&(r.recentBlockhash||(r.recentBlockhash=b.TOKEN_PROGRAM_ID.toBase58()),r.feePayer||(r.feePayer=a.Keypair.generate().publicKey)),e.push(B(r))}),console.log("simulate tx string:",e),e}function N(t){let e=t.serialize({requireAllSignatures:!1,verifySignatures:!1});return t instanceof a.VersionedTransaction&&(e=S(e)),e.toString("base64")}0&&(module.exports={MAX_BASE64_SIZE,addComputeBudget,checkLegacyTxSize,checkV0TxSize,confirmTransaction,findProgramAddress,forecastTransactionSize,getEpochInfo,getRecentBlockHash,parseSimulateLogToJson,parseSimulateValue,printSimulate,simulateMultipleInstruction,simulateTransaction,toBuffer,transformTxToBase64,txToBase64});
//# sourceMappingURL=txUtils.js.map