协议合约示例
V5 的 Ethers、Viem、Web3.js 和 Python Web3.py 示例,包括估算、买入、卖出、借款、borrowMore 和还款。
合约参考
V5 地址和可读 ABI 字符串数组。
公共 API 参考
代币列表、代币详情、配置、交易、K 线、投资组合和用户数据的端点概览。
Base URL
https://lfg.rich/api/bsc/tokens
{
"success": true,
"data": "...",
"total": 123,
"page": 1,
"limit": 20
}
success 为 false 时,请检查 error。
获取最新代币
const res = await fetch('https://lfg.rich/api/bsc/tokens?sort=latest&page=1&limit=20');
const json = await res.json();
if (!json.success) throw new Error(json.error || 'Failed to fetch latest tokens');
console.log(json.data);
获取最高成交量、活跃代币或涨幅榜
const sorts = ['volume', 'active', 'gainers', 'latest'];
for (const sort of sorts) {
const res = await fetch(`https://lfg.rich/api/bsc/tokens?sort=${sort}&page=1&limit=20`);
const json = await res.json();
console.log(sort, json.data);
}
搜索代币
const query = 'bull';
const res = await fetch(
`https://lfg.rich/api/bsc/tokens?search=${encodeURIComponent(query)}&page=1&limit=20`
);
const json = await res.json();
if (!json.success) throw new Error(json.error || 'Search failed');
console.log(json.data);
只获取已验证代币
const res = await fetch('https://lfg.rich/api/bsc/tokens?verified=true&page=1&limit=20');
const json = await res.json();
console.log(json.data);
is_verified。
获取代币详情
const tokenAddress = '0x29Ede1E1254419Fe5a3c803fC2b015042075A49A';
const res = await fetch(`https://lfg.rich/api/bsc/tokens/${tokenAddress}`);
const json = await res.json();
if (!json.success || !json.data) throw new Error('Token not found');
console.log(json.data.pool_id);
console.log(json.data.current_price);
console.log(json.data.floor_price);
pool_id 字段对 V5 很重要。Hook 读取和估算需要使用它。如果外部集成中没有该字段,可以用 hook.tokenToPoolId(tokenAddress) 解析。获取代币及其交易记录
const tokenAddress = '0x29Ede1E1254419Fe5a3c803fC2b015042075A49A';
const [tokenRes, tradesRes] = await Promise.all([
fetch(`https://lfg.rich/api/bsc/tokens/${tokenAddress}`),
fetch(`https://lfg.rich/api/bsc/tokens/${tokenAddress}/trades?page=1&limit=50`)
]);
const token = await tokenRes.json();
const trades = await tradesRes.json();
if (!token.success) throw new Error('Token request failed');
if (!trades.success) throw new Error('Trades request failed');
console.log(token.data);
console.log(trades.data);
buy
sell
borrow
borrowmore
repay
获取 K 线
const tokenAddress = '0x29Ede1E1254419Fe5a3c803fC2b015042075A49A';
const res = await fetch(
`https://lfg.rich/api/bsc/tokens/${tokenAddress}/candles?interval=5m&limit=100`
);
const json = await res.json();
if (!json.success) throw new Error(json.error || 'Failed to fetch candles');
console.log(json.data);
console.log(json.floorData);
1m
5m
1h
1d
交易后刷新代币数据
钱包操作后,应用会触发一次后端 sync,然后重新获取代币、交易记录和 K 线。async function refreshTokenAfterTrade(tokenAddress, interval = '5m') {
fetch(`https://lfg.rich/api/bsc/tokens/${tokenAddress}/sync`, {
method: 'POST'
}).catch(() => {});
const [tokenRes, tradesRes, candlesRes] = await Promise.all([
fetch(`https://lfg.rich/api/bsc/tokens/${tokenAddress}?_nocache=1`),
fetch(`https://lfg.rich/api/bsc/tokens/${tokenAddress}/trades?page=1&limit=50`),
fetch(`https://lfg.rich/api/bsc/tokens/${tokenAddress}/candles?interval=${interval}`)
]);
return {
token: await tokenRes.json(),
trades: await tradesRes.json(),
candles: await candlesRes.json()
};
}
获取协议合约配置
const res = await fetch('https://lfg.rich/api/bsc/tokens/config/contracts');
const json = await res.json();
if (!json.success) throw new Error(json.error || 'Failed to fetch contracts config');
console.log(json.data.factoryAddress);
console.log(json.data.hookAddress);
console.log(json.data.swapRouterAddress);
console.log(json.data.referralAddress);
console.log(json.data.treasuryAddress);
console.log(json.data.poolManagerAddress);
console.log(json.data.abiVersion);
console.log(json.data.factoryABI);
获取 JSON ABI 配置
const res = await fetch('https://lfg.rich/api/bsc/tokens/jsonconfig/contracts');
const json = await res.json();
if (!json.success) throw new Error(json.error || 'Failed to fetch JSON ABI config');
console.log(json.data.hookABI);
获取原生 BNB 价格
const res = await fetch('https://lfg.rich/api/bsc/tokens/config/native-price');
const json = await res.json();
if (!json.success) throw new Error(json.error || 'Failed to fetch native price');
console.log(json.data.nativeUsdPrice);
获取创建者代币
const creator = '0xYourWalletAddress';
const res = await fetch(`https://lfg.rich/api/bsc/tokens/creator/${creator}`);
const json = await res.json();
if (!json.success) throw new Error(json.error || 'Failed to fetch creator tokens');
console.log(json.data);
获取投资组合持仓
const wallet = '0xYourWalletAddress';
const res = await fetch(`https://lfg.rich/api/bsc/tokens/portfolio/${wallet}`);
const json = await res.json();
if (!json.success) throw new Error(json.error || 'Failed to fetch portfolio');
console.log(json.data);
获取投资组合借款仓位
const wallet = '0xYourWalletAddress';
const res = await fetch(`https://lfg.rich/api/bsc/tokens/portfolio/${wallet}/borrows`);
const json = await res.json();
if (!json.success) throw new Error(json.error || 'Failed to fetch borrow portfolio');
console.log(json.data);
获取用户的代币余额和 BNB 余额
const tokenAddress = '0x29Ede1E1254419Fe5a3c803fC2b015042075A49A';
const wallet = '0xYourWalletAddress';
const res = await fetch(`https://lfg.rich/api/bsc/tokens/${tokenAddress}/user-data/${wallet}`);
const json = await res.json();
if (!json.success) throw new Error(json.error || 'Failed to fetch user data');
console.log(json.data.tokenBalance);
console.log(json.data.ethBalance);
获取用户在某个代币上的借款数据
const tokenAddress = '0x29Ede1E1254419Fe5a3c803fC2b015042075A49A';
const wallet = '0xYourWalletAddress';
const tokenRes = await fetch(`https://lfg.rich/api/bsc/tokens/${tokenAddress}`);
const tokenJson = await tokenRes.json();
const poolId = tokenJson.data.pool_id || tokenJson.data.poolId;
const borrowRes = await fetch(
`https://lfg.rich/api/bsc/tokens/${tokenAddress}/borrow-data/${wallet}?poolId=${poolId}`
);
const borrowJson = await borrowRes.json();
if (!borrowJson.success) throw new Error(borrowJson.error || 'Failed to fetch borrow data');
console.log(borrowJson.data.borrowedETH);
console.log(borrowJson.data.collateralBalance);
console.log(borrowJson.data.borrowMoreEstimate);
获取某个钱包的代币角色
const tokenAddress = '0x29Ede1E1254419Fe5a3c803fC2b015042075A49A';
const wallet = '0xYourWalletAddress';
const res = await fetch(`https://lfg.rich/api/bsc/tokens/${tokenAddress}/roles/${wallet}`);
const json = await res.json();
if (!json.success) throw new Error(json.error || 'Failed to fetch roles');
console.log(json.data.isCreator);
console.log(json.data.isFactoryOwner);
console.log(json.data.factoryBalance);
获取 owner 数据
const wallet = '0xOwnerWalletAddress';
const res = await fetch(`https://lfg.rich/api/bsc/tokens/config/owner-data/${wallet}`);
const json = await res.json();
if (!json.success) throw new Error(json.error || 'Failed to fetch owner data');
console.log(json.data.isOwner);
console.log(json.data.factoryBalance);
console.log(json.data.treasuryBalance);
console.log(json.data.treasuryRecipient);
API + 合约示例:使用 API 代币数据报价
这个示例适合机器人和仪表盘:先用 API 发现代币,再用合约读取精确报价数据。import { ethers } from 'ethers';
async function quoteTokenBuy(tokenAddress, bnbAmount, walletAddress) {
const [cfgJson, tokenJson] = await Promise.all([
fetch('https://lfg.rich/api/bsc/tokens/config/contracts').then(r => r.json()),
fetch(`https://lfg.rich/api/bsc/tokens/${tokenAddress}`).then(r => r.json())
]);
const cfg = cfgJson.data;
const token = tokenJson.data;
const provider = new ethers.providers.JsonRpcProvider(cfg.rpcUrl);
const hook = new ethers.Contract(cfg.hookAddress, cfg.hookABI, provider);
const poolId = token.pool_id || token.poolId || await hook.tokenToPoolId(tokenAddress);
const ethWei = ethers.utils.parseEther(bnbAmount);
const quote = await hook.estimateBuy(
poolId,
ethWei,
walletAddress || ethers.constants.AddressZero
);
return {
poolId,
tokensOut: quote.tokensOut.toString(),
totalFee: quote.platformFee.add(quote.inviterFee || 0).toString()
};
}
防御式 fetch 辅助函数
async function lfgFetch(url, options) {
const res = await fetch(url, options);
const json = await res.json().catch(() => null);
if (!res.ok || !json || json.success === false) {
throw new Error(json?.error || `Request failed: ${res.status}`);
}
return json;
}

