Skip to content

Instantly share code, notes, and snippets.

@fend25
Last active June 19, 2023 16:13
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/9e68370f900d7e7bc626444886fafbdf to your computer and use it in GitHub Desktop.
Save fend25/9e68370f900d7e7bc626444886fafbdf to your computer and use it in GitHub Desktop.
Example of minting nested tokens at once on the Unique blockchain https://unique.network
import {Sr25519Account} from '@unique-nft/sr25519'
import Sdk from '@unique-nft/sdk'
import {Address} from '@unique-nft/utils'
export const getLinkToCollection = (sdk: Sdk, collectionId: number) => {
return `${sdk.options.baseUrl}/collections?collectionId=${collectionId}`
}
export const getLinkToToken = (sdk: Sdk, collectionId: number, tokenId: number) => {
return `${sdk.options.baseUrl}/tokens?collectionId=${collectionId}&tokenId=${tokenId}`
}
type IBaseCreatedParams = {
parentCollectionId: number
childCollectionId: number
tokenIdInParentCollection: number
}
const createCollections = async (sdk: Sdk): Promise<IBaseCreatedParams> => {
const parentCollectionResult = await sdk.collection.create({
name: 'Parent Collection',
description: 'Parent Collection Description',
tokenPrefix: 'PARENT',
tokenPropertyPermissions: [{
key: 'a',
permission: {mutable: true, collectionAdmin: true, tokenOwner: true},
}],
permissions: {
nesting: {
collectionAdmin: true,
tokenOwner: true,
}
}
})
if (!parentCollectionResult.parsed) {
throw parentCollectionResult.error
}
const parentCollectionId = parentCollectionResult.parsed.collectionId
console.log(`Parent collection created, id ${parentCollectionId}. ${getLinkToCollection(sdk, parentCollectionId)}`)
const childCollectionResult = await sdk.collection.create({
name: 'Child Collection',
description: 'Child Collection Description',
tokenPrefix: 'CHILD',
tokenPropertyPermissions: [{
key: 'a',
permission: {mutable: true, collectionAdmin: true, tokenOwner: true},
}],
})
if (!childCollectionResult.parsed) {
throw childCollectionResult.error
}
const childCollectionId = childCollectionResult.parsed.collectionId
console.log(`Child collection created, id ${childCollectionId}. ${getLinkToCollection(sdk, childCollectionId)}`)
const tokenMintingResult = await sdk.token.create({
collectionId: parentCollectionResult.parsed.collectionId,
properties: [{key: 'a', value: 'a'}]
})
if (!tokenMintingResult.parsed) {
throw tokenMintingResult.error
}
const tokenIdInParentCollection = tokenMintingResult.parsed.tokenId
console.log(`Token minted in parent collection, id ${tokenIdInParentCollection}. ${getLinkToToken(sdk, parentCollectionId, tokenIdInParentCollection)}`)
return {
parentCollectionId,
childCollectionId,
tokenIdInParentCollection,
}
}
const mintTokens = async (sdk: Sdk, params: IBaseCreatedParams) => {
const {parentCollectionId, childCollectionId, tokenIdInParentCollection} = params
const owner = Address.nesting.idsToAddress(parentCollectionId, tokenIdInParentCollection)
console.log(`Parent token: ${parentCollectionId}/${tokenIdInParentCollection} - ${owner}`)
const properties = [{key: 'a', value: 'a'}]
const tokensMintingResult = await sdk.token.createMultiple({
collectionId: childCollectionId,
tokens: [{owner, properties}, {owner, properties}, {owner, properties}]
})
if (!tokensMintingResult.parsed) {
throw tokensMintingResult.error
}
const tokenIdsInChildCollection = tokensMintingResult.parsed.map(({tokenId}) => tokenId)
console.log(`Tokens minted in child collection, ids ${tokenIdsInChildCollection.join(', ')}`)
for (const tokenId of tokenIdsInChildCollection) {
console.log(`${getLinkToToken(sdk, childCollectionId, tokenId)}`)
}
}
const main = async () => {
// init substrate account and sdk
const account = Sr25519Account.fromUri(process.env.MNEMONIC)
const sdk = new Sdk({baseUrl: 'https://rest.unique.network/opal/v1', account})
const result = await createCollections(sdk)
await mintTokens(sdk, result)
}
main()
.catch((error) => console.error(error))
{
"scripts": {
"mint": "tsx nesting.ts",
},
"dependencies": {
"@unique-nft/sdk": "0.2.17",
"@unique-nft/sr25519": "0.0.1",
"@unique-nft/utils": "0.3.13"
},
"devDependencies": {
"tsup" "^7.0.0",
"tsx": "^3.12.7"
}
}
import {defineConfig} from 'tsup'
const entryName = 'nesting'
const entryNameIife = `${entryName}.min`
const entryPath = './nesting.ts'
export default defineConfig([
{
entry: {[entryName]: entryPath},
format: ['esm', 'cjs'],
target: 'es2020',
dts: true,
sourcemap: true,
},
{
entry: {[entryNameIife]: entryPath},
format: ['iife'],
platform: 'browser',
outExtension: () => ({js: '.js'}),
globalName: 'nesting',
minify: true,
target: 'es2020',
dts: true,
splitting: false,
sourcemap: true,
noExternal: [/.*/],
},
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment