Soroban smart contracts for the Creditra adaptive credit protocol on Stellar.
This repo contains the credit contract: it maintains credit lines, tracks utilization, enforces limits, and exposes methods for opening lines, drawing, repaying, and updating risk parameters. Token transfers and interest accrual are still TODO.
Behavior notes:
- after
suspend_credit_line,draw_creditfor that borrower reverts - after
default_credit_line,draw_creditreverts andrepay_creditremains allowed repay_creditremains allowed while suspended or defaulted This repo contains the credit contract: it maintains credit lines, tracks utilization, enforces limits, and exposes methods for opening lines, drawing, repaying, and updating risk parameters. Draw logic includes a liquidity reserve check and token transfer flow.
Contract data model:
CreditStatus: Active, Suspended, Defaulted, ClosedCreditLineData: borrower, credit_limit, utilized_amount, interest_rate_bps, risk_score, status
Methods: init, open_credit_line, draw_credit, repay_credit, update_risk_parameters, suspend_credit_line, close_credit_line, default_credit_line, reinstate_credit_line, get_credit_line.
Methods: init, set_liquidity_token, set_liquidity_source, open_credit_line, draw_credit, repay_credit, update_risk_parameters, suspend_credit_line, close_credit_line.
draw_creditnow checks configured liquidity token balance at the configured liquidity source before transfer.- If reserve balance is less than requested draw amount, the transaction reverts with:
Insufficient liquidity reserve for requested draw amount. initdefaults liquidity source to the contract address.repay_credit(when a liquidity token is configured) usestransfer_fromto move tokens from the borrower to the configured liquidity source; borrowers must approve an allowance for the credit contract.- Admin can configure:
set_liquidity_token— token contract used for reserve and draw transfers.set_liquidity_source— reserve address to fund draws (contract or external source).
suspend_credit_lineis admin only and requires the credit line to exist.- Only lines in
Activestatus can be suspended. draw_creditrejects any draw when the line is notActive(includingSuspended).- Repayments are intended to remain allowed while suspended.
- Rust (edition 2021)
- soroban-sdk (Stellar Soroban)
- Build target: wasm32 for Soroban
-
Rust 1.75+ (recommend latest stable)
-
wasm32target:rustup target add wasm32-unknown-unknown
-
Stellar Soroban CLI for deploy and invoke (optional for local build).
cd creditra-contracts
cargo build --release -p creditra-creditThe workspace uses a release profile tuned for contract size (opt-level "z", LTO, strip symbols). To build the contract for Soroban:
rustup target add wasm32-unknown-unknown
cargo build --release --target wasm32-unknown-unknown -p creditra-creditWASM output is at target/wasm32-unknown-unknown/release/creditra_credit.wasm. Size is kept small by:
opt-level = "z"(optimize for size)lto = true(link-time optimization)strip = "symbols"(no debug symbols in release)codegen-units = 1(better optimization)
Avoid large dependencies; prefer minimal use of the Soroban SDK surface to stay within practical Soroban deployment limits.
cargo test -p creditra-creditThe credit contract includes dedicated overflow and large-value tests in
contracts/credit/src/lib.rs:
test_draw_credit_near_i128_max_succeeds_without_overflowtest_draw_credit_overflow_reverts_with_defined_errortest_draw_credit_large_values_exceed_limit_reverts_with_defined_error
These tests validate that:
- near-
i128::MAXdraws succeed when within limit; - arithmetic overflow reverts with the defined
"overflow"panic; - large-value over-limit draws revert with the defined
"exceeds credit limit"panic.
Run coverage with:
cargo llvm-cov --workspace --all-targets --fail-under-lines 95Current result:
- Regions:
99.51% - Lines:
98.94%
This satisfies the 95% minimum coverage target.
Once the Soroban CLI and a network are configured:
soroban contract deploy --wasm target/wasm32-unknown-unknown/release/creditra_credit.wasm --source <identity> --network <network>See Stellar Soroban docs for details.
Cargo.toml— workspace and release profile (opt for contract size)contracts/credit/— credit line contractCargo.toml— crate config, soroban-sdk dependencysrc/lib.rs— contract types and impl (stubs)
This repo is a standalone git repository. After adding your remote:
git remote add origin <your-creditra-contracts-repo-url>
git push -u origin main