Skip to content

Instantly share code, notes, and snippets.

@fend25
Last active July 12, 2022 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fend25/1c097e6bceace8c5c29562dbffaa16ed to your computer and use it in GitHub Desktop.
Save fend25/1c097e6bceace8c5c29562dbffaa16ed to your computer and use it in GitHub Desktop.
Read video punk
const {SchemaTools} = require('@unique-nft/api/schema')
const {ApiPromise, WsProvider} = require('@polkadot/api')
const rpc = require('@unique-nft/opal-testnet-types/definitions').unique.rpc
const WS_ENDPOINT = `wss://opal.unique.network`
const COLLECTION_ID = 28
const TOKEN_ID = 10
const run = async() => {
const api = new ApiPromise({
provider: new WsProvider(WS_ENDPOINT),
rpc: {
unique: rpc
},
})
await api.isReady
const rawCollection = (await api.rpc.unique.collectionById(COLLECTION_ID)).toHuman()
const schemaDecodingResult = SchemaTools.decode.collectionSchema(rawCollection.properties)
if (!schemaDecodingResult.isValid) {
console.error(schemaDecodingResult.validationError)
return
}
const schema = schemaDecodingResult.decoded
console.log('\n\n')
console.log('////////////////////////////////')
console.log('// decoded collection schema:')
console.log('////////////////////////////////')
console.log(JSON.stringify(schema, null, 2))
const rawToken = (await api.rpc.unique.tokenData(COLLECTION_ID, TOKEN_ID)).toHuman()
const tokenDecodingResult = SchemaTools.decode.token(rawToken, schema)
if (!tokenDecodingResult.isValid) {
console.error(tokenDecodingResult.validationError)
return
}
const token = tokenDecodingResult.decoded
console.log('\n\n')
console.log('////////////////////////////////')
console.log('// decoded token:')
console.log('////////////////////////////////')
console.log(JSON.stringify(token, null, 2))
await api.disconnect()
}
run().catch(err => {console.error(err); throw err})
/*
type DecodingResult<T> = {
isValid: true
decoded: T
} | {
isValid: false
validationError: Error
}
*/
{
"schemaName": "unique",
"schemaVersion": "1.0.0",
"image": {
"urlTemplate": "https://ipfs.unique.network/ipfs/{infix}"
},
"video": {
"urlTemplate": "https://bafybeib5lxymirwhmoj6ofppxealqh7eyw5bu7wmvk64mll5dvcqfzykwu.ipfs.nftstorage.link/videos/{infix}.mp4"
},
"coverPicture": {
"urlInfix": "QmcAcH4F9HYQtpqKHxBFwGvkfKb8qckXj2YWUrcc8yd24G/image1.png",
"fullUrl": "https://ipfs.unique.network/ipfs/QmcAcH4F9HYQtpqKHxBFwGvkfKb8qckXj2YWUrcc8yd24G/image1.png"
},
"attributesSchemaVersion": "1.0.0",
"attributesSchema": {
"0": {
"name": {"en": "gender"},
"type": 261,
"kind": 0,
"enumValues": {
"0": {"en": "Male"},
"1": {"en": "Female"}
}
},
"1": {
"name": {"en": "traits"},
"type": 261,
"kind": 1,
"enumValues": {
"0": {"en": "Black Lipstick"},
"1": {"en": "Red Lipstick"},
"2": {"en": "Smile"},
"3": {"en": "Teeth Smile"},
"4": {"en": "Purple Lipstick"},
"5": {"en": "Nose Ring"},
"6": {"en": "Asian Eyes"},
"7": {"en": "Sunglasses"},
"8": {"en": "Red Glasses"},
"9": {"en": "Round Eyes"},
"10": {"en": "Left Earring"},
"11": {"en": "Right Earring"},
"12": {"en": "Two Earrings"},
"13": {"en": "Brown Beard"},
"14": {"en": "Mustache Beard"},
"15": {"en": "Mustache"},
"16": {"en": "Regular Beard"},
"17": {"en": "Up Hair"},
"18": {"en": "Down Hair"},
"19": {"en": "Mahawk"},
"20": {"en": "Red Mahawk"},
"21": {"en": "Orange Hair"},
"22": {"en": "Bubble Hair"},
"23": {"en": "Emo Hair"},
"24": {"en": "Thin Hair"},
"25": {"en": "Bald"},
"26": {"en": "Blonde Hair"},
"27": {"en": "Caret Hair"},
"28": {"en": "Pony Tails"},
"29": {"en": "Cigar"},
"30": {"en": "Pipe"}
}
}
}
}
{
"owner": {
"Substrate": "ungch4QhrnYVHQeNKxZTHy811iyWR7cKKhU5mfPVLaWT1ExgE"
},
"image": {
"ipfsCid": "QmWLH3pcPn7uc1evgSvN7UimgsZkuxMRCYgq2DZUZLDFZr",
"fullUrl": "https://ipfs.unique.network/ipfs/QmWLH3pcPn7uc1evgSvN7UimgsZkuxMRCYgq2DZUZLDFZr"
},
"video": {
"urlInfix": "10",
"fullUrl": "https://bafybeib5lxymirwhmoj6ofppxealqh7eyw5bu7wmvk64mll5dvcqfzykwu.ipfs.nftstorage.link/videos/10.mp4"
},
"attributes": {
"0": {
"name": {"en": "gender"},
"value": {"en": "Male"},
"isArray": false,
"type": 1025,
"kind": 0,
"technicalTypeName": "localizedStringDictionary",
"technicalKindName": "enum"
},
"1": {
"name": {"en": "traits"},
"value": [
{"en": "Smile"},
{"en": "Nose Ring"},
{"en": "Round Eyes"},
{"en": "Up Hair"}
],
"isArray": true,
"type": 1025,
"kind": 1,
"technicalTypeName": "localizedStringDictionary",
"technicalKindName": "enumMultiple"
}
}
}
const {SchemaTools} = require('@unique-nft/api/schema')
const {ApiPromise, WsProvider} = require('@polkadot/api')
const rpc = require('@unique-nft/opal-testnet-types/definitions').unique.rpc
const WS_ENDPOINT = `wss://opal.unique.network`
const punksAttributesSchema: CollectionAttributesSchema = {
'0': {
name: {en: 'gender'},
type: AttributeType.localizedStringDictionaryIndex,
kind: AttributeKind.enum,
enumValues: {
0: {en: 'Male'},
1: {en: 'Female'}
}
},
'1': {
name: {en: 'traits'},
type: AttributeType.localizedStringDictionaryIndex,
kind: AttributeKind.enumMultiple,
enumValues: {
0: {"en": "Black Lipstick"},
1: {"en": "Red Lipstick"},
2: {"en": "Smile"},
3: {"en": "Teeth Smile"},
4: {"en": "Purple Lipstick"},
5: {"en": "Nose Ring"},
6: {"en": "Asian Eyes"},
7: {"en": "Sunglasses"},
8: {"en": "Red Glasses"},
9: {"en": "Round Eyes"},
10: {"en": "Left Earring"},
11: {"en": "Right Earring"},
12: {"en": "Two Earrings"},
13: {"en": "Brown Beard"},
14: {"en": "Mustache Beard"},
15: {"en": "Mustache"},
16: {"en": "Regular Beard"},
17: {"en": "Up Hair"},
18: {"en": "Down Hair"},
19: {"en": "Mahawk"},
20: {"en": "Red Mahawk"},
21: {"en": "Orange Hair"},
22: {"en": "Bubble Hair"},
23: {"en": "Emo Hair"},
24: {"en": "Thin Hair"},
25: {"en": "Bald"},
26: {"en": "Blonde Hair"},
27: {"en": "Caret Hair"},
28: {"en": "Pony Tails"},
29: {"en": "Cigar"},
30: {"en": "Pipe"}
}
},
}
const videoPunkSchema: UniqueCollectionSchemaToCreate = {
schemaName: COLLECTION_SCHEMA_NAME,
schemaVersion: '1.0.0',
image: {
urlTemplate: `https://ipfs.unique.network/ipfs/{infix}`
},
video: {
urlTemplate: `https://bafybeib5lxymirwhmoj6ofppxealqh7eyw5bu7wmvk64mll5dvcqfzykwu.ipfs.nftstorage.link/videos/{infix}.mp4`
},
coverPicture: {
urlInfix: 'QmcAcH4F9HYQtpqKHxBFwGvkfKb8qckXj2YWUrcc8yd24G/image1.png'
},
attributesSchemaVersion: '1.0.0',
attributesSchema: punksAttributesSchema
}
const punkToken: UniqueTokenToCreate = {
image: {
ipfsCid: 'QmS8YXgfGKgTUnjAPtEf3uf5k4YrFLP2uDcYuNyGLnEiNb'
},
encodedAttributes: {0: 0, 1: [3, 17]}
}
const collectionProperties = SchemaTools.encodeUnique.collectionSchema(videoPunkSchema)
const tokenPropertyPermissions = SchemaTools.encodeUnique.collectionTokenPropertyPermissions(videoPunkSchema)
const tokenProperties = SchemaTools.encodeUnique.token(punkToken, videoPunkSchema)
api.tx.createCollectionEx({
... ,
properties: collectionProperties,
tokenPropertyPermissions: tokenPropertyPermissions,
})
api.tx.createItem({
owner: {Substrate: "5..."},
properties: tokenProperties
})
const collectionInChain = {
owner: 'ungch4QhrnYVHQeNKxZTHy811iyWR7cKKhU5mfPVLaWT1ExgE',
mode: 'NFT',
name: [
'78', '69', '87',
'95', '80', '85',
'78', '75', '83'
],
description: [
'80', '117', '110', '107', '115', '32', '99',
'111', '108', '108', '101', '99', '116', '105',
'111', '110', '32', '119', '105', '116', '104',
'32', '115', '97', '100', '117', '32', '118',
'105', '100', '101', '111', '115', '44', '32',
'116', '104', '101', '32', '98', '114', '97',
'110', '100', '32', '110', '101', '119', '32',
'85', '110', '105', '113', '117', '101', '32',
'115', '99', '104', '101', '109', '97'
],
tokenPrefix: 'VPNK',
sponsorship: 'Disabled',
limits: {
accountTokenOwnershipLimit: null,
sponsoredDataSize: null,
sponsoredDataRateLimit: null,
tokenLimit: null,
sponsorTransferTimeout: '0',
sponsorApproveTimeout: '0',
ownerCanTransfer: null,
ownerCanDestroy: null,
transfersEnabled: null
},
permissions: {
access: 'Normal',
mintMode: false,
nesting: {
tokenOwner: false,
collectionAdmin: false,
restricted: null,
permissive: false
}
},
tokenPropertyPermissions: [
{ key: 'd', permission: { mutable: false, collectionAdmin: true, tokenOwner: false } },
{ key: 'n', permission: { mutable: false, collectionAdmin: true, tokenOwner: false } },
{ key: 'a.0', permission: { mutable: false, collectionAdmin: true, tokenOwner: false } },
{ key: 'a.1', permission: { mutable: false, collectionAdmin: true, tokenOwner: false } },
{ key: 'i.c', permission: { mutable: false, collectionAdmin: true, tokenOwner: false } },
{ key: 'i.h', permission: { mutable: false, collectionAdmin: true, tokenOwner: false } },
{ key: 'i.i', permission: { mutable: false, collectionAdmin: true, tokenOwner: false } },
{ key: 'i.u', permission: { mutable: false, collectionAdmin: true, tokenOwner: false } },
{ key: 'v.c', permission: { mutable: false, collectionAdmin: true, tokenOwner: false } },
{ key: 'v.h', permission: { mutable: false, collectionAdmin: true, tokenOwner: false } },
{ key: 'v.i', permission: { mutable: false, collectionAdmin: true, tokenOwner: false } },
{ key: 'v.u', permission: { mutable: false, collectionAdmin: true, tokenOwner: false } }
],
properties: [
{
key: 'attributesSchema.0',
value: '{"name":{"en":"gender"},"type":261,"kind":0,"enumValues":{"0":{"en":"Male"},"1":{"en":"Female"}}}'
},
{
key: 'attributesSchema.1',
value: '{"name":{"en":"traits"},"type":261,"kind":1,"enumValues":{"0":{"en":"Black Lipstick"},"1":{"en":"Red Lipstick"},"2":{"en":"Smile"},"3":{"en":"Teeth Smile"},"4":{"en":"Purple Lipstick"},"5":{"en":"Nose Ring"},"6":{"en":"Asian Eyes"},"7":{"en":"Sunglasses"},"8":{"en":"Red Glasses"},"9":{"en":"Round Eyes"},"10":{"en":"Left Earring"},"11":{"en":"Right Earring"},"12":{"en":"Two Earrings"},"13":{"en":"Brown Beard"},"14":{"en":"Mustache Beard"},"15":{"en":"Mustache"},"16":{"en":"Regular Beard"},"17":{"en":"Up Hair"},"18":{"en":"Down Hair"},"19":{"en":"Mahawk"},"20":{"en":"Red Mahawk"},"21":{"en":"Orange Hair"},"22":{"en":"Bubble Hair"},"23":{"en":"Emo Hair"},"24":{"en":"Thin Hair"},"25":{"en":"Bald"},"26":{"en":"Blonde Hair"},"27":{"en":"Caret Hair"},"28":{"en":"Pony Tails"},"29":{"en":"Cigar"},"30":{"en":"Pipe"}}}'
},
{ key: 'attributesSchemaVersion', value: '"1.0.0"' },
{
key: 'coverPicture.urlInfix',
value: '"QmcAcH4F9HYQtpqKHxBFwGvkfKb8qckXj2YWUrcc8yd24G/image1.png"'
},
{
key: 'image.urlTemplate',
value: '"https://ipfs.unique.network/ipfs/{infix}"'
},
{ key: 'schemaName', value: '"unique"' },
{ key: 'schemaVersion', value: '"1.0.0"' },
{
key: 'video.urlTemplate',
value: '"https://bafybeib5lxymirwhmoj6ofppxealqh7eyw5bu7wmvk64mll5dvcqfzykwu.ipfs.nftstorage.link/videos/{infix}.mp4"'
}
],
readOnly: false
}
const tokenInChain = {
properties: [
{ key: 'a.0', value: '0' },
{ key: 'a.1', value: '2,5,9,17' },
{ key: 'i.c', value: 'QmWLH3pcPn7uc1evgSvN7UimgsZkuxMRCYgq2DZUZLDFZr' },
{ key: 'v.i', value: '10' }
],
owner: { Substrate: 'ungch4QhrnYVHQeNKxZTHy811iyWR7cKKhU5mfPVLaWT1ExgE' }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment