UNPKG

test-raydium-sdk-v2

Version:

An SDK for building applications on top of Raydium.

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