moonshot-sdk
Example 1: Direct RPC call prepare and send transaction
Generate a keypair and send funds for the right environment (devnet or mainnet).
import { Moonshot, Environment } from '@wen-moon-ser/moonshot-sdk';
import {
ComputeBudgetProgram,
Connection,
Keypair,
TransactionMessage,
VersionedTransaction,
} from '@solana/web3.js';
import testWallet from '../test-wallet.json';
const main = async (): Promise<void> => {
const rpcUrl = 'https://api.devnet.solana.com';
const connection = new Connection(rpcUrl);
const moonshot = new Moonshot({
rpcUrl,
authToken: 'YOUR_AUTH_TOKEN',
environment: Environment.MAINNET,
});
const token = moonshot.Token({
mintAddress: '3Rai792zaN5adyc2oEFGg1JLV4S9SYi51HrtMw7qRz8o',
});
const curvePos = await token.getCurvePosition();
console.log('Current position of the curve: ', curvePos);
const creator = Keypair.fromSecretKey(Uint8Array.from(testWallet));
console.log('Creator: ', creator.publicKey.toBase58());
const tokenAmount = 100000n * 1000000000n;
const collateralAmount = await token.getCollateralAmountByTokens({
tokenAmount,
tradeDirection: 'BUY',
});
const { ixs } = await token.prepareIxs({
slippageBps: 100,
creatorPK: creator.publicKey.toBase58(),
tokenAmount,
collateralAmount,
tradeDirection: 'BUY',
});
const priorityIx = ComputeBudgetProgram.setComputeUnitPrice({
microLamports: 200_000,
});
const blockhash = await connection.getLatestBlockhash('confirmed');
const messageV0 = new TransactionMessage({
payerKey: creator.publicKey,
recentBlockhash: blockhash.blockhash,
instructions: [priorityIx, ...ixs],
}).compileToV0Message();
const transaction = new VersionedTransaction(messageV0);
transaction.sign([creator]);
const txHash = await connection.sendTransaction(transaction, {
skipPreflight: false,
maxRetries: 0,
preflightCommitment: 'confirmed',
});
console.log('Transaction hash:', txHash);
};