This is the TON blockchain (FunC) implementation of the DeFAI Staking contract, transpiled from the original Solana Rust implementation.
The DeFAI Staking contract enables users to stake DEFAI tokens and earn rewards based on a tiered system. The contract has been adapted for TON's architecture while maintaining the core functionality and security features of the original Solana implementation.
- defai_staking.fc - Core staking contract with all business logic
- defai_jetton_handler.fc - Jetton (TON's fungible token standard) integration layer
- deploy.fif - Deployment script for the contract
- tests.fc - Comprehensive test suite
The contract implements three staking tiers with different APY rates:
- Gold Tier: 10M - 99.99M DEFAI (0.5% APY)
- Titanium Tier: 100M - 999.99M DEFAI (0.75% APY)
- Infinite Tier: 1B+ DEFAI (1% APY)
- Stake Tokens: Users can stake DEFAI tokens to earn rewards
- Unstake Tokens: Withdraw staked tokens with time-based penalties
- Claim Rewards: Claim accumulated rewards from the escrow
- Compound Rewards: Reinvest rewards to increase stake
- Fund Escrow: Add tokens to the reward pool
- Initialize: Set up the contract with initial parameters
- Propose Authority Change: Propose new admin with 48-hour timelock
- Accept Authority Change: Execute pending authority change after timelock
- Pause/Unpause: Emergency pause functionality
- Timelock: 48-hour delay for critical admin actions
- Lock Period: 7-day initial lock on staked tokens
- Penalty System: Early unstaking penalties (2% < 30 days, 1% < 90 days)
- Pause Mechanism: Emergency pause for security incidents
The contract uses an optimized storage structure:
- Main contract state (158 bits): initialized, paused, total users, total staked
- Authority addresses (512 bits): current and pending authority
- Escrow data (248 bits): balance and distribution tracking
- User stakes: Stored in a dictionary with comprehensive stake data
Standard operations:
0x5fcc3d14- Initialize0x6d69747a- Stake0x756e7374- Unstake0x636c616d- Claim rewards0x636f6d70- Compound rewards
Jetton operations:
0xf8a7ea5- Jetton transfer0x7362d09c- Transfer notification0x53544b45- Stake notification
101- Not initialized102- Already initialized103- Unauthorized104- Amount too low105- Insufficient stake106- Tokens locked107- No rewards108- Program paused109- Insufficient escrow
- TON development environment (func, fift)
- DEFAI jetton contract deployed
- TON wallet with funds for deployment
- Compile the FunC contracts:
func -o defai_staking.fif -SPA defai_staking.fc
func -o defai_jetton_handler.fif -SPA defai_jetton_handler.fc- Run the deployment script:
fift -s deploy.fif 0 <jetton-wallet-address> defai-staking- Send deployment transaction:
lite-client -C "sendfile defai-staking-deploy.boc"- Initialize the contract:
lite-client -C "sendfile defai-staking-init.boc"Run the test suite:
func -o tests.fif -SPA tests.fc
fift -s tests.fifUsers interact with the contract through jetton transfers with specific forward payloads:
- Staking: Send DEFAI tokens with stake notification
- Unstaking: Send unstake request message
- Claiming: Send claim request message
The contract provides getter methods for querying:
get_contract_state()- Overall contract statusget_user_stake_info(address)- User's stake detailsget_pending_rewards(address)- Unclaimed rewardsget_escrow_info()- Escrow balance and distribution
The TON implementation includes several optimizations:
- Efficient storage packing
- Optimized arithmetic operations using
muldiv - Minimal message passing
- Dictionary-based user data storage
Key differences from the Solana implementation:
- Account Model: TON uses smart contracts instead of Solana's account model
- Token Standard: Jettons instead of SPL tokens
- Message Passing: Asynchronous message-based instead of CPI
- Storage: Cell-based storage with dictionaries
- Time: Unix timestamps instead of slot-based time
- Reentrancy: Protected through TON's message ordering
- Integer Overflow: Using safe arithmetic operations
- Access Control: Role-based with address verification
- Timelock: Enforced for critical operations
- Emergency Pause: Available for security incidents
Based on the original Solana audit, the following have been addressed:
- Proper authority validation
- Escrow balance tracking
- Penalty calculations
- Reward distribution logic
- Emergency mechanisms
This implementation maintains the same license as the original Solana contract.
For issues or questions about the TON implementation:
- Review the test suite for usage examples
- Check the original Solana implementation for business logic
- Consult TON documentation for platform-specific features