However, this is would not work. How do we define/pre-allocate an address automatically to each item on creation? We do not control address creation and allocation on the EVM.
Solution
To get a unique address per item -> each item has its own smart contract. Therefore, the create() should result in deployment on a new item AND its smart contract.
We will split the code up, keeping the item smart contract as a separate child contract.
//SPDX-License-Identifier: MITpragmasolidity ^0.8.1;import"./ItemManager.sol";contract Item {uintpublic priceInWei;uintpublic paidWei;uintpublic index;//variable of type ItemManager -> contract object. ItemManager parentContract; // creation requires inputs: price & index.constructor(ItemManager_parentContract,uint_priceInWei,uint_index) public { priceInWei = _priceInWei; index = _index; parentContract = _parentContract; }receive() externalpayable {require(msg.value == priceInWei,"We don't support partial payments");require(paidWei ==0,"Item is already paid!"); paidWei += msg.value; (bool success, ) =address(parentContract).call{value:msg.value}(abi.encodeWithSignature("triggerPayment(uint256)", index));require(success,"Delivery did not work"); }fallback () external { }}
//SPDX-License-Identifier: MITpragmasolidity ^0.8.1;contract ItemManager {enumitemState{ Created, Paid, Delivered //created - 0, paid -1,.. }structItem { Item _item; //_item variable is contract object? by above logicstring id;uint price; itemState state; }// to create a dataframe-like structuremapping(uint=> Item) public item_list;uint item_index;eventSupplyChainStep(uint _index, uint _state);functioncreateItem(stringmemory_id,uint_price) public { Item item =newItem(this, _price, item_index); //create new Item contract. this: the current contract, explicitly convertible to address item_list[item_index]._item = item; item_list[item_index].id = _id; item_list[item_index].price = _price; item_list[item_index].state = itemState.Created;emitSupplyChainStep(item_index,uint(item_list[item_index].state)); item_index++; }functiontriggerPayment(uint_index) publicpayable {require(item_list[_index].price == msg.value,"please pay exact full amount");require(item_list[_index].state == itemState.Created,"Item is not available"); item_list[_index].state = itemState.Paid; //update state to paid// emit payment eventemitSupplyChainStep(item_index,uint(item_list[item_index].state)); }functiontriggerDeliver(uint_index) public {require(item_list[_index].state == itemState.Paid,"Item is not for delivery"); item_list[_index].state == itemState.Delivered;// emit deliveryemitSupplyChainStep(item_index,uint(item_list[item_index].state)); }}
How do these 2 contracts interact?
createItem
When createItem() is called Item item = new Item(this, _price, item_index), creates a new instance of the Item contract based on the price provided and index counter.
this : the current contract, explicitly convertible to address.
this is passed into the child contract's constructor (Item{}), for the field ItemManager _parentContract, where it eventually assigned to variable parentContract
parentContract = _parentContract;
// evaluates to:
parentContract = this;
This is to store the parent contract association.
struc Item {}
struc Item now has Item _item; which is a contract object. It is assigned value under the createItem(), after the new item contract object has been created.
item_list[item_index]._item = item;
Thoughts
This approach allows us to create an item using the parent contract, assign values to both parent and child