Skip to content

Conversation

sidmore
Copy link

@sidmore sidmore commented Jan 13, 2025

Title: Implement Lending Protocol with Collateralized Borrowing and Liquidity Management

Overview

This PR introduces the core functionality for a decentralized lending protocol, including:

Collateralized Lending:
Users can deposit BTC and ETH as collateral to borrow USDT.
Borrowing limits are enforced using a configurable collateral factor.

Liquidity Pool Management:
Separate liquidity pools for BTC and ETH.
Users can add or remove liquidity, and interest rewards are distributed as DEFI tokens.

Wallet Management:
Users can deposit, withdraw, and query their wallet balances for supported tokens (BTC, ETH, USDT, and DEFI).

Interest Accrual:
Liquidity providers earn DEFI tokens as interest, distributed proportionally based on their contribution to the pools.

Major Changes

Endpoints:
depositTokenEndpoint: Deposit tokens into the user's wallet.
withdrawTokenEndpoint: Withdraw tokens from the user's wallet.
getWalletBalanceEndpoint: Retrieve wallet balance for a specific token.
addCollateralEndpoint: Deposit tokens as collateral.
borrowEndpoint: Borrow USDT tokens against collateral.
removeLiquidityEndpoint: Withdraw liquidity from BTC or ETH pools.

Modules:
Wallet Module:
depositToken: Adds tokens to a user's wallet.
withdrawToken: Deducts tokens from a user's wallet.

Lending Module:
addCollateral: Manages collateral deposits for borrowing.
borrow: Validates collateral, withdraws tokens from liquidity, and credits them to the user's wallet.
removeLiquidity: Allows users to withdraw their liquidity from the pool.

Liquidity Pools:
Added poolTable to manage BTC and ETH pools.
Tracks totalLiquidity and user contributions for each token.

Workflow Examples

Adding Liquidity:
User deposits 2 BTC into the BTC pool using addLiquidity.
The pool's total liquidity increases, and the user's contribution is tracked.

Borrowing Tokens:
User with sufficient BTC collateral borrows 1000 USDT.
Tokens are deducted from the USDT pool and credited to the user's wallet.

Interest Accrual:
Liquidity providers in BTC and ETH pools earn DEFI tokens proportional to their contributions.

Wallet Operations:
Users can deposit, withdraw, or query their wallet balances for supported tokens.

Configuration

Collateral Factor: Configurable via COLLATERAL_FACTOR (default: 1.5).
Base Interest Rate: Configurable via BASE_RATE (default: 2% per period).
Key Features
DeFi Standards:
Aligns with real-world DeFi protocols like Aave, enabling collateralized borrowing and interest distribution.
Scalable Design:
Modular structure with separate modules for wallet, lending, and liquidity pool management.
Security and Validation:
Comprehensive input validation and error handling in all endpoints.

@sidmore sidmore changed the title Sample Lending App Lending App example Jan 13, 2025
@sidmore sidmore requested review from msftsettiy and a team January 14, 2025 17:49
@sidmore sidmore marked this pull request as ready for review January 14, 2025 17:50
@@ -0,0 +1,4 @@
import "./endpoints/lending";
import "./endpoints/wallet";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why liquidityPools endpoints not imported?

@@ -0,0 +1,76 @@
# Lending Protocol on CCF Framework
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An explanation how to use it would be great as it would make the whole example standalone.

- **Withdraw Token**: `/wallet/withdraw`
- **Get Wallet Balance**: `/wallet/balance`

### Lending Endpoints
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these endpoints correct? lending.ts has only 2 methods exported

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will update the lending.ts as i forgot to add endpt to remove collateral and remove liquidity

@@ -0,0 +1,9 @@
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scripts section is missing, I would expect some basic ones such as build or test

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let me add the build script to generate app.json

@ivarprudnikov
Copy link
Member

I could not start it as there is a missing config it seems:

# start the ccf sandbox container ...
docker run --rm -it --entrypoint=/bin/bash -v `pwd`:/app ghcr.io/microsoft/ccf/app/dev/virtual:ccf-5.0.0

# install nodejs
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - && sudo apt-get install -y nodejs

# build the app
cd /app/lending-app
npm i
npx tsc

# run the app
/opt/ccf_virtual/bin/sandbox.sh --js-app-bundle ./dist/

ends up with the error:

[12:30:10.430] Starting 1 CCF node...
Traceback (most recent call last):
  File "/opt/ccf_virtual/bin/start_network.py", line 221, in <module>
    run(args)
  File "/opt/ccf_virtual/bin/start_network.py", line 94, in run
    network.start_and_open(args)
  File "/opt/ccf_virtual/bin/infra/network.py", line 640, in start_and_open
    self.open(args)
  File "/opt/ccf_virtual/bin/infra/network.py", line 607, in open
    self.consortium.set_js_app_from_dir(
  File "/opt/ccf_virtual/bin/infra/consortium.py", line 560, in set_js_app_from_dir
    bundle = self.read_bundle_from_dir(bundle_path)
  File "/opt/ccf_virtual/bin/infra/consortium.py", line 542, in read_bundle_from_dir
    with open(metadata_path, encoding="utf-8") as f:
FileNotFoundError: [Errno 2] No such file or directory: './dist/app.json'

@sidmore
Copy link
Author

sidmore commented Jan 23, 2025

I could not start it as there is a missing config it seems:

# start the ccf sandbox container ...
docker run --rm -it --entrypoint=/bin/bash -v `pwd`:/app ghcr.io/microsoft/ccf/app/dev/virtual:ccf-5.0.0

# install nodejs
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - && sudo apt-get install -y nodejs

# build the app
cd /app/lending-app
npm i
npx tsc

# run the app
/opt/ccf_virtual/bin/sandbox.sh --js-app-bundle ./dist/

ends up with the error:

[12:30:10.430] Starting 1 CCF node...
Traceback (most recent call last):
  File "/opt/ccf_virtual/bin/start_network.py", line 221, in <module>
    run(args)
  File "/opt/ccf_virtual/bin/start_network.py", line 94, in run
    network.start_and_open(args)
  File "/opt/ccf_virtual/bin/infra/network.py", line 640, in start_and_open
    self.open(args)
  File "/opt/ccf_virtual/bin/infra/network.py", line 607, in open
    self.consortium.set_js_app_from_dir(
  File "/opt/ccf_virtual/bin/infra/consortium.py", line 560, in set_js_app_from_dir
    bundle = self.read_bundle_from_dir(bundle_path)
  File "/opt/ccf_virtual/bin/infra/consortium.py", line 542, in read_bundle_from_dir
    with open(metadata_path, encoding="utf-8") as f:
FileNotFoundError: [Errno 2] No such file or directory: './dist/app.json'

will fix thanks for catching it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants