This example demonstrates how to create a DOB using the DOB/0 protocol, with regular image URLs as the primary rendering objects. 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('');
}
/**
* Generate cluster description
*/
function generateClusterDescriptionUnderDobProtocol() {
const clusterDescription = "A cluster with regular image URLs as the primary rendering objects.";
const dob0Pattern: ccc.spore.dob.PatternElementDob0[] = [
{
traitName: "prev.type",
dobType: "String",
dnaOffset: 0,
dnaLength: 1,
patternType: "options",
traitArgs: ['image'],
},
{
traitName: "prev.bg",
dobType: "String",
dnaOffset: 0,
dnaLength: 1,
patternType: "options",
traitArgs:[
"https://qzp7in5ts2wbmhobxzavurfjexhnufs2hy2ji4s3blg2wvrwn2ea.akrd.net/hl_0N7OWrBYdwb5BWkSpJc7aFlo-NJRyWwrNq1Y2bog",
],
},
{
traitName: "Type",
dobType: "Number",
dnaOffset: 1,
dnaLength: 1,
patternType: "range",
traitArgs: [10, 50],
},
{
traitName: "Timestamp",
dobType: "Number",
dnaOffset: 2,
dnaLength: 4,
patternType: "rawNumber",
},
];
const dob0: ccc.spore.dob.Dob0 = {
description: clusterDescription,
dob: {
ver: 0,
decoder: ccc.spore.dob.getDecoder(client, "dob0"),
pattern: dob0Pattern,
},
};
return ccc.spore.dob.encodeClusterDescriptionForDob0(dob0);
}
/**
* create cluster
*/
const { tx: clusterTx, id: clusterId } = await ccc.spore.createSporeCluster({
signer,
data: {
name: "Regular link PNG",
description: generateClusterDescriptionUnderDobProtocol(),
},
});
await clusterTx.completeFeeBy(signer, 2000n);
const clusterTxHash = await signer.sendTransaction(clusterTx);
console.log("Create cluster tx sent:", clusterTxHash, `Cluster ID: ${clusterId}`);
await signer.client.waitTransaction(clusterTxHash);
console.log("Create cluster tx committed:", getExplorerTxUrl(clusterTxHash), `Cluster ID: ${clusterId}`);
/**
* create spore
*/
//const clusterId = '0x3ae41180f64a22ad6c73058d27f956f8195c17bab3bc03222b5e5683771407c4';
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(sporeTxHash);
console.log("Mint DOB tx committed:", getExplorerTxUrl(sporeTxHash), `Spore ID: ${sporeId}`);
You can also open and edit the code online in ccc-playground:
This example adds two traits to set the background image:
prev.type
andprev.bg
.
prev.type
: Render asimage
ortext
.prev.bg
: Set the background image.
-
- clusterId:
0x3ae41180f64a22ad6c73058d27f956f8195c17bab3bc03222b5e5683771407c4
(type_script.args) - clusterTypeHash:
0x8ea60aa94ae49b14e965b4c244576566522f96e93e1fab98c989a88b540f319e
(hash(type_script(cluster cell)))
- clusterId:
-
- sporeId:
0xe6b003cdbb042eff3b56fdceee725b42f9397b9044b8323056f283161c828357
(type_script.args) - sporeTypeHash:
0x280ebe097d3d804cb51be76f8361fbff827d64697285ab9efb59e6e53aa5e684
(hash(type_script(spore cell)))
- sporeId:
-
- clusterId:
0xf9e6cba2730adfca0ee0fe1fccbb8edb64a3af0db29157dbcd8516de36375104
(type_script.args) - clusterTypeHash:
0x24f9fb8dbe9f3b7fcd23b1ab2ebe0be07c6e7d99e598dfd70452f8cb5df6fbc9
(hash(type_script(cluster cell)))
- clusterId:
-
- sporeId:
0x12cbb27daa19a5a6d1012c64782aef8260d894d837b8522e011e56c4ecd58430
(type_script.args) - sporeTypeHash:
0x35017ed709244b36181bf74b46cbc0586071055ba8eb5c116f9a2d4a37591731
(hash(type_script(spore cell)))
- sporeId:
JoyID | Omiga | CKB Explorer | Mobit | Dobby | |
---|---|---|---|---|---|
Testnet | ✅ | ✅ | ✅ | ✅ | ✅ |
Mainnet | ✅ | ✅ | ✅ | ✅ | ✅ |