Loopring Dev Docs
Ask or search…
⌃K

Mint Counterfactual NFT

Mint a Layer 2 NFT. Loopring follows the IPFS NFT format.
The SDK can be used to mint NFTs on Loopring's Layer 2. Minting uses IPFS and the CID will convert into the nftId. Review MetaNFT.md for additional details.
Step 1: Get Account
Prepare your Loopring Layer 2 account. Retrieve account information.
1
const {accInfo} = await LoopringAPI.exchangeAPI.getAccount({
2
owner: LOOPRING_EXPORTED_ACCOUNT.address,
3
});
4
console.log("accInfo:", accInfo);
Step 2: Get eddsaKey
Once you have the account information, retrieve the account's EdDSA key for the minting process. Get Layer 2 EdDSA key.
1
const eddsaKey = await signatureKeyPairMock(accInfo);
2
console.log("eddsaKey:", eddsaKey.sk);
Step 3: Get apiKey
Retrieve the account's API key.
1
const {apiKey} = await LoopringAPI.userAPI.getUserApiKey(
2
{
3
accountId: accInfo.accountId,
4
},
5
eddsaKey.sk
6
);
7
console.log("apiKey:", apiKey);
Step 4: Get storageId
Get fee token storageId
1
const storageId = await LoopringAPI.userAPI.getNextStorageId(
2
{
3
accountId: accInfo.accountId,
4
sellTokenId: LOOPRING_EXPORTED_ACCOUNT.nftTokenId, // same as maxFee tokenId
5
},
6
apiKey
7
);
Step 5: Get tokenAddress
Before mint user should create an collection for information with an url, follow code using the user own collection-list first item as demo
1
const collectionRes = await LoopringAPI.userAPI
2
.getUserOwenCollection({
3
owner: accInfo.owner,
4
isMintable: true
5
},
6
apiKey
7
)
8
if ((collectionRes &&
9
((collectionRes as sdk.RESULT_INFO).code ||
10
(collectionRes as sdk.RESULT_INFO).message)) || !collectionRes.collections.length
11
) {
12
console.log("Collection is disable to mint ");
13
throw "Collection is disable to mint ";
14
}
15
​
16
const collectionMeta = (collectionRes as any).collections[ 0 ] as CollectionMeta;
17
​
18
const counterFactualNftInfo: NFTCounterFactualInfo = {
19
nftOwner: accInfo.owner,
20
nftFactory: collectionMeta.nftFactory ?? sdk.NFTFactory_Collection[ sdk.ChainId.GOERLI ],
21
nftBaseUri: collectionMeta.baseUri,
22
};
23
const nftTokenAddress = collectionMeta.contractAddress,
Step 6: Get fee
Retrieve the mint fee.
1
const fee = await LoopringAPI.userAPI.getNFTOffchainFeeAmt(
2
{
3
accountId: accInfo.accountId,
4
tokenAddress: nftTokenAddress,
5
requestType: sdk.OffchainNFTFeeReqType.NFT_MINT,
6
},
7
apiKey
8
);
Step 7: Mint
Mint the NFT
1
const response = await LoopringAPI.userAPI.submitNFTMint({
2
request: {
3
exchange: LOOPRING_EXPORTED_ACCOUNT.exchangeAddress,
4
minterId: accInfo.accountId,
5
minterAddress: accInfo.owner,
6
toAccountId: accInfo.accountId,
7
toAddress: accInfo.owner,
8
nftType: 0,
9
tokenAddress: nftTokenAddress,
10
nftId: LOOPRING_EXPORTED_ACCOUNT.nftId, //nftId.toString(16),
11
amount: "1",
12
validUntil: LOOPRING_EXPORTED_ACCOUNT.validUntil,
13
storageId: storageId.offchainId ?? 9,
14
maxFee: {
15
tokenId: TOKEN_INFO.tokenMap["LRC"].tokenId,
16
amount: fee.fees["LRC"].fee ?? "9400000000000000000",
17
},
18
royaltyPercentage: 5,
19
counterFactualNftInfo,
20
forceToMint: true, // suggest use as false, for here is just for run test
21
},
22
web3,
23
chainId: sdk.ChainId.GOERLI,
24
walletType: sdk.ConnectorNames.Unknown,
25
eddsaKey: eddsaKey.sk,
26
apiKey: apiKey,
27
});
Congratulations! You have now minted your counterfactual NFT!

​

​