更新时间:2023-03-17 GMT+08:00
分享

调用示例

Go-sdk 调用示例:

// 初始化SDK
sdk, err:=tdcsdk.New("/root/test-tdc-sdk-conf/conf.yaml")
if err != nil {
	fmt.Println(err)
	return
}
// 创建名为“tiger01” 的集合
collectionIndex := "tiger01"
txid, err := sdk.CreateNFTCollection(collectionIndex)
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println("Create collection txid:",txid)
// 生成数字资产meta信息
meta, err := sdk.NewImageMetadata([]byte("123"),"tigerImage",
	"http://obs.huawei.com/1/tiger","description",1)
if err != nil {
	fmt.Println(err)
	return
}
// 铸造数字资产
ntf, txid, err := sdk.NFT721.Mint(collectionIndex,"1",meta)
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println("Mint txid:",txid)
fmt.Println("Mint ntf:",ntf)

// 查询数字资产所有者
owner, err := sdk.NFT721.OwnerOf(collectionIndex,"1")
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println("Collection:",collectionIndex,"token:1 owner is ",owner)

// 生成新的用户信息,包括证书,私钥和地址
cert, sk, receiverAddr, err := sdk.GenerateIdentity(utils.EcdsaWithSha256,"user1")
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println("cert:",cert)
fmt.Println("sk:",sk)
fmt.Println("addr:",receiverAddr)

// 数字资产转移
txid, err = sdk.NFT721.TransferFrom(collectionIndex,owner,receiverAddr,"1")
if err != nil{
	fmt.Println(err)
	return
}
fmt.Println("TransferFrom txid:", txid)

// 批量铸造数字资产
batchMintData := &mgmt.BatchMintData{StartTokenID: "5000", Amount: "5000", TokenURI: meta}
txid, err = sdk.NFT1155.BatchMint(collectionIndex, batchMintData)
if err != nil{
	fmt.Println(err)
	return
}
fmt.Println("BatchMint txid:", txid)

// 铸造可组合资产
nft, txid, err := sdk.NFT998.Mint(collectionIndex, "10", meta)
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println("Mint of composable asset txid:",txid)
fmt.Println("Mint of composable asset nft:",nft)

// 铸造数字资产
nft, txid, err = sdk.NFT721.Mint(collectionIndex,"2",meta)
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println("Mint txid:",txid)
fmt.Println("Mint nft:",nft)

// 查询数字资产拥有者
owner, err = sdk.NFT721.OwnerOf(collectionIndex, "2")
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println("the nft of owner:", owner)

// 加入可组合资产
txid, err = sdk.NFT998.GetChild(owner, collectionIndex, "10", collectionIndex, "2")
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println("GetChild txid:", txid)

// 切换成新的用户身份
err = sdk.SwitchIdentityWithPem(utils.EcdsaWithSha256,cert,sk)

// resourceByte 可以从本地文件获取或者从网络请求中接收图片的字节数组
resourceByte := []byte("资源的byte数组")
resourcePath, resourceUrl, metadata, err := sdk.ToolManager.UploadResourceWithAudit(resourceByte, mgmt.Image, mgmt.ImageJPEG, "1678671385", false)
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println("UploadResourceWithAudit resourceUrl:", resourceUrl)
fmt.Println("UploadResourceWithAudit metadata:", metadata)
fmt.Println("UploadResourceWithAudit resourcePath:", resourcePath)
// 其他业务调用...

相关文档