CosmicDeFi

Monad,Sui and Sepolia Extension of 1inch Fusion+ swap. We made custom relayer and db.

CosmicDeFi

Created At

Unite Defi

Project Description

Cosmic DeFi is a custom-built, decentralized finance (DeFi) project that provides a cross-chain trading infrastructure for creating and filling limit orders. The core inspiration for the project comes from the 1inch ecosystem, and it leverages a modified version of the @1inch/cross-chain-sdk and LimitOrderProtocol.

The primary goal of Cosmic DeFi is to enable users to create off-chain, signed limit orders that can be securely and permissionlessly filled by a network of "resolvers." This system is designed to be trust-minimized, meaning it reduces the need for users to trust a central authority.

Here is a detailed breakdown of the project's key components and functionality:

Core Functionality Cross-Chain Limit Orders: The project's main feature is the ability to place limit orders that can execute across different blockchain networks. The key innovation is the integration of the Monad Testnet, which serves as both a source and destination chain for these orders. This extends the capabilities of the original 1inch cross-chain SDK.

Off-Chain Order Book: Unlike on-chain order books, which can be expensive and slow, Cosmic DeFi uses a MongoDB database to store the signed orders and their metadata. This acts as a decentralized, off-chain order book that resolvers can access.

Trust-Minimized Resolution: The system uses a hashlock mechanism to secure trades. When a user creates an order, they include a hashlock secret. This secret is crucial for the resolver to finalize the trade. The process is as follows:

A resolver polls the backend for pending orders.

The resolver deploys their own Resolver.sol smart contract, which interacts with the protocol's escrows.

The resolver initiates an escrow on the source chain, locking the funds via the EscrowFactory.

An escrow is also deployed on the destination chain.

Once both escrows are active, the secret is revealed to the resolver. The resolver can then use this secret to unlock and withdraw the funds from the source escrow, completing the cross-chain swap.

Key Deployments and Technologies To facilitate this process, the project has deployed the LimitOrderProtocol (LOP) and EscrowFactory smart contracts on two testnets:

Ethereum Sepolia

Monad Testnet

The project is built using a modern technology stack that includes:

Solidity: For the smart contracts (e.g., the modified 1inch LimitOrderProtocol).

1inch CrossChain SDK (custom fork): The foundational library for cross-chain functionality, specifically modified to support Monad.

Ethers.js: A library for interacting with the Ethereum blockchain and smart contracts.

MongoDB: The database used for the off-chain order book.

Framer Motion / React: The frontend technologies used to build the user interface and resolver dashboard.

In summary, Cosmic DeFi is a sophisticated and decentralized platform that enables secure, cross-chain limit orders by combining a custom-modified version of the 1inch protocol with an off-chain order book and a trust-minimized resolution mechanism powered by hashlocks and independent resolver contracts.

How it's Made

Core Technologies and Their Integration The project's architecture is a combination of on-chain smart contracts and off-chain services, all working together to facilitate cross-chain limit orders.

  1. Smart Contracts (On-Chain Logic):

Solidity: The smart contracts are written in Solidity, the primary language for the Ethereum Virtual Machine (EVM).

1inch LimitOrderProtocol (LOP): This is the foundational contract for the project. It handles the core logic of creating and filling limit orders on a single chain. The project uses a modified version of this protocol.

EscrowFactory: This contract is responsible for creating and managing escrow contracts. It's a crucial component for the cross-chain functionality, as it ensures funds are securely held on both the source and destination chains during a swap.

Resolver.sol: This is a minimal, Ownable contract that each resolver deploys. It acts as an intermediary, handling all the complex interactions with the EscrowFactory and LOP contracts on behalf of the resolver, ensuring separation and auditability.

  1. Off-Chain Services (Backend and Frontend):

monad-backend: This is a custom fork of the @1inch/cross-chain-sdk. The key modification here was adding support for the Monad Testnet, allowing the project to use it as a valid chain for cross-chain swaps. This backend is responsible for the low-level logic of building and handling cross-chain order data.

backend: This service is the heart of the off-chain system. It handles two primary functions:

Order Creation: It takes user input and generates the EIP-712 compliant signature for an off-chain order.

Order Book: It interacts with a MongoDB database to store and retrieve signed orders. This database acts as the off-chain order book, which resolvers poll to find available trades.

cosmic-defi (Frontend): This is the user-facing application built with React and Framer Motion. It provides a user interface for creating orders and a dashboard for resolvers to monitor pending orders and initiate the filling process.

Ethers.js: This JavaScript library is used throughout the backend and frontend to interact with the Ethereum and Monad testnets. It handles tasks like signing transactions, interacting with smart contracts, and managing wallets.

How It's All Pieced Together The architecture follows a clear flow:

User creates an order: The user interacts with the React frontend to specify the trade details.

Off-chain signing: The frontend uses Ethers.js to generate an EIP-712 signature for the order. This signature, along with the order details and the hashlock secret, is sent to the backend service.

Storing the order: The backend service saves the order data in MongoDB. At this point, the order is open and waiting to be filled.

Resolver discovers the order: A resolver, using their dashboard on the React frontend, polls the backend service to find available orders in the MongoDB database.

Resolver begins the process: The resolver's frontend sends a request to their Resolver.sol contract. This contract then interacts with the LOP and EscrowFactory on the source chain (e.g., Sepolia) to initiate the escrow and lock the funds.

Cross-chain communication: The resolver then uses the monad-backend to deploy the corresponding escrow on the destination chain (Monad).

Settle the trade: Once both escrows are active, the original hashlock secret is revealed. The resolver uses this secret to prove ownership and unlock the funds on the source chain, completing the swap.

Notable "Hacks" and Modifications 1inch SDK Fork: The most significant modification was forking the @1inch/cross-chain-sdk. The original SDK did not have support for the Monad Testnet, so the project's developers had to extend the code to recognize and handle it as a valid network for cross-chain orders. This was essential for the project's core functionality.

Independent Resolver Contracts: Instead of a single, shared resolver contract, the project requires each resolver to deploy their own minimal Resolver.sol. This design choice is particularly notable because it promotes decentralization and trust-minimization. By giving each resolver their own contract, it isolates their activity, making it easier to audit and ensuring that no single resolver's malicious actions can compromise the entire system.

Off-chain/On-chain Hybrid: The project smartly uses a hybrid approach. The orders themselves are created and stored off-chain in a MongoDB database, which is cheap and fast. However, the critical and trust-sensitive parts—locking funds, revealing secrets, and settling the trade—are all handled by immutable smart contracts on the blockchain. This architecture combines the efficiency of off-chain data storage with the security and immutability of on-chain logic.

background image mobile

Join the mailing list

Get the latest news and updates