Skip to main content

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 ID
  • attributeId: 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 values
    • attributeId: Array of attribute IDs
    • tokenURI: Metadata URI
    • characterSlot: 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 data
  • tokenId: 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 address
  • tokenId: Token ID to mint
  • amount: 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 address
  • ids: Array of token IDs
  • values: 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 from
  • id: Token ID to burn
  • value: 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 from
  • ids: Array of token IDs
  • values: 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:

SlotValueDescription
None0Unequipped items
Weapon1Primary weapon
Shield2Shield/Off-hand
Helmet3Head protection
Armor4Body armor
Gauntlets5Hand armor
Boots6Foot armor
Amulet7Neck accessory
Ring8Finger accessory
Consumable9Potions, food, etc.
Material10Crafting materials
Quest11Quest items

Standard ERC1155 Functions

The contract implements all standard ERC1155 functions including:

  • safeTransferFrom
  • safeBatchTransferFrom
  • balanceOf
  • balanceOfBatch
  • setApprovalForAll
  • isApprovedForAll

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);