Developer Resources
Moria Protocol is fully open source, enabling developers to build alternative front-ends, integrations, and tooling.
Smart Contract Source Code
- Moria Contract Repository: gitlab.com/riftenlabs/moria/moria-contract
- CashScript contract implementation
- Full protocol logic and specifications
- Open source under OSI-approved license
Community Tools
The following third-party open source tools provide programmatic and command-line access to Moria Protocol. These are not maintained by Riften Labs.
| Tool | Type | Description |
|---|---|---|
| CashLab | TypeScript library | Programmatic API for loan operations |
| VegaBCH | CLI application | Command-line interface for managing loans |
Both are built by hosseinzoda. CashLab is the foundational library; VegaBCH wraps it into a CLI.
CashLab
CashLab is a set of TypeScript packages for BCH DeFi. The @cashlab/moria package provides programmatic access to Moria loan operations. It works in both Node.js and browser environments.
- Repository: github.com/hosseinzoda/cashlab
- API Documentation: hosseinzoda.github.io/cashlab/moria/
- License: ISC
Installation
npm install @cashlab/common @cashlab/moria
@cashlab/common is always required alongside @cashlab/moria. It provides the transaction construction primitives that the moria package builds on.
Key Concepts
The @cashlab/moria package exports a moria1 namespace for Moria v1 operations:
| Export | Description |
|---|---|
MoriaMutator |
Main class for v1 loan operations (mint, repay, add collateral, liquidate, redeem, refinance) |
createMoriaMUSDV1CompilerContext() |
Create the CashScript compiler context for MUSD v1 contracts |
createMoriaMutationContext() |
Create mutation context for loan state changes |
verifyTxResult() |
Verify a Moria transaction is valid before broadcast |
MoriaTxResult |
Transaction result type containing raw transaction bytes |
MoriaMutationContext |
Context for loan mutations |
MoriaCompilerContext |
Compiler context type |
Pay2NFTHWithdrawEntry |
Entry for pay-to-NFT-hash withdrawal |
The moria1.compiler and moria1.util sub-namespaces provide lower-level CashScript compilation and NFT commitment parsing utilities.
Common Package Types
The @cashlab/common package provides the shared types used across all CashLab packages:
| Type | Description |
|---|---|
SpendableCoin |
A UTXO with the signing parameters needed to spend it |
SpendableCoinType |
P2PKH or UNLOCK_ON_DEMAND |
PayoutRule |
Defines how transaction outputs are distributed |
PayoutAmountRuleType |
FIXED (exact amount) or CHANGE (remainder) |
createPayoutTx() |
Build a single transaction |
createPayoutChainedTx() |
Build multi-step chained transactions |
For the full API reference, see the common package documentation.
VegaBCH CLI
VegaBCH is a command-line tool for BCH DeFi operations. It wraps CashLab with wallet management, Electrum connectivity, and a daemon mode.
- Repository: github.com/hosseinzoda/vegabch
- npm: npmjs.com/package/vegabch
- License: MIT
Installation
npm install -g vegabch
Configuration
VegaBCH requires a JSON config file passed via --config <path>. The simplest setup is standalone mode:
{
"type": "standalone",
"vega_storage_file": "./vega-storage.json",
"main_electrum_node": "wss://electrum.imaginary.cash:50004",
"cauldron_indexer_node": "wss://electrum.cauldron.quest:50004"
}
VegaBCH also supports a daemon mode (persistent HTTP/RPC server for automation) and a client mode (connects to a running daemon). See the VegaBCH README for details.
Wallet Setup
# Generate a new HD wallet
vegabch wallet:generate mywallet seed --config config.json
# Or import an existing mnemonic
vegabch wallet:create mywallet seed \
--config config.json \
--mnemonic 'your twelve words here'
# Pin it as the default
vegabch wallet:pin mywallet --config config.json
Moria v1 Operations
Minting a Loan
# Mint 100 MUSD with 2 BCH collateral at 3% annual interest
vegabch moria1:mint-loan 100.00 2.00000000 3.0 \
--config config.json \
--wallet mywallet \
--txfee-per-byte 1 \
--broadcast
Arguments:
LOAN_AMOUNT- Amount in MUSD (minimum 1.00)COLLATERAL_AMOUNT- BCH collateral (decimal, e.g.2.00000000= 2 BCH)ANNUAL_INTEREST_RATE- Interest rate in percent (0% - 327%)
Managing Loans
# List all active loans
vegabch moria1:get-loans --config config.json
# List only liquidable loans
vegabch moria1:get-loans --config config.json --liquidable
# List only redeemable loans
vegabch moria1:get-loans --config config.json --redeemable
# Check protocol status
vegabch moria1:status --config config.json
Adding Collateral
# Add 0.5 BCH collateral to a loan
vegabch moria1:add-collateral <txid>:<index> 0.50000000 \
--config config.json \
--wallet mywallet \
--txfee-per-byte 1 \
--broadcast
Repaying a Loan
vegabch moria1:repay-loan <txid>:<index> \
--config config.json \
--wallet mywallet \
--txfee-per-byte 1 \
--broadcast
Refinancing
Refinance an existing loan into a new one with different parameters in a single transaction:
vegabch moria1:refi-loan <txid>:<index> 150.00 2.50000000 2.5 \
--config config.json \
--wallet mywallet \
--txfee-per-byte 1 \
--broadcast
Liquidating
vegabch moria1:liquidate-loan <txid>:<index> \
--config config.json \
--wallet mywallet \
--txfee-per-byte 1 \
--broadcast
Redeeming
vegabch moria1:redeem-loan <txid>:<index> \
--config config.json \
--wallet mywallet \
--txfee-per-byte 1 \
--broadcast
Moria v1 Command Reference
| Command | Description |
|---|---|
moria1:mint-loan AMOUNT COLLATERAL RATE |
Mint MUSD with BCH collateral and annual interest rate |
moria1:repay-loan LOAN_OUTPOINT |
Repay loan and reclaim collateral |
moria1:add-collateral LOAN_OUTPOINT AMOUNT |
Add BCH collateral to a loan |
moria1:liquidate-loan LOAN_OUTPOINT |
Liquidate undercollateralized loan |
moria1:redeem-loan LOAN_OUTPOINT |
Redeem a loan (third-party repayment) |
moria1:refi-loan OUTPOINT AMOUNT COLLATERAL RATE |
Refinance into a new loan |
moria1:get-loans |
List loans (filters: --liquidable, --redeemable) |
moria1:status |
Protocol status |
moria1:set-wallet-settings WALLET_NAME |
Configure wallet auto-management |
moria1:print-wallet-settings WALLET_NAME |
View wallet settings |
moria1:wallet-withdraw-from-agent-p2nfth NFTHASH |
Withdraw from pay-to-NFT-hash output |
These tools allow developers to interact with Moria Protocol independently of the official front-end, supporting the protocol's decentralization goals.