|
| 1 | +import { |
| 2 | + Address, |
| 3 | + TokenManagementTransactionsFactory, |
| 4 | + TransactionComputer, |
| 5 | + TransactionsFactoryConfig, |
| 6 | + TokenManagementTransactionsOutcomeParser, |
| 7 | + TransactionsConverter, |
| 8 | + TransactionWatcher, |
| 9 | +} from "@multiversx/sdk-core"; |
| 10 | +import { |
| 11 | + syncAndGetAccount, |
| 12 | + senderAddress, |
| 13 | + getSigner, |
| 14 | + apiNetworkProvider, |
| 15 | +} from "./setup.js"; |
| 16 | + |
| 17 | +const collectionName = "NFTCollection"; |
| 18 | +const collectionTicker = "TTTTT"; |
| 19 | +const collectionId = "TTTTT-adfc1e"; // It will be the ticker + hash, you'll get it after issuance |
| 20 | + |
| 21 | +const parseOutcome = async (txHash, type) => { |
| 22 | + console.log("Pending..."); |
| 23 | + |
| 24 | + // Get the transaction on network, we need to wait for results here, we use TransactionWatcher for that |
| 25 | + const transactionOnNetwork = await new TransactionWatcher( |
| 26 | + apiNetworkProvider |
| 27 | + ).awaitCompleted(txHash); |
| 28 | + |
| 29 | + // Parse the outcome |
| 30 | + const converter = new TransactionsConverter(); |
| 31 | + const parser = new TokenManagementTransactionsOutcomeParser(); |
| 32 | + const transactionOutcome = |
| 33 | + converter.transactionOnNetworkToOutcome(transactionOnNetwork); |
| 34 | + |
| 35 | + let parsedOutcome; |
| 36 | + |
| 37 | + if (type === "issue") { |
| 38 | + parsedOutcome = parser.parseIssueNonFungible(transactionOutcome); |
| 39 | + |
| 40 | + console.log(`Token identifier: ${parsedOutcome?.[0]?.tokenIdentifier}`); |
| 41 | + } |
| 42 | + if (type === "create") { |
| 43 | + parsedOutcome = parser.parseNftCreate(transactionOutcome); |
| 44 | + |
| 45 | + console.log( |
| 46 | + `Token identifier: ${parsedOutcome?.[0]?.tokenIdentifier} Nonce: ${parsedOutcome?.[0]?.nonce}` |
| 47 | + ); |
| 48 | + } |
| 49 | +}; |
| 50 | + |
| 51 | +/** |
| 52 | + * Common operation to process each transaction |
| 53 | + */ |
| 54 | +const processTransaction = async (transaction, type) => { |
| 55 | + const user = await syncAndGetAccount(); |
| 56 | + const computer = new TransactionComputer(); |
| 57 | + const signer = await getSigner(); |
| 58 | + |
| 59 | + // Increase the nonce |
| 60 | + transaction.nonce = user.getNonceThenIncrement(); |
| 61 | + |
| 62 | + // Serialize the transaction for signing |
| 63 | + const serializedTransaction = computer.computeBytesForSigning(transaction); |
| 64 | + |
| 65 | + // Sign the transaction with our signer |
| 66 | + transaction.signature = await signer.sign(serializedTransaction); |
| 67 | + |
| 68 | + // Broadcast the transaction |
| 69 | + const txHash = await apiNetworkProvider.sendTransaction(transaction); |
| 70 | + |
| 71 | + await parseOutcome(txHash, type); |
| 72 | + |
| 73 | + console.log( |
| 74 | + "Check in the Explorer: ", |
| 75 | + `https://devnet-explorer.multiversx.com/transactions/${txHash}` |
| 76 | + ); |
| 77 | +}; |
| 78 | + |
| 79 | +const getTransactionFactory = () => { |
| 80 | + const factoryConfig = new TransactionsFactoryConfig({ chainID: "D" }); |
| 81 | + const factory = new TokenManagementTransactionsFactory({ |
| 82 | + config: factoryConfig, |
| 83 | + }); |
| 84 | + return factory; |
| 85 | +}; |
| 86 | + |
| 87 | +/** |
| 88 | + * Issue an NFT collection |
| 89 | + */ |
| 90 | +const issueNftCollection = () => { |
| 91 | + const factory = getTransactionFactory(); |
| 92 | + |
| 93 | + const transaction = factory.createTransactionForIssuingNonFungible({ |
| 94 | + canAddSpecialRoles: true, |
| 95 | + canChangeOwner: true, |
| 96 | + canFreeze: true, |
| 97 | + canPause: true, |
| 98 | + canTransferNFTCreateRole: true, |
| 99 | + canUpgrade: true, |
| 100 | + canWipe: true, |
| 101 | + sender: new Address(senderAddress), |
| 102 | + tokenName: collectionName, |
| 103 | + tokenTicker: collectionTicker, |
| 104 | + }); |
| 105 | + |
| 106 | + processTransaction(transaction, "issue"); |
| 107 | +}; |
| 108 | + |
| 109 | +/** |
| 110 | + * Set special roles for the NFT collection |
| 111 | + */ |
| 112 | +const setSpecialRolesForNft = () => { |
| 113 | + const factory = getTransactionFactory(); |
| 114 | + |
| 115 | + const transaction = |
| 116 | + factory.createTransactionForSettingSpecialRoleOnNonFungibleToken({ |
| 117 | + addRoleNFTCreate: true, |
| 118 | + addRoleNFTBurn: true, |
| 119 | + addRoleNFTAddURI: true, |
| 120 | + addRoleNFTUpdateAttributes: true, |
| 121 | + addRoleESDTTransferRole: false, |
| 122 | + sender: new Address(senderAddress), |
| 123 | + tokenIdentifier: collectionId, |
| 124 | + user: new Address(senderAddress), // The owner of the role, in our case the same as sender |
| 125 | + }); |
| 126 | + |
| 127 | + processTransaction(transaction); |
| 128 | +}; |
| 129 | + |
| 130 | +/** |
| 131 | + * create an actual NFT |
| 132 | + */ |
| 133 | +const createNft = () => { |
| 134 | + const factory = getTransactionFactory(); |
| 135 | + |
| 136 | + const attributes = new TextEncoder().encode( |
| 137 | + "metadata:bafybeihof6n2b4gkahn2nwfhcxe72kvms4o5uevcok5chvg6f2zpfsi6hq/33.json" |
| 138 | + ); |
| 139 | + const hash = ""; |
| 140 | + |
| 141 | + const transaction = factory.createTransactionForCreatingNFT({ |
| 142 | + // You can read more about attributes in the docs. The data should be in the proper format to be picked up by MultiversX services like Explorer, etc. |
| 143 | + attributes, |
| 144 | + hash, // It can be any hash, for example hash of the attributes, images, etc. (not mandatory) |
| 145 | + initialQuantity: 1n, // It will be always 1 (BigInt) for NFT |
| 146 | + name: "Some NFT name for the token", |
| 147 | + royalties: 500, // number from 0 to 10000 where 10000 is 100% |
| 148 | + sender: new Address(senderAddress), |
| 149 | + tokenIdentifier: collectionId, |
| 150 | + uris: [ |
| 151 | + "https://ipfs.io/ipfs/bafybeibimqon4pjm54x27n6we5qohx57gd6n2mnbkxu2r6nejp3nbenk7u/33.png", |
| 152 | + "https://ipfs.io/ipfs/bafybeihof6n2b4gkahn2nwfhcxe72kvms4o5uevcok5chvg6f2zpfsi6hq/33.json", |
| 153 | + ], |
| 154 | + }); |
| 155 | + |
| 156 | + // Very custom gasLimit calculation. Sometimes (like in this case), the built-in gas calculation isn't enough |
| 157 | + // Check docs for more info on how gas limits are calculated |
| 158 | + // Anyway, you can define your own value through transaction.gasLimit |
| 159 | + transaction.gasLimit = BigInt( |
| 160 | + transaction.data.length * 1500 + |
| 161 | + (attributes?.length || 0 + hash?.length || 0) * 50000 |
| 162 | + ); |
| 163 | + |
| 164 | + processTransaction(transaction, "create"); |
| 165 | +}; |
| 166 | + |
| 167 | +/** |
| 168 | + * Here we will manage which function to call |
| 169 | + */ |
| 170 | +const nftManagement = () => { |
| 171 | + const args = process.argv.slice(2); |
| 172 | + const functionName = args[0]; |
| 173 | + |
| 174 | + const functions = { |
| 175 | + issueNftCollection, |
| 176 | + setSpecialRolesForNft, |
| 177 | + createNft, |
| 178 | + }; |
| 179 | + |
| 180 | + if (functionName in functions) { |
| 181 | + functions[functionName](); |
| 182 | + } else { |
| 183 | + console.log("Function not found!"); |
| 184 | + } |
| 185 | +}; |
| 186 | + |
| 187 | +nftManagement(); |
0 commit comments