add project
This commit is contained in:
192
apps/api/src/routes/bsc-swap-proxy.routes.ts
Normal file
192
apps/api/src/routes/bsc-swap-proxy.routes.ts
Normal file
@@ -0,0 +1,192 @@
|
||||
import { Request, Response, Router } from 'express';
|
||||
import { ethers } from 'ethers';
|
||||
|
||||
const router = Router();
|
||||
|
||||
const BSC_RPC = 'https://bsc-dataseed.binance.org';
|
||||
const BSC_CHAIN_ID = 56;
|
||||
const BSC_TIMEOUT_MS = 15_000;
|
||||
|
||||
// PancakeSwap V2 Router
|
||||
const PANCAKE_ROUTER = '0x10ED43C718714eb63d5aA57B78B54704E256024E';
|
||||
const WBNB = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c';
|
||||
|
||||
// Supported tokens
|
||||
const TOKEN_MAP: Record<string, string> = {
|
||||
BNB: WBNB,
|
||||
USDT: '0x55d398326f99059fF775485246999027B3197955',
|
||||
DOGE: '0xbA2aE424d960c26247Dd6c32edC70B295c744C43',
|
||||
};
|
||||
|
||||
const TOKEN_DECIMALS: Record<string, number> = {
|
||||
BNB: 18,
|
||||
USDT: 18,
|
||||
DOGE: 8,
|
||||
};
|
||||
|
||||
const ROUTER_ABI = [
|
||||
'function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts)',
|
||||
'function swapExactETHForTokensSupportingFeeOnTransferTokens(uint amountOutMin, address[] calldata path, address to, uint deadline) external payable',
|
||||
'function swapExactTokensForETHSupportingFeeOnTransferTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external',
|
||||
];
|
||||
|
||||
const ERC20_ABI = [
|
||||
'function approve(address spender, uint256 amount) external returns (bool)',
|
||||
'function allowance(address owner, address spender) external view returns (uint256)',
|
||||
];
|
||||
|
||||
router.get('/quote', getSwapQuote);
|
||||
router.post('/build', buildSwapTx);
|
||||
|
||||
export default router;
|
||||
|
||||
// ─── GET /quote ───
|
||||
|
||||
async function getSwapQuote(req: Request, res: Response) {
|
||||
const from = String(req.query.from || '').toUpperCase();
|
||||
const to = String(req.query.to || '').toUpperCase();
|
||||
const amount = String(req.query.amount || '');
|
||||
|
||||
if (!TOKEN_MAP[from] || !TOKEN_MAP[to]) {
|
||||
res.status(400).json({ success: false, error: 'Invalid from/to. Supported: BNB, USDT, DOGE' });
|
||||
return;
|
||||
}
|
||||
if (from === to) {
|
||||
res.status(400).json({ success: false, error: 'from and to must be different' });
|
||||
return;
|
||||
}
|
||||
|
||||
const amountBigInt = BigInt(amount || '0');
|
||||
if (amountBigInt <= 0n) {
|
||||
res.status(400).json({ success: false, error: 'amount must be positive' });
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const provider = new ethers.providers.StaticJsonRpcProvider(BSC_RPC, BSC_CHAIN_ID);
|
||||
const routerContract = new ethers.Contract(PANCAKE_ROUTER, ROUTER_ABI, provider);
|
||||
|
||||
const path = [TOKEN_MAP[from], TOKEN_MAP[to]];
|
||||
const amounts: ethers.BigNumber[] = await withTimeout(
|
||||
routerContract.getAmountsOut(amount, path),
|
||||
BSC_TIMEOUT_MS,
|
||||
'PancakeSwap quote timed out'
|
||||
);
|
||||
|
||||
const amountOut = amounts[amounts.length - 1].toString();
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
amountIn: amountBigInt.toString(),
|
||||
amountOut,
|
||||
from,
|
||||
to,
|
||||
fromDecimals: TOKEN_DECIMALS[from],
|
||||
toDecimals: TOKEN_DECIMALS[to],
|
||||
});
|
||||
} catch (error) {
|
||||
const msg = error instanceof Error ? error.message : 'Failed to get BSC swap quote';
|
||||
res.status(502).json({ success: false, error: msg });
|
||||
}
|
||||
}
|
||||
|
||||
// ─── POST /build ───
|
||||
|
||||
async function buildSwapTx(req: Request, res: Response) {
|
||||
const { from, to, amount, amountOutMin, userAddress } = req.body;
|
||||
|
||||
if (!from || !to || !amount || !amountOutMin || !userAddress) {
|
||||
res.status(400).json({ success: false, error: 'Missing required fields: from, to, amount, amountOutMin, userAddress' });
|
||||
return;
|
||||
}
|
||||
|
||||
const fromUpper = String(from).toUpperCase();
|
||||
const toUpper = String(to).toUpperCase();
|
||||
|
||||
if (!TOKEN_MAP[fromUpper] || !TOKEN_MAP[toUpper] || fromUpper === toUpper) {
|
||||
res.status(400).json({ success: false, error: 'Invalid from/to pair' });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!ethers.utils.isAddress(userAddress)) {
|
||||
res.status(400).json({ success: false, error: 'Invalid BSC address' });
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const provider = new ethers.providers.StaticJsonRpcProvider(BSC_RPC, BSC_CHAIN_ID);
|
||||
const routerContract = new ethers.Contract(PANCAKE_ROUTER, ROUTER_ABI, provider);
|
||||
const deadline = Math.floor(Date.now() / 1000) + 1200; // 20 minutes
|
||||
const path = [TOKEN_MAP[fromUpper], TOKEN_MAP[toUpper]];
|
||||
|
||||
const transactions: Array<{ type: string; to: string; data: string; value: string }> = [];
|
||||
|
||||
if (fromUpper === 'BNB') {
|
||||
// BNB → Token: swapExactETHForTokensSupportingFeeOnTransferTokens
|
||||
const data = routerContract.interface.encodeFunctionData(
|
||||
'swapExactETHForTokensSupportingFeeOnTransferTokens',
|
||||
[amountOutMin, path, userAddress, deadline]
|
||||
);
|
||||
|
||||
transactions.push({
|
||||
type: 'swap',
|
||||
to: PANCAKE_ROUTER,
|
||||
data,
|
||||
value: amount, // BNB amount in wei
|
||||
});
|
||||
} else {
|
||||
// Token → BNB: check allowance, build approve if needed, then swap
|
||||
const tokenAddress = TOKEN_MAP[fromUpper];
|
||||
const tokenContract = new ethers.Contract(tokenAddress, ERC20_ABI, provider);
|
||||
const currentAllowance: ethers.BigNumber = await withTimeout(
|
||||
tokenContract.allowance(userAddress, PANCAKE_ROUTER),
|
||||
BSC_TIMEOUT_MS,
|
||||
'Allowance check timed out'
|
||||
);
|
||||
|
||||
if (currentAllowance.lt(ethers.BigNumber.from(amount))) {
|
||||
// Build approve tx
|
||||
const approveData = tokenContract.interface.encodeFunctionData(
|
||||
'approve',
|
||||
[PANCAKE_ROUTER, ethers.constants.MaxUint256]
|
||||
);
|
||||
|
||||
transactions.push({
|
||||
type: 'approve',
|
||||
to: tokenAddress,
|
||||
data: approveData,
|
||||
value: '0',
|
||||
});
|
||||
}
|
||||
|
||||
// Build swap tx
|
||||
const swapData = routerContract.interface.encodeFunctionData(
|
||||
'swapExactTokensForETHSupportingFeeOnTransferTokens',
|
||||
[amount, amountOutMin, path, userAddress, deadline]
|
||||
);
|
||||
|
||||
transactions.push({
|
||||
type: 'swap',
|
||||
to: PANCAKE_ROUTER,
|
||||
data: swapData,
|
||||
value: '0',
|
||||
});
|
||||
}
|
||||
|
||||
res.json({ success: true, transactions });
|
||||
} catch (error) {
|
||||
const msg = error instanceof Error ? error.message : 'Failed to build BSC swap';
|
||||
res.status(502).json({ success: false, error: msg });
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Utils ───
|
||||
|
||||
function withTimeout<T>(promise: Promise<T>, timeoutMs: number, message: string): Promise<T> {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
const timeoutId = setTimeout(() => reject(new Error(message)), timeoutMs);
|
||||
promise
|
||||
.then((value) => { clearTimeout(timeoutId); resolve(value); })
|
||||
.catch((error) => { clearTimeout(timeoutId); reject(error); });
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user