L1StandardBridge

Standard cross-chain bridge for base Ton tokens and Jetton tokens.

The L1StandardBridge is responsible for transferring TON and Jettons between L1 and L2. In the case that a Jetton is native to L1, it will be escrowed within this contract. If the Jetton is native to L2, it will be burnt. Before the introduction of Bedrock, TON was stored within this contract. After Bedrock, TON is instead stored inside the ParaTonPortal contract.

import "@stdlib/deploy";
import "@stdlib/ownable";

contract L1StandardBridge with Deployable, Ownable {
    // Contracts and configuration
    superchainConfig: Address; // Address of the SuperchainConfig contract
    messenger: Address; // Address of the CrossDomainMessenger contract
    otherBridge: Address; // Address of the corresponding bridge contract on L2
    owner: Address; // Address of the contract owner
    version: String; // Version string of the contract

    // Deposits mapping structure
    deposits: map<Int, Bool>;

    // Initial setup function
    // Initializes the contract with the given addresses
    init(superchainConfig: Address, messenger: Address, otherBridge: Address) {
        self.superchainConfig = superchainConfig;
        self.messenger = messenger;
        self.otherBridge = otherBridge;
        self.owner = sender(); // Initialize the owner as the sender of the deploy transaction
        self.deposits = emptyMap(); // Initialize the deposits map as empty
        self.version = "1.0.0"; // Set the contract version
    }

    // Function to check if the contract is paused
    // Returns false as a placeholder
    get fun paused(): Bool {
        return false; // Placeholder logic, replace with actual pause check
    }

    // Placeholder function to simulate sending a raw message
    // In a real implementation, this would send a message to another contract
    fun send_raw_message(_to: Address, _message: Cell, _flags: Int) {
        // This is a placeholder function and does not perform any operations
    }

    // Placeholder function for finalizing Ton withdrawal
    fun finalizeBridgeTon(_from: Address, _to: Address, _amount: Int, _extraData: Slice) {
        // This is a placeholder function and does not perform any operations
    }

    // Placeholder function for finalizing Jetton withdrawal
    fun finalizeBridgeJetton(_l1Token: Address, _l2Token: Address, _from: Address, _to: Address, _amount: Int, _extraData: Slice) {
        // This is a placeholder function and does not perform any operations
    }

    // Function to finalize Ton withdrawal from L2 to L1
    receive("finalizeTonWithdrawal") {
        let _from: Address = sender();
        let _to: Address = sender();
        let _amount: Int = 0;
        let _extraData: Slice = beginCell().endCell().beginParse();

        // Finalize the Ton withdrawal
        self.finalizeBridgeTon(_from, _to, _amount, _extraData);

        // Emit the TonWithdrawalFinalized event
        let event_data: Cell = beginCell()
            .storeAddress(_from)
            .storeAddress(_to)
            .storeUint(_amount, 256)
            .storeSlice(_extraData)
            .endCell();
        emit(event_data);
    }

    // Function to finalize Jetton withdrawal from L2 to L1
    receive("finalizeJettonWithdrawal") {
        let _l1Token: Address = sender();
        let _l2Token: Address = sender();
        let _from: Address = sender();
        let _to: Address = sender();
        let _amount: Int = 0;
        let _extraData: Slice = beginCell().endCell().beginParse();

        // Finalize the Jetton withdrawal
        self.finalizeBridgeJetton(_l1Token, _l2Token, _from, _to, _amount, _extraData);

        // Emit the JettonWithdrawalFinalized event
        let event_data: Cell = beginCell()
            .storeAddress(_l1Token)
            .storeAddress(_l2Token)
            .storeAddress(_from)
            .storeAddress(_to)
            .storeUint(_amount, 256)
            .storeSlice(_extraData)
            .endCell();
        emit(event_data);
    }

    // Function to initiate Ton deposit from L1 to L2
    receive("depositTon") {
        let _minGasLimit: Int = 0;
        let _extraData: Slice = beginCell().endCell().beginParse();

        // Initiate the Ton deposit
        self._initiateTonDeposit(sender(), sender(), _minGasLimit, _extraData);
    }

    // Function to initiate Jetton deposit from L1 to L2
    receive("depositJetton") {
        let _l1Token: Address = sender();
        let _l2Token: Address = sender();
        let _amount: Int = 0;
        let _minGasLimit: Int = 0;
        let _extraData: Slice = beginCell().endCell().beginParse();

        // Initiate the Jetton deposit
        self._initiateJettonDeposit(_l1Token, _l2Token, sender(), sender(), _amount, _minGasLimit, _extraData);
    }

    // Internal function for initiating an Ton deposit
    fun _initiateTonDeposit(_from: Address, _to: Address, _minGasLimit: Int, _extraData: Slice) {
        self._initiateBridgeTon(_from, _to, 0, _minGasLimit, _extraData);
    }

    // Internal function for initiating an Jetton deposit
    fun _initiateJettonDeposit(
        _l1Token: Address,
        _l2Token: Address,
        _from: Address,
        _to: Address,
        _amount: Int,
        _minGasLimit: Int,
        _extraData: Slice
    ) {
        self._initiateBridgeJetton(_l1Token, _l2Token, _from, _to, _amount, _minGasLimit, _extraData);
    }

    // Placeholder function for initiating Ton bridge
    fun _initiateBridgeTon(_from: Address, _to: Address, _amount: Int, _minGasLimit: Int, _extraData: Slice) {
        // This is a placeholder function and does not perform any operations
    }

    // Placeholder function for initiating Jetton bridge
    fun _initiateBridgeJetton(
        _l1Token: Address,
        _l2Token: Address,
        _from: Address,
        _to: Address,
        _amount: Int,
        _minGasLimit: Int,
        _extraData: Slice
    ) {
        // This is a placeholder function and does not perform any operations
    }

    // Function to emit TonBridgeInitiated event
    fun _emitTonBridgeInitiated(
        _from: Address,
        _to: Address,
        _amount: Int,
        _extraData: Slice
    ) {
        let event_data: Cell = beginCell()
            .storeAddress(_from)
            .storeAddress(_to)
            .storeUint(_amount, 256)
            .storeSlice(_extraData)
            .endCell();
        emit(event_data);
    }

    // Function to emit JettonBridgeInitiated event
    fun _emitJettonBridgeInitiated(
        _localToken: Address,
        _remoteToken: Address,
        _from: Address,
        _to: Address,
        _amount: Int,
        _extraData: Slice
    ) {
        let event_data: Cell = beginCell()
            .storeAddress(_localToken)
            .storeAddress(_remoteToken)
            .storeAddress(_from)
            .storeAddress(_to)
            .storeUint(_amount, 256)
            .storeSlice(_extraData)
            .endCell();
        emit(event_data);
    }

    // Function to emit TonBridgeFinalized event
    fun _emitTonBridgeFinalized(
        _from: Address,
        _to: Address,
        _amount: Int,
        _extraData: Slice
    ) {
        let event_data: Cell = beginCell()
            .storeAddress(_from)
            .storeAddress(_to)
            .storeUint(_amount, 256)
            .storeSlice(_extraData)
            .endCell();
        emit(event_data);
    }

    // Function to emit JettonBridgeFinalized event
    fun _emitJettonBridgeFinalized(
        _localToken: Address,
        _remoteToken: Address,
        _from: Address,
        _to: Address,
        _amount: Int,
        _extraData: Slice
    ) {
        let event_data: Cell = beginCell()
            .storeAddress(_localToken)
            .storeAddress(_remoteToken)
            .storeAddress(_from)
            .storeAddress(_to)
            .storeUint(_amount, 256)
            .storeSlice(_extraData)
            .endCell();
        emit(event_data);
    }
}

Last updated