-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path5.regular-link-svg.ts
188 lines (167 loc) · 5.8 KB
/
5.regular-link-svg.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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 link svg 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: 1,
dnaLength: 1,
patternType: "options",
traitArgs:[
"https://www.nervos.org/favicon.svg",
],
},
{
traitName: "Type",
dobType: "Number",
dnaOffset: 3,
dnaLength: 1,
patternType: "range",
traitArgs: [10, 50],
},
{
traitName: "Timestamp",
dobType: "Number",
dnaOffset: 4,
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 SVG",
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
*/
// testnet
//const clusterId = '0x3ae41180f64a22ad6c73058d27f956f8195c17bab3bc03222b5e5683771407c4';
// mainnet
//const clusterId = '0xc38e390673f9ddda5277d617736f53c7e518941b82b407df0e981f7ed1f57337';
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}`);
/**
* The code below helps you to view the dob you just minted
*/
const getDobTypeHash = (sporeId: string, version?: ccc.spore.SporeVersion | ccc.spore.SporeVersion.V2 ) => {
const sporeScriptInfo = ccc.spore.getSporeScriptInfo(client, version)
const dobTypeScript = ccc.Script.from({
codeHash: sporeScriptInfo.codeHash,
hashType: sporeScriptInfo.hashType,
args: sporeId
})
return dobTypeScript.hash();
}
const getClusterTypeHash = (clusterId: string, version?: ccc.spore.SporeVersion | ccc.spore.SporeVersion.V2 ) => {
const clusterScriptInfo = ccc.spore.getClusterScriptInfo(client, version)
const clusterTypeScript = ccc.Script.from({
codeHash: clusterScriptInfo.codeHash,
hashType: clusterScriptInfo.hashType,
args: clusterId
})
return clusterTypeScript.hash();
}
enum PlatformSupportedDOB {
JOYID = "joyid",
CKBEXPLORER = "ckb explorer",
OMIGA = "omiga",
DOBBY = "dobby",
MOBIT = "mobit",
}
const viewDobUrl = (platform : PlatformSupportedDOB, clusterId: string, sporeId: string) => {
const isMainnet = client.addressPrefix === 'ckb';
let url = ''
switch (platform) {
case PlatformSupportedDOB.JOYID:
url = isMainnet
? `https://app.joy.id/nft/${sporeId.slice(2)}`
: `https://testnet.joyid.dev/nft/${sporeId.slice(2)}`;
break;
case PlatformSupportedDOB.OMIGA:
const sporeTypeHash = getDobTypeHash(sporeId);
url = isMainnet
? `https://omiga.io/info/dobs/${sporeTypeHash}`
: `https://test.omiga.io/info/dobs/${sporeTypeHash}`;
break;
case PlatformSupportedDOB.CKBEXPLORER:
const clusterTypeHash = getClusterTypeHash(clusterId);
url = isMainnet
? `https://explorer.nervos.org/nft-info/${clusterTypeHash}/${sporeId}`
: `https://testnet.explorer.nervos.org/nft-info/${clusterTypeHash}/${sporeId}`;
break;
case PlatformSupportedDOB.DOBBY:
url = isMainnet
? `https://app.dobby.market/item-detail_ckb/${sporeId}`
: `https://test-dobby.entrust3.com/item-detail_ckb/${sporeId}`;
break;
case PlatformSupportedDOB.MOBIT:
url = isMainnet
? `https://mobit.app/dob/${sporeId.slice(2)}?chain=ckb`
: `https://mobit.app/dob/${sporeId.slice(2)}?chain=ckb`;
break;
default:
throw new Error(`Unsupported platform: ${platform}`);
}
return url;
}
console.log('Now you can view the dob on JoyId, Omiga, CKB Explorer, Mobit, Dobby...');
Object.values(PlatformSupportedDOB).forEach(platform => {
console.log(`View on ${platform}: 👉🔗`, viewDobUrl(platform, clusterId, sporeId));
});