# Blockchain Solution for Buildings' Multi-Energy Flexibility Trading Using Multi-Token Standards
## 3 Materials and Methods
```
1. function registerOrder(GridOperableLibrary.Order1155 memory order) external override returns (bytes32){
2....
3. If (order. orderType==GridOperableLibrary.OrderType.OFFSET){
4. for (uint i:i+order.tokenIs.length++){
5....mint (order.prosumerAddress, order.tokenIs[i], order.quantities[i]);
6. } 7. } 8...
9. orderid = bytes32(orderSize++);
10. _orders[orderid] = order;
11. for (uint k =0; k<order.involvedMarkets.length;k++){
12. _marketsTypeOrders [GridOperableLibrary.MarkeSessionType1155(k)][or derorderSidepush(orderid);d)
13. emit OrderRegister(order.prosumerAddress,order.id);
14. return order.id;
15. } ```  
**Algorithm 2** Function for registering heat or electricity orders  
Because our work represents the energy in token format, in our design, we also mint non-fungible energy tokens when a building places them in an offer of energy (see Algorithm 2, lines 3-7). By doing so, once the settlement is reached, the producer can easily transfer the energy tokens to the buyer. The order will be added to the orders' mapping and emit a blockchain event (see lines 9-14). For ease of retrieval, the order will be added to all relevant markets based on the order type. This approach allows for the overlap between sessions regarding an order that contains multiple types of energies. As we want a flexible system, different properties of the market sessions are delegated to smart contract functions. Algorithm 3 shows an example for the reference price property of the market session. The smart contract function will check all session types (i.e., heat or electricity) and, once one is identified as relevant based on the token type information at hand, it will delegate it to the appropriate address. ```
1. function determineReferencePrice (uint tokenId) public returns (uint){
2....
3. If (checkMask(tokenId,
4. marketSessionMap [GridOperableLibrary.MarkeSessionType1155(j).tokenMask)){
5. Return AssetMarketSession1155(marketSessionMap [MarketSessionType1155(j)]. 6. marketSession).getReferencePrice();
7....
8. } ```  
**Algorithm 3** Determine the market session properties based on the token type. To participate in energy trading, one needs to be registered as a prosumer. This is accomplished by calling the smart contract function from Algorithm 4 with the prosumer \begin{table}
\begin{tabular}{l l l l l} \hline
**Condition** & **No. Bits** & **Mask Position** & **Scenario and Alternatives** & **Precondition** \\ \hline NFT/FT token & 1 & 255 & Used for checking ERC20 compliance \\ Hour & 5 & 250–254 individually determined & 24 h of the day for which the order is submitted & Is an NFT \\ Energy type & 4 & 226–230 & Electricity or heat & Is an NFT \\ \hline \end{tabular}
\end{table} 
Table 1: Masks for determining token properties. certification and payment proof for deposit--in either case, for consuming or promising energy. This function will then mint the fungible tokens based on ERC-1155 for this prosumer. ```
1. function registerProsumer(address prosumerAddress, address prosumerCertificate, uint amount) returns (bool){
2. if (isValidCertificate(prosumerAddress,prosumerCertificate))
3. if (checkDeposit(prosumerAddress, amount)){
4. _mint(prosumerAddress, MONEY_TOKEN, amount); return true;}
5. return false;
6. } ```  
**Algorithm 4** Using ERC-1155 for creating tokens for energy payment  
Finally, the token bits can be used to determine the type of market (electricity or heat) on which the building energy order needs to be submitted (see Algorithm 5). ```
1. function determineInvolvedMarkets (uint[] memory tokens) public returns (uint[] memory){
2....
3. for (uint i = 0; i< tokenIds.length++){
4. for (int i = 0; i< sizej++){
5. if(checkMask(tokenIds[i], marketSessionMap [GridOperableLibrary. MarketSessionType1155()].tokenMask)){
6. GridOperableLibrary.MarkSessionType1155 typeSession = GridOperableLibrary.MarkSessionType1155();
7. Session memory session = marketSessionMap[typeSession];
8. involvedMarkets[counter] =uint(session.sessionType);
9. } 10. } 11. return involvedMarkets
13. }