buy()
Purchase tokens on a supported platform. Specify the token address, the amount of native currency to spend, and an optional slippage tolerance. This method requires a configured wallet with a private key.
Signature
const skill = getSkill(skills, "pumpfun");
const result: TradeResult = await skill.buy({
tokenAddress: "TokenMintAddress...",
amount: "0.1",
slippage: 10,
}); Parameters
| Parameter | Type | Description |
|---|---|---|
| tokenAddress * | string | The address of the token to buy. |
| amount * | string | Amount of native currency to spend (e.g. SOL, ETH, BNB, TRX). |
| slippage | number | Slippage tolerance as a percentage (e.g. 10 for 10%). Platform default if omitted. |
Return Value
Returns a Promise<TradeResult>.
See the Types reference for the full
TradeResult definition.
Platform Support
| Platform | Supported |
|---|---|
| Pump.fun | Yes |
| LetsBonk | Yes |
| Moonshot | Yes |
| Zora | Yes |
| Clanker | Yes |
| Four.meme | Yes |
| SunPump | Yes |
Example
buy-token.ts
import { createSkills, getSkill } from "dex-skills";
const skills = createSkills({
wallets: {
solana: {
privateKey: "your-base58-private-key",
rpcUrl: "https://api.mainnet-beta.solana.com",
},
},
});
// Buy tokens on Pump.fun
const pumpfun = getSkill(skills, "pumpfun");
const result = await pumpfun.buy({
tokenAddress: "TokenMintAddress...",
amount: "0.1",
slippage: 10,
});
console.log("Platform:", result.platform);
console.log("Tx Hash:", result.txHash);
console.log("Action:", result.action);
console.log("Token Amount:", result.tokenAmount);
console.log("Native Amount:", result.nativeAmount); Private key required
The buy() method submits an on-chain transaction and requires a wallet with a private key configured
for the target platform's chain.
Slippage tolerance
If no slippage value is provided, the platform's default slippage tolerance is used. High-volatility
tokens may require a higher slippage value to avoid failed transactions.