👨💻Developer Docs
Learn how to create buy/ sell transactions for PixFun. The simplest way to interact with out Solana Program is by using the Anchor IDL and Type.
Initializing the program
import { PixFun } from "@/contracts/pix_fun";
import idl from "@/contracts/pix_fun.json";
import { AnchorProvider, Program, Wallet } from "@coral-xyz/anchor";
import { bs58 } from "@coral-xyz/anchor/dist/cjs/utils/bytes";
import { Connection, Keypair } from "@solana/web3.js";
const connection = new Connection(process.env.RPC_URL!, "confirmed");
const wallet = new Wallet(Keypair.fromSecretKey(bs58.decode(process.env.PRIVATE_KEY!)));
const program = new Program(
idl as PixFun,
new AnchorProvider(connection, wallet),
);
Making swaps
You can create either a fixedIn
or fixedOut
type swap. Both types are available within the same instruction, passing values for your desired type and 0 for the other.
You should specify wether you want to buy or to sell with the
isBuy
boolean.For a
fixedIn
swap, you pass the input SOL (or token) amount asamountIn
and the slippage adjusted value asminAmountOut
. You leaveamountOut
andmaxAmountIn
as 0.For a
fixedOut
swap, you pass the desired SOL (or token) amount asamountOut
and the slippage adjusted value asmaxAmountIn
. You leaveamountIn
andminAmountOut
as 0. The on-chain program will calculate your input value.
You can consider this code for a simplified view:
const amountIn = swapType === "fixedIn" ? amount : 0;
const minAmountOut = swapType === "fixedIn" ? slippageAmount : 0;
const amountOut = swapType === "fixedOut" ? amount : 0;
const maxAmountIn = swapType === "fixedOut" ? slippageAmount : 0;
const ix = await program.methods
.swap(
tradeType === "buy",
amountIn,
minAmountOut,
amountOut,
maxAmountIn,
)
.accounts({ mint: tokenAddress, user, program: program.programId })
.instruction();
IMPORTANT: The program will take care of creating the Token Account, so you don't need to add a separate instruction for it.
Reading the bonding curve
const [poolAddress] = PublicKey.findProgramAddressSync(
[Buffer.from("pool"), new PublicKey(tokenAddress).toBuffer()],
new PublicKey(program.programId),
);
const poolState = await program.account.poolState.fetch(poolAddress);
// Account fields:
const {
virtualSolReserves,
virtualTokenReserves,
realTokenReserves,
realSolReserves,
tokenTotalSupply,
complete,
} = poolState;
Important: The bonding curve price, market cap and liquidity are calculated based on the virtual reserves above.
Swaping on Raydium
Once a token completes its bonding curve, it migrates to a Raydium CPMM pool, with its respective SDK. The PixFun token is set as Base and SOL is set as Quote.
Last updated