This exercise contains a simple Node project using Typescript, that executes an advanced transaction block on the Sui blockchain, to mint a Hero NFT and a Sword NFT, and attach the Sword to the Hero as a Dynamic Object Field.
- Programmable Transaction Blocks
- Sui TS SDK | Programmable Transaction Blocks
- Sui TS SDK | Passing inputs to a transaction
- Create a .env file in the mint-hero/ directory, following the structure of the .env.example:
SUI_NETWORK=testnet
PACKAGE_ID=0xc413c2e2c1ac0630f532941be972109eae5d6734e540f20109d75a59a1efea1e
USER_SECRET_KEY=
- Run the following commands:
cd mint-hero
npm i
npm run test
- Notice that all of the tests in the mintHeroWithSword.test.ts file are failing:
Transaction Status: Validates that the status of the transfer transaction is "success".Created Hero And Sword: Validates that a Hero NFT and a Sword NFT are created.Hero is equiped with a Sword: Validates that our Hero is equiped with a Sword.
- Modify the src/helpers/mintHeroWithSword.ts function, by adding the implementation:
- You should use the
tx.moveCallmethod for making custom move calls. - You should use the
mint_herofunction of the hero module to mint the Hero object. - You should use the
new_swordfunction of the blacksmith module to mint the Sword object. - You should use the
equip_swordfunction of the hero module to equip the Sword object. - You should use the include effects and objectTypes in the output, you can use:
the object changes will be included underinclude: { effects: true, objectTypes: true }effects.objectChanges- Last but not least, do not forget to transfer the Hero object to an address, since the Hero object is an actual NFT that does not have the Drop ability, so it must be "consumed" somehow!!
- You should use the
- Modify the parseCreatedObjectsIds.ts function, so that you can extract both the Hero's and the Sword's object ID:
- You should filter the object changes by
idOperation === "Created". - You should pass
objectTypes and objectChangesas inputs, which are obtained from the move call before. - Filter by the corresponding
objectTypeto differentiate between the Hero and the Sword.
- You should filter the object changes by
- Modify the getHeroSwordIds.ts function, so that you can extract the object IDs of the Swords that are attached to a Hero object:
- You should use the
suiClient.listDynamicFieldsfunction to get the dynamic fields that are attached to the Hero. - You should filter them by the objects type, to keep only the Swords.
- You should use the
suiClient.getDynamicObjectFieldfunction for every object, to get the object ids of the attached swords.
- You should use the