polymarket 0.1.0

Rust SDK for Polymarket prediction market - CLOB trading, on-chain operations, and WebSocket streaming
Documentation
use alloy::sol;

// Define all contract interfaces using alloy's sol! macro

sol! {
    /// ERC20 token interface (for USDC)
    #[sol(rpc)]
    interface IERC20 {
        /// Get token balance of an address
        function balanceOf(address owner) external view returns (uint256);

        /// Get token decimals
        function decimals() external view returns (uint8);

        /// Approve spending
        function approve(address spender, uint256 amount) external returns (bool);

        /// Check allowance
        function allowance(address owner, address spender) external view returns (uint256);

        /// Transfer tokens
        function transfer(address to, uint256 amount) external returns (bool);

        /// Transfer tokens from another address
        function transferFrom(address from, address to, uint256 amount) external returns (bool);

        /// Get token symbol
        function symbol() external view returns (string memory);

        /// Get token name
        function name() external view returns (string memory);

        /// Get total supply
        function totalSupply() external view returns (uint256);
    }
}

sol! {
    /// ERC1155 token interface (for CTF tokens)
    #[sol(rpc)]
    interface IERC1155 {
        /// Get token balance for a specific ID
        function balanceOf(address owner, uint256 id) external view returns (uint256);

        /// Get balances for multiple owners and IDs
        function balanceOfBatch(
            address[] calldata owners,
            uint256[] calldata ids
        ) external view returns (uint256[] memory);

        /// Set approval for all tokens
        function setApprovalForAll(address operator, bool approved) external;

        /// Check if operator is approved for all tokens
        function isApprovedForAll(address owner, address operator) external view returns (bool);

        /// Safe transfer a specific token
        function safeTransferFrom(
            address from,
            address to,
            uint256 id,
            uint256 amount,
            bytes calldata data
        ) external;

        /// Safe batch transfer multiple tokens
        function safeBatchTransferFrom(
            address from,
            address to,
            uint256[] calldata ids,
            uint256[] calldata amounts,
            bytes calldata data
        ) external;
    }
}

sol! {
    /// Conditional Tokens Framework - main contract for split/merge/redeem
    #[sol(rpc)]
    interface IConditionalTokens {
        /// Split collateral into outcome tokens
        function splitPosition(
            address collateralToken,
            bytes32 parentCollectionId,
            bytes32 conditionId,
            uint256[] calldata partition,
            uint256 amount
        ) external;

        /// Merge outcome tokens back into collateral
        function mergePositions(
            address collateralToken,
            bytes32 parentCollectionId,
            bytes32 conditionId,
            uint256[] calldata partition,
            uint256 amount
        ) external;

        /// Redeem positions after market resolution
        function redeemPositions(
            address collateralToken,
            bytes32 parentCollectionId,
            bytes32 conditionId,
            uint256[] calldata indexSets
        ) external;

        /// Get balance of a specific position
        function balanceOf(address owner, uint256 positionId) external view returns (uint256);

        /// Calculate position ID from collateral and collection
        function getPositionId(
            address collateralToken,
            bytes32 collectionId
        ) external pure returns (uint256);

        /// Get collection ID from parent, condition, and index
        function getCollectionId(
            bytes32 parentCollectionId,
            bytes32 conditionId,
            uint256 indexSet
        ) external view returns (bytes32);

        /// Get outcome slot count for a condition
        function getOutcomeSlotCount(bytes32 conditionId) external view returns (uint256);

        /// Check if payout has been received
        function payoutDenominator(bytes32 conditionId) external view returns (uint256);

        /// Get payout numerators for a condition
        function payoutNumerators(bytes32 conditionId, uint256 index) external view returns (uint256);
    }
}

sol! {
    /// Proxy Wallet Factory - for email/Magic accounts
    #[sol(rpc)]
    interface IProxyFactory {
        /// Proxy transaction struct
        struct ProxyTransaction {
            uint8 typeCode;
            address to;
            uint256 value;
            bytes data;
        }

        /// Execute batch of transactions through proxy
        function proxy(
            ProxyTransaction[] calldata calls
        ) external payable returns (bytes[] memory);

        /// Derive proxy wallet address from salt
        function deriveInstanceAddress(bytes32 salt) external view returns (address);

        /// Check if proxy wallet is deployed
        function isDeployed(bytes32 salt) external view returns (bool);
    }
}

sol! {
    /// Gnosis Safe interface
    #[sol(rpc)]
    interface ISafe {
        /// Execute a transaction from the Safe
        function execTransaction(
            address to,
            uint256 value,
            bytes calldata data,
            uint8 operation,
            uint256 safeTxGas,
            uint256 baseGas,
            uint256 gasPrice,
            address gasToken,
            address payable refundReceiver,
            bytes memory signatures
        ) external payable returns (bool success);

        /// Get transaction hash for signing
        function getTransactionHash(
            address to,
            uint256 value,
            bytes calldata data,
            uint8 operation,
            uint256 safeTxGas,
            uint256 baseGas,
            uint256 gasPrice,
            address gasToken,
            address refundReceiver,
            uint256 _nonce
        ) external view returns (bytes32);

        /// Get current nonce
        function nonce() external view returns (uint256);

        /// Get threshold (number of required signatures)
        function getThreshold() external view returns (uint256);

        /// Get list of owners
        function getOwners() external view returns (address[] memory);

        /// Check if address is an owner
        function isOwner(address owner) external view returns (bool);
    }
}

sol! {
    /// Safe Factory for deploying new Safe wallets
    #[sol(rpc)]
    interface ISafeFactory {
        /// Create a new Safe proxy
        function createProxyWithNonce(
            address _singleton,
            bytes memory initializer,
            uint256 saltNonce
        ) external returns (address proxy);

        /// Calculate proxy address
        function calculateCreateProxyWithNonceAddress(
            address _singleton,
            bytes calldata initializer,
            uint256 saltNonce
        ) external view returns (address proxy);
    }
}

sol! {
    /// Negative Risk Adapter - for neg risk markets
    #[sol(rpc)]
    interface INegRiskAdapter {
        /// Split position in a neg risk market
        function splitPosition(
            address collateralToken,
            bytes32 parentCollectionId,
            bytes32 conditionId,
            uint256[] calldata partition,
            uint256 amount
        ) external;

        /// Merge positions in a neg risk market
        function mergePositions(
            address collateralToken,
            bytes32 parentCollectionId,
            bytes32 conditionId,
            uint256[] calldata partition,
            uint256 amount
        ) external;
    }
}

sol! {
    /// CTF Exchange - for trading on Polymarket
    #[sol(rpc)]
    interface ICTFExchange {
        /// Fill an order
        function fillOrder(
            bytes calldata order,
            uint256 fillAmount,
            bytes calldata signature
        ) external;

        /// Fill multiple orders
        function fillOrders(
            bytes[] calldata orders,
            uint256[] calldata fillAmounts,
            bytes[] calldata signatures
        ) external;

        /// Cancel an order
        function cancelOrder(bytes32 orderHash) external;

        /// Cancel multiple orders
        function cancelOrders(bytes32[] calldata orderHashes) external;

        /// Check if order is filled
        function getOrderStatus(bytes32 orderHash) external view returns (uint256 filledAmount);

        /// Get maker's nonce
        function nonces(address maker) external view returns (uint256);
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_interfaces_compile() {
        // Just verify that all the sol! macros compile correctly
        // The actual contract instances would need a provider
    }
}