# AgentPerp DEX (v2) — Binance Agentic Wallet (`baw`) Calldata Skill

Use this guide when executing AgentPerp v2 trades via `baw contract-call preview` and `baw contract-call execute` on BNB Smart Chain (`--chain bsc`).

- **AgentPerpVault (v2):** `0x7cE99689e6Fd215ef7bd47d763F33606CE58eDF4`
- **AgentTwapOracle (v2):** `0x741285E703cbb744Fbc2D95169195315F7075F50`
- **AGC Token:** `0x63dea5aCdbE1F27c968907D08610A5f0A0284bA6`
- **Binance `.ai` Agent ID Registry:** `0x60B9262456dE011F7d15a54CdD8c48bAaa8B022F`

## 1. Check Agent Status (`eth_call`)
Selector: `0x1ffbb064` (`isAgent(address)`)
```
data = 0x1ffbb064 + pad32(yourWalletAddress)
```
Returns `0x...01` if your wallet holds a Binance `.ai` Agent ID.

## 2. Read Market Execution Price (`eth_call` on `AgentTwapOracle` v2)
Selector: `0xfe0ba5b1` (`getExecutionPrice(uint256 marketId, bool isLong, bool isOpen)`)
Example — Open Long (`isLong=1, isOpen=1`) on `AGCBNB` (`marketId=0`; `AIBNB=1`, `STARSAIBNB=2`):
```
0xfe0ba5b1
  0000000000000000000000000000000000000000000000000000000000000000
  0000000000000000000000000000000000000000000000000000000000000001
  0000000000000000000000000000000000000000000000000000000000000001
```
Return words (each 32 bytes):
- Word 0 (`0..64`): `execPrice` (`1e30` BNB per base token)
- Word 1 (`64..128`): `spotPrice` (`1e30`)
- Word 2 (`128..192`): `twapPrice` (`1e30`)
- Word 3 (`192..256`): `deviationBps` (must be `<= 1500` on open, close, AND liquidation)

Check effective pool-depth OI cap (`0x749904be` `getEffectiveOiCaps(uint256)`): returns `(effectiveLongCapAgc, effectiveShortCapAgc, poolDepthCapAgc, poolWbnb)`.

## 3. Approve AGC (`0x63dea5aCdbE1F27c968907D08610A5f0A0284bA6`)
Selector: `0x095ea7b3` (`approve(address spender, uint256 amount)`)
```bash
baw contract-call preview \
  --chain bsc \
  --to 0x63dea5aCdbE1F27c968907D08610A5f0A0284bA6 \
  --signature "approve(address,uint256)" \
  --args "0x7cE99689e6Fd215ef7bd47d763F33606CE58eDF4" "1005000000000000000000000"
```

## 4. Open 5× Long on `AGCBNB` PERP (`marketId = 0`)
Collateral = `1,005,000 AGC` (`1,000,000` net + `5,000` open fee -> `4,750 AGC` burned, `250 AGC` owner).
Size = `5,000,000 AGC`.
Selector: `0xeaf1f1e6` (`openPosition(uint256,bool,uint256,uint256,uint256)`)
```bash
baw contract-call preview \
  --chain bsc \
  --to 0x7cE99689e6Fd215ef7bd47d763F33606CE58eDF4 \
  --signature "openPosition(uint256,bool,uint256,uint256,uint256)" \
  --args "0" "true" "1005000000000000000000000" "5000000000000000000000000" "<ACCEPTABLE_PRICE_1E30>"
```
Pre-encoded calldata example (`marketId=0, isLong=true, collateral=1,005,000e18, size=5,000,000e18, acceptablePrice=0`):
```
0xeaf1f1e60000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000d4816908fb8777740000000000000000000000000000000000000000000000422ca8b0a00a42500000000000000000000000000000000000000000000000000000000000000000000000
```
*(In production always pass a non-zero `acceptablePrice` derived from `getExecutionPrice` ± 1%.)*

## 5. Check Position Health (`eth_call`)
Selector: `0x22fb73d9` (`getPositionStatus(uint256 positionId)`)
Return words:
- Word 0: `pnlAgc` (signed `int256`, close execution price)
- Word 1: `borrowFeeAgc` (`uint256`)
- Word 2: `closeFeeAgc` (`uint256`)
- Word 3: `netEquityAgc` (signed `int256`)
- Word 4: `execPrice` (`1e30`)
- Word 5: `liquidatable` (`0` or `1`, computed strictly on **TWAP only** + `deviationBps <= 1500`)

## 6. Close Position (after >= 60s and when spot/TWAP deviation <= 15%)
Selector: `0x2d6ce61d` (`closePosition(uint256 positionId, uint256 minPayoutAgc)`)
Note: `closePosition` reverts with `PriceDeviationTooHigh()` if live spot deviates >15% from the 15-minute TWAP; wait for spot and TWAP to converge within 15%.
```bash
baw contract-call preview \
  --chain bsc \
  --to 0x7cE99689e6Fd215ef7bd47d763F33606CE58eDF4 \
  --signature "closePosition(uint256,uint256)" \
  --args "1" "<MIN_PAYOUT_WEI>"
```

## 7. Deposit / Withdraw LP (Agent LP Vault)
- Deposit `10,000,000 AGC`: `0xeb37acfc` `depositLP(uint256)`
- Withdraw after 1h cooldown (reverts if withdrawal would push vault utilization above 80%): `0xe4456ecb` `withdrawLP(uint256)`
- **LP Risk Notice:** On thin pools, a sustained pump/dump can move the 15m TWAP and losses fall on LPs; LP share price excludes unrealized PnL; owner can raise configured OI caps via `setMarketParams` without a timelock (bounded on-chain at 5% of pool WBNB depth).
