USD Contract
This page explains how to create a USD-pegged token using the Glittr SDK with oracle-based price feeds. The token's mint price will be determined by the current BTC/USD exchange rate.
1. Setup and Configuration
import {
Account,
GlittrSDK,
OpReturnMessage,
electrumFetchNonGlittrUtxos,
BitcoinUTXO,
Output,
addFeeToTx,
txBuilder,
BlockTxTuple,
} from "@glittr-sdk/sdk";
import {
OracleMessage,
OracleMessageSigned,
} from "@glittr-sdk/sdk/dist/transaction/calltype/types";
import { schnorr, getPublicKey } from "@noble/secp256k1";
import { sha256 } from "bitcoinjs-lib/src/crypto";
const NETWORK = "regtest";
const client = new GlittrSDK({
network: NETWORK,
apiKey: 'your-api-key',
glittrApi: "https://devnet-core-api.glittr.fi", // devnet
electrumApi: "https://devnet-electrum.glittr.fi" // devnet
});We start by setting up the SDK client for the regtest network (development environment) and import necessary functions.
2. Account and Oracle Setup
We set up:
A creator account that will deploy the contract
A minter account that will mint tokens
An oracle account that will provide BTC/USD price feeds
3. Creating the USD-Pegged Contract
The contract creation:
Sets up a token with 1 decimal place precision
Uses an oracle for price determination
Allows a 5-block window for price updates
Sends BTC to the creator's address during mints
4. Minting USD-Pegged Tokens
The minting process:
Gets current block height
Creates an oracle message with the BTC/USD price
Signs the message with the oracle's private key
Creates and broadcasts a mint transaction
5. Checking Asset Details
This function allows you to check the details of minted assets.
Key Concepts
Oracle-Based Pricing: The contract uses an oracle to determine the BTC/USD exchange rate
Price Updates: Allows a 5-block window for price updates to account for network delays
USD Pegging: Tokens are minted based on the current BTC/USD rate
Example Rate: In this example, 1 satoshi = 0.0007 USD (BTC price of ~$70,000)
This implementation creates a USD-pegged token where users can mint tokens by sending BTC, with the amount of tokens received based on the current BTC/USD exchange rate provided by the oracle.
Usage Flow
Setup
Create Contract
Mint Tokens
Check Asset Details
Full Code Example
Last updated