Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions submissions/pancakeswap-v2/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2026 skylavis-sky

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
35 changes: 35 additions & 0 deletions submissions/pancakeswap-v2/plugin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
schema_version: 1
name: pancakeswap-v2
version: "0.2.0"
description: "Swap tokens and provide full-range liquidity on PancakeSwap V2 — the xyk AMM on BSC and Base"
author:
name: skylavis-sky
github: skylavis-sky
category: defi-protocol
tags:
- dex
- swap
- liquidity
- amm
- pancakeswap
- bsc
- v2
- xyk
- lp
license: MIT

components:
skill:
dir: skills/pancakeswap-v2

build:
lang: rust
binary_name: pancakeswap-v2
source_repo: skylavis-sky/onchainos-plugins
source_commit: "83b306e1e3ccf4b14fc11a76bd5b906297340ef4"
source_dir: pancakeswap-v2

api_calls:
- "https://bsc-rpc.publicnode.com"
- "https://base-rpc.publicnode.com"
- "https://api.pancakeswap.info/api/v2/tokens"
268 changes: 268 additions & 0 deletions submissions/pancakeswap-v2/skills/pancakeswap-v2/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
---
name: pancakeswap-v2
description: "Swap tokens and provide full-range liquidity on PancakeSwap V2 — the xyk AMM on BSC and Base. Trigger phrases: swap on pancakeswap v2, pancake swap, pcs v2 swap, add liquidity pancakeswap, remove liquidity pancake, pancake amm, pancakeswap v2 quote, check pancake pair. Chinese: 在PancakeSwap V2兑换代币, PancakeSwap添加流动性, PancakeSwap移除流动性, 查询Pancake报价"
license: MIT
metadata:
author: skylavis-sky
version: "0.2.0"
---

## Changelog

### v0.2.0 (2026-04-11)

**Bug fixes in `remove-liquidity`:**

1. **`lpBalance` showed zero-address balance in dry-run** — When `--dry-run` was used without `--from`, the wallet was set to `0x0000…0000` for all reads, so `lpBalance` returned whatever LP tokens the zero address holds (typically the locked MINIMUM_LIQUIDITY) instead of the user's real balance. Fixed: dry-run now uses `--from` (if provided) or `onchainos::resolve_wallet` for reads; zero address is only the final fallback when no wallet is available.

2. **`expectedTokenA / expectedTokenB` overflow for large pools** — The formula `reserve * lp_burned / total_supply` used raw `u128` multiplication. For high-liquidity pools (e.g. the BSC BNB/USDT pool with reserves > 10²² raw units), `reserve × lp_burned` overflows u128 (max ~3.4×10³⁸), producing garbage values. Fixed: `safe_mul_div` tries `checked_mul` first and falls back to `f64` arithmetic on overflow, matching the approach already used in `lp-balance`.

Both bugs confirmed fixed via full live-transaction regression test suite (12 tests, BSC + Base, 2026-04-11).

---

## Architecture

- Read ops (quote, get-pair, get-reserves, lp-balance) → direct `eth_call` via public RPC; no confirmation needed
- Write ops (swap, add-liquidity, remove-liquidity) → after user confirmation, submits via `onchainos wallet contract-call --force`
- ERC-20 approvals → manually encoded `approve()` calldata, submitted via `onchainos wallet contract-call --force`
- Supports BSC (chain 56, default) and Base (chain 8453)
- V2 uses constant-product xyk formula; LP tokens are standard ERC-20 (not NFTs); fixed 0.25% swap fee

## Execution Flow for Write Operations

1. Run with `--dry-run` first to preview calldata and estimated amounts
2. **Ask user to confirm** the transaction details before proceeding
3. Execute only after explicit user approval
4. Report transaction hash and block explorer link

---

## Command Routing

| User intent | Command |
|-------------|---------|
| "How much CAKE for 100 USDT?" | `pancakeswap-v2 quote` |
| "Swap 100 USDT for CAKE on PancakeSwap V2" | `pancakeswap-v2 swap` |
| "Add liquidity CAKE/BNB on PancakeSwap" | `pancakeswap-v2 add-liquidity` |
| "Remove my CAKE/USDT liquidity on Pancake" | `pancakeswap-v2 remove-liquidity` |
| "What is the CAKE/BNB pair address on PancakeSwap V2?" | `pancakeswap-v2 get-pair` |
| "What are the reserves in the CAKE/BNB pool?" | `pancakeswap-v2 get-reserves` |
| "How much LP do I have for CAKE/BNB?" | `pancakeswap-v2 lp-balance` |

---

## quote — Get Expected Swap Output

**Trigger phrases:** quote pancakeswap, how much would I get, pancake v2 price, estimate swap

**Usage:**
```
pancakeswap-v2 --chain 56 quote --token-in USDT --token-out CAKE --amount-in 100000000000000000000
```

**Parameters:**
| Name | Flag | Description |
|------|------|-------------|
| tokenIn | `--token-in` | Input token: symbol (USDT, CAKE, WBNB) or hex address |
| tokenOut | `--token-out` | Output token: symbol or hex address |
| amountIn | `--amount-in` | Input amount in minimal units (e.g. 100e18 = 100 tokens for 18-decimal token) |
| chain | `--chain` | Chain ID: 56 (BSC, default) or 8453 (Base) |

**Example output:**
```json
{
"ok": true,
"data": {
"tokenIn": "0x55d398326f99059fF775485246999027B3197955",
"tokenOut": "0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82",
"symbolIn": "USDT",
"symbolOut": "CAKE",
"amountIn": "100000000000000000000",
"amountOut": "23500000000000000000",
"amountOutHuman": "23.500000",
"path": ["0x55d3...", "0x0E09..."],
"fee": "0.25%",
"chain": 56
}
}
```

Read-only operation — no confirmation required.

---

## swap — Swap Tokens

**Trigger phrases:** swap on pancakeswap v2, pancake swap, exchange tokens on pcs, trade on pancakeswap

**Usage:**
```
pancakeswap-v2 --chain 56 swap --token-in USDT --token-out CAKE --amount-in 100000000000000000000
```

**Parameters:**
| Name | Flag | Description |
|------|------|-------------|
| tokenIn | `--token-in` | Input token: symbol or address. Use BNB/ETH for native |
| tokenOut | `--token-out` | Output token: symbol or address |
| amountIn | `--amount-in` | Input amount in minimal units |
| slippageBps | `--slippage-bps` | Slippage in basis points (default 50 = 0.5%) |
| deadlineSecs | `--deadline-secs` | Seconds until deadline (default 300) |
| dryRun | `--dry-run` | Preview calldata only, no broadcast |

**Execution flow:**
1. Run `--dry-run` to preview the swap calldata and expected output
2. **Ask user to confirm** the swap details (tokenIn, tokenOut, amountIn, amountOutMin, slippage)
3. If tokenIn is an ERC-20 and allowance is insufficient, first submit an approve tx via `onchainos wallet contract-call --force`; **ask user to confirm** the approval
4. Submit swap via `onchainos wallet contract-call --force`
5. Report txHash and BscScan/BaseScan link

**Supported swap variants:**
- Token → Token (`swapExactTokensForTokens`)
- BNB/ETH → Token (`swapExactETHForTokens`, pass `--token-in BNB`)
- Token → BNB/ETH (`swapExactTokensForETH`, pass `--token-out BNB`)

**Example output:**
```json
{
"ok": true,
"steps": [
{"step": "approve", "txHash": "0xabc..."},
{"step": "swapExactTokensForTokens", "txHash": "0xdef...", "explorer": "bscscan.com/tx/0xdef..."}
]
}
```

---

## add-liquidity — Add Liquidity

**Trigger phrases:** add liquidity on pancakeswap, provide liquidity pancake v2, become LP on pancakeswap, join pancake pool

**Usage:**
```
# Token + Token
pancakeswap-v2 --chain 56 add-liquidity --token-a CAKE --token-b USDT --amount-a 10000000000000000000 --amount-b 50000000000000000000

# Token + native BNB
pancakeswap-v2 --chain 56 add-liquidity --token-a CAKE --token-b BNB --amount-a 10000000000000000000 --amount-b 50000000000000000
```

**Parameters:**
| Name | Flag | Description |
|------|------|-------------|
| tokenA | `--token-a` | First token: symbol or address. Use BNB/ETH for native |
| tokenB | `--token-b` | Second token. Use BNB/ETH for native |
| amountA | `--amount-a` | Desired amount of tokenA in minimal units |
| amountB | `--amount-b` | Desired amount of tokenB (or native BNB/ETH) in minimal units |
| slippageBps | `--slippage-bps` | Slippage tolerance (default 50 = 0.5%) |
| dryRun | `--dry-run` | Preview calldata only |

**Execution flow:**
1. Check current pair reserves and ratio
2. Run `--dry-run` to preview the transaction
3. **Ask user to confirm** the amounts and LP token receipt before proceeding
4. Approve Router02 to spend tokenA/tokenB via `onchainos wallet contract-call --force` (if needed); **ask user to confirm** each approval
5. Submit `addLiquidity` or `addLiquidityETH` via `onchainos wallet contract-call --force`
6. Report txHash and LP tokens received

---

## remove-liquidity — Remove Liquidity

**Trigger phrases:** remove liquidity pancakeswap, withdraw liquidity from pancake, exit pancakeswap pool, burn LP tokens pancake

**Usage:**
```
# Remove all LP
pancakeswap-v2 --chain 56 remove-liquidity --token-a CAKE --token-b USDT

# Remove specific amount
pancakeswap-v2 --chain 56 remove-liquidity --token-a CAKE --token-b USDT --liquidity 1000000000000000000
```

**Parameters:**
| Name | Flag | Description |
|------|------|-------------|
| tokenA | `--token-a` | First token |
| tokenB | `--token-b` | Second token. Use BNB/ETH to receive native |
| liquidity | `--liquidity` | LP tokens to burn (minimal units). Omit to remove all |
| slippageBps | `--slippage-bps` | Slippage tolerance (default 50 = 0.5%) |
| dryRun | `--dry-run` | Preview only |

**Execution flow:**
1. Fetch LP balance and compute expected token withdrawals
2. Display summary: LP amount, expected tokenA and tokenB out
3. Run `--dry-run` to preview calldata
4. **Ask user to confirm** the removal details before proceeding
5. Approve LP tokens to Router02 via `onchainos wallet contract-call --force`; **ask user to confirm**
6. Submit `removeLiquidity` or `removeLiquidityETH` via `onchainos wallet contract-call --force`
7. Report txHash

---

## get-pair — Look Up Pair Address

**Trigger phrases:** find pancakeswap pair, what is the pancake pair address, does pancake v2 have a pool for

**Usage:**
```
pancakeswap-v2 --chain 56 get-pair --token-a CAKE --token-b BNB
```

Read-only — no confirmation required.

---

## get-reserves — Get Pool Reserves

**Trigger phrases:** pancakeswap pool reserves, pancake pool price, what is the price in pancake v2, check pancake liquidity

**Usage:**
```
pancakeswap-v2 --chain 56 get-reserves --token-a CAKE --token-b BNB
```

Read-only — no confirmation required.

---

## lp-balance — Check LP Token Balance

**Trigger phrases:** how much LP do I have in pancake, check my pancakeswap position, my pancake v2 liquidity

**Usage:**
```
pancakeswap-v2 --chain 56 lp-balance --token-a CAKE --token-b BNB
pancakeswap-v2 --chain 56 lp-balance --token-a CAKE --token-b BNB --wallet 0xYourAddress
```

Read-only — no confirmation required.

---

## Token Symbols (BSC)

| Symbol | Address |
|--------|---------|
| WBNB / BNB | `0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c` |
| CAKE | `0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82` |
| USDT | `0x55d398326f99059fF775485246999027B3197955` |
| USDC | `0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d` |
| BUSD | `0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56` |

For Base (8453): WETH `0x4200000000000000000000000000000000000006`, USDC `0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913`.

---

## Troubleshooting

| Error | Likely cause | Fix |
|-------|-------------|-----|
| "No V2 liquidity path found" | No direct or WBNB-routed pair exists | Use a different token pair or check on BscScan |
| "You have no LP tokens for this pair" | Wallet has 0 LP balance | Verify correct wallet address and chain |
| txHash is "pending", never broadcasts | Missing `--force` flag | Plugin always adds `--force`; check onchainos version |
| Swap reverts on-chain | Slippage too tight or stale price | Increase `--slippage-bps` (e.g. 100 for 1%) |
| "Cannot resolve wallet address" | onchainos not logged in | Run `onchainos wallet login` or pass `--from <address>` |
| "Unsupported chain ID" | Chain not 56 or 8453 | Use `--chain 56` (BSC) or `--chain 8453` (Base) |
Binary file not shown.
Loading
Loading