# Blockchain Solution for Buildings' Multi-Energy Flexibility Trading Using Multi-Token Standards
## 3 Materials and Methods  
We define our solution for trading building multi-carrier energy flexibility by minting and integrating ERC-1155 electrical and heat tokens with P2P energy flexibility markets (see Figure 2), such as the one described in [26]. The buildings forecast of electricity and heat for the next day is used to generate heat and electricity orders (i.e., either bids or offers). They are placed in the flexibility market session and, for each order, a deposit with fungible tokens will be locked to ensure that everyone can afford and pay for their orders. The building producing electricity and heat places an offer, and non-fungible tokens representing this promised amount of energy for each hour and energy type will be minted. After a session is closed, the matching is performed. Based on the result of the matching, some orders will be matched completely, matched partially, or unmatched. For all of the producer's orders, for token quantities that are unmatched, the corresponding tokens will be burnt and the deposits unlocked. In the case of matched orders, the settlement will take care of transferring the energy tokens and the transfer of currency tokens to the building that produced the heat or electricity. As the energy tokens are 'promise tokens'--that is, the energy was not yet produced when the token was minted--a system is required to attach an energy certificate that will allow the producer to retrieve its deposit at the reference price when it produces the energy it promised. An order submitted by a building in the market session is comprised of an order ID, for identification purposes, as well as the order type, prosumer address, and the three same-length arrays (see Algorithm 1): token IDs array (i.e., defining energy type and moment of the day), quantities array, and prices array. Figure 2: Trading different types of energy using ERC-1155 and smart contracts. Our design embraces two concepts. First, the ability to differentiate between different hours of the day and energy types using the higher part of the 256 bits, and second, the ability to place orders with different prices for different hours. To better understand how ERC-1155 can better serve our purpose, we combined the non-fungible tokens with additional meaning stored in the token ID. In our approach, we used masking and C/C++ bitwise operators [47] to check the properties of the token (see Figure 3). The operators are performed on uint 256 bits for the token ID. We use the higher half bits of the token ID for token characteristics of the token, such as fungible or non-fungible, type of energy digitized (i.e., either heat or electricity), and the corresponding hour. As depicted in Figure 4, using different masks, we can check that the token in the example is not fungible and depicts a token for 1 a.m. of the energy type heat. For checking multiple properties, we create different masks (one per property) and perform a bitwise OR operation. In this way, all true conditions are passed to the final mask (see Table 1). When we want to check against the mask, we perform the bitwise AND operation to ensure that everything contained in the mask is also found in the checked token. This approach allows high flexibility regarding energy digitized by a building as tokens that can be submitted to a market session. Figure 4: Using masks to determine the properties of an energy token. Figure 3: Check hour and energy type digitized by the token. For implementing the trading process, an interface named GridOperable was designed. It contains a function for registering an order and one for applying the settlement. Using an interface, each smart contract that is ERC-1155 compliant in our model can decide how a specific trading scenario should be handled. The advantage of this approach is that, by implementing the interface, a building can define and submit a single order using the function described in Algorithm 2, by aggregating for each hour of the day tokens of different energy types.