Skip to main content

Environment Setup

Creating the Environment File

The CTF Exchange requires specific environment variables for deployment. Create a .env file in the root directory:
1

Copy the Template

Start by copying the example environment file:
cp .env.example .env
2

Configure Variables

Open .env and configure the required variables:
PK=your_private_key_here
ADMIN=0x_admin_address_here
RPC_URL=https://your_rpc_url_here
COLLATERAL=0x_collateral_token_address
CTF=0x_conditional_tokens_framework_address
PROXY_FACTORY=0x_proxy_factory_address
SAFE_FACTORY=0x_gnosis_safe_factory_address

Environment Variables Reference

PK
string
required
Private key of the deployer account. This account will deploy the exchange contract and will have initial admin privileges that are later renounced.
ADMIN
address
required
Address that will be granted admin and operator roles after deployment. This address will maintain control after the deployer renounces their roles.
RPC_URL
string
required
RPC endpoint for the target network (e.g., Polygon, Amoy testnet).
COLLATERAL
address
required
Address of the ERC20 collateral token (e.g., USDC) used for trading.
CTF
address
required
Address of the Conditional Tokens Framework (CTF) contract.
PROXY_FACTORY
address
required
Address of the Polymarket proxy factory contract.
SAFE_FACTORY
address
required
Address of the Gnosis Safe factory contract used for multisig support.
Never commit your .env file to version control. The file is already included in .gitignore to prevent accidental exposure of sensitive information.

Deployment Script

Understanding the Deployment Flow

The exchange deployment script is located at src/exchange/scripts/ExchangeDeployment.s.sol. Here’s how it works:
function deployExchange(
    address admin,
    address collateral,
    address ctf,
    address proxyFactory,
    address safeFactory
) public returns (address exchange) {
    vm.startBroadcast();

    // Deploy the CTF Exchange contract
    CTFExchange exch = new CTFExchange(
        collateral,
        ctf,
        proxyFactory,
        safeFactory
    );

    // Grant admin privileges to the admin address
    exch.addAdmin(admin);
    exch.addOperator(admin);

    // Revoke the deployer's authorization
    exch.renounceAdminRole();
    exch.renounceOperatorRole();

    exchange = address(exch);
}

Deployment Steps

1

Constructor Parameters

The exchange is initialized with five critical parameters:
new CTFExchange(
    collateral,    // ERC20 token for settlements
    ctf,           // Conditional Tokens Framework
    proxyFactory,  // Polymarket proxy factory
    safeFactory    // Gnosis Safe factory
)
2

Admin Role Assignment

The deployment script grants both admin and operator roles to the specified admin address:
  • addAdmin(admin) - Grants administrative privileges
  • addOperator(admin) - Grants operator privileges for trading operations
3

Deployer Role Renunciation

For security, the deployer immediately renounces all privileges:
  • renounceAdminRole() - Removes deployer’s admin access
  • renounceOperatorRole() - Removes deployer’s operator access
This ensures that only the designated admin address retains control over the exchange.

Running the Deployment

Local Deployment (Testing)

Test your deployment script on a local Anvil instance:
# Terminal 1: Start local node
anvil

Testnet Deployment

Deploy to a testnet (e.g., Amoy):
forge script src/exchange/scripts/ExchangeDeployment.s.sol:ExchangeDeployment \
  --rpc-url $RPC_URL \
  --private-key $PK \
  --broadcast \
  --verify
The --verify flag will automatically verify your contract on the block explorer if you have an API key configured.

Production Deployment

For mainnet deployment (e.g., Polygon), use additional safety flags:
forge script src/exchange/scripts/ExchangeDeployment.s.sol:ExchangeDeployment \
  --rpc-url $RPC_URL \
  --private-key $PK \
  --broadcast \
  --verify \
  --slow \
  --with-gas-price 50gwei
Always perform a dry run without --broadcast first to simulate the deployment and verify gas costs.

Current Deployments

The CTF Exchange is currently deployed on the following networks:

Polygon Mainnet

Address: 0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982EView on Polygonscan →

Amoy Testnet

Address: 0xdFE02Eb6733538f8Ea35D585af8DE5958AD99E40View on Amoy Explorer →

Configuration Options

Solidity Compiler Settings

The compiler is configured in foundry.toml for optimal gas efficiency:
solc = "0.8.15"
optimizer_runs = 1000000
  • Solidity Version: 0.8.15 (tested and audited)
  • Optimizer Runs: 1,000,000 for maximum runtime gas optimization

Gas Reporting

Gas reporting is enabled for all contracts:
gas_reports = ["*"]
Generate a gas report during testing:
forge test --gas-report

Fuzz Testing Profiles

Two fuzz testing profiles are available:
[profile.default.fuzz]
runs = 256
Run tests with the intense profile:
FOUNDRY_PROFILE=intense forge test

Post-Deployment Configuration

Registering Token Pairs

After deployment, token pairs must be registered before trading can begin:
// Admin must call registerToken for each outcome token pair
exchange.registerToken(
    token0,         // First outcome token ID
    token1,         // Complementary token ID
    conditionId     // Associated condition ID
);

Adding Operators

Additional operators can be added for order matching:
// Admin adds a new operator
exchange.addOperator(operatorAddress);

Pausing Trading

In emergency situations, trading can be paused:
// Admin pauses trading
exchange.pauseTrading();

// Resume trading when safe
exchange.unpauseTrading();

Security Considerations

The CTF Exchange has been audited by ChainSecurity. Review the audit report before deployment.

Private Key Security

Never share or commit private keys. Use hardware wallets for production deployments.

Admin Controls

The admin address has significant control. Consider using a multisig wallet.

Testing

Always test deployments on testnet before mainnet deployment.

Verification

Verify contract source code on block explorers for transparency.

Build docs developers (and LLMs) love