# Leveraging Blockchain Usage to Enhance Slag Exchange
## 4 Results
### Implementation  
To implement the use case described, we extended the Traceblock traceability platform [28]. Traceblock is a framework written in Golang [29] which helps to create smart contracts that can be deployed in a Hyperledger Fabric network [30] to trace assets for different use cases and industries. Figure 3 shows the base data model that Traceblock offers to track an asset. Each asset is individually identified and has a _type_, a _quantity_ associated to the specified _units_, composition information (i.e., _parents_ and _children_), a _location_, the current _owner_ and some status information about its creation, last modification or removal (if it applies). This model can be extended through the _fields_ field, which can contain any arbitrary data. In particular, we use it to include a customized status (_slag_, _hatched_, _stocked_, _sold_ or _discarded_), the composition (as a range of maximum and minimum _CaO_ percentage in the stock) and a list of the bids received for the slag stock. Each _Bid_ contains a quantity, a unitary price, bidder information (automatically recorded by the smart contract) and its status (i.e., _proposed_, _accepted_ or _rejected_). Traceblock offers some off-the-shelf operations which are used in the implementation like _transfer_, _join_ or _split_. _Join_ and _split_ allow one to aggregate and separate slags in batches and stocks to the owner of the asset (i.e., the steel company in the beginning). _Transfer_ is used to change the owner of an asset. Figure 3: Traceblock data model and its extension for this paper (in blue). Apart from these generic operations, we implemented \(bid\), \(bid-accept\), \(bid-reject\) and \(stats\). \(Bid\) is the operation used by cement companies to bid for an specific slag stock. If the amount bid is greater to the available stock, the bid is automatically rejected. Otherwise, this operation appends a \(Bid\) object to the \(bids\) array inside the \(fields\) field. \(Bid-reject\) simply marks a given bid as \(rejected\). Algorithm 1 describes the \(bid-accept\) operation in pseudo-code. ```
Function bid-accept(stockId, winner): \(bidableAsset\gets findStock(stockId)\) \(bid\gets findByBidder(bidableAsset.fields.bids,winner)\) \(soldAsset\gets bidableAsset\) if\(bid.quantity<soldAsset.quantity\)then remainingQuantity\(\leftarrow\)\(bidableAsset.quantity-bid.quantity\) \(children\gets split(bidableAsset,remainingQuantity)\) \(soldAsset\gets children[1]\)  end if\(soldAsset.fields.status\leftarrow\)\"\(sold\)\" \(soldAsset.owner\gets winner\) \(registerSale(soldAsset)\)
```  
**Algorithm 1**Function bid-accept. The \(registerSale\) function registers statistical information about the operation in the smart contract. This information is later used to provide aggregated information to public administrations as a result of calling the \(stats\) operation. Figure 4 shows the data structures used to store this information. These data structures are not linked to the \(TraceableAsset\) model. The list of steel and cement companies are stored in the \(SteelOrganizations\) and \(CementOrganizations\) as arrays. Each steel and cement company have their raw stats stored in \(RawSteelOrgStats\) and \(RawCementOrgStats\) respectively. Whenever the \(stats\) operation is invoked, raw data are refined into \(SteelStats\) and \(CementStats\) for each organization. In Hyperledger Fabric, the certificate authority (CA) of each organization issues credentials to its users. The smart contract can read the user who signed each transaction and it can access the attributes associated to her certificate. These attributes are taken into account to control the access to the different operations as described by Figure 2. Specifically, it considers the type of organization and the role of the user. To ensure both conciseness and transparency, the code developed for this paper has been publicly shared [31]. This code was analyzed with Sonarqube [32]. No bugs or vulnerabilities were detected. The code had 23 code smells, and only 3 of them major.