===========================================
POP CALL CHAIN
===========================================

Call a chain (execute extrinsic)

Usage: pop call chain [OPTIONS]

Options:
  -p, --pallet <PALLET>      The pallet containing the dispatchable function to execute
  -f, --function <FUNCTION>  The dispatchable function, storage item, or constant to execute/query
                             within the specified pallet. It must match the exact name as in the
                             source code
  -a, --args [<ARGS>...]     The dispatchable function arguments, encoded as strings
  -u, --url <URL>            Websocket endpoint of a node
  -s, --suri <SURI>          Secret key URI for the account signing the extrinsic
                             e.g. - for a dev account "//Alice"
                                  - with a password "//Alice///SECRET_PASSWORD"
  -w, --use-wallet           Use a browser extension wallet to sign the extrinsic
  -c, --call <call>          SCALE encoded bytes representing the call data of the extrinsic
  -S, --sudo                 Authenticates the sudo key and dispatches a function call with `Root`
                             origin
  -y, --skip-confirm         Automatically signs and submits the extrinsic without prompting for
                             confirmation
  -h, --help                 Print help

===========================================
USAGE
===========================================

# Interactive mode (recommended)
pop call chain

# Call with specific pallet and function
pop call chain \
  --pallet System \
  --function remark \
  --args "Hello World" \
  --use-wallet

# Sudo call (requires sudo privileges)
pop call chain \
  --pallet Balances \
  --function force_transfer \
  --args 5GrwvaEF... 5FHneW... 1000000000000 \
  --sudo \
  --use-wallet

# Call with raw SCALE bytes
pop call chain \
  --call 0x00000000... \
  --use-wallet

===========================================
WHAT IT DOES
===========================================

- Executes extrinsics on-chain
- Can call any pallet function
- Requires transaction signing
- Changes persisted on-chain

Common Pallets:
- System: Core chain functions (remark, set_code, etc.)
- Balances: Token transfers and management
- Timestamp: Time operations
- Sudo: Root-level operations (dev chains)
- Assets: Multi-asset management
- Staking: Validator staking operations

===========================================
KEY FLAGS EXPLAINED
===========================================

--pallet <PALLET>:
- Pallet name containing the function
- Must match exactly (case-sensitive)
- Examples: "System", "Balances", "Timestamp"

--function <FUNCTION>:
- Function name within pallet
- Must match exactly as in source code
- Examples: "remark", "transfer", "set_code"

--args [<ARGS>...]:
- Function arguments as strings
- Space-separated
- Encoded based on function signature
- Examples:
  - Address: 5GrwvaEF...
  - Balance: 1000000000000
  - String: "Hello"
  - Boolean: true, false

--url <URL>:
- Chain endpoint to call
- Default: local node
- Examples:
  - Local: ws://localhost:9944
  - Paseo Testnet: wss://paseo-rpc.dwellir.com
  - Kusama: wss://kusama-rpc.dwellir.com
  - Polkadot: wss://polkadot-rpc.dwellir.com

--suri <SURI>:
- Secret URI for signing account
- For local dev only!
- Examples: //Alice, //Bob, //Charlie
- With password: //Alice///MyPassword
- NEVER use on testnet/mainnet

--use-wallet:
- Use browser extension wallet
- ALWAYS use for testnet/mainnet
- More secure than --suri
- Prompts for signature
- Supports: polkadot-js, Talisman, SubWallet

--sudo (-S):
- Execute with Root origin
- Requires account to be sudo key
- Use for privileged operations:
  - Runtime upgrades
  - Force transfers
  - Governance overrides
- Only works on dev/test chains with sudo

--call <CALL_DATA>:
- Pre-encoded SCALE bytes
- Alternative to --pallet/--function/--args
- For advanced users
- Example: 0x00000000...

--skip-confirm (-y):
- Auto-sign without confirmation prompt
- Use for scripts/automation
- Be careful - no confirmation!

===========================================
EXAMPLES
===========================================

# System remark (no-op, stores data)
pop call chain \
  --pallet System \
  --function remark \
  --args "Hello from Pop CLI" \
  --use-wallet

# Transfer tokens
pop call chain \
  --pallet Balances \
  --function transfer_keep_alive \
  --args 5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty 1000000000000 \
  --use-wallet

# Sudo call (requires sudo key)
pop call chain \
  --pallet System \
  --function set_code \
  --args 0x... \
  --sudo \
  --suri //Alice

# Call on testnet
pop call chain \
  --url wss://paseo-rpc.dwellir.com \
  --pallet System \
  --function remark \
  --args "Testnet call" \
  --use-wallet

# Transfer all balance
pop call chain \
  --pallet Balances \
  --function transfer_all \
  --args 5FHneW... false \
  --use-wallet

# Batch multiple calls
pop call chain \
  --pallet Utility \
  --function batch \
  --args [[System,remark,"Call 1"],[System,remark,"Call 2"]] \
  --use-wallet

# Force transfer (sudo required)
pop call chain \
  --pallet Balances \
  --function force_transfer \
  --args 5GrwvaEF... 5FHneW... 1000000000000 \
  --sudo \
  --suri //Alice

# Auto-confirm (scripting)
pop call chain \
  --pallet System \
  --function remark \
  --args "Automated call" \
  --suri //Alice \
  --skip-confirm

===========================================
COMMON OPERATIONS
===========================================

Transfer Tokens:
```bash
pop call chain \
  --pallet Balances \
  --function transfer_keep_alive \
  --args <RECIPIENT> <AMOUNT> \
  --use-wallet
```

Check Balance (query storage):
```bash
pop call chain \
  --pallet System \
  --function Account \
  --args <ADDRESS> \
  --url wss://paseo-rpc.dwellir.com
```

Store Data On-Chain:
```bash
pop call chain \
  --pallet System \
  --function remark \
  --args "My data" \
  --use-wallet
```

Runtime Upgrade (sudo):
```bash
# Upload new runtime code
pop call chain \
  --pallet System \
  --function set_code \
  --args <RUNTIME_WASM_HEX> \
  --sudo \
  --suri //Alice
```

===========================================
WORKFLOW: GOVERNANCE PROPOSAL
===========================================

# For chains with democracy/governance

1. Submit proposal:
pop call chain \
  --pallet Democracy \
  --function propose \
  --args <PROPOSAL_HASH> <VALUE> \
  --use-wallet

2. Second proposal:
pop call chain \
  --pallet Democracy \
  --function second \
  --args <PROPOSAL_INDEX> \
  --use-wallet

3. Vote on referendum:
pop call chain \
  --pallet Democracy \
  --function vote \
  --args <REF_INDEX> <AYE_OR_NAY> <VOTE_VALUE> \
  --use-wallet

===========================================
WORKFLOW: ASSET MANAGEMENT
===========================================

# For chains with Assets pallet

Create Asset:
pop call chain \
  --pallet Assets \
  --function create \
  --args <ASSET_ID> <ADMIN_ACCOUNT> <MIN_BALANCE> \
  --use-wallet

Mint Asset:
pop call chain \
  --pallet Assets \
  --function mint \
  --args <ASSET_ID> <BENEFICIARY> <AMOUNT> \
  --use-wallet

Transfer Asset:
pop call chain \
  --pallet Assets \
  --function transfer \
  --args <ASSET_ID> <RECIPIENT> <AMOUNT> \
  --use-wallet

===========================================
TROUBLESHOOTING
===========================================

Error: "Pallet not found":
Solution: Check pallet name spelling
- Must match exactly (case-sensitive)
- Check runtime configuration
- Use interactive mode for autocomplete

Error: "Function not found":
Solution: Check function name
- Must match source code exactly
- Some functions may not be public
- Check pallet documentation

Error: "Invalid arguments":
Solution: Check argument types and count
- Review function signature
- Ensure correct number of arguments
- Check argument encoding

Error: "BadOrigin":
Solution: Check account permissions
- Some functions require sudo
- Use --sudo flag for Root origin
- Or use account with proper permissions

Error: "InsufficientBalance":
Solution: Fund calling account
- Local: Use dev accounts (have balance)
- Testnet: Get tokens from faucet
- Check balance before calling

Error: "Module" error:
Solution: Check you're calling correct network
- Some pallets only on specific chains
- Verify pallet is in runtime

Sudo call fails:
Solution:
- Only works on dev/test chains
- Account must be sudo key (usually //Alice on dev)
- Mainnet chains don't have sudo

===========================================
BEST PRACTICES
===========================================

1. Use --use-wallet for testnet/mainnet:
   - More secure than --suri
   - Never use dev accounts on production

2. Test on local before testnet:
   - Catch errors early
   - No cost for mistakes

3. Verify arguments before calling:
   - Wrong arguments can lead to lost funds
   - Use interactive mode for safety

4. Save transaction hashes:
   - For tracking and verification
   - Check on block explorer

5. Be careful with sudo:
   - Very powerful operations
   - Can break chain if misused
   - Only for dev/test environments

6. Use descriptive remarks:
   - Helps with debugging
   - Visible on block explorer
   - Track operations

===========================================
INTERACTIVE MODE
===========================================

For ease of use, run interactive mode:

```bash
pop call chain
```

Prompts for:
1. Chain URL (if remote)
2. Pallet name (with autocomplete)
3. Function name (with autocomplete)
4. Arguments (type-aware)
5. Sudo (if applicable)
6. Sign transaction

Interactive mode is recommended for:
- Exploring chain functions
- Testing during development
- Learning pallet interfaces
- Safety (validates inputs)

===========================================
QUERYING VS CALLING
===========================================

Querying Storage (read-only):
```bash
pop call chain \
  --pallet System \
  --function Account \
  --args <ADDRESS>
```
- Free (no transaction)
- Instant result
- No signing required

Calling Extrinsic (state-changing):
```bash
pop call chain \
  --pallet Balances \
  --function transfer \
  --args <RECIPIENT> <AMOUNT> \
  --use-wallet
```
- Costs transaction fee
- Requires signing
- Modifies chain state

===========================================
NEXT STEPS
===========================================

After calling chain:

# Verify transaction on block explorer
# Check event logs
# Query storage to confirm changes

# Common verification pattern:
# 1. Query initial state
pop call chain --pallet System --function Account --args <ADDR>

# 2. Execute transaction
pop call chain --pallet Balances --function transfer --args <TO> <AMT> --use-wallet

# 3. Query new state
pop call chain --pallet System --function Account --args <ADDR>

See: up/chain.txt, build/chain.txt
