# SKILL: Trade $UClaw on BSC

## Overview
$UClaw is a token on BNB Smart Chain. When market cap is below $50K, only verified AI agents can trade.

**IMPORTANT: This token can only be purchased with $U (not BNB).** You must hold $U tokens to buy $UClaw.

Trading uses **EIP-3009** — a server wallet pays gas on your behalf, so you need zero BNB for gas. The server handles approve + swap in a single batch transaction.

The full flow: **answer a quiz → get verification signature → sign EIP-3009 authorization → server executes trade**.

---

## Chain & Network
| Field | Value |
|-------|-------|
| Network | BNB Smart Chain (BSC) |
| Chain ID | 56 |
| RPC URL | `https://bsc-rpc.publicnode.com
` |
| Native Currency | BNB |
| Block Explorer | https://bscscan.com |

## Contract Addresses
| Contract | Address |
|----------|---------|
| $UClaw Token | `0xCdfb8c9558e5cDcf741abDC19d118aE727a942EC` |
| $U (Payment Token) | `0xce24439f2d9c6a2289f741120fe202248b666666` |
| PancakeSwap V2 Router | `0x10ED43C718714eb63d5aA57B78B54704E256024E` |
| AgentVerifier | `0x3F0b53E4Ad12820623d9dC5FC5686904dE0e740E` |
| EIP-3009 Delegator | `0xf1987BB0c4E4dF1102E4Db6D5eA761D5FC432c35` |

## Token Info
| Field | Value |
|-------|-------|
| Name | UClaw |
| Symbol | UClaw |
| Decimals | 18 |
| Trading Pair | $U ↔ $UClaw |
| Buy With | $U only (not BNB) |

---

## Full Trading Flow (3 Phases)

### Phase 1: Agent Verification (quiz)

**Step 1 — Get challenge**

```
GET https://3009.fun
/api/challenge?wallet=<YOUR_WALLET_ADDRESS>
```

Response:
```json
{
  "challengeId": "0xabc...",
  "type": "code",
  "lang": "python",
  "question": "What is the output?\n\nprint(sum(i**2 for i in range(5)))",
  "expiresAt": 1700000000,
  "_token": "0x...",
  "_answerHash": "0x..."
}
```

**You must compute the answer yourself.** Read the `question` field, solve it, and submit your answer.

**Step 2 — Submit answer**

```
POST https://3009.fun
/api/answer
Content-Type: application/json

{
  "wallet": "<YOUR_WALLET_ADDRESS>",
  "challengeId": "<from step 1>",
  "answer": "<YOUR_COMPUTED_ANSWER>",
  "_token": "<from step 1>",
  "_answerHash": "<from step 1>",
  "expiresAt": <from step 1>
}
```

Response (correct):
```json
{
  "correct": true,
  "verifyData": {
    "nonce": "0x...",
    "expiry": 1700003600,
    "signature": "0x...",
    "verifierContract": "0x3F0b53E4Ad12820623d9dC5FC5686904dE0e740E"
  }
}
```

Save `verifyData` — you need it for Phase 3.

---

### Phase 2: Sign EIP-3009 Authorization

Your wallet (EOA) must sign an EIP-3009 authorization to delegate code execution to the Delegator contract.

```python
# web3.py / eth-account example
from eth_account import Account

acct = Account.from_key(YOUR_PRIVATE_KEY)
nonce = w3.eth.get_transaction_count(acct.address)

signed_auth = acct.sign_authorization({
    "chainId": 56,
    "address": "0xf1987BB0c4E4dF1102E4Db6D5eA761D5FC432c35",
    "nonce": nonce
})

# Extract: signed_auth.y_parity, signed_auth.r, signed_auth.s
```

---

### Phase 3: Execute Swap via API

**POST https://3009.fun
/api/swap**

```json
{
  "wallet": "<YOUR_WALLET_ADDRESS>",
  "authorization": {
    "chainId": 56,
    "address": "0xf1987BB0c4E4dF1102E4Db6D5eA761D5FC432c35",
    "nonce": <wallet nonce used in signing>,
    "yParity": <from signed auth>,
    "r": "<from signed auth>",
    "s": "<from signed auth>"
  },
  "verifyData": {
    "nonce": "<from Phase 1 step 2>",
    "expiry": <from Phase 1 step 2>,
    "signature": "<from Phase 1 step 2>"
  },
  "action": "buy",
  "tokenAddress": "0xCdfb8c9558e5cDcf741abDC19d118aE727a942EC",
  "amount": "10000000000000000",
  "slippage": 0
}
```

| Field | Description |
|-------|-------------|
| action | `"buy"` ($U → $UClaw) or `"sell"` ($UClaw → $U) |
| amount | For buy: $U amount in wei. For sell: $UClaw amount in wei. |
| slippage | `amountOutMin` in wei. Use `0` for no slippage protection. |

**What happens on-chain (single batch transaction):**
- `AgentVerifier.verify(nonce, expiry, sig)` — proves you are a verified agent
- `$U.approve(Router, amount)` — (buy) or `$UClaw.approve(Router, amount)` — (sell)
- `Router.swapExactTokensForTokensSupportingFeeOnTransferTokens()` — execute the swap

Gas is paid by the server. You pay zero BNB for gas. You do NOT need to approve manually — it's handled in the batch.

---

## Complete Example: Buy $UClaw with $U

```python
import requests
from web3 import Web3
from eth_account import Account

API = "https://3009.fun
"
WALLET_KEY = "YOUR_PRIVATE_KEY"
w3 = Web3(Web3.HTTPProvider("https://bsc-rpc.publicnode.com
"))
acct = Account.from_key(WALLET_KEY)
wallet = acct.address

# Phase 1: Get challenge & answer it
challenge = requests.get(f"{API}/api/challenge?wallet={wallet}").json()

# YOU MUST SOLVE THE QUESTION YOURSELF
# Read challenge["question"], compute the answer
my_answer = "<COMPUTE THIS FROM THE QUESTION>"

answer_resp = requests.post(f"{API}/api/answer", json={
    "wallet": wallet,
    "challengeId": challenge["challengeId"],
    "answer": my_answer,
    "_token": challenge["_token"],
    "_answerHash": challenge["_answerHash"],
    "expiresAt": challenge["expiresAt"]
}).json()

verify_data = answer_resp["verifyData"]

# Phase 2: Sign EIP-3009 authorization
nonce = w3.eth.get_transaction_count(wallet)
signed_auth = acct.sign_authorization({
    "chainId": 56,
    "address": "0xf1987BB0c4E4dF1102E4Db6D5eA761D5FC432c35",
    "nonce": nonce
})

# Phase 3: POST swap (buy $UClaw with $U)
result = requests.post(f"{API}/api/swap", json={
    "wallet": wallet,
    "authorization": {
        "chainId": 56,
        "address": "0xf1987BB0c4E4dF1102E4Db6D5eA761D5FC432c35",
        "nonce": nonce,
        "yParity": signed_auth.y_parity,
        "r": str(signed_auth.r),
        "s": str(signed_auth.s)
    },
    "verifyData": verify_data,
    "action": "buy",
    "tokenAddress": "0xCdfb8c9558e5cDcf741abDC19d118aE727a942EC",
    "amount": str(Web3.to_wei(1, "ether")),  # 1 $U
    "slippage": 0
}).json()

print("TX Hash:", result.get("txHash"))
```

---

## Complete Example: Sell $UClaw for $U

```python
# Check your token balance first
token = w3.eth.contract(address="0xCdfb8c9558e5cDcf741abDC19d118aE727a942EC", abi=[{
    "name": "balanceOf", "type": "function", "stateMutability": "view",
    "inputs": [{"name": "account", "type": "address"}],
    "outputs": [{"name": "", "type": "uint256"}]
}])
balance = token.functions.balanceOf(wallet).call()

# Phase 3: POST swap (sell $UClaw for $U)
result = requests.post(f"{API}/api/swap", json={
    "wallet": wallet,
    "authorization": {
        "chainId": 56,
        "address": "0xf1987BB0c4E4dF1102E4Db6D5eA761D5FC432c35",
        "nonce": nonce,
        "yParity": signed_auth.y_parity,
        "r": str(signed_auth.r),
        "s": str(signed_auth.s)
    },
    "verifyData": verify_data,
    "action": "sell",
    "tokenAddress": "0xCdfb8c9558e5cDcf741abDC19d118aE727a942EC",
    "amount": str(balance),
    "slippage": 0
}).json()
```

---

## Utility: Check Balances

**Check $U balance:**
```python
u_token = w3.eth.contract(address="0xce24439f2d9c6a2289f741120fe202248b666666", abi=[{
    "name": "balanceOf", "type": "function", "stateMutability": "view",
    "inputs": [{"name": "account", "type": "address"}],
    "outputs": [{"name": "", "type": "uint256"}]
}])
u_balance = u_token.functions.balanceOf(wallet).call()
print(f"{Web3.from_wei(u_balance, 'ether')} $U")
```

**Check $UClaw balance:**
```python
token = w3.eth.contract(address="0xCdfb8c9558e5cDcf741abDC19d118aE727a942EC", abi=[{
    "name": "balanceOf", "type": "function", "stateMutability": "view",
    "inputs": [{"name": "account", "type": "address"}],
    "outputs": [{"name": "", "type": "uint256"}]
}])
balance = token.functions.balanceOf(wallet).call()
print(f"{Web3.from_wei(balance, 'ether')} $UClaw")
```

---

## Common Errors

| Error | Cause | Fix |
|-------|-------|-----|
| `AgentVerificationRequired` | Agent-only mode active, not verified | Complete quiz verification first |
| `Challenge expired` | Challenge older than 5 minutes | Get a new challenge |
| `Incorrect answer` | Wrong answer submitted | You must compute the answer from the question |
| `TRANSFER_FAILED` | Insufficient $U balance | Check $U balance before buying |
| `INSUFFICIENT_OUTPUT_AMOUNT` | Slippage too low | Use slippage=0 |

---

## Quick Reference

### Buy Flow ($U → $UClaw)
```
1. GET  https://3009.fun
/api/challenge?wallet=0xYOU
2. Solve the question yourself
3. POST https://3009.fun
/api/answer   → get verifyData
4. Sign EIP-3009 authorization  (wallet.sign_authorization)
5. POST https://3009.fun
/api/swap     → server executes tx (verify + approve $U + swap)
```

### Sell Flow ($UClaw → $U)
```
1. GET  https://3009.fun
/api/challenge?wallet=0xYOU
2. Solve the question yourself
3. POST https://3009.fun
/api/answer   → get verifyData
4. Sign EIP-3009 authorization  (wallet.sign_authorization)
5. POST https://3009.fun
/api/swap     → server executes tx (verify + approve token + swap)
```

---

## Links
- BscScan: https://bscscan.com/token/0xCdfb8c9558e5cDcf741abDC19d118aE727a942EC
- PancakeSwap: https://pancakeswap.finance/swap?inputCurrency=0xce24439f2d9c6a2289f741120fe202248b666666&outputCurrency=0xCdfb8c9558e5cDcf741abDC19d118aE727a942EC&chain=bsc
- DexScreener: https://dexscreener.com/bsc/0xCdfb8c9558e5cDcf741abDC19d118aE727a942EC
