blog

Decentralized marketplace using smart contracts

The SOFIE project will run four pilots in three diverse fields: energy, food-chain, and mixed reality mobile gaming. In all of these fields there is a demand for having a marketplace, in which actors (companies, public services, end users, etc.) can offer their services and products to each other. Taking this idea further, a decentralized marketplace is a marketplace that doesn't have a single entity owning or managing it, which in turn enhances its security, resiliency, transparency, and traceability. The decentralized marketplace can be partially decentralized, where for example a group of independent agriculture producers and retailers are managing it, or a fully decentralized where anyone can join and use the marketplace. Distributed ledger technologies (DLTs) allow creation of both types of decentralized marketplaces. In this post we describe our work on (partially) decentralized marketplaces using smart contracts.


In the SOFIE project, we have implemented a proof of concept decentralized marketplace using smart contracts on top of the Ethereum blockchain. The marketplace contains a set of data (such as the stipulations of requests and offers, details of services and products, history of the previous transactions, information about the managers, regulations of the marketplace, etc.), and operations with different access permissions (adding new requests and offers, changing the states of those requests and offers, selecting the best set of offers for a request based on predefined criteria, modifying the roles of managers, getting information about different entities of the marketplace, etc.). By calling the functions of the marketplace’s smart contracts, different parties can observe the state of the marketplace, make transactions they have permission for, and subsequently change the state of the marketplace.

We have detected the potential similarities in features that SOFIE pilots demand for their specific marketplaces. Based on those similarities, we have designed a generic decentralized marketplace which can be further extended to meet the specific requirements of SOFIE pilots. Furthermore, we have developed smart contracts of this generic marketplace to be deployed on Ethereum blockchain, and also tested the integrity and performance of the smart contracts using Truffle development framework. In addition, we have developed a backend to interact with the smart contracts of the marketplace. Below we describe development tools used.

For the development of smart contracts for decentralized marketplace, we used Solidity programming language, which is the most popular and recommended contract-oriented language for Ethereum. It has major similarities to Java and JavaScript, which makes it easy for the programmer of other languages to get acquainted with it. However, the current implementation of Solidity has some flaws. It does not pass a sufficient information about the error when it happens, which makes it difficult to write proper error handlers. As a workaround, we had to add the “status code” parameter to the return value of all functions in our smart contracts. Furthermore, Solidity does not provide a way to externally pass structs to/from public functions. Moreover, there are some restrictions regarding defining structs in interfaces, which to some extent hindered us from further generalizing our smart contracts. Solidity is still under development, and we hope these issues be addressed in the future.

In order to compile, deploy, and test our Solidity smart contracts, we used Truffle development framework. This framework is easy to use, and smart contracts can be compiled and deployed using simple commands. Truffle is compatible with live blockchain test networks, which can be used for deployment of smart contracts.

For testing purposes, we used Ganache, a personal Ethereum blockchain that was created by Truffle developers. The friendly UI of Ganache made it easy for us to observe the process and transactions during testing.

Truffle also makes it possible to write tests in Solidity or JavaScript and provides libraries to streamline developing tests, and accessing deployed contracts from the test files. Overall, in our experience Truffle significantly simplifies smart contract development process  and we recommend it for the future use.

We will release the source code of our decentralized marketplace implementation in the SOFIE project’s GitHub repository by the end of this year:

https://github.com/SOFIE-project.