A complete, modular Rust project for building blockchain and DeFi primitives in a single workspace.
This project includes:
- ⛓️ A proof-of-work blockchain engine
- 👛 Wallet key generation and address derivation
- 📥 A fee-prioritized mempool
- 💱 DeFi modules (AMM, lending, staking)
- 🌐 Peer networking layer
- 💾 Persistent storage wrapper
- 🛠️ RPC/CLI crate with a runnable node binary
blockchain/- block, chain, and mining logicwallet/- keypair generation, address derivation, transaction typemempool/- pending transaction pool with fee ordering and TTL pruningdefi/amm/- constant-product AMM simulationdefi/lending/- collateralized borrowing simulationdefi/staking/- token locking and reward computationnetwork/- basic peer registry and message broadcasting/gossip behaviorstorage/- sled-based key-value storage wrapperrpc/- request/response types, CLI config, and executable entrypoint (defi-chain)
- Rust (stable)
- Cargo
Install Rust from: https://rustup.rs
cargo buildcargo testCurrent status: tests pass in this workspace (one AMM unit test).
The binary is defined in rpc/src/main.rs and named defi-chain.
cargo run -p rpc -- --port 3000 --difficulty 2With an initial peer:
cargo run -p rpc -- --port 3000 --difficulty 3 --peer 127.0.0.1:4001--port <u16>(default:3000)--difficulty <usize>(default:2)--peer <string>(optional)
Blockincludes index, timestamp, previous hash, nonce, data, and hashmineincrements nonce until hash hasdifficultyleading zeroesChain::newcreates and mines a genesis blockChain::add_blockmines and appends a new block
- Uses
k256for ECDSA key generation - Derives addresses by Keccak-256 hashing compressed pubkey bytes and taking the last 20 bytes (
0x-prefixed) - Includes a
Transactionstruct with signing payload bytes helper
- Stores txs grouped by fee in a
BTreeMap pop_batch(n)returns highest-fee txs firstprune_expired(now)removes old txs based on TTL
- AMM: constant product style swap with 0.3% fee approximation
- Lending: collateral tracking, borrow limit (75%), health factor, liquidation path
- Staking: lock positions and compute block-based rewards
- Tracks known peers
- Broadcasts messages to all peers
- Simulates gossip rebroadcast to peers except sender
- Wraps
sledwithopen,save, andload
use blockchain::chain::Chain;
fn main() {
let mut chain = Chain::new(2);
chain.add_block("first block".to_string());
println!("Tip hash: {}", chain.tip().hash);
}DeFi Chain is structured as a complete Rust workspace with dedicated crates for chain execution, wallet logic, mempool processing, DeFi protocols, networking, storage, and RPC/CLI interaction.
Add a LICENSE file to define project licensing.