# Blockchain Solution for Buildings' Multi-Energy Flexibility Trading Using Multi-Token Standards
## 3 Materials and Methods
```  
**Algorithm 5** Using the token to determine the market session for placing a building order  
To facilitate the integration of different algorithms for matching of different markets, we store the newly created orders in a mapping that has as key the market session type (e.g., heat or electricity) and as value a secondary mapping (see Algorithm 6). The secondary market has as key the order side (buy or sell) and as value the array of order IDs. In this way, it is possible to retrieve all orders for a session market and side. ```
1. mapping (GridOperableLibrary.MarkSessionType1155
2. \(\Rightarrow\)mapping(GridOperableLibrary.OrderSide=>bytes32[]) internal_marketsTypeOrders;
3. enum MarketSessionType1155 (HEAT, ELECTRICITY)
```  
**Algorithm 6** ERC-1155 orders and market type  
By matching the bids and offers, registered trades are created. Each trade requires the order IDs for a buyer and a seller and their addresses, as well as an array of token IDs, quantities, and prices for energy flexibility (see Algorithm 7). ```
1. struct Trade1155{
2. bytes32 id;
3. bytes32 buyOrderId;
4. bytes32 sellOrderId;
5. address payable prosumerBuyingAddress;
6. address payable prosumerSellingAddress;
7. uint1 energyTokenIds;
8. uint1 quantities;
9. uint1 prices;
10. } ```  
**Algorithm 7** ERC-1155 flexibility trade structure  
```
1. struct Trade1155{
2. bytes32 id;
3. bytes32 buyOrderId;
4. bytes32 sellOrderId;
5. address payable prosumerBuyingAddress;
6. address payable prosumerSellingAddress;
7. uint1 energyTokenIds;
8. uint2 quantities;
9. uint3 prices;In the case of other protocols such as ERC-20, we would have multiple trades independently for each session. The ERC-1155 protocol instead allows for multiple independent tokens to be transferred in batches between the same sender and receiver addresses. This approach allows us to have all energy token transfers between partners as well as the fungible currency transfer carried out once with the total due, rather than having to do multiple transfers. Algorithm 8 shows the settlement process that takes, as a parameter, a list of trades and, for each trade with the same buyer and seller addresses, a single batch operation will be performed to transfer all energy tokens, in their respective quantities (lines 6-9). The fungible currency transfer is also carried out once with the total due, rather than having multiple transactions between the same partners (once for each session market). The balances change as energy tokens are transferred, and the money token balance reflects this change for both the buyer and seller. ```
1. function settle ( GridOperableLibrary.Trade1155[] memory trades) external override {
2. for (uint i:i-trades.length; i++)}
3. GridOperableLibrary.Trade1155 memory trade = trades[i];
4. GridOperableLibrary.Order1155 memory sellingOrder = _orders[trade.sellOrderId];
5. GridOperableLibrary.Order1155 memory buyingOrder = _orders[trade.buyOrderId];
6. bytes memory y = abi.encode(0);
7. this.safeBatchTransferFrom(trade.prosumerSellingAddress, trade.prosumerBuyingAddress, trade.quantities, y);
8. this.safeTransferFrom(trade.prosumerBuyingAddress, trade.prosumerSellingAddress, MONEY_TOKEN, trade.totalDue, y);
9. } 10. } ```
**Algorithm 8** Transaction settlement and using ERC-1155 batch transactions

## 4 Evaluation Results

In this section, we evaluate the above ERC-1155 solution in the context of the decentralized peer-to-peer energy flexibility markets for heat and electricity that we have reported on in [26]. The smart contracts are implemented in Solidity programming language and were tested using the Ethereum platform in the Remix IDE. This approach allows testing on multiple chains (already available in the Remix workspace). In our evaluation, the Remix VM (Merge) environment chain was used to run all scenarios because of its sandbox blockchain properties, meaning that, each time the blockchain is reloaded, it is in the same state as the last time it was started.