feat/PancakeSwap CLMM LP Management & MasterChef Integration on BSC#606
feat/PancakeSwap CLMM LP Management & MasterChef Integration on BSC#606VeXHarbinger wants to merge 15 commits into
Conversation
|
Wait there is a file that should have been removed |
|
When you said I created bsc as a chain (Chain/BSC) I misunderstood the comment and just saw what you were seeing. That was old and has been deleted, I thought you were referring to a config change. |
|
I am getting worried that my PR could drift from dev but don't want to keep resolving conflicts until I know it's in queue |
|
hey thanks for using the BSC Pancakeswap connector and applying these fixes. To get these reviewed and potentially merged, I recommend splitting this PR into separate ones for:
|
|
Also, please base your PRs on the |
@fengtality It is, can you not? The problem was all the existing code was commented out in main but didn't exist at the time in dev. We had discussed it at the time. Which was why I made the mistake and forked main in the other repo PR but reverted that back to dev once I notced. |
|
@VeXHarbinger OK I figured out how to use Github and changed the base branch to development. Did you have a chance to address my comments earlier? IMO most important is treating BSC as an ETH network rather than separate chain |
resolved it on Feb 21st, I believe |
|
@fengtality I remember fixing the branching for 2 of the 3 repos when you said it, this was the one with code in main that wasn't in dev, I asked you about it before I began to code. which was back in Jan . Sorry it became a problem for you. Was not my intent |

PR Summary: PancakeSwap CLMM LP Management & MasterChef Integration on BSC
@cardosofede ~ You said you'd take a look at this and I just saw that new post about bsc and don't know if it resolves these issues or not. I did move them up a level in the path like you asked
What's Included
This PR adds complete PancakeSwap CLMM (Concentrated Liquidity Market Maker) position management and MasterChef staking integration on BSC.
Key Features Added
1. MasterChef Staking System
POST /connectors/pancakeswap/masterchef-stake- Stake LP NFT positions in MasterChefPOST /connectors/pancakeswap/masterchef-unstake- Unstake LP NFT from MasterChefwithdraw()functionPOST /connectors/pancakeswap/masterchef-unstake-and-close✨ NEWPOST /connectors/pancakeswap/masterchef-knows-pool✨ NEW2. Critical Fixes
openPositionandquoteSwapto properly handle numeric values3. BSC Network Support
/wallet/balancesendpoint for getting wallet balances4. Enhanced Parameters
positionsOwned- AddedactiveOnlyfilter to show only positions with active liquidityexecuteSwap- Added optionalpoolAddressparameter for manual pool selectionmasterchef-stake/unstake- AddedwalletAddressparameter for transaction signingFiles Modified
Core Implementation (5 files)
src/connectors/pancakeswap/pancakeswap.ts- MasterChef integration methodssrc/connectors/pancakeswap/clmm-routes/index.ts- Route registrationsrc/connectors/pancakeswap/masterchef-stake.ts✨ NEW - Staking endpointsrc/connectors/pancakeswap/masterchef-unstake.ts✨ NEW - Unstaking endpointsrc/connectors/pancakeswap/PancakeswapV3Masterchef.abi.json✨ NEW - MasterChef ABINew Endpoints
src/connectors/pancakeswap/masterchef-stake.ts✨ NEWsrc/connectors/pancakeswap/masterchef-unstake-and-close.ts✨ NEWsrc/connectors/pancakeswap/masterchef-knows-pool.ts✨ NEWFixes & Enhancements (5 files)
src/connectors/pancakeswap/clmm-routes/quoteSwap.ts- BigInt fixessrc/connectors/pancakeswap/clmm-routes/openPosition.ts- Amount handlingsrc/connectors/pancakeswap/clmm-routes/positionsOwned.ts- activeOnly parametersrc/connectors/pancakeswap/clmm-routes/positionInfo.ts- Error handlingsrc/connectors/pancakeswap/schemas.ts- Schema updateswallet Support (4 files)
src/wallet/schemas.ts- BSC chain supportsrc/wallet/utils.ts- BSC wallet handlingsrc/wallet/balances.ts✨ NEW - wallet balance endpointTesting Checklist
Before merging, verify:
POST /connectors/pancakeswap/masterchef-stakesucceeds (after NFT approval)POST /connectors/pancakeswap/execute-swaphandles decimal amounts correctlyPOST /connectors/pancakeswap/open-positionaccepts string and number inputsPOST /connectors/pancakeswap/masterchef-unstake-and-closecompletes both operations/wallet/balancesreturns correct wallet balancesValue Conversions Corrected
All the following value conversion issues were fixed:
Breaking Changes
None. All changes are backward compatible with existing endpoints.
Fix 2: Execute-Swap BigInt Conversion Error
Problem
{ "statusCode": 500, "error": "HttpError", "message": "Failed to execute swap: Cannot convert 200000000000000000 to a BigInt" }When calling with
amount: 0.2, getting BigInt conversion error.File:
src/connectors/pancakeswap/clmm-routes/executeSwap.tsStep-by-Step Testing Guide
Replace the following placeholders with your actual values:
<YOUR_WALLET_ADDRESS>- Your BSC wallet address (e.g.,0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb)<YOUR_POOL_ADDRESS>- Pool contract address (e.g.,0x7f51c8aaa6b0599abd16674e2b17fec7a9f674a1)<YOUR_NFT_ID>- NFT token ID from opened position (e.g.,12345)Prerequisites
Start Gateway (from
~/trash/hummingbot-core/)Verify Gateway is Running
Add Your Wallet to Gateway
Test 1: Get Pool Information
curl "http://localhost:15888/connectors/pancakeswap/clmm/pool-info?network=bsc&poolAddress=<YOUR_POOL_ADDRESS>"Expected Response:
{ "address": "0x7f51c8aaa6b0599abd16674e2b17fec7a9f674a1", "baseTokenAddress": "0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82", "quoteTokenAddress": "0x55d398326f99059fF775485246999027B3197955", "feePct": 0.25, "price": 3.75, "baseTokenAmount": 1250000, "quoteTokenAmount": 4687500, "activeBinId": 85176 }Test 2: Quote a New Position
Expected Response:
{ "baseTokenAmount": 100, "quoteTokenAmount": 375.25, "lowerPrice": 3.5, "upperPrice": 4.0, "liquidity": "150000000000000000", "baseLimited": true }Test 3: Open a Position (Decimal Amount Handling)
Before running: Ensure you have approved both tokens for the Position Manager (
0xEfF92A263d31888d860bD50809A8D171709b7b1c)Expected Response:
{ "signature": "0x1234567890abcdef...", "status": 1, "data": { "fee": 0.00123, "positionAddress": "12345", "positionRent": 0, "baseTokenAmountAdded": 0.5, "quoteTokenAmountAdded": 1.875 } }Save the
positionAddressvalue as<YOUR_NFT_ID>for subsequent tests.Test 4: Get Position Information
curl "http://localhost:15888/connectors/pancakeswap/clmm/position-info?network=bsc&positionAddress=<YOUR_NFT_ID>"Expected Response:
{ "address": "12345", "poolAddress": "0x7f51c8aaa6b0599abd16674e2b17fec7a9f674a1", "baseTokenAddress": "0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82", "quoteTokenAddress": "0x55d398326f99059fF775485246999027B3197955", "baseTokenAmount": 0.5, "quoteTokenAmount": 1.875, "baseFeeAmount": 0, "quoteFeeAmount": 0, "lowerBinId": 85150, "upperBinId": 85200, "lowerPrice": 3.5, "upperPrice": 4.0, "price": 3.75 }Test 5: Execute Swap (BigInt Fix Test)
Before running: Ensure you have approved both tokens for the SwapRouter02 (
0x1b81D678ffb9C0263b24A97847620C99d213eB14)Expected Response:
{ "signature": "0xabcdef1234567890...", "status": 1, "data": { "tokenIn": "0x55d398326f99059fF775485246999027B3197955", "tokenOut": "0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82", "amountIn": 0.75, "amountOut": 0.2, "fee": 0.00089, "baseTokenBalanceChange": 0.2, "quoteTokenBalanceChange": -0.75 } }Test 6: Check if Pool is Registered in MasterChef
Expected Response (if registered):
{ "isRegistered": true, "poolId": 42 }Expected Response (if not registered):
{ "isRegistered": false }Test 7: Stake NFT in MasterChef (Optional)
Before running: Ensure you have approved MasterChef for NFT transfers via the Position Manager's
setApprovalForAllfunction.Expected Response:
{ "signature": "0x9876543210fedcba...", "status": 1, "message": "NFT staked successfully in MasterChef" }Test 8: Unstake NFT from MasterChef
Expected Response:
{ "signature": "0xfedcba0987654321...", "status": 1, "message": "NFT unstaked successfully from MasterChef", "rewardsCollected": 12.5 }Test 9: Close Position
Expected Response:
{ "signature": "0x1122334455667788...", "status": 1, "data": { "fee": 0.00095, "baseTokenAmountRemoved": 0.5, "quoteTokenAmountRemoved": 1.875, "baseFeeAmount": 0.002, "quoteFeeAmount": 0.0075 } }Test 10: Get Wallet Balances
curl "http://localhost:15888/wallet/balances?chain=ethereum&network=bsc&address=<YOUR_WALLET_ADDRESS>"Expected Response:
{ "balances": { "BNB": "1.25", "CAKE": "150.5", "USDT": "500.25" } }Test 11: List All Positions Owned (with activeOnly filter)
curl "http://localhost:15888/connectors/pancakeswap/clmm/positions-owned?network=bsc&walletAddress=<YOUR_WALLET_ADDRESS>&activeOnly=true"Expected Response:
[ { "address": "12345", "poolAddress": "0x7f51c8aaa6b0599abd16674e2b17fec7a9f674a1", "baseTokenAmount": 0.5, "quoteTokenAmount": 1.875, "lowerPrice": 3.5, "upperPrice": 4.0, "price": 3.75 } ]Common Error Scenarios to Test
Error 1: Insufficient Allowance for Swap
Expected Error:
{ "statusCode": 400, "error": "Bad Request", "message": "Insufficient allowance for USDT. Current: 0 USDT, Required: 0.75 USDT. To swap with PancakeSwap CLMM, you need to approve the spender \"pancakeswap/clmm/swap\" instead of \"pancakeswap/clmm\". This will approve the SwapRouter02 address (0x1b81D678ffb9C0263b24A97847620C99d213eB14)..." }Error 2: Pool Not Registered in MasterChef
Expected Error:
{ "statusCode": 400, "error": "Bad Request", "message": "Pool 0x... is not registered in MasterChef. Only pools registered in MasterChef can be staked." }Changes Made:
Comprehensive Logging - Added logging at every step to trace where conversion happens:
Raw Amount Formatting - Ensured all quote values are properly formatted strings:
Direct String Passing - Pass raw strings directly to contract functions:
Error Tracing - Enhanced error messages with stack traces:
Expected Behavior Now
When you call the endpoint again with the same payload, you'll see detailed logs like:
This will show us EXACTLY where any conversion issue occurs, allowing us to pinpoint and fix it.
Follow-up Testing
After deploying these changes:
/connectors/pancakeswap/clmm/execute-swapwith your test payloadRelated Issues