Giter Site home page Giter Site logo

geeklabx / definex Goto Github PK

View Code? Open in Web Editor NEW

This project forked from y2labs-0sh/definex

0.0 1.0 0.0 2.23 MB

definex defi project with two experimental modules offering 2 kinds of money market

License: GNU General Public License v3.0

Rust 99.75% Shell 0.03% HTML 0.22%

definex's Introduction

This project is meant for our Web3 grant application.

How to

  1. Run a local test node:
  • git clone https://github.com/y2labs-0sh/definex.git && cd definex
  • cargo build -p node-cli --release
  • ./target/release/substrate purge-chain --dev
  • ./target/release/substrate --dev (since We are expecting to follow substrate's development releases, we are just leaving the binary name unchanged.)
  1. Run a local web wallet:
  • git clone https://github.com/y2labs-0sh/definex-wallet.git && cd definex-wallet
  • yarn install
  • yarn run start this will automatically open localhost:8000 in your browser trying to connecting 'ws://localhost:9944'
  1. Connect https://polkadot.js.org/apps/ to your local node
  2. Use Alice to mint some DUSD and BTC to some accounts in the genericAsset as a faucet
  3. Then you can do 'borrow&lend' in pallets/p2p and 'saving&loan' in pallets/deposit-loan

P2P module is at pallets/p2p

In this DeFi pallet, there are 3 major roles:

1. maker: those who want to borrow money. they can publish their needs (collateral amount, borrow amount, how long they will repay, a specific interest rate, etc.) on the platform.

2. taker: those who bring liquidity to the platform. they select the borrows that most profitable, and lend the money to the borrower. By doing this, they earn the negotiated interest.

3. liquidator: those who keep monitoring if there is any loan with a ltv lower than the 'LTVLiquidate'. By doing this, they would be rewarded.

Money Market Module is at pallets/deposit-loan

deposit-loan is an implementation of Financial market protocol that provides both liquid money markets for cross-chain assets and capital markets for longer-term cryptocurrency loans.

- It will automatically adjust the interest rates based on the amount saved and the amount borrowed.

- We are working on a three-level interest rate based on cash utilization rate that is partially influenced by the economic pricing for scarce resources and our belief that the demand for stable coin is relatively inelastic in different utilization rate intervals. The exact loan interest rate is yet to be determined but it would look like this :

`f(x) = 0.1x + 0.05 (0≤x<0.4)|| 0.2x + 0.01 (0.4≤x<0.8) || 0.3x^6 + 0.1x^3 + 0.06 (0.8≤x≤1)`

In which, Utilization rate X = Total borrows / (Total deposits + Total Borrows)

price is fed through offchain worker

pallets/new-oracle

You can customize your crypto price sources by "add_source".

And by default, DUSD(USDT) and BTC are provided.

assets are based on pallets/generic-asset

This is a modified version of frame/pallet-generic-asset.

We need every asset dynamically created can be reserved with a lock on the balance. But the default frame/pallet-generic-asset implementation doesn't support that. so we removed those complicated "**Currency", and make all assets lockable with respective lock id design.

JS types

This is just for frontend developer

{
    "TxHash": "H256",
    "Deposit": {
        "account_id": "AccountId",
        "tx_hash": "Option<TxHash>",
        "amount": "Balance"
    },
    "Auth": {
        "_enum": ["All", "Deposit", "Withdraw", "Refund", "Mark"]
    },
    "BlackOrWhite": {
        "_enum": ["Black", "White"]
    },
    "ExtrinsicIndex": "u32",
    "LineNumber": "u32",
    "AuctionBalance": "Balance",
    "TotalLoanBalance": "Balance",
    "CollateralBalanceAvailable": "Balance",
    "CollateralBalanceOriginal": "Balance",
    "Price": "u128",
    "PriceReport": {
        "reporter": "AccountId",
        "price": "Price"
    },
    "P2PLoanHealth": {
        "_enum": ["Well", "ToBeLiquidated", "Overdue", "Liquidated", "Dead", "Completed"]
    },
    "P2PLoan": {
        "id": "P2PLoanId",
        "borrow_id": "P2PBorrowId",
        "borrower_id": "AccountId",
        "loaner_id": "AccountId",
        "due": "BlockNumber",
        "collateral_asset_id": "AssetId",
        "collateral_balance": "Balance",
        "loan_balance": "Balance",
        "loan_asset_id": "AssetId",
        "status": "P2PLoanHealth",
        "interest_rate": "u64",
        "liquidation_type": "LiquidationType"
    },
    "P2PBorrow": {
        "id": "P2PBorrowId",
        "lock_id": "u128",
        "who": "AccountId",
        "status": "P2PBorrowStatus",
        "borrow_asset_id": "AssetId",
        "collateral_asset_id": "AssetId",
        "borrow_balance": "Balance",
        "collateral_balance": "Balance",
        "terms": "u64",
        "interest_rate": "u64",
        "dead_after": "Option<BlockNumber>",
        "loan_id": "Option<P2PLoanId>"
    },
    "LTV": "u64",
    "P2PBorrowId": "u128",
    "P2PLoanId": "u128",
    "LiquidationType": {
        "_enum": ["JustCollateral", "SellCollateral"]
    },
    "P2PBorrowStatus": {
        "_enum": ["Available", "Taken", "Canceled", "Completed", "Dead", "Liquidated"]
    },
    "TradingPair": {
        "collateral": "u32",
        "borrow": "u32"
    },
    "TradingPairPrices": {
        "borrow_asset_price": "u64",
        "collateral_asset_price": "u64"
    },
    "P2PBorrowOptions": {
        "amount": "Balance",
        "terms": "u64",
        "interest_rate": "u64",
        "warranty": "Option<BlockNumber>"
    },
    "StrBytes": "Vec<u8>",
    "BalanceLock": {
        "id": "u128",
        "asset_id": "AssetId",
        "amount": "Balance",
        "reasons": "WithdrawReasons"
    },
    "PriceInUSDT": "u64",
    "LoanHealth": {
        "_enum": ["Well", "Liquidating"]
    },
    "LoanPackageStatus": {
        "_enum": [
            "Active",
            "Inactive"
        ]
    },
    "Loan": {
        "id": "LoanId",
        "who": "AccountId",
        "collateral_balance_original": "Balance",
        "collateral_balance_available": "Balance",
        "loan_balance_total": "Balance",
        "status": "LoanHealth"
    },
    "ReleaseTrigger": {
        "_enum": {
            "PhaseChange": null,
            "BlockNumber": "BlockNumber"
        }
    },
    "LoanId": "u64",
    "LoanPackageId": "u64",
    "PhaseId": "u32",
    "UserAssets": {
      "asset_id": "AssetId",
      "symbol": "String",
      "balance": "String"
    },
    "P2PLoanRPC": {
      "id": "P2PLoanId",
      "borrow_id": "P2PBorrowId",
      "borrower_id": "AccountId",
      "loaner_id": "AccountId",
      "secs_left": "u64",
      "collateral_asset_id": "AssetId",
      "collateral_balance": "Balance",
      "loan_balance": "Balance",
      "loan_asset_id": "AssetId",
      "status": "P2PLoanHealth",
      "interest_rate": "u64",
      "liquidation_type": "LiquidationType",
      "can_be_liquidate": "bool",
      "due_height": "BlockNumber"
    }
}

RPC types

Since alpha5, substrate runtime storage seems no longer provides "linked_map". So we provide some 'list' functions by default to offer some basic support for our web wallet.

{
  "genericAsset": {
    "symbolsList": {
      "params": [],
      "type": "Vec<(AssetId, String)>"
    },
    "userAssets": {
      "params": [
        {
          "name": "who",
          "type": "AccountId"
        }
      ],
      "type": "Vec<UserAssets<AssetId, Balance>>"
    }
  },
  "pToP": {
    "borrows": {
      "params": [
        {
          "name": "size",
          "type": "Option<u64>",
        },
        {
          "name": "offset",
          "type": "Option<u64>",
        },
      ],
      "type": "Vec<P2PBorrow>",
    },
    "userBorrows": {
      "params": [
        {
          "name": "who",
          "type": "AccountId",
        },
        {
          "name": "size",
          "type": "Option<u64>",
        },
        {
          "name": "offset",
          "type": "Option<u64>",
        },
      ],
      "type": "Vec<P2PBorrow>",
    },
    "availableBorrows": {
      "params": [
        {
          "name": "size",
          "type": "Option<u64>",
        },
        {
          "name": "offset",
          "type": "Option<u64>",
        },
      ],
      "type": "Vec<P2PBorrow>",
    },
    "loans": {
      "params": [
        {
          "name": "size",
          "type": "Option<u64>",
        },
        {
          "name": "offset",
          "type": "Option<u64>",
        },
      ],
      "type": "Vec<P2PLoanRPC>",
    },
    "userLoans": {
      "params": [
        {
          "name": "who",
          "type": "AccountId",
        },
        {
          "name": "size",
          "type": "Option<u64>",
        },
        {
          "name": "offset",
          "type": "Option<u64>",
        },
      ],
      "type": "Vec<P2PLoanRPC>",
    },
    "availableLoans": {
      "params": [
        {
          "name": "size",
          "type": "Option<u64>",
        },
        {
          "name": "offset",
          "type": "Option<u64>",
        },
      ],
      "type": "Vec<P2PLoanRPC>",
    }
  },
  "depositLoan": {
    "loans": {
      "params": [
        {
            "name": "size",
            "type": "Option<u64>"
        },
        {
            "name": "offset",
            "type": "Option<u64>"
        }
      ],
      "type": "Vec<Loan>"
    },
    "userLoans": {
      "params": [
        {
            "name": "who",
            "type": "AccountId"
        },
        {
            "name": "size",
            "type": "Option<u64>"
        },
        {
            "name": "offset",
            "type": "Option<u64>"
        }
      ],
      "type": "Vec<Loan>"
    }
  }
}

definex's People

Contributors

kaigedong avatar kylidboy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.