Inventory Contract
The Vidya Inventory contract (IInventoryV1155) is an ERC1155 multi-token contract that manages in-game items with role-based access control and attribute system.
Contract Interface
interface IInventoryV1155 is IERC1155, IAccessControl {
struct Item {
uint256[] attributeData;
uint256[] attributeId;
string tokenURI;
uint256 characterSlot;
}
}
Functions
View Functions
tokenID()
function tokenID() external view returns (uint256)
Returns the current token ID counter.
uri(uint256 tokenId)
function uri(uint256 tokenId) external view returns (string memory)
Returns the metadata URI for a specific token.
Parameters:
tokenId: The ID of the token
tokenExist(uint256 tokenId)
function tokenExist(uint256 tokenId) external view returns (bool)
Checks if a token ID exists.
Parameters:
tokenId: The ID to check
getCharacterSlot(uint256 tokenId)
function getCharacterSlot(uint256 tokenId) external view returns (uint256)
Returns the equipment slot for an item.
Parameters:
tokenId: The item's token ID
Returns: Slot number (0-11)
itemAttributeIdDetail(uint256 tokenId, uint256 attributeId)
function itemAttributeIdDetail(uint256 tokenId, uint256 attributeId)
external view returns (uint256)
Gets a specific attribute value for an item.
Parameters:
tokenId: The item's token IDattributeId: The attribute ID to query
getItemAttributes(uint256 tokenId)
function getItemAttributes(uint256 tokenId)
external view returns (uint256[] memory, uint256[] memory)
Returns all attributes for an item.
Parameters:
tokenId: The item's token ID
Returns:
- Array of attribute IDs
- Array of attribute values
fullBalanceOf(address account)
function fullBalanceOf(address account) external view returns (uint256[] memory)
Returns all token balances for an account.
Parameters:
account: The address to query
Returns: Array of token balances
State-Changing Functions
addItem(Item memory newItem)
function addItem(Item memory newItem) external
Creates a new item type. Requires ADMIN_ROLE.
Parameters:
newItem: Item struct containing:attributeData: Array of attribute valuesattributeId: Array of attribute IDstokenURI: Metadata URIcharacterSlot: Equipment slot (0-11)
updateItemData(Item memory updateItem, uint256 tokenId)
function updateItemData(Item memory updateItem, uint256 tokenId) external
Updates an existing item's data. Requires ADMIN_ROLE.
Parameters:
updateItem: Updated item datatokenId: The token ID to update
mint(address to, uint256 tokenId, uint256 amount)
function mint(address to, uint256 tokenId, uint256 amount) external
Mints tokens to an address. Requires MINTER_ROLE.
Parameters:
to: Recipient addresstokenId: Token ID to mintamount: Quantity to mint
mintBatch(address to, uint256[] memory ids, uint256[] memory values)
function mintBatch(address to, uint256[] memory ids, uint256[] memory values) external
Mints multiple token types in one transaction. Requires MINTER_ROLE.
Parameters:
to: Recipient addressids: Array of token IDsvalues: Array of amounts to mint
burn(address from, uint256 id, uint256 value)
function burn(address from, uint256 id, uint256 value) external
Burns tokens from an address. Requires appropriate permissions.
Parameters:
from: Address to burn fromid: Token ID to burnvalue: Amount to burn
burnBatch(address from, uint256[] memory ids, uint256[] memory values)
function burnBatch(address from, uint256[] memory ids, uint256[] memory values) external
Burns multiple token types in one transaction.
Parameters:
from: Address to burn fromids: Array of token IDsvalues: Array of amounts to burn
Events
event ItemAdded(uint256 indexed tokenId, address admin);
event ItemUpdated(uint256 indexed tokenId, address admin);
- ItemAdded: Emitted when a new item type is created
- ItemUpdated: Emitted when item data is updated
Errors
error ItemDataAndIDMisMatch(address admin, uint256 length);
error TokenDoesNotExist(uint256 tokenId);
- ItemDataAndIDMisMatch: Attribute data and ID arrays have different lengths
- TokenDoesNotExist: Attempted operation on non-existent token
Access Roles
The contract uses OpenZeppelin's AccessControl with two main roles:
- ADMIN_ROLE: Can create and update item types
- MINTER_ROLE: Can mint new items to addresses
Character Slots
Items can be assigned to specific equipment slots:
| Slot | Value | Description |
|---|---|---|
| None | 0 | Unequipped items |
| Weapon | 1 | Primary weapon |
| Shield | 2 | Shield/Off-hand |
| Helmet | 3 | Head protection |
| Armor | 4 | Body armor |
| Gauntlets | 5 | Hand armor |
| Boots | 6 | Foot armor |
| Amulet | 7 | Neck accessory |
| Ring | 8 | Finger accessory |
| Consumable | 9 | Potions, food, etc. |
| Material | 10 | Crafting materials |
| Quest | 11 | Quest items |
Standard ERC1155 Functions
The contract implements all standard ERC1155 functions including:
safeTransferFromsafeBatchTransferFrombalanceOfbalanceOfBatchsetApprovalForAllisApprovedForAll
Usage Example
// Creating a new sword item (requires ADMIN_ROLE)
Item memory sword = Item({
attributeData: [100, 10, 5], // damage, durability, weight
attributeId: [1, 2, 3], // attribute type IDs
tokenURI: "ipfs://...", // metadata URI
characterSlot: 1 // weapon slot
});
inventory.addItem(sword);
// Minting swords to player (requires MINTER_ROLE)
inventory.mint(playerAddress, swordTokenId, 1);
// Batch minting multiple items
uint256[] memory ids = [swordId, shieldId, potionId];
uint256[] memory amounts = [1, 1, 5];
inventory.mintBatch(playerAddress, ids, amounts);