Skip to main content

Merchant Contract

The Vidya Merchant contract (IMerchantV1) enables direct sales of inventory items for ETH with role-based shop management.

Contract Interface

interface IMerchantV1 {
struct Merchandise {
uint256 tokenId;
uint256 unitPrice;
uint256 quantity;
uint256 sold;
bool isActive;
bool isSoldOut;
}
}

Functions

View Functions

SHOP_ROLE()

function SHOP_ROLE() external view returns (bytes32)

Returns the shop keeper role identifier.

treasury()

function treasury() external view returns (address)

Returns the current treasury address where funds are sent.

inventory1155()

function inventory1155() external view returns (address)

Returns the address of the connected inventory contract.

getUnitPrice(uint256 merchandiseId)

function getUnitPrice(uint256 merchandiseId) 
external view returns (uint256 unitPrice, bool isActive)

Gets the price and status of a merchandise item.

Parameters:

  • merchandiseId: The merchandise ID to query

Returns:

  • unitPrice: Price per unit in wei
  • isActive: Whether the item is currently for sale

getMerchandise(uint256 merchandiseId)

function getMerchandise(uint256 merchandiseId) 
external view returns (Merchandise memory)

Returns complete merchandise information.

Parameters:

  • merchandiseId: The merchandise ID to query

Returns: Merchandise struct containing all item data

getMerchandiseCount()

function getMerchandiseCount() external view returns (uint256)

Returns the total number of merchandise items.

State-Changing Functions

setTreasury(address _treasury)

function setTreasury(address _treasury) external

Updates the treasury address. Requires ADMIN_ROLE.

Parameters:

  • _treasury: New treasury address

setInventory1155(address _inventory1155)

function setInventory1155(address _inventory1155) external

Updates the inventory contract address. Requires ADMIN_ROLE.

Parameters:

  • _inventory1155: New inventory contract address

setMerchandiseActive(uint256 merchandiseId, bool isActive)

function setMerchandiseActive(uint256 merchandiseId, bool isActive) external

Activates or deactivates a merchandise item. Requires SHOP_ROLE.

Parameters:

  • merchandiseId: The merchandise to update
  • isActive: New active status

setMerchandiseUnitPrice(uint256 merchandiseId, uint256 unitPrice)

function setMerchandiseUnitPrice(uint256 merchandiseId, uint256 unitPrice) external

Updates the price of a merchandise item. Requires SHOP_ROLE.

Parameters:

  • merchandiseId: The merchandise to update
  • unitPrice: New price in wei

addMerchandise(uint256 tokenId, uint256 unitPrice, uint256 quantity)

function addMerchandise(uint256 tokenId, uint256 unitPrice, uint256 quantity) external

Lists a new item for sale. Requires ADMIN_ROLE.

Parameters:

  • tokenId: Inventory token ID to sell
  • unitPrice: Price per unit in wei
  • quantity: Initial stock quantity

buyMerchandise(uint256 merchandiseId, uint256 quantity)

function buyMerchandise(uint256 merchandiseId, uint256 quantity) external payable

Purchases merchandise items.

Parameters:

  • merchandiseId: Item to purchase
  • quantity: Amount to buy

Requirements:

  • Must send exact ETH amount (unitPrice * quantity)
  • Item must be active
  • Sufficient stock available

buyMerchandiseBatch(uint256[] memory merchandiseIds, uint256[] memory quantities)

function buyMerchandiseBatch(
uint256[] memory merchandiseIds,
uint256[] memory quantities
) external payable

Purchases multiple merchandise items in one transaction.

Parameters:

  • merchandiseIds: Array of merchandise IDs
  • quantities: Array of quantities to purchase

Requirements:

  • Arrays must be same length
  • Must send exact total ETH amount
  • All items must be active with sufficient stock

restockMerchandise(uint256 merchandiseId, uint256 quantity)

function restockMerchandise(uint256 merchandiseId, uint256 quantity) external

Adds more stock to existing merchandise. Requires SHOP_ROLE.

Parameters:

  • merchandiseId: Item to restock
  • quantity: Additional quantity to add

withdraw()

function withdraw() external

Withdraws contract balance to treasury. Requires ADMIN_ROLE.

Events

event TreasuryUpdated(address indexed oldTreasury, address indexed newTreasury);
event InventoryUpdated(address indexed oldInventory, address indexed newInventory);
event MerchandiseAdded(
uint256 indexed merchandiseId,
uint256 indexed tokenId,
uint256 unitPrice,
uint256 quantity
);
event MerchandisePurchased(
uint256 indexed merchandiseId,
address indexed buyer,
uint256 quantity,
uint256 totalPrice
);
event MerchandiseBatchPurchased(
address indexed buyer,
uint256[] merchandiseIds,
uint256[] quantities,
uint256 totalPrice
);
event MerchandiseRestocked(
uint256 indexed merchandiseId,
uint256 addedQuantity,
uint256 newTotalQuantity
);
event MerchandiseStatusChanged(uint256 indexed merchandiseId, bool isActive);
event MerchandisePriceUpdated(
uint256 indexed merchandiseId,
uint256 oldPrice,
uint256 newPrice
);
event Withdrawal(address indexed to, uint256 amount);

Access Roles

The contract uses two main roles:

  • ADMIN_ROLE: Can add merchandise, set treasury/inventory, and withdraw funds
  • SHOP_ROLE: Can update prices, restock, and activate/deactivate items

Usage Example

// Adding new merchandise (requires ADMIN_ROLE)
merchant.addMerchandise(
swordTokenId, // token ID from inventory
0.1 ether, // price per sword
100 // initial stock
);

// Purchasing items
uint256 quantity = 2;
uint256 totalPrice = 0.1 ether * quantity;
merchant.buyMerchandise{value: totalPrice}(merchandiseId, quantity);

// Batch purchase
uint256[] memory ids = [swordId, shieldId, potionId];
uint256[] memory quantities = [1, 1, 5];
uint256 totalCost = calculateTotalCost(ids, quantities);
merchant.buyMerchandiseBatch{value: totalCost}(ids, quantities);

// Shop management (requires SHOP_ROLE)
merchant.setMerchandiseUnitPrice(merchandiseId, 0.08 ether); // discount
merchant.restockMerchandise(merchandiseId, 50); // add 50 more
merchant.setMerchandiseActive(merchandiseId, false); // temporarily disable

Error Conditions

The contract will revert if:

  • Insufficient ETH sent for purchase
  • Merchandise is not active
  • Insufficient stock available
  • Unauthorized role attempting admin/shop functions
  • Invalid array lengths in batch operations