Craft and Burn Items
Learn how to craft new items and burn existing ones using HandCash Items
Craft and Burn Items
Overview
HandCash Items provides powerful functionality to manage the lifecycle of digital items within your application. Two key features are:
- Burning Items: Remove items from a user’s inventory, destroying them permanently.
- Crafting Items: Create new items by combining or upgrading existing ones.
These features allow you to implement complex item economies and upgrade systems in your applications.
Burning Items
Burning items removes them from a user’s inventory and destroys them permanently. This process automatically decreases the circulating supply of the particular group of items.
The HandcashMinter must be initialized with the users connect authorization token who owns the items in which you wish to burn
Example: Burn Items
import { HandCashConnect, HandCashMinter } from '@handcash/handcash-connect';
const handCashConnect = new HandCashConnect({
appId: '<app-id>',
appSecret: '<secret>',
});
const handCashMinter = HandCashMinter.fromAppCredentials({
appId: process.env.HANDCASH_APP_ID,
authToken: process.env.HANDCASH_AUTH_TOKEN, // Users connected authorization token
appSecret: process.env.HANDCASH_APP_SECRET
});
async function burnItems(origin: string) {
const burnOrderResult = await handCashMinter.burnAndCreateItemsOrder({
burn: {
origins: [origin],
}
});
console.log('Burn Order Result:', burnOrderResult);
}
Crafting Items
Crafting allows you to burn existing items and simultaneously create new ones. This is useful for implementing upgrade systems or item combinations in games.
Example: Craft Items
import { HandCashConnect, HandCashMinter } from '@handcash/handcash-connect';
const handCashConnect = new HandCashConnect({
appId: '<app-id>',
appSecret: '<secret>',
});
const handCashMinter = HandCashMinter.fromAppCredentials({
appId: process.env.HANDCASH_APP_ID,
authToken: process.env.HANDCASH_AUTH_TOKEN,
appSecret: process.env.HANDCASH_APP_SECRET
});
async function craftItems(collectionId: string) {
// Get items from user's inventory
const handcashAccount = await handCashConnect.getAccountFromAuthToken('<user-auth-token>');
let items = await handcashAccount.items.getItemsInventory({
searchString: 'Rafa',
collectionId,
groupingValue: '0df774cab8b1c51d6d74fccd577fe436'
});
const itemsToBurn = items.slice(0, 2);
// Define the new item to create
const itemToCreate = {
// Define your new item properties here
};
// Burn 2 "Rafa" items to craft 1 "Alex" item
let burnOrderResult = await handCashMinter.burnAndCreateItemsOrder({
burn: {
origins: itemsToBurn.map((item: any) => item.origin),
},
issue: {
items: [itemToCreate],
collectionId: collectionId,
}
});
console.log('Craft Order Result:', burnOrderResult);
}
Important Considerations
- Supply Tracking: The supply of items is automatically tracked. When items are burned, the circulating supply decreases.
- Irreversible Process: Burning items is permanent and cannot be undone. Ensure proper validation before executing burn orders.
- User Authentication: Make sure you have the correct authentication key for the user whose items you’re manipulating.
- Error Handling: Implement proper error handling to manage cases where items might not exist or the user doesn’t have sufficient items to burn.
Best Practices
- Confirm Actions: Always confirm with users before burning their items, especially if it’s a valuable or rare item.
- Clear Communication: Clearly communicate the results of crafting to users, showing what items were used and what new item was created.
- Balanced Economy: Design your crafting system carefully to maintain a balanced item economy in your application.
- Logging: Keep detailed logs of all burn and craft operations for troubleshooting and auditing purposes.
Follow this example use case to create an Openable Card Pack