Skip to content
Verified contractPartialMIT

SwayBridge

Address0xae3f60bd56613dff8e4493c14d70807ee974a3f0
Balance

0 ETH

 

Transactions

536

 

Compiler

solc 0.8.19

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.19+commit.7dd6d404
Language
solidity
Optimization
No
EVM version
paris
License
MIT
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
20,025,189
Creation transaction
0x9845747236…71fccadfc6

/contracts/sway/SwayBridge.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/utils/Context.sol";
import "../interfaces/IStarknetCore.sol";
import "../interfaces/ISwayToken.sol";

contract SwayBridge is Context {
  ISwayToken public l1TokenContract;
  IStarknetCore public starknetCore;
  uint256 public l2BridgeContract;

  uint256 constant DEPOSIT_SELECTOR =
    1285101517810983806491589552491143496277809242732141897358598292095611420389; // handle_deposit
  uint256 constant CAIRO_PRIME =
    3618502788666131213697322783095070105623107215331596699973092056135872020481;
  uint256 constant BRIDGE_MODE_WITHDRAW = 1; // operation ID sent in the L2 -> L1 message

  event LogDeposit(address indexed l1Sender, uint256 amount, uint256 l2Recipient);
  event LogWithdrawal(address indexed l1Recipient, uint256 amount);

  function splitUint256(uint256 value) internal pure returns (uint256, uint256) {
    uint256 low = value & ((1 << 128) - 1);
    uint256 high = value >> 128;
    return (low, high);
  }

  constructor(address _starknetCore, address _l1TokenContract, uint256 _l2BridgeContract) {
    require(_l2BridgeContract < CAIRO_PRIME, "Bridge/invalid-dest-bridge");

    starknetCore = IStarknetCore(_starknetCore);
    l1TokenContract = ISwayToken(_l1TokenContract);
    l2BridgeContract = _l2BridgeContract;
  }

  /**
    Function used to bridge SWAY from L1 to L2
    @param amount How many SWAY to send from msg.sender
    @param l2Recipient To which L2 address should we deposit the SWAY to
  */
  function deposit(uint256 amount, uint256 l2Recipient) external payable {
      require(amount > 0, "Bridge/zero-amount");
      require(
          l2Recipient != 0 && l2Recipient != l2BridgeContract && l2Recipient < CAIRO_PRIME,
          "Bridge/invalid-recipient"
      );

      uint256[] memory payload = new uint256[](4);
      payload[0] = l2Recipient;
      (payload[1], payload[2]) = splitUint256(amount);
      payload[3] = uint256(uint160(_msgSender()));

      // Store the sender with the nonce in case we need to cancel
      l1TokenContract.transferFrom(msg.sender, address(this), amount);
      starknetCore.sendMessageToL2{value: msg.value}(l2BridgeContract, DEPOSIT_SELECTOR, payload);

      emit LogDeposit(msg.sender, amount, l2Recipient);
  }

  /**
    Function to process the L2 withdrawal
    @param amount How many SWAY were sent from L2
    @param l1Recipient Recipient of the bridged SWAY
  */
  function withdraw(uint256 amount, address l1Recipient) external {
      uint256[] memory payload = new uint256[](4);
      payload[0] = BRIDGE_MODE_WITHDRAW;
      payload[1] = uint256(uint160(l1Recipient));
      (payload[2], payload[3]) = splitUint256(amount);

      // The call to consumeMessageFromL2 will succeed only if a
      // matching L2->L1 message exists and is ready for consumption.
      starknetCore.consumeMessageFromL2(l2BridgeContract, payload);
      l1TokenContract.transfer(l1Recipient, amount);

      emit LogWithdrawal(l1Recipient, amount);
  }

  /**
    Cancels the bridging request from L1 -> L2
    @param amount How many SWAY were sent from L1
    @param l2Recipient Recipient of the bridged SWAY
    @param nonce Nonce of the L1 -> L2 message
  */
  function startDepositCancellation(uint256 amount, uint256 l2Recipient, uint256 nonce) external {
    // build payload
    uint256[] memory payload = new uint256[](4);
    payload[0] = l2Recipient;
    (payload[1], payload[2]) = splitUint256(amount);
    payload[3] = uint256(uint160(_msgSender())); // ensures only original sender can cancel

    // start cancellation process
    starknetCore.startL1ToL2MessageCancellation(l2BridgeContract, DEPOSIT_SELECTOR, payload, nonce);
  }

  /**
    Finishes the cancellationbridging request from L1 -> L2
    @param amount How many SWAY were sent from L1
    @param l2Recipient Recipient of the bridged SWAY
    @param nonce Nonce of the L1 -> L2 message
  */
  function finishDepositCancellation(uint256 amount, uint256 l2Recipient, uint256 nonce) external {
    // build payload
    uint256[] memory payload = new uint256[](4);
    payload[0] = l2Recipient;
    (payload[1], payload[2]) = splitUint256(amount);
    payload[3] = uint256(uint160(_msgSender())); // ensures only original sender can cancel

    // finish cancellation process
    starknetCore.cancelL1ToL2Message(l2BridgeContract, DEPOSIT_SELECTOR, payload, nonce);

    // Transfer back the SWAY tokens
    l1TokenContract.transfer(_msgSender(), amount);
  }
}

/contracts/interfaces/IStarknetCore.sol

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

interface IStarknetCore {
    // This event needs to be compatible with the one defined in Output.sol.
    event LogMessageToL1(uint256 indexed fromAddress, address indexed toAddress, uint256[] payload);

    // An event that is raised when a message is sent from L1 to L2.
    event LogMessageToL2(
        address indexed fromAddress,
        uint256 indexed toAddress,
        uint256 indexed selector,
        uint256[] payload,
        uint256 nonce
    );

    // An event that is raised when a message from L2 to L1 is consumed.
    event ConsumedMessageToL1(
        uint256 indexed fromAddress,
        address indexed toAddress,
        uint256[] payload
    );

    // An event that is raised when a message from L1 to L2 is consumed.
    event ConsumedMessageToL2(
        address indexed fromAddress,
        uint256 indexed toAddress,
        uint256 indexed selector,
        uint256[] payload,
        uint256 nonce
    );

    // An event that is raised when a message from L1 to L2 Cancellation is started.
    event MessageToL2CancellationStarted(
        address indexed fromAddress,
        uint256 indexed toAddress,
        uint256 indexed selector,
        uint256[] payload,
        uint256 nonce
    );

    // An event that is raised when a message from L1 to L2 is canceled.
    event MessageToL2Canceled(
        address indexed fromAddress,
        uint256 indexed toAddress,
        uint256 indexed selector,
        uint256[] payload,
        uint256 nonce
    );

    /**
      Sends a message to an L2 contract.

      Returns the hash of the message.
    */
    function sendMessageToL2(
        uint256 toAddress,
        uint256 selector,
        uint256[] calldata payload
    ) external payable returns (bytes32, uint256);

    /**
      Consumes a message that was sent from an L2 contract.

      Returns the hash of the message.
    */
    function consumeMessageFromL2(uint256 fromAddress, uint256[] calldata payload)
        external
        returns (bytes32);

    /**
      Starts the cancellation of an L1 to L2 message.
      A message can be canceled messageCancellationDelay() seconds after this function is called.

      Note: This function may only be called for a message that is currently pending and the caller
      must be the sender of the that message.
    */
    function startL1ToL2MessageCancellation(
        uint256 toAddress,
        uint256 selector,
        uint256[] calldata payload,
        uint256 nonce
    ) external returns (bytes32);

    /**
      Cancels an L1 to L2 message, this function should be called messageCancellationDelay() seconds
      after the call to startL1ToL2MessageCancellation().
    */
    function cancelL1ToL2Message(
        uint256 toAddress,
        uint256 selector,
        uint256[] calldata payload,
        uint256 nonce
    ) external returns (bytes32);

    // Message registry
    function l2ToL1Messages(bytes32 msgHash) external view returns (uint256);
}

/contracts/interfaces/ISwayToken.sol

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;


interface ISwayToken {

  function currentPeriod() external view returns (uint256);

  function decimals() external returns (uint8);

  function mint(address to, uint256 amount) external;

  function totalSupply() external returns (uint256);

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

  function transferFrom(address from, address to, uint256 value) external returns (bool success);
}

@openzeppelin/contracts/utils/Context.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)

pragma solidity ^0.8.0;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}
Compiler settingsShow
{
  "metadata": {
    "bytecodeHash": "ipfs"
  },
  "libraries": {},
  "optimizer": {
    "runs": 200,
    "enabled": false
  },
  "evmVersion": "paris",
  "remappings": []
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.