Glittr
  • Introduction
  • Protocol Overview
  • Contract Primitives
    • MOAs - Mint Only Assets
    • MBAs - Mint and Burn Assets
      • Vaults
    • Spec Contracts
    • Oracle Commitments
  • Building on Glittr
    • Setup
    • Faucet
    • Glittr SDK
    • Glittr Transactions
    • Glittr Contracts
    • Installing Glittr Wallet
    • Local Development Guide
  • GLIP
  • Node
    • Run a Bitcoin Node
    • Run a Glittr Node
  • Examples & Guide
    • Creating Tokens
    • Contract Custody
    • Complex Tokens
    • Vesting and Freemint Contract
    • Wrapped BTC Contract
    • USD Contract
    • Oracle Implementation Guide
    • Glittr Output Structure
    • AMM Contract
  • API Reference
    • Getting Assets API
  • APP EXAMPLES
    • Freemint App (NextJS)
    • Freemint App (React + Vite)
    • NFT App (React + Vite)
Powered by GitBook
On this page
  1. Examples & Guide

Creating Tokens

As with ERC-20s, creating a token in Glittr is creating a contract.

There are some token standards on Glittr that developer can use:

Simple Assets

After declaring a contract creation, we declare the creation of an asset. Tokens which are meant to be purely fungible can be created using the Simple Asset primitive.

After declaring the asset and its properties, we can add additional statements about minting, mining, allocation, and other mechanisms of obtention.

Example:

You can use GlittrSDK to deploy a new free mint contract with a prebuilt transaction

import { Account, GlittrSDK, GlittrTransaction } from "@glittr-sdk/sdk"

async function deployFreeMintContract() {
    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
    })
    const account = new Account({
        network: NETWORK,
        wif: "cW84FgWG9U1MpKvdzZMv4JZKLSU7iFAzMmXjkGvGUvh5WvhrEASj",
    })
    const transaction = new GlittrTransaction({
        client,
        account
    })

    const txid = await transaction.contractDeployment.freeMint(
        "GLITTR", // ticker
        18, // divisibility
        "1", // amount per mint
        "1000000" // supply cap
    )

    console.log("TXID : ", txid)
}

deployFreeMintContract()

Alternatively, you can use our helper functions to manually construct the transaction, including its inputs and outputs :

import { Account, addFeeToTx, BitcoinUTXO, electrumFetchNonGlittrUtxos, GlittrSDK, OpReturnMessage, Output, txBuilder } from "@glittr-sdk/sdk";

async function deployFreeMintContract() {
  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
  })
  const account = new Account({
    network: NETWORK,
    wif: "cW84FgWG9U1MpKvdzZMv4JZKLSU7iFAzMmXjkGvGUvh5WvhrEASj",
  })
  
  // Build the tx message
  const tx: OpReturnMessage = {
    contract_creation: {
      contract_type: {
        moa: {
          divisibility: 18,
          live_time: 0,
          supply_cap: "1000000000",
          ticker: "FKBTC",
          mint_mechanism: { free_mint: { amount_per_mint: "10", supply_cap: "1000000000" } }
        }
      },
    },
  };

  // Helper function to fetch non glittr utxos
  const utxos = await electrumFetchNonGlittrUtxos(client.electrumApi, client.apiKey, account.p2wpkh().address)
  
  const nonFeeInputs: BitcoinUTXO[] = []
  
  // Put tx message on output #0
  const nonFeeOutputs: Output[] = [
    { script: txBuilder.compile(tx), value: 0 }
  ]

  // Helper function to add UTXO fee into the tx
  const { inputs, outputs } = await addFeeToTx(
    NETWORK,
    account.p2wpkh().address,
    utxos,
    nonFeeInputs,
    nonFeeOutputs
  )

  const txid = await client.createAndBroadcastRawTx({
    account: account.p2wpkh(),
    inputs,
    outputs
  })
  
  console.log("TXID:", txid);
}

deployFreeMintContract()
PreviousRun a Glittr NodeNextContract Custody

Last updated 3 months ago