===========================================
POP UP CONTRACT
===========================================

Deploy a smart contract

Usage: pop up [OPTIONS] [PATH]

Arguments:
  [PATH]  Directory path without flag for your project [default: current directory]

Options:
      --path <PATH>  Path to the project directory
  -h, --help         Print help

Smart contract deployment options:
  -c, --constructor <CONSTRUCTOR>  The name of the contract constructor to call [default: new]
  -a, --args [<ARGS>...]           The constructor arguments, encoded as strings
  -v, --value <VALUE>              Transfers an initial balance to the instantiated contract
                                   [default: 0]
  -g, --gas <gas>                  Maximum amount of gas to be used for this command. If not
                                   specified it will perform a dry-run to estimate the gas consumed
                                   for the instantiation
  -P, --proof-size <PROOF_SIZE>    Maximum proof size for the instantiation. If not specified it
                                   will perform a dry-run to estimate the proof size required
  -S, --salt <SALT>                A salt used in the address derivation of the new contract. Use to
                                   create multiple instances of the same contract code from the same
                                   account
  -u, --url <URL>                  Websocket endpoint of a chain [default: ws://localhost:9944/]
  -s, --suri <SURI>                Secret key URI for the account deploying the contract
                                   e.g. - for a dev account "//Alice"
                                        - with a password "//Alice///SECRET_PASSWORD"
                                   [default: //Alice]
  -w, --use-wallet                 Use a browser extension wallet to sign the extrinsic
  -D, --dry-run                    Perform a dry-run via RPC to estimate the gas usage. This does
                                   not submit a transaction
  -U, --upload-only                Uploads the contract only, without instantiation
  -y, --skip-confirm               Automatically source or update the needed binary required without
                                   prompting for confirmation
      --skip-build                 Skip building the contract before deployment. If the contract is
                                   not built, it will be built regardless

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

# Deploy to local node (auto-launched)
cd my-contract
pop up --constructor new --args false --suri //Alice

# Deploy to testnet with wallet
pop up --url wss://testnet-passet-hub.polkadot.io --constructor new --args false --use-wallet

# Deploy ERC20 with constructor args
pop up --constructor new \
  --args "1000000" "MyToken" "MTK" "18" \
  --use-wallet

# Dry run (estimate gas)
pop up --constructor new --args false --dry-run --suri //Alice

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

Full Deployment (default):
1. Builds contract if not already built
2. Uploads contract bytecode to chain (code upload)
3. Calls constructor to instantiate contract
4. Returns contract address

Upload Only (--upload-only):
1. Builds contract if not already built
2. Uploads contract bytecode to chain
3. Returns code hash (not contract address)
4. No instantiation - use for:
   - Code verification
   - Multi-sig deployments
   - Separate upload and instantiate steps

Dry Run (--dry-run):
1. Simulates deployment via RPC
2. Estimates gas and proof size required
3. Does NOT submit transaction
4. Use before actual deployment

IMPORTANT: Save the contract address from output!

===========================================
DEPLOYMENT FLAGS EXPLAINED
===========================================

--constructor <NAME>:
- Name of constructor function to call
- Default: "new"
- Must match constructor name in contract
- Example: "new", "default", "init"

--args [<ARGS>...]:
- Constructor arguments as strings
- Space-separated
- Encoded automatically
- Match constructor signature order
- Examples:
  - Boolean: true, false
  - Number: 1000000
  - String: "MyToken"
  - Address: 5GrwvaEF...

--value <VALUE>:
- Initial balance to transfer to contract
- In smallest unit (plancks)
- Default: 0
- Use when contract needs initial funds
- Example: 1000000000000 (1 token with 12 decimals)

--gas <GAS>:
- Max gas limit for deployment
- Auto-estimated if not provided (dry-run)
- Set higher if deployment fails with OutOfGas
- Example: 100000000000

--proof-size <SIZE>:
- Max proof size for deployment
- Auto-estimated if not provided (dry-run)
- Substrate storage proof limit
- Example: 1000000

--salt <SALT>:
- Salt for deterministic address derivation
- Use to deploy multiple instances from same account
- Each salt creates unique address
- Example: "0x01", "0x02", "salt1"

--url <URL>:
- Chain endpoint to deploy to
- Default: ws://localhost:9944/
- Examples:
  - Local: ws://localhost:9944
  - PassetHub Testnet: wss://testnet-passet-hub.polkadot.io

--suri <SURI>:
- Secret URI for signing account
- Default: //Alice (dev account)
- Dev accounts: //Alice, //Bob, //Charlie
- With password: //Alice///MyPassword
- NEVER use dev accounts on production!

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

--dry-run:
- Simulate deployment without submitting
- Estimates gas and proof size
- Validates constructor args
- No transaction fee
- Use before actual deployment

--upload-only:
- Only upload contract code
- Does NOT instantiate contract
- Returns code hash instead of contract address
- Use cases:
  - Code verification
  - Multi-sig instantiation
  - Governance deployment
  - Separate upload and instantiate

--skip-confirm:
- Auto-source binaries without prompting
- Useful for CI/CD pipelines
- Automatically confirms downloads

--skip-build:
- Skip building contract before deployment
- Only if contract already built
- Will build if no artifacts found
- Saves time in repeated deployments

===========================================
NETWORKS
===========================================

Local (default):
  ws://localhost:9944
  - Auto-launches ink-node if not running
  - Dev accounts (//Alice) work
  - Fast iteration
  - Use for development

PassetHub Testnet (recommended for testing):
  wss://testnet-passet-hub.polkadot.io
  - Public testnet
  - Free testnet tokens from faucet
  - Use --use-wallet for security
  - Realistic environment

PassetHub Mainnet (production):
  wss://passet-hub.polkadot.io
  - Production network
  - Costs real tokens
  - ALWAYS use --use-wallet
  - Test on testnet first!

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

# Simple deployment to local
pop up --constructor new --args false --suri //Alice

# Deploy with browser wallet
pop up --use-wallet

# Deploy to testnet
pop up \
  --url wss://testnet-passet-hub.polkadot.io \
  --constructor new \
  --args "1000000" \
  --use-wallet

# Estimate gas first (dry-run)
pop up --dry-run --constructor new --args false --suri //Alice

# Deploy ERC20 token
pop up --constructor new \
  --args "1000000" "MyToken" "MTK" "18" \
  --suri //Alice

# Deploy with initial balance
pop up --constructor new \
  --args false \
  --value 1000000000000 \
  --suri //Alice

# Deploy multiple instances (same account)
pop up --constructor new --args false --salt "0x01" --suri //Alice
pop up --constructor new --args false --salt "0x02" --suri //Alice

# Upload only (multi-sig deployment)
pop up --upload-only --use-wallet

# Deploy with custom gas limit
pop up --constructor new \
  --args false \
  --gas 200000000000 \
  --proof-size 2000000 \
  --suri //Alice

# Skip build (already built)
pop up --skip-build --constructor new --args false --suri //Alice

# CI/CD deployment
pop up --constructor new \
  --args false \
  --skip-confirm \
  --suri "$DEPLOY_KEY"

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

Error: "contract already exists":
Solution: Use different --salt
pop up --constructor new --args false --salt "0x01"

Error: "OutOfGas":
Solution: Increase --gas limit
pop up --constructor new --args false --gas 200000000000

Error: "insufficient balance":
Solution: Fund account first
- Local: Accounts have balance by default
- Testnet: Get tokens from faucet

Error: "constructor not found":
Solution: Check constructor name in contract
pop up --constructor default  # if constructor named "default"

Error: "invalid argument":
Solution: Check constructor argument types and order
- Review contract constructor signature
- Ensure argument count matches

Error: "connection refused":
Solution: Check chain is running and URL is correct
- Local: Ensure ink-node is running
- Remote: Check network connectivity

Wallet doesn't popup:
Solution:
- Install polkadot-js, Talisman, or SubWallet extension
- Ensure extension is unlocked
- Check browser allows popups

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

1. Always dry-run first:
   - Estimates gas/proof-size
   - Validates constructor args
   - No transaction fee

2. Use --use-wallet for testnet/mainnet:
   - More secure than --suri
   - Never hardcode private keys

3. Save deployment info:
   - Contract address
   - Code hash
   - Constructor args
   - Network deployed to
   - Block number

4. Test on testnet before mainnet:
   - Catch issues early
   - No cost for mistakes

5. Verify constructor args:
   - Double-check values
   - Irreversible after deployment

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

After deployment, save contract address:

CONTRACT_ADDR="5CLPm..."

Then interact with contract:

# Read-only query
pop call contract \
  --contract $CONTRACT_ADDR \
  --message get \
  --use-wallet

# State-changing call
pop call contract \
  --contract $CONTRACT_ADDR \
  --message flip \
  --use-wallet \
  -x

See: call/contract.txt
