A comprehensive, modular JavaScript implementation of the RGB protocol, focusing on RGB20 non-inflatable assets. Built with a test-driven approach for learning and production use.
๐ Try the Live Demo - Interactive RGB20 contract creation with real-time visualization
import { RGB20Contract } from 'rgbtest';
// Create a simple RGB20 asset
const contract = RGB20Contract.create({
ticker: 'SBTC',
name: 'Synthetic Bitcoin',
precision: 8,
terms: 'Bitcoin-backed synthetic asset',
supply: 2100000000000000, // 21M BTC in satoshis
utxoRef: '1a2b3c4d5e6f78901234567890abcdef1234567890abcdef1234567890abcdef:0'
});
console.log(`Contract ID: ${contract.getFormattedContractId()}`);
console.log(`Total Supply: ${contract.getTotalSupply().toNumber()} satoshis`);
npm install rgbtest
The library is organized into 5 development stages:
import { RGB20Contract } from 'rgbtest';
// Stablecoin (6 decimals like USDC)
const stablecoin = RGB20Contract.create({
ticker: 'SUSD',
name: 'Synthetic USD',
precision: 6,
terms: 'USD-backed stablecoin',
supply: 1000000000000, // 1M USD
utxoRef: 'abc123...def:1'
});
// NFT-like collection (0 decimals)
const nft = RGB20Contract.create({
ticker: 'RARE',
name: 'Digital Art Collection',
precision: 0,
terms: 'Limited edition digital art',
supply: 100,
utxoRef: 'def456...abc:2'
});
// Check if a transfer is valid
try {
const validation = contract.validateTransfer(
'source_utxo:0',
1000, // amount
'destination_utxo:1'
);
console.log(`Transfer valid: ${validation.amount} tokens`);
} catch (error) {
console.log(`Transfer failed: ${error.message}`);
}
const btcAsset = RGB20Contract.create({
ticker: 'BTC',
name: 'Bitcoin',
precision: 8, // 8 decimal places
// ... other params
});
// Convert between atomic and decimal amounts
const atomic = btcAsset.getAssetInfo().totalSupply; // in satoshis
const decimal = atomic / Math.pow(10, btcAsset.getPrecision()); // in BTC
Run the comprehensive test suite:
# Run all tests
npm test
# Run specific test files
npm test test/types/asset-spec.test.js
npm test test/genesis/genesis.test.js
npm test test/contract/rgb20.test.js
# Clone and install
git clone https://github.com/rgbjs/rgbtest.git
cd rgbtest
npm install
# Run examples
node examples/stage1-utilities.js
node examples/stage2-types.js
node examples/complete-rgb20-example.js
# Run all tests
npm test
Main contract interface:
RGB20Contract.create(params)
- Create new contractgetContractId()
- Get deterministic contract IDgetFormattedContractId()
- Get RGB-formatted IDgetTicker()
, getName()
, getPrecision()
- Asset infogetTotalSupply()
- Get total token supplygetGenesisAssignments()
- Get initial token assignmentsvalidateTransfer(from, amount, to)
- Validate transferAssetSpec.createNonInflatable(ticker, name, precision)
encode()
- Strict encodinggetAtomicAmount(decimal)
/ getDecimalAmount(atomic)
- Unit conversionnew Amount(value)
- Create amount instanceadd()
, subtract()
, multiply()
, divide()
- Safe arithmeticequals()
, greaterThan()
, lessThan()
- Comparisonsnew ContractTerms(text, media?)
- Create termsgetWordCount()
, preview(maxLength)
- Text utilitiesgit checkout -b feature/amazing-feature
)npm test
)git commit -m 'Add amazing feature'
)git push origin feature/amazing-feature
)MIT License - see LICENSE file for details.
Built with โค๏ธ for the RGB ecosystem