328 lines
9.9 KiB
Plaintext
328 lines
9.9 KiB
Plaintext
#pragma version ^0.4.1;
|
|
#include "../imports/stdlib.fc";
|
|
;; ============================================================================
|
|
;;
|
|
;; TONFi Protocol Constants
|
|
;; Defines configuration parameters, error codes, and system constants
|
|
;;
|
|
;; ============================================================================
|
|
|
|
;; ============================================================================
|
|
;; System Constants
|
|
;; ============================================================================
|
|
|
|
;; Protocol Version
|
|
const int PROTOCOL_VERSION = 1;
|
|
const int MIN_SUPPORTED_VERSION = 1;
|
|
|
|
;; Network IDs
|
|
const int CHAIN_TON = 1;
|
|
const int CHAIN_ETH = 2;
|
|
const int CHAIN_BSC = 3;
|
|
|
|
;; Gas Constants
|
|
const int GAS_FOR_SWAP = 10000000; ;; 0.01 TON
|
|
const int GAS_FOR_BRIDGE = 50000000; ;; 0.05 TON
|
|
const int MIN_TON_FOR_STORAGE = 1000000; ;; 0.001 TON
|
|
|
|
;; ============================================================================
|
|
;; AMM Constants
|
|
;; ============================================================================
|
|
|
|
;; Fee Configuration
|
|
const int FEE_DENOMINATOR = 1000;
|
|
const int DEFAULT_SWAP_FEE = 30; ;; 3%
|
|
const int MAX_SWAP_FEE = 20; ;; 2%
|
|
const int PROTOCOL_FEE_SHARE = 5; ;; 0.5%
|
|
|
|
;; Liquidity Constants
|
|
const int MIN_LIQUIDITY = 1000; ;; Minimum LP tokens to mint
|
|
const int MAX_LIQUIDITY = 1000000000000; ;; Maximum LP tokens (1 trillion)
|
|
|
|
|
|
;; Slippage Protection
|
|
const int DEFAULT_SLIPPAGE = 50; ;; 0.5%
|
|
const int MAX_SLIPPAGE = 1000; ;; 10%
|
|
const int PRICE_IMPACT_WARNING = 100; ;; 1% price impact warning
|
|
const int MAX_PRICE_IMPACT = 2000; ;; 20% maximum price impact
|
|
|
|
;; Pool Constraints
|
|
const int MIN_POOL_AMOUNT = 1000000000; ;; 1 TON minimum pool deposit
|
|
const int MAX_POOL_IMBALANCE = 10000; ;; 100x maximum pool imbalance ratio
|
|
|
|
;; ============================================================================
|
|
;; Bridge Constants
|
|
;; ============================================================================
|
|
|
|
;; Validator Configuration
|
|
const int MIN_VALIDATORS = 3;
|
|
const int MAX_VALIDATORS = 21;
|
|
const int DEFAULT_THRESHOLD = 67; ;; 67% consensus required
|
|
|
|
;; Bridge Limits
|
|
const int MIN_BRIDGE_AMOUNT = 1000000000; ;; 1 TON minimum bridge amount
|
|
const int MAX_BRIDGE_AMOUNT = 1000000000000; ;; 1000 TON maximum bridge amount
|
|
const int BRIDGE_LOCK_PERIOD = 3600; ;; 1 hour lock period
|
|
const int BRIDGE_COOLDOWN = 300; ;; 5 minutes between transfers
|
|
|
|
;; Bridge Fees
|
|
const int BASE_BRIDGE_FEE = 500000000; ;; 0.5 TON base fee
|
|
const int BRIDGE_FEE_DENOMINATOR = 10000;
|
|
const int BRIDGE_FEE_NUMERATOR = 30; ;; 0.3% bridge fee
|
|
|
|
;; ============================================================================
|
|
;; Router Constants
|
|
;; ============================================================================
|
|
|
|
;; Route Configuration
|
|
const int MAX_HOPS = 3; ;; Maximum number of swaps in route
|
|
const int PATH_DEADLINE = 300; ;; Route valid for 5 minutes
|
|
const int MIN_OUTPUT_AMOUNT = 1000000; ;; 0.001 TON minimum output
|
|
|
|
;; ============================================================================
|
|
;; Error Codes
|
|
;; ============================================================================
|
|
|
|
;; Common Errors (100-199)
|
|
const int error::unauthorized = 401;
|
|
const int error::expired = 101;
|
|
const int error::invalid_amount = 102;
|
|
const int error::insufficient_balance = 103;
|
|
const int error::invalid_address = 104;
|
|
const int error::invalid_operation = 105;
|
|
|
|
;; Pool Errors (200-299)
|
|
const int error::insufficient_liquidity = 200;
|
|
const int error::invalid_token = 201;
|
|
const int error::excessive_slippage = 202;
|
|
const int error::price_impact_too_high = 203;
|
|
const int error::pool_imbalanced = 204;
|
|
const int error::min_amount_not_met = 205;
|
|
const int error::pool_not_found = 404;
|
|
const int error::paused = 403;
|
|
const int error::reentrancy = 405;
|
|
const int error::insufficient_amount = 406;
|
|
|
|
;; Bridge Errors (300-399)
|
|
const int error::insufficient_validators = 300;
|
|
const int error::invalid_signature = 301;
|
|
const int error::transfer_locked = 302;
|
|
const int error::bridge_limit_exceeded = 303;
|
|
const int error::cooldown_active = 304;
|
|
const int error::invalid_chain = 305;
|
|
|
|
;; Router Errors (400-499)
|
|
const int error::invalid_path = 400;
|
|
const int error::excessive_hops = 401;
|
|
const int error::no_route_found = 402;
|
|
const int error::route_too_expensive = 403;
|
|
|
|
;; ============================================================================
|
|
;; Operation Codes
|
|
;; ============================================================================
|
|
|
|
;; Pool Operations
|
|
const int op::swap = 1;
|
|
const int op::add_liquidity = 2;
|
|
const int op::remove_liquidity = 3;
|
|
const int op::collect_fees = 4;
|
|
const int op::update_fee = 5;
|
|
|
|
;; Bridge Operations
|
|
const int op::lock = 100;
|
|
const int op::unlock = 101;
|
|
const int op::verify_transfer = 102;
|
|
const int op::add_validator = 103;
|
|
const int op::remove_validator = 104;
|
|
|
|
;; Router Operations
|
|
const int op::swap_exact_tokens = 200;
|
|
const int op::swap_tokens_exact = 201;
|
|
const int op::quote = 202;
|
|
const int op::build_route = 203;
|
|
|
|
;; Admin Operations
|
|
const int op::upgrade = 900;
|
|
const int op::pause = 901;
|
|
const int op::unpause = 902;
|
|
const int op::set_parameter = 903;
|
|
|
|
;; ============================================================================
|
|
;; Getter Methods
|
|
;; ============================================================================
|
|
|
|
;; Define missing constants
|
|
const int BRIDGE_FEE = 1000;
|
|
const int MIN_LOCK_AMOUNT = 10;
|
|
const int MAX_LOCK_AMOUNT = 10000;
|
|
|
|
;; Get AMM configuration
|
|
(int, int, int) get_amm_config() method_id {
|
|
return (
|
|
DEFAULT_SWAP_FEE,
|
|
MIN_LIQUIDITY,
|
|
PROTOCOL_FEE_SHARE
|
|
);
|
|
}
|
|
|
|
;; Get bridge configuration
|
|
(int, int, int) get_bridge_config() method_id {
|
|
return (
|
|
BRIDGE_FEE,
|
|
MIN_LOCK_AMOUNT,
|
|
MAX_LOCK_AMOUNT
|
|
);
|
|
}
|
|
|
|
;; Get router configuration
|
|
(int, int, int) get_router_config() method_id {
|
|
return (
|
|
MAX_HOPS,
|
|
PATH_DEADLINE,
|
|
MIN_OUTPUT_AMOUNT
|
|
);
|
|
}
|
|
|
|
;; Get error message
|
|
slice get_error_message(int error_code) method_id {
|
|
;; Common errors
|
|
if (error_code == error::unauthorized) {
|
|
return "Unauthorized operation";
|
|
}
|
|
if (error_code == error::expired) {
|
|
return "Operation expired";
|
|
}
|
|
if (error_code == error::invalid_amount) {
|
|
return "Invalid amount specified";
|
|
}
|
|
|
|
;; Pool errors
|
|
if (error_code == error::insufficient_liquidity) {
|
|
return "Insufficient liquidity";
|
|
}
|
|
if (error_code == error::price_impact_too_high) {
|
|
return "Price impact too high";
|
|
}
|
|
|
|
;; Bridge errors
|
|
if (error_code == error::insufficient_validators) {
|
|
return "Not enough validator signatures";
|
|
}
|
|
if (error_code == error::bridge_limit_exceeded) {
|
|
return "Bridge limit exceeded";
|
|
}
|
|
|
|
;; Router errors
|
|
if (error_code == error::no_route_found) {
|
|
return "No valid route found";
|
|
}
|
|
if (error_code == error::excessive_hops) {
|
|
return "Too many hops in route";
|
|
}
|
|
|
|
return "Unknown error";
|
|
}
|
|
|
|
;; ============================================================================
|
|
;; Helper Functions
|
|
;; ============================================================================
|
|
|
|
;; Convert percentage to basis points
|
|
int to_basis_points(int percentage) inline {
|
|
return percentage * 100;
|
|
}
|
|
|
|
;; Convert basis points to percentage
|
|
int from_basis_points(int basis_points) inline {
|
|
return basis_points / 100;
|
|
}
|
|
|
|
;; Calculate protocol fee
|
|
|
|
;; Calculate bridge fee
|
|
int calculate_bridge_fee(int amount) inline {
|
|
return max(
|
|
BASE_BRIDGE_FEE,
|
|
muldiv(amount, BRIDGE_FEE_NUMERATOR, BRIDGE_FEE_DENOMINATOR)
|
|
);
|
|
}
|
|
;; ============================================================================
|
|
;; Helper functions
|
|
;; ============================================================================
|
|
|
|
slice get_current_slice() inline {
|
|
slice cs = get_data().begin_parse(); ;; Get the current message data
|
|
return cs;
|
|
}
|
|
|
|
;; ============================================================================
|
|
;; Protocol Constants
|
|
;; ============================================================================
|
|
|
|
const int PROTOCOL_VERSION = 1;
|
|
const int MIN_SUPPORTED_VERSION = 1;
|
|
|
|
;; Network IDs
|
|
const int CHAIN_TON = 1;
|
|
const int CHAIN_ETH = 2;
|
|
const int CHAIN_BSC = 3;
|
|
|
|
;; Gas Constants
|
|
const int GAS_FOR_SWAP = 10000000; ;; 0.01 TON
|
|
const int GAS_FOR_BRIDGE = 50000000; ;; 0.05 TON
|
|
const int MIN_TON_FOR_STORAGE = 1000000; ;; 0.001 TON
|
|
|
|
;; Liquidity Constants
|
|
const int MIN_LIQUIDITY = 1000;
|
|
const int MAX_LIQUIDITY = 1000000000000;
|
|
|
|
;; Fee Configuration
|
|
const int FEE_DENOMINATOR = 1000;
|
|
;;const int DEFAULT_SWAP_FEE = 3; ;; 0.3%
|
|
const int MAX_SWAP_FEE = 20; ;; 2%
|
|
;;const int PROTOCOL_FEE_SHARE = 166667; ;; 1/6 of fees (≈16.67%)
|
|
|
|
;; Time Constants
|
|
const int HOUR = 3600;
|
|
const int DAY = 86400;
|
|
const int WEEK = 604800;
|
|
|
|
;; ============================================================================
|
|
;; Error Codes
|
|
;; ============================================================================
|
|
|
|
;; Common Errors (100-199)
|
|
;;const int error::unauthorized = 100;
|
|
const int error::expired = 101;
|
|
const int error::invalid_amount = 102;
|
|
const int error::insufficient_balance = 103;
|
|
|
|
;; Pool Errors (200-299)
|
|
const int error::insufficient_liquidity = 200;
|
|
const int error::invalid_token = 201;
|
|
const int error::price_impact_too_high = 203;
|
|
|
|
;; Bridge Errors (300-399)
|
|
const int error::insufficient_validators = 300;
|
|
const int error::invalid_signature = 301;
|
|
const int error::bridge_limit_exceeded = 303;
|
|
|
|
;; ============================================================================
|
|
;; Operation Codes
|
|
;; ============================================================================
|
|
|
|
const int op::swap = 1;
|
|
const int op::add_liquidity = 2;
|
|
const int op::remove_liquidity = 3;
|
|
const int op::collect_fees = 4;
|
|
|
|
;; Bridge Operations
|
|
const int op::lock = 100;
|
|
const int op::unlock = 101;
|
|
|
|
|
|
|
|
;; Required main procedure for compilation
|
|
() main() {
|
|
;; Main function for constants
|
|
} |