Skip to content

Instantly share code, notes, and snippets.

@amilz
Last active January 10, 2025 17:12
Show Gist options
  • Save amilz/e1a31d04ffd9316517ca908bd455326c to your computer and use it in GitHub Desktop.
Save amilz/e1a31d04ffd9316517ca908bd455326c to your computer and use it in GitHub Desktop.
QuickNode Stream - Raydium Swap Parse
/**
* Major modifications from V1:
* 1. Changed architecture to class-based
* 2. Added support for multiple mints
* 3. Added native SOL support
* 4. Handled 0 pre-balance token accounts
* 5. Handled optional accounts for Raydium's swap instructions
*/
class AmmInstructionProcessor {
constructor(idl) {
this.idl = idl;
this.targetInstructions = ["swapBaseIn", "swapBaseOut"];
this.targetAccounts = ["userSourceTokenAccount", "userDestinationTokenAccount", "userSourceOwner"];
}
processInstruction(ix, decodedData, preBalances, postBalances, preSolBalances, postSolBalances, accountKeys) {
const idlInstruction = this.idl.instructions[decodedData[0]];
if (!idlInstruction?.name || !this.targetInstructions.includes(idlInstruction.name)) {
return null;
}
const idlIxLength = idlInstruction.accounts.length;
const ixLength = ix.accounts.length;
let txAccountIndex = 0;
const accounts = idlInstruction.accounts
.map(account => {
// This will skip all IDL accounts listed as optional
// if the current instruction has more accounts than the IDL's account list
// This requires manual addition of optional accounts to the IDL
// Seems fine for Raydium's swap instructions, but
// this means multiple optional accounts in an instruction could cause issues
// instruction-specific may require additional modifications
if (account.optional && idlIxLength > ixLength) {
return null;
}
if (!this.targetAccounts.includes(account.name)) {
txAccountIndex++;
return null;
}
return {
accountName: account.name,
accountAddress: ix.accounts[txAccountIndex++]
};
})
.filter(Boolean);
const ownerAccount = accounts.find(account =>
account.accountName === "userSourceOwner"
);
if (!ownerAccount) return null;
const solBalanceChange = this.getSolBalanceChange(ownerAccount, accountKeys, preSolBalances, postSolBalances);
const tokenBalanceChanges = this.getBalanceChanges(preBalances, postBalances, ownerAccount.accountAddress);
const ownerBalanceChanges = [...tokenBalanceChanges, solBalanceChange];
return { ownerBalanceChanges };
}
getSolBalanceChange(accountOwner, accountKeys, preBalances, postBalances) {
const LAMPORTS_PER_SOL = 1_000_000_000;
const accountIndex = accountKeys.findIndex(key => key.pubkey === accountOwner.accountAddress);
const preSolBalance = preBalances[accountIndex] / LAMPORTS_PER_SOL;
const postSolBalance = postBalances[accountIndex] / LAMPORTS_PER_SOL;
return {
mint: 'So11111111111111111111111111111111111111111', // Native
owner: accountOwner.accountAddress,
change: postSolBalance - preSolBalance,
preBalance: preSolBalance,
postBalance: postSolBalance
}
}
getBalanceChanges(preBalances, postBalances, ownerAddress) {
const filterByOwner = balances => balances.filter(balance => balance.owner === ownerAddress);
const filtered = {
pre: filterByOwner(preBalances),
post: filterByOwner(postBalances)
};
// Get all unique mints for this owner (across pre and post)
const allMints = new Set([
...filtered.pre.map(b => b.mint),
...filtered.post.map(b => b.mint)
]);
return Array.from(allMints).map(mint => {
const preMint = filtered.pre.find(b => b.mint === mint);
const postMint = filtered.post.find(b => b.mint === mint);
const preAmount = preMint?.uiTokenAmount.uiAmount ?? 0;
const postAmount = postMint?.uiTokenAmount.uiAmount ?? 0;
return {
mint,
owner: ownerAddress,
change: postAmount - preAmount,
preBalance: preAmount,
postBalance: postAmount
};
});
}
}
class TransactionValidator {
constructor(targetMints, voteProgram, solMint, solDeductionThreshold) {
this.targetMints = targetMints;
this.voteProgram = voteProgram;
this.solMint = solMint;
this.solDeductionThreshold = solDeductionThreshold;
}
isValid(tx, idl) {
return !this.hasError(tx)
&& !this.isVoteTransaction(tx)
&& this.invokesTargetProgram(tx, idl)
&& this.hasTargetMintActivity(tx);
}
hasError(tx) {
return tx.meta?.err != null;
}
isVoteTransaction(tx) {
return tx.transaction?.message?.instructions?.some(inst =>
inst.program === "vote" || inst.programId === this.voteProgram
);
}
invokesTargetProgram(tx, idl) {
const { meta: { logMessages = [], innerInstructions = [] }, transaction: { message: { instructions = [] } } } = tx;
return instructions.some(inst => inst.programId === idl.address)
|| innerInstructions.some(inst => inst.instructions.some(inner => inner.programId === idl.address))
|| logMessages.some(log => log.includes(`Program ${idl.address} invoke [1]`));
}
hasTargetMintActivity(tx) {
const { meta: { preTokenBalances = [], postTokenBalances = [] } } = tx;
const relevantBalances = [...preTokenBalances, ...postTokenBalances].filter(
balance => this.targetMints.includes(balance.mint)
);
return relevantBalances.length > 0;
}
validateSolDeduction(balanceChanges) {
const solChange = balanceChanges.find(change => change.mint === this.solMint);
if (!solChange || solChange.change >= 0) return false;
return Math.abs(solChange.change) >= this.solDeductionThreshold;
}
}
class StreamHandler {
constructor(config) {
this.validator = new TransactionValidator(
config.targetMints,
config.voteProgram,
config.solMint,
config.solDeductionThreshold
);
this.processor = new AmmInstructionProcessor(config.idl);
this.base58Decoder = config.base58Decoder;
}
processTransaction(tx) {
const { transaction, meta: {
preTokenBalances = [],
postTokenBalances = [],
preBalances = [],
postBalances = []
} } = tx;
const signature = transaction.signatures[0];
const { accountKeys } = transaction.message;
const instructions = transaction.message.instructions
.filter(ix => ix.programId === this.processor.idl.address)
.map(ix => {
try {
const decodedData = this.base58Decoder(ix.data);
return this.processor.processInstruction(ix, decodedData, preTokenBalances, postTokenBalances, preBalances, postBalances, accountKeys);
} catch (e) {
console.error('Instruction processing error:', e);
return null;
}
})
.filter(Boolean);
return instructions.length > 0 ? { signature, ...instructions[0] } : null;
}
handleStream(stream) {
try {
const data = stream[0];
if (!data?.transactions?.length) return null;
const matchedTransactions = data.transactions
.filter(tx => this.validator.isValid(tx, this.processor.idl))
.map(tx => this.processTransaction(tx))
.filter(Boolean);
return matchedTransactions.length > 0
? { numTx: matchedTransactions.length, transactions: matchedTransactions }
: null;
} catch (error) {
return { error: error.message, stack: error.stack };
}
}
}
const IDL = {
"version": "0.3.0",
"name": "raydium_amm",
"address": "675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8",
"instructions": [
{
"name": "initialize",
"accounts": [
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
},
{
"name": "systemProgram",
"isMut": false,
"isSigner": false
},
{
"name": "rent",
"isMut": false,
"isSigner": false
},
{
"name": "amm",
"isMut": true,
"isSigner": false
},
{
"name": "ammAuthority",
"isMut": false,
"isSigner": false
},
{
"name": "ammOpenOrders",
"isMut": true,
"isSigner": false
},
{
"name": "lpMintAddress",
"isMut": true,
"isSigner": false
},
{
"name": "coinMintAddress",
"isMut": false,
"isSigner": false
},
{
"name": "pcMintAddress",
"isMut": false,
"isSigner": false
},
{
"name": "poolCoinTokenAccount",
"isMut": false,
"isSigner": false
},
{
"name": "poolPcTokenAccount",
"isMut": false,
"isSigner": false
},
{
"name": "poolWithdrawQueue",
"isMut": true,
"isSigner": false
},
{
"name": "poolTargetOrdersAccount",
"isMut": true,
"isSigner": false
},
{
"name": "userLpTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "poolTempLpTokenAccount",
"isMut": false,
"isSigner": false
},
{
"name": "serumProgram",
"isMut": false,
"isSigner": false
},
{
"name": "serumMarket",
"isMut": false,
"isSigner": false
},
{
"name": "userWallet",
"isMut": true,
"isSigner": true
}
],
"args": [
{
"name": "nonce",
"type": "u8"
},
{
"name": "openTime",
"type": "u64"
}
]
},
{
"name": "initialize2",
"accounts": [
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
},
{
"name": "splAssociatedTokenAccount",
"isMut": false,
"isSigner": false
},
{
"name": "systemProgram",
"isMut": false,
"isSigner": false
},
{
"name": "rent",
"isMut": false,
"isSigner": false
},
{
"name": "amm",
"isMut": true,
"isSigner": false
},
{
"name": "ammAuthority",
"isMut": false,
"isSigner": false
},
{
"name": "ammOpenOrders",
"isMut": true,
"isSigner": false
},
{
"name": "lpMint",
"isMut": true,
"isSigner": false
},
{
"name": "coinMint",
"isMut": false,
"isSigner": false
},
{
"name": "pcMint",
"isMut": false,
"isSigner": false
},
{
"name": "poolCoinTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "poolPcTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "poolWithdrawQueue",
"isMut": true,
"isSigner": false
},
{
"name": "ammTargetOrders",
"isMut": true,
"isSigner": false
},
{
"name": "poolTempLp",
"isMut": true,
"isSigner": false
},
{
"name": "serumProgram",
"isMut": false,
"isSigner": false
},
{
"name": "serumMarket",
"isMut": false,
"isSigner": false
},
{
"name": "userWallet",
"isMut": true,
"isSigner": true
},
{
"name": "userTokenCoin",
"isMut": true,
"isSigner": false
},
{
"name": "userTokenPc",
"isMut": true,
"isSigner": false
},
{
"name": "userLpTokenAccount",
"isMut": true,
"isSigner": false
}
],
"args": [
{
"name": "nonce",
"type": "u8"
},
{
"name": "openTime",
"type": "u64"
},
{
"name": "initPcAmount",
"type": "u64"
},
{
"name": "initCoinAmount",
"type": "u64"
}
]
},
{
"name": "monitorStep",
"accounts": [
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
},
{
"name": "rent",
"isMut": false,
"isSigner": false
},
{
"name": "clock",
"isMut": false,
"isSigner": false
},
{
"name": "amm",
"isMut": true,
"isSigner": false
},
{
"name": "ammAuthority",
"isMut": false,
"isSigner": false
},
{
"name": "ammOpenOrders",
"isMut": true,
"isSigner": false
},
{
"name": "ammTargetOrders",
"isMut": true,
"isSigner": false
},
{
"name": "poolCoinTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "poolPcTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "poolWithdrawQueue",
"isMut": true,
"isSigner": false
},
{
"name": "serumProgram",
"isMut": false,
"isSigner": false
},
{
"name": "serumMarket",
"isMut": true,
"isSigner": false
},
{
"name": "serumCoinVaultAccount",
"isMut": true,
"isSigner": false
},
{
"name": "serumPcVaultAccount",
"isMut": true,
"isSigner": false
},
{
"name": "serumVaultSigner",
"isMut": false,
"isSigner": false
},
{
"name": "serumReqQ",
"isMut": true,
"isSigner": false
},
{
"name": "serumEventQ",
"isMut": true,
"isSigner": false
},
{
"name": "serumBids",
"isMut": true,
"isSigner": false
},
{
"name": "serumAsks",
"isMut": true,
"isSigner": false
}
],
"args": [
{
"name": "planOrderLimit",
"type": "u16"
},
{
"name": "placeOrderLimit",
"type": "u16"
},
{
"name": "cancelOrderLimit",
"type": "u16"
}
]
},
{
"name": "deposit",
"accounts": [
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
},
{
"name": "amm",
"isMut": true,
"isSigner": false
},
{
"name": "ammAuthority",
"isMut": false,
"isSigner": false
},
{
"name": "ammOpenOrders",
"isMut": false,
"isSigner": false
},
{
"name": "ammTargetOrders",
"isMut": true,
"isSigner": false
},
{
"name": "lpMintAddress",
"isMut": true,
"isSigner": false
},
{
"name": "poolCoinTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "poolPcTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "serumMarket",
"isMut": false,
"isSigner": false
},
{
"name": "userCoinTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "userPcTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "userLpTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "userOwner",
"isMut": false,
"isSigner": true
},
{
"name": "serumEventQueue",
"isMut": false,
"isSigner": false
}
],
"args": [
{
"name": "maxCoinAmount",
"type": "u64"
},
{
"name": "maxPcAmount",
"type": "u64"
},
{
"name": "baseSide",
"type": "u64"
}
]
},
{
"name": "withdraw",
"accounts": [
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
},
{
"name": "amm",
"isMut": true,
"isSigner": false
},
{
"name": "ammAuthority",
"isMut": false,
"isSigner": false
},
{
"name": "ammOpenOrders",
"isMut": true,
"isSigner": false
},
{
"name": "ammTargetOrders",
"isMut": true,
"isSigner": false
},
{
"name": "lpMintAddress",
"isMut": true,
"isSigner": false
},
{
"name": "poolCoinTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "poolPcTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "poolWithdrawQueue",
"isMut": true,
"isSigner": false
},
{
"name": "poolTempLpTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "serumProgram",
"isMut": false,
"isSigner": false
},
{
"name": "serumMarket",
"isMut": true,
"isSigner": false
},
{
"name": "serumCoinVaultAccount",
"isMut": true,
"isSigner": false
},
{
"name": "serumPcVaultAccount",
"isMut": true,
"isSigner": false
},
{
"name": "serumVaultSigner",
"isMut": false,
"isSigner": false
},
{
"name": "userLpTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "uerCoinTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "uerPcTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "userOwner",
"isMut": false,
"isSigner": true
},
{
"name": "serumEventQ",
"isMut": true,
"isSigner": false
},
{
"name": "serumBids",
"isMut": true,
"isSigner": false
},
{
"name": "serumAsks",
"isMut": true,
"isSigner": false
}
],
"args": [
{
"name": "amount",
"type": "u64"
}
]
},
{
"name": "migrateToOpenBook",
"accounts": [
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
},
{
"name": "systemProgram",
"isMut": false,
"isSigner": false
},
{
"name": "rent",
"isMut": false,
"isSigner": false
},
{
"name": "amm",
"isMut": true,
"isSigner": false
},
{
"name": "ammAuthority",
"isMut": false,
"isSigner": false
},
{
"name": "ammOpenOrders",
"isMut": true,
"isSigner": false
},
{
"name": "ammTokenCoin",
"isMut": true,
"isSigner": false
},
{
"name": "ammTokenPc",
"isMut": true,
"isSigner": false
},
{
"name": "ammTargetOrders",
"isMut": true,
"isSigner": false
},
{
"name": "serumProgram",
"isMut": false,
"isSigner": false
},
{
"name": "serumMarket",
"isMut": true,
"isSigner": false
},
{
"name": "serumBids",
"isMut": true,
"isSigner": false
},
{
"name": "serumAsks",
"isMut": true,
"isSigner": false
},
{
"name": "serumEventQueue",
"isMut": true,
"isSigner": false
},
{
"name": "serumCoinVault",
"isMut": true,
"isSigner": false
},
{
"name": "serumPcVault",
"isMut": true,
"isSigner": false
},
{
"name": "serumVaultSigner",
"isMut": false,
"isSigner": false
},
{
"name": "newAmmOpenOrders",
"isMut": true,
"isSigner": false
},
{
"name": "newSerumProgram",
"isMut": false,
"isSigner": false
},
{
"name": "newSerumMarket",
"isMut": false,
"isSigner": false
},
{
"name": "admin",
"isMut": true,
"isSigner": true
}
],
"args": []
},
{
"name": "setParams",
"accounts": [
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
},
{
"name": "amm",
"isMut": true,
"isSigner": false
},
{
"name": "ammAuthority",
"isMut": false,
"isSigner": false
},
{
"name": "ammOpenOrders",
"isMut": true,
"isSigner": false
},
{
"name": "ammTargetOrders",
"isMut": true,
"isSigner": false
},
{
"name": "ammCoinVault",
"isMut": true,
"isSigner": false
},
{
"name": "ammPcVault",
"isMut": true,
"isSigner": false
},
{
"name": "serumProgram",
"isMut": false,
"isSigner": false
},
{
"name": "serumMarket",
"isMut": true,
"isSigner": false
},
{
"name": "serumCoinVault",
"isMut": true,
"isSigner": false
},
{
"name": "serumPcVault",
"isMut": true,
"isSigner": false
},
{
"name": "serumVaultSigner",
"isMut": false,
"isSigner": false
},
{
"name": "serumEventQueue",
"isMut": true,
"isSigner": false
},
{
"name": "serumBids",
"isMut": true,
"isSigner": false
},
{
"name": "serumAsks",
"isMut": true,
"isSigner": false
},
{
"name": "ammAdminAccount",
"isMut": false,
"isSigner": true
}
],
"args": [
{
"name": "param",
"type": "u8"
},
{
"name": "value",
"type": {
"option": "u64"
}
},
{
"name": "newPubkey",
"type": {
"option": "publicKey"
}
},
{
"name": "fees",
"type": {
"option": {
"defined": "Fees"
}
}
},
{
"name": "lastOrderDistance",
"type": {
"option": {
"defined": "LastOrderDistance"
}
}
},
{
"name": "needTakeAmounts",
"type": {
"option": {
"defined": "NeedTake"
}
}
}
]
},
{
"name": "withdrawPnl",
"accounts": [
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
},
{
"name": "amm",
"isMut": true,
"isSigner": false
},
{
"name": "ammConfig",
"isMut": false,
"isSigner": false
},
{
"name": "ammAuthority",
"isMut": false,
"isSigner": false
},
{
"name": "ammOpenOrders",
"isMut": true,
"isSigner": false
},
{
"name": "poolCoinTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "poolPcTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "coinPnlTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "pcPnlTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "pnlOwnerAccount",
"isMut": false,
"isSigner": true
},
{
"name": "ammTargetOrders",
"isMut": true,
"isSigner": false
},
{
"name": "serumProgram",
"isMut": false,
"isSigner": false
},
{
"name": "serumMarket",
"isMut": true,
"isSigner": false
},
{
"name": "serumEventQueue",
"isMut": false,
"isSigner": false
},
{
"name": "serumCoinVaultAccount",
"isMut": true,
"isSigner": false
},
{
"name": "serumPcVaultAccount",
"isMut": true,
"isSigner": false
},
{
"name": "serumVaultSigner",
"isMut": false,
"isSigner": false
}
],
"args": []
},
{
"name": "withdrawSrm",
"accounts": [
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
},
{
"name": "amm",
"isMut": false,
"isSigner": false
},
{
"name": "ammOwnerAccount",
"isMut": false,
"isSigner": true
},
{
"name": "ammAuthority",
"isMut": false,
"isSigner": false
},
{
"name": "srmToken",
"isMut": true,
"isSigner": false
},
{
"name": "destSrmToken",
"isMut": true,
"isSigner": false
}
],
"args": [
{
"name": "amount",
"type": "u64"
}
]
},
{
"name": "swapBaseIn",
"accounts": [
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
},
{
"name": "amm",
"isMut": true,
"isSigner": false
},
{
"name": "ammAuthority",
"isMut": false,
"isSigner": false
},
{
"name": "ammOpenOrders",
"isMut": true,
"isSigner": false
},
{
"name": "ammTargetOrders",
"isMut": true,
"isSigner": false,
"optional": true
},
{
"name": "poolCoinTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "poolPcTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "serumProgram",
"isMut": false,
"isSigner": false
},
{
"name": "serumMarket",
"isMut": true,
"isSigner": false
},
{
"name": "serumBids",
"isMut": true,
"isSigner": false
},
{
"name": "serumAsks",
"isMut": true,
"isSigner": false
},
{
"name": "serumEventQueue",
"isMut": true,
"isSigner": false
},
{
"name": "serumCoinVaultAccount",
"isMut": true,
"isSigner": false
},
{
"name": "serumPcVaultAccount",
"isMut": true,
"isSigner": false
},
{
"name": "serumVaultSigner",
"isMut": false,
"isSigner": false
},
{
"name": "userSourceTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "userDestinationTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "userSourceOwner",
"isMut": false,
"isSigner": true
}
],
"args": [
{
"name": "amountIn",
"type": "u64"
},
{
"name": "minimumAmountOut",
"type": "u64"
}
]
},
{
"name": "preInitialize",
"accounts": [
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
},
{
"name": "systemProgram",
"isMut": false,
"isSigner": false
},
{
"name": "rent",
"isMut": false,
"isSigner": false
},
{
"name": "ammTargetOrders",
"isMut": true,
"isSigner": false
},
{
"name": "poolWithdrawQueue",
"isMut": true,
"isSigner": false
},
{
"name": "ammAuthority",
"isMut": false,
"isSigner": false
},
{
"name": "lpMintAddress",
"isMut": true,
"isSigner": false
},
{
"name": "coinMintAddress",
"isMut": false,
"isSigner": false
},
{
"name": "pcMintAddress",
"isMut": false,
"isSigner": false
},
{
"name": "poolCoinTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "poolPcTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "poolTempLpTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "serumMarket",
"isMut": false,
"isSigner": false
},
{
"name": "userWallet",
"isMut": true,
"isSigner": true
}
],
"args": [
{
"name": "nonce",
"type": "u8"
}
]
},
{
"name": "swapBaseOut",
"accounts": [
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
},
{
"name": "amm",
"isMut": true,
"isSigner": false
},
{
"name": "ammAuthority",
"isMut": false,
"isSigner": false
},
{
"name": "ammOpenOrders",
"isMut": true,
"isSigner": false
},
// Optional (recommended not to use)
{
"name": "ammTargetOrders",
"isMut": true,
"isSigner": false,
"optional": true
},
{
"name": "poolCoinTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "poolPcTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "serumProgram",
"isMut": false,
"isSigner": false
},
{
"name": "serumMarket",
"isMut": true,
"isSigner": false
},
{
"name": "serumBids",
"isMut": true,
"isSigner": false
},
{
"name": "serumAsks",
"isMut": true,
"isSigner": false
},
{
"name": "serumEventQueue",
"isMut": true,
"isSigner": false
},
{
"name": "serumCoinVaultAccount",
"isMut": true,
"isSigner": false
},
{
"name": "serumPcVaultAccount",
"isMut": true,
"isSigner": false
},
{
"name": "serumVaultSigner",
"isMut": false,
"isSigner": false
},
{
"name": "userSourceTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "userDestinationTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "userSourceOwner",
"isMut": false,
"isSigner": true
}
],
"args": [
{
"name": "maxAmountIn",
"type": "u64"
},
{
"name": "amountOut",
"type": "u64"
}
]
},
{
"name": "simulateInfo",
"accounts": [
{
"name": "amm",
"isMut": false,
"isSigner": false
},
{
"name": "ammAuthority",
"isMut": false,
"isSigner": false
},
{
"name": "ammOpenOrders",
"isMut": false,
"isSigner": false
},
{
"name": "poolCoinTokenAccount",
"isMut": false,
"isSigner": false
},
{
"name": "poolPcTokenAccount",
"isMut": false,
"isSigner": false
},
{
"name": "lpMintAddress",
"isMut": false,
"isSigner": false
},
{
"name": "serumMarket",
"isMut": false,
"isSigner": false
},
{
"name": "serumEventQueue",
"isMut": false,
"isSigner": false
}
],
"args": [
{
"name": "param",
"type": "u8"
},
{
"name": "swapBaseInValue",
"type": {
"option": {
"defined": "SwapInstructionBaseIn"
}
}
},
{
"name": "swapBaseOutValue",
"type": {
"option": {
"defined": "SwapInstructionBaseOut"
}
}
}
]
},
{
"name": "adminCancelOrders",
"accounts": [
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
},
{
"name": "amm",
"isMut": false,
"isSigner": false
},
{
"name": "ammAuthority",
"isMut": false,
"isSigner": false
},
{
"name": "ammOpenOrders",
"isMut": true,
"isSigner": false
},
{
"name": "ammTargetOrders",
"isMut": true,
"isSigner": false
},
{
"name": "poolCoinTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "poolPcTokenAccount",
"isMut": true,
"isSigner": false
},
{
"name": "ammOwnerAccount",
"isMut": false,
"isSigner": true
},
{
"name": "ammConfig",
"isMut": true,
"isSigner": false
},
{
"name": "serumProgram",
"isMut": false,
"isSigner": false
},
{
"name": "serumMarket",
"isMut": true,
"isSigner": false
},
{
"name": "serumCoinVaultAccount",
"isMut": true,
"isSigner": false
},
{
"name": "serumPcVaultAccount",
"isMut": true,
"isSigner": false
},
{
"name": "serumVaultSigner",
"isMut": false,
"isSigner": false
},
{
"name": "serumEventQ",
"isMut": true,
"isSigner": false
},
{
"name": "serumBids",
"isMut": true,
"isSigner": false
},
{
"name": "serumAsks",
"isMut": true,
"isSigner": false
}
],
"args": [
{
"name": "limit",
"type": "u16"
}
]
},
{
"name": "createConfigAccount",
"accounts": [
{
"name": "admin",
"isMut": true,
"isSigner": true
},
{
"name": "ammConfig",
"isMut": true,
"isSigner": false
},
{
"name": "owner",
"isMut": false,
"isSigner": false
},
{
"name": "systemProgram",
"isMut": false,
"isSigner": false
},
{
"name": "rent",
"isMut": false,
"isSigner": false
}
],
"args": []
},
{
"name": "updateConfigAccount",
"accounts": [
{
"name": "admin",
"isMut": false,
"isSigner": true
},
{
"name": "ammConfig",
"isMut": true,
"isSigner": false
}
],
"args": [
{
"name": "param",
"type": "u8"
},
{
"name": "owner",
"type": "publicKey"
}
]
}
],
"accounts": [
{
"name": "TargetOrders",
"type": {
"kind": "struct",
"fields": [
{
"name": "owner",
"type": {
"array": [
"u64",
4
]
}
},
{
"name": "buyOrders",
"type": {
"array": [
{
"defined": "TargetOrder"
},
50
]
}
},
{
"name": "padding1",
"type": {
"array": [
"u64",
8
]
}
},
{
"name": "targetX",
"type": "u128"
},
{
"name": "targetY",
"type": "u128"
},
{
"name": "planXBuy",
"type": "u128"
},
{
"name": "planYBuy",
"type": "u128"
},
{
"name": "planXSell",
"type": "u128"
},
{
"name": "planYSell",
"type": "u128"
},
{
"name": "placedX",
"type": "u128"
},
{
"name": "placedY",
"type": "u128"
},
{
"name": "calcPnlX",
"type": "u128"
},
{
"name": "calcPnlY",
"type": "u128"
},
{
"name": "sellOrders",
"type": {
"array": [
{
"defined": "TargetOrder"
},
50
]
}
},
{
"name": "padding2",
"type": {
"array": [
"u64",
6
]
}
},
{
"name": "replaceBuyClientId",
"type": {
"array": [
"u64",
10
]
}
},
{
"name": "replaceSellClientId",
"type": {
"array": [
"u64",
10
]
}
},
{
"name": "lastOrderNumerator",
"type": "u64"
},
{
"name": "lastOrderDenominator",
"type": "u64"
},
{
"name": "planOrdersCur",
"type": "u64"
},
{
"name": "placeOrdersCur",
"type": "u64"
},
{
"name": "validBuyOrderNum",
"type": "u64"
},
{
"name": "validSellOrderNum",
"type": "u64"
},
{
"name": "padding3",
"type": {
"array": [
"u64",
10
]
}
},
{
"name": "freeSlotBits",
"type": "u128"
}
]
}
},
{
"name": "Fees",
"type": {
"kind": "struct",
"fields": [
{
"name": "minSeparateNumerator",
"type": "u64"
},
{
"name": "minSeparateDenominator",
"type": "u64"
},
{
"name": "tradeFeeNumerator",
"type": "u64"
},
{
"name": "tradeFeeDenominator",
"type": "u64"
},
{
"name": "pnlNumerator",
"type": "u64"
},
{
"name": "pnlDenominator",
"type": "u64"
},
{
"name": "swapFeeNumerator",
"type": "u64"
},
{
"name": "swapFeeDenominator",
"type": "u64"
}
]
}
},
{
"name": "AmmInfo",
"type": {
"kind": "struct",
"fields": [
{
"name": "status",
"type": "u64"
},
{
"name": "nonce",
"type": "u64"
},
{
"name": "orderNum",
"type": "u64"
},
{
"name": "depth",
"type": "u64"
},
{
"name": "coinDecimals",
"type": "u64"
},
{
"name": "pcDecimals",
"type": "u64"
},
{
"name": "state",
"type": "u64"
},
{
"name": "resetFlag",
"type": "u64"
},
{
"name": "minSize",
"type": "u64"
},
{
"name": "volMaxCutRatio",
"type": "u64"
},
{
"name": "amountWave",
"type": "u64"
},
{
"name": "coinLotSize",
"type": "u64"
},
{
"name": "pcLotSize",
"type": "u64"
},
{
"name": "minPriceMultiplier",
"type": "u64"
},
{
"name": "maxPriceMultiplier",
"type": "u64"
},
{
"name": "sysDecimalValue",
"type": "u64"
},
{
"name": "fees",
"type": {
"defined": "Fees"
}
},
{
"name": "outPut",
"type": {
"defined": "OutPutData"
}
},
{
"name": "tokenCoin",
"type": "publicKey"
},
{
"name": "tokenPc",
"type": "publicKey"
},
{
"name": "coinMint",
"type": "publicKey"
},
{
"name": "pcMint",
"type": "publicKey"
},
{
"name": "lpMint",
"type": "publicKey"
},
{
"name": "openOrders",
"type": "publicKey"
},
{
"name": "market",
"type": "publicKey"
},
{
"name": "serumDex",
"type": "publicKey"
},
{
"name": "targetOrders",
"type": "publicKey"
},
{
"name": "withdrawQueue",
"type": "publicKey"
},
{
"name": "tokenTempLp",
"type": "publicKey"
},
{
"name": "ammOwner",
"type": "publicKey"
},
{
"name": "lpAmount",
"type": "u64"
},
{
"name": "clientOrderId",
"type": "u64"
},
{
"name": "padding",
"type": {
"array": [
"u64",
2
]
}
}
]
}
}
],
"types": [
{
"name": "WithdrawDestToken",
"type": {
"kind": "struct",
"fields": [
{
"name": "withdrawAmount",
"type": "u64"
},
{
"name": "coinAmount",
"type": "u64"
},
{
"name": "pcAmount",
"type": "u64"
},
{
"name": "destTokenCoin",
"type": "publicKey"
},
{
"name": "destTokenPc",
"type": "publicKey"
}
]
}
},
{
"name": "WithdrawQueue",
"type": {
"kind": "struct",
"fields": [
{
"name": "owner",
"type": {
"array": [
"u64",
4
]
}
},
{
"name": "head",
"type": "u64"
},
{
"name": "count",
"type": "u64"
},
{
"name": "buf",
"type": {
"array": [
{
"defined": "WithdrawDestToken"
},
64
]
}
}
]
}
},
{
"name": "TargetOrder",
"type": {
"kind": "struct",
"fields": [
{
"name": "price",
"type": "u64"
},
{
"name": "vol",
"type": "u64"
}
]
}
},
{
"name": "OutPutData",
"type": {
"kind": "struct",
"fields": [
{
"name": "needTakePnlCoin",
"type": "u64"
},
{
"name": "needTakePnlPc",
"type": "u64"
},
{
"name": "totalPnlPc",
"type": "u64"
},
{
"name": "totalPnlCoin",
"type": "u64"
},
{
"name": "poolOpenTime",
"type": "u64"
},
{
"name": "punishPcAmount",
"type": "u64"
},
{
"name": "punishCoinAmount",
"type": "u64"
},
{
"name": "orderbookToInitTime",
"type": "u64"
},
{
"name": "swapCoinInAmount",
"type": "u128"
},
{
"name": "swapPcOutAmount",
"type": "u128"
},
{
"name": "swapTakePcFee",
"type": "u64"
},
{
"name": "swapPcInAmount",
"type": "u128"
},
{
"name": "swapCoinOutAmount",
"type": "u128"
},
{
"name": "swapTakeCoinFee",
"type": "u64"
}
]
}
},
{
"name": "AmmConfig",
"type": {
"kind": "struct",
"fields": [
{
"name": "pnlOwner",
"type": "publicKey"
},
{
"name": "cancelOwner",
"type": "publicKey"
},
{
"name": "pending1",
"type": {
"array": [
"u64",
28
]
}
},
{
"name": "pending2",
"type": {
"array": [
"u64",
32
]
}
}
]
}
},
{
"name": "LastOrderDistance",
"type": {
"kind": "struct",
"fields": [
{
"name": "lastOrderNumerator",
"type": "u64"
},
{
"name": "lastOrderDenominator",
"type": "u64"
}
]
}
},
{
"name": "NeedTake",
"type": {
"kind": "struct",
"fields": [
{
"name": "needTakePc",
"type": "u64"
},
{
"name": "needTakeCoin",
"type": "u64"
}
]
}
},
{
"name": "SwapInstructionBaseIn",
"type": {
"kind": "struct",
"fields": [
{
"name": "amountIn",
"type": "u64"
},
{
"name": "minimumAmountOut",
"type": "u64"
}
]
}
},
{
"name": "SwapInstructionBaseOut",
"type": {
"kind": "struct",
"fields": [
{
"name": "maxAmountIn",
"type": "u64"
},
{
"name": "amountOut",
"type": "u64"
}
]
}
}
],
"errors": [
{
"code": 0,
"name": "AlreadyInUse",
"msg": "AlreadyInUse"
},
{
"code": 1,
"name": "InvalidProgramAddress",
"msg": "InvalidProgramAddress"
},
{
"code": 2,
"name": "ExpectedMint",
"msg": "ExpectedMint"
},
{
"code": 3,
"name": "ExpectedAccount",
"msg": "ExpectedAccount"
},
{
"code": 4,
"name": "InvalidCoinVault",
"msg": "InvalidCoinVault"
},
{
"code": 5,
"name": "InvalidPCVault",
"msg": "InvalidPCVault"
},
{
"code": 6,
"name": "InvalidTokenLP",
"msg": "InvalidTokenLP"
},
{
"code": 7,
"name": "InvalidDestTokenCoin",
"msg": "InvalidDestTokenCoin"
},
{
"code": 8,
"name": "InvalidDestTokenPC",
"msg": "InvalidDestTokenPC"
},
{
"code": 9,
"name": "InvalidPoolMint",
"msg": "InvalidPoolMint"
},
{
"code": 10,
"name": "InvalidOpenOrders",
"msg": "InvalidOpenOrders"
},
{
"code": 11,
"name": "InvalidSerumMarket",
"msg": "InvalidSerumMarket"
},
{
"code": 12,
"name": "InvalidSerumProgram",
"msg": "InvalidSerumProgram"
},
{
"code": 13,
"name": "InvalidTargetOrders",
"msg": "InvalidTargetOrders"
},
{
"code": 14,
"name": "InvalidWithdrawQueue",
"msg": "InvalidWithdrawQueue"
},
{
"code": 15,
"name": "InvalidTempLp",
"msg": "InvalidTempLp"
},
{
"code": 16,
"name": "InvalidCoinMint",
"msg": "InvalidCoinMint"
},
{
"code": 17,
"name": "InvalidPCMint",
"msg": "InvalidPCMint"
},
{
"code": 18,
"name": "InvalidOwner",
"msg": "InvalidOwner"
},
{
"code": 19,
"name": "InvalidSupply",
"msg": "InvalidSupply"
},
{
"code": 20,
"name": "InvalidDelegate",
"msg": "InvalidDelegate"
},
{
"code": 21,
"name": "InvalidSignAccount",
"msg": "Invalid Sign Account"
},
{
"code": 22,
"name": "InvalidStatus",
"msg": "InvalidStatus"
},
{
"code": 23,
"name": "InvalidInstruction",
"msg": "Invalid instruction"
},
{
"code": 24,
"name": "WrongAccountsNumber",
"msg": "Wrong accounts number"
},
{
"code": 25,
"name": "WithdrawTransferBusy",
"msg": "Withdraw_transfer is busy"
},
{
"code": 26,
"name": "WithdrawQueueFull",
"msg": "WithdrawQueue is full"
},
{
"code": 27,
"name": "WithdrawQueueEmpty",
"msg": "WithdrawQueue is empty"
},
{
"code": 28,
"name": "InvalidParamsSet",
"msg": "Params Set is invalid"
},
{
"code": 29,
"name": "InvalidInput",
"msg": "InvalidInput"
},
{
"code": 30,
"name": "ExceededSlippage",
"msg": "instruction exceeds desired slippage limit"
},
{
"code": 31,
"name": "CalculationExRateFailure",
"msg": "CalculationExRateFailure"
},
{
"code": 32,
"name": "CheckedSubOverflow",
"msg": "Checked_Sub Overflow"
},
{
"code": 33,
"name": "CheckedAddOverflow",
"msg": "Checked_Add Overflow"
},
{
"code": 34,
"name": "CheckedMulOverflow",
"msg": "Checked_Mul Overflow"
},
{
"code": 35,
"name": "CheckedDivOverflow",
"msg": "Checked_Div Overflow"
},
{
"code": 36,
"name": "CheckedEmptyFunds",
"msg": "Empty Funds"
},
{
"code": 37,
"name": "CalcPnlError",
"msg": "Calc pnl error"
},
{
"code": 38,
"name": "InvalidSplTokenProgram",
"msg": "InvalidSplTokenProgram"
},
{
"code": 39,
"name": "TakePnlError",
"msg": "Take Pnl error"
},
{
"code": 40,
"name": "InsufficientFunds",
"msg": "Insufficient funds"
},
{
"code": 41,
"name": "ConversionFailure",
"msg": "Conversion to u64 failed with an overflow or underflow"
},
{
"code": 42,
"name": "InvalidUserToken",
"msg": "user token input does not match amm"
},
{
"code": 43,
"name": "InvalidSrmMint",
"msg": "InvalidSrmMint"
},
{
"code": 44,
"name": "InvalidSrmToken",
"msg": "InvalidSrmToken"
},
{
"code": 45,
"name": "TooManyOpenOrders",
"msg": "TooManyOpenOrders"
},
{
"code": 46,
"name": "OrderAtSlotIsPlaced",
"msg": "OrderAtSlotIsPlaced"
},
{
"code": 47,
"name": "InvalidSysProgramAddress",
"msg": "InvalidSysProgramAddress"
},
{
"code": 48,
"name": "InvalidFee",
"msg": "The provided fee does not match the program owner's constraints"
},
{
"code": 49,
"name": "RepeatCreateAmm",
"msg": "Repeat create amm about market"
},
{
"code": 50,
"name": "NotAllowZeroLP",
"msg": "Not allow Zero LP"
},
{
"code": 51,
"name": "InvalidCloseAuthority",
"msg": "Token account has a close authority"
},
{
"code": 52,
"name": "InvalidFreezeAuthority",
"msg": "Pool token mint has a freeze authority"
},
{
"code": 53,
"name": "InvalidReferPCMint",
"msg": "InvalidReferPCMint"
},
{
"code": 54,
"name": "InvalidConfigAccount",
"msg": "InvalidConfigAccount"
},
{
"code": 55,
"name": "RepeatCreateConfigAccount",
"msg": "Repeat create staking config account"
},
{
"code": 56,
"name": "UnknownAmmError",
"msg": "Unknown Amm Error"
}
]
}
const VOTE_PROGRAM_ID = "Vote111111111111111111111111111111111111111";
const SOL_MINT = "So11111111111111111111111111111111111111112";
const SOL_DEDUCTION_THRESHOLD = 1.0;
const TARGET_MINTS = [
'Df6yfrKC8kZE3KNkrHERKzAetSxbrWeniQfyJY4Jpump', //CHILLGUY
'2qEHjDLDLbuBgRYvsxhc5D6uDWAivNFZGan56P1tpump', //Pnut
'CzLSujWBLFsSjncfkh59rUFqvafWcY5tzedWJSuypump', //GOAT
'8x5VqbHA8D7NkD52uNuS5nnt3PwA8pLD34ymskeSo2Wn', //ZEREBRO
'MEW1gQWJ3nEXg2qgERiKu7FAFj79PHvQVREQUzScPP5', //MEW
'ukHH6c7mMyiWCf1b9pnWe25TSpkDDt3H5pQZgZ74J82', //BOME
'7atgF8KQo4wJrD5ATGX7t1V2zVvykPJbFfNeVf1icFv1', //catwifhat
'GJAFwWjJ3vnTsrQVabjBVK2TYB1YtRCQXRDfDgUnpump', //ACT
'ED5nyyWEzpPPiWimP8vYm7sD7TD3LAt3Q3gRTWHzPJBY', //MOODENG
'8Ki8DpuWNxu9VsS3kQbarsCWMcFGWkzzA8pUPto9zBd5', //LOCKIN
'2oGLxYuNBJRcepT1mEV6KnETaLD7Bf6qq3CM6skasBfe', //PUPS
'kinXdEcpDQeHPEuQnqmUgtYykqKGVFq6CeVX5iAHJq6', //KIN
'kinXdEcpDQeHPEuQnqmUgtYykqKGVFq6CeVX5iAHJq6', //dogwifhat
'CLoUDKc4Ane7HeQcPpE3YHnznRxhMimJ4MyaUqyHFzAu', //cloud
'ATLASXmbPQxBUYbxPsV97usA3fPQYEqzQBUHgiFCUsXx', //Star Atlas
'28tVhteKZkzzWjrdHGXzxfm4SQkhrDrjLur9TYCDVULE', //BUU
'9psiRdn9cXYVps4F1kFuoNjd2EtmqNJXrCPmRppJpump', //UBC
]
function decodeBase58(encoded) {
if (typeof encoded !== 'string') return [];
const result = [];
for (let i = 0; i < encoded.length; i++) {
let carry = BASE58_ALPHABET.indexOf(encoded[i]);
if (carry < 0) return []; // Invalid character, return empty array
for (let j = 0; j < result.length; j++) {
carry += result[j] * 58;
result[j] = carry & 0xff;
carry >>= 8;
}
while (carry > 0) {
result.push(carry & 0xff);
carry >>= 8;
}
}
// Add leading zeros
for (let i = 0; i < encoded.length && encoded[i] === '1'; i++) {
result.push(0);
}
return result.reverse();
}
const BASE58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
function main(stream) {
const config = {
targetMints: TARGET_MINTS,
voteProgram: VOTE_PROGRAM_ID,
solMint: SOL_MINT,
solDeductionThreshold: SOL_DEDUCTION_THRESHOLD,
idl: IDL,
base58Decoder: decodeBase58
};
const handler = new StreamHandler(config);
return handler.handleStream(stream);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment