Skip to content

Latest commit

 

History

History
217 lines (179 loc) · 8.13 KB

0.basic-shape.md

File metadata and controls

217 lines (179 loc) · 8.13 KB

Intro

This example demonstrates the basic usage of DOB1 protocol, including creating a cluster and minting a DOB1. Afterward, you can view the DOB on JoyID, Omiga, CKB Explorer, Mobit, Dobby.

import { ccc } from "@ckb-ccc/ccc";
import { client, signer } from "@ckb-ccc/playground";

function getExplorerTxUrl(txHash: string) {
  const isMainnet = client.addressPrefix === 'ckb';
  const baseUrl = isMainnet ? 'https://explorer.nervos.org' : 'https://testnet.explorer.nervos.org';

  return `${baseUrl}/transaction/${txHash}`
}

function generateSimpleDNA(length: number): string {
  return Array.from(
    { length }, 
    () => Math.floor(Math.random() * 16).toString(16)
  ).join('');
}

function generateClusterDescriptionUnderDobProtocol() {
  /**
   * Generation example for DOB0
   */
  const clusterDescription = "This is a basic-shape example for dob1.";
  const dob0Pattern: ccc.spore.dob.PatternElementDob0[] = [
    {
      traitName: "Shape",
      dobType: "String",
      dnaOffset: 1,
      dnaLength: 1,
      patternType: "options",
      traitArgs: ["circle", "square", "triangle", "star", "text"],
    },
    {
      traitName: "BackgroundColor",
      dobType: "String",
      dnaOffset: 0,
      dnaLength: 1,
      patternType: "options",
      traitArgs: ["red", "blue", "green", "yellow", "pink"],
    }
  ];
  
  /**
   * Generation example for DOB1
   */
  const dob1Pattern: ccc.spore.dob.PatternElementDob1[] = [
    {
      imageName: "IMAGE.0",
      svgFields: "attributes",
      traitName: "",
      patternType: "raw",
      traitArgs: "xmlns='http://www.w3.org/2000/svg' viewBox='0 0 500 500'",
    },
    {
      imageName: "IMAGE.0",
      svgFields: "elements",
      traitName: "BackgroundColor",
      patternType: "options",
      traitArgs: [
        ["red", "<rect width='500' height='500' x='0' y='0' fill='red' />"],
        ["blue", "<rect width='500' height='500' x='0' y='0' fill='blue' />"],
        ["green", "<rect width='500' height='500' x='0' y='0' fill='green' />"],
        ["yellow", "<rect width='500' height='500' x='0' y='0' fill='yellow' />"],
        [["*"], "<rect width='500' height='500' x='0' y='0' fill='pink' />"],
      ]
    },
    {
      imageName: "IMAGE.0",
      svgFields: "elements",
      traitName: "Shape",
      patternType: "options",
      traitArgs: [
        ["circle", "<circle cx='250' cy='250' r='150' fill='white' stroke='black' stroke-width='2' />"],
        ["square", "<rect width='300' height='300' x='100' y='100' fill='white' stroke='black' stroke-width='2' />"],
        ["triangle", "<polygon points='250,100 400,400 100,400' fill='white' stroke='black' stroke-width='2' />"],
        ["star", "<path d='M250 100 L290 200 L400 200 L310 260 L340 370 L250 300 L160 370 L190 260 L100 200 L210 200 Z' fill='white' stroke='black' stroke-width='2' />"],
        ["text", "<text x='250' y='250' text-anchor='middle' dominant-baseline='middle' font-size='48' fill='black'>DOB1</text>"],
        [["*"], "<circle cx='250' cy='250' r='150' fill='white' stroke='black' stroke-width='2' />"]
      ],
    }
  ];

  const dob1: ccc.spore.dob.Dob1 = {
    description: clusterDescription,
    dob: {
      ver: 1,
      decoders: [
        {
          decoder: ccc.spore.dob.getDecoder(client, "dob0"),
          pattern: dob0Pattern,
        },
        {
          decoder: ccc.spore.dob.getDecoder(client, "dob1"),
          pattern: dob1Pattern,
        },
      ],
    },
  };

  return ccc.spore.dob.encodeClusterDescriptionForDob1(dob1);
}


/**
 * create cluster
 */

const { tx: clusterTx, id: clusterId } = await ccc.spore.createSporeCluster({
  signer,
  data: {
    name: "dob1-basic-shape",
    description: generateClusterDescriptionUnderDobProtocol(),
  },
});
await clusterTx.completeFeeBy(signer, 2000n);
const clusterTxHash = await signer.sendTransaction(clusterTx);
console.log("Create cluster tx sent:", clusterTxHash, `Cluster ID: ${clusterId}`);

/**
 * create spore
 */
//const clusterId = '0xc2e4164c6b390b0ca31138d1715201c24dc9aafae1a75b1047763cd06602de4e';
const { tx: sporeTx, id: sporeId } = await ccc.spore.createSpore({
  signer,
  data: {
    contentType: "dob/0",
    content: ccc.bytesFrom(`{ "dna": "${generateSimpleDNA(16)}" }`, "utf8"),
    clusterId: clusterId,
  },
  clusterMode: "clusterCell",
});
await sporeTx.completeFeeBy(signer, 2000n);
const sporeTxHash = await signer.sendTransaction(sporeTx);
console.log("Mint DOB tx sent:", sporeTxHash, `Spore ID: ${sporeId}`);

await signer.client.waitTransaction(clusterTxHash);
console.log("Create cluster tx committed:", getExplorerTxUrl(clusterTxHash), `Cluster ID: ${clusterId}`);
await signer.client.waitTransaction(sporeTxHash);
console.log("Mint DOB tx committed:", getExplorerTxUrl(sporeTxHash), `Spore ID: ${sporeId}`);

You can also open and edit the code online in ccc-playground:

CCC Playground Guide

On-chain test cluster and DOB

Testnet

  • 👉🔗 createCluster tx

    • clusterId: 0xc2e4164c6b390b0ca31138d1715201c24dc9aafae1a75b1047763cd06602de4e (type_script.args)
    • clusterTypeHash: 0xe55557af70bd7912364a1ea87c42a1e85fd6e886c5070f86f3a075bda8fd70ec (hash(type_script(cluster cell)))
  • 👉🔗 mintSpore tx

    • sporeId: 0x638af6b3ca36b88232b9d3ce46898f52f10b2308f2ce466776de641959171d0b (type_script.args)
    • sporeTypeHash: 0x4b96e21825674147aad632c24b1e9b41eb865781032ce77d27390fa626dc8797 (hash(type_script(spore cell)))

Mainnet

  • 👉🔗 createCluster tx

    • clusterId: 0x0d3cdf07dd357d55795f09e04d83945a6cf674e4dd5cc1e344c873d1c816740b (type_script.args)
    • clusterTypeHash: 0xe2b7b0daa27b28630c3f2e467bc34a5607ffff4a01d9a2bf08feb988bf4f6817 (hash(type_script(cluster cell)))
  • 👉🔗 mintSpore tx

    • sporeId: 0xf8b30166217ae79b3497a15db871603278c4fc005f71de1b06c56bc1674ccc7d (type_script.args)
    • sporeTypeHash: 0xe1c9b34d27fbdcc3ac49973b8d3303fcbc52c65800b1271b9edb88e0b51a14dc (hash(type_script(spore cell)))

Platform Preview(Testnet)

JoyID

View on JoyID

Omiga

0.basic-shape-omiga.png

View on Omiga

Mobit

0.basic-shape-mobit.png

View on mobit

Dobby

0.basic-shape-dobby.png View on Dobby

Explorer

0.basic-shape-explorer.png View on CKB Explorer

Compatibility

JoyID Omiga CKB Explorer Mobit Dobby
Testnet
Mainnet