XCM SDK
The following is a quick guide on how to use Unique SDK to work with (NFT) XCM. Find how to work with Unique SDK in the official documentation.
IMPORTANT:
The NFT SDK currently functions only in a development environment. For production use, the following tasks still need to be completed:
- Implement
XcmPaymentApi
(Polkadot SDK). Required for handling the payment and fee logic within cross-chain messages.- Implement
DryRunApi
(Polkadot SDK). Allows for dry-run validation of cross-chain transactions.- Add an XCM Asset Transactor for NFTs. This transactor—using the new XCM + NFT types (currently under review for the Polkadot SDK)—enables native NFT transfers on your chain.
- (Optional) Support Receiving External Derivatives: Implement a mechanism for registering external NFT collections. This can be done with a pallet-derivatives approach, which is also under review for inclusion in the Polkadot SDK.
- Add an additional XCM Asset Transactor specifically for derivative NFTs.
Prerequisites
Install the @unique-nft/sdk
package.
npm install @unique-nft/sdk
Establish a connection
For this demo-stand you can use the following SDK instances:
- Unique Network: https://rest.uniquenetwork.dev/v2/xcm-unique
- Asset Hub: https://rest.uniquenetwork.dev/v2/xcm-assethub
Below is an example of how to instantiate the Asset Hub SDK:
import { AssetHub } from '@unique-nft/sdk';
const assetHub = AssetHub({ baseUrl: "https://rest.uniquenetwork.dev/v2/xcm-assethub" });
Creating collections and NFTs
Although creating collections and NFTs is outside the scope of XCM functionality, you can learn more about these processes in the official documentation:
Creating derivatives
A derivative is a cross-chain representation of an NFT collection that allows one chain to reference a collection hosted on another chain. On production networks, creating derivatives will be managed through governance processes.
IMPORTANT:
Unique SDK provides the
derivative.register
method solely for testing. It will not work on production networks and is likely to be removed in future SDK versions.
await assetHub.derivative.register({
universalLocation: {
globalConsensus: {
byGenesis: '0x0000000000000000000000000000000000000000000000000000000000000000'
},
parachain: uniqueParaId,
generalIndex: collectionId,
}
});
Check if derivative is registered
To confirm whether a derivative is already registered for a specific NFT collection, use the derivative.registered
method:
const { registered } = await assetHub.derivative.registered({
universalLocation: {
globalConsensus: {
byGenesis: '0x0000000000000000000000000000000000000000000000000000000000000000'
},
parachain: uniqueParaId,
generalIndex: collectionId,
}
});
XCM Fees
When performing cross-chain transfers or calls, you typically need to pay fees in an asset recognized by the destination chain. Use the xcm.feeAssets
method to retrieve a list of valid fee assets for a particular parachain:
const feeAssets = await assetHub.xcm.feeAssets({ parachainId });
This method returns the universalLocation
for the assets accepted as fees on the specified parachain. Once you know the acceptable fee assets, you can estimate fees for a specific transfer:
const fees = assetHub.xcm.transferNft.fee(transferParams);
Transfer NFT
Finally, you can initiate an XCM-based NFT transfer:
const result = await assetHub.xcm.transferNft(params);
const { xcmMessageId, xcmMessageHash } = result;
Here, xcmMessageId
and xcmMessageHash
serve as identifiers for the cross-chain message, which can be used to track the transfer status.