📜 ⬆️ ⬇️

Rewrite the application under Blockchain

Before and after


I will note right away that this article is not about how to write code for Solidity, but how the existing classical architecture of your application can be translated into blockchain rails and thought in the key of decentralization.


A couple of years ago I was working on one interesting web application for the p2p service of parcel delivery. For some reason, the development had to be frozen at the prototype stage, so I just put the source code on GitHub and forgot about it.


Recently, by the nature of my work, I happened to work with several projects related to cryptocurrency and blockchain technologies. Having got acquainted closer with Ethereum and its ideology of decentralized applications (ĐApp) I just got sick of this idea: no censorship, no one can hide your business, no one can confiscate your funds, it’s impossible to just turn off and turn off the server on which your application is running. At a certain point, I came to the conclusion that it was in such an environment that my project could have chances to live.


So, take a look at the front works.


Front of works


The original idea of ​​the project was that people who travel frequently could carry something in their suitcases or cars. A sort of Uber for delivery. Users were divided into travelers (donkeys) and customers. If you are going to go, for example, from Moscow to Minsk for this weekend, you can add a new trip (trip) in the service describing what you can carry and when. Clients who subscribed to this direction received a notification about a new trip and could add delivery requests, such as some kind of medicine or an iPad for mom. The parties agreed on payment (samopisny escrow on PayPal) and delivery terms. After the execution of the order, the parties could leave ratings and comments about each other. In the profile of each user there was a small statistic.


Over time, there was a rethinking of some points in the algorithm of the service. It became clear that it would be right if the customers first added their shipping orders, and the donkeys would have already chosen whom to provide the service. Next, you will see how this concept provides flexibility and scalability.


In fact, we need to rewrite the entire back-end into smart contracts and clap on the blockchain. The application will be completely open, immutable (immutable), decentralized and, therefore, free from regulation, even by the developer. Thus, we get a decentralized platform on the basis of which anyone can realize their front-end, mobile or server application.


The protocol of work of such a service in a brief form will look as follows:



Let's get started


This protocol will be implemented by a smart contract for Solidity, which we will call Osliki. To begin with, we will create variables for storing orders, offers, invoices, and statistics:


Order[] public orders; //  Offer[] public offers; //  (       ) Invoice[] public invoices; //  mapping (address => Stat) internal stats; //       

Description of all structures:


 struct Order { address customer; // ethereum-  string from; //     "lat,lon"  ethereum- "0x..." (   , ,    )     (    ,   ) string to; //      string params; //     "(),(),(),()" uint expires; //    , Unix-   string message; //      uint[] offerIds; //  id  address carrier; // ethereum-      uint invoiceId; //    EnumOrderStatus status; //   uint createdAt; //   uint updatedAt; //   } struct Offer { address carrier; // ethereum-  uint orderId; // id  string message; //      string respond; //       . uint createdAt; uint updatedAt; } struct Invoice { address sender; // ethereum-  uint orderId; // id  uint prepayment; //    (  0) uint deposit; //    (  0) uint expires; //    EnumCurrency currency; //      bytes32 depositHash; //  Ethereum-SHA-3 (Keccak-256)   (     ) EnumInvoiceStatus status; //   uint createdAt; uint updatedAt; } struct Stat { uint[] orders; //  id           uint rateSum; //   uint rateCount; //  ,   averageRate = rateSum / rateCount mapping (uint => Review) reviews; //  id        } struct Review { uint8 rate; //   1  5 string text; //   uint createdAt; } 

Statuses:


 enum EnumOrderStatus { New, Process, Fulfilled } enum EnumInvoiceStatus { New, Settled, Closed, Refund } enum EnumCurrency { ETH, OSLIK } //     ( 1%)      OSLIK ( ) 

Further, I will not bring the body functions, otherwise it will take too much space. Below is a link to the source code on GitHub.


Customer adds Order:


 function addOrder( string from, // "" || "lat,lon" || ethereum- "0x..." string to, // "" || "lat,lon" || ethereum- "0x..." string params, // "weight(kg),length(m),width(m),height(m)" uint expires, string message ) public; 

Donkey adds a sentence:


 function addOffer( uint orderId, string message ) public; 

The client sends the answer:


 function respond( uint offerId, string message ) public; //      

Donkey sends invoice:


 function addInvoice( uint orderId, uint prepayment, uint deposit, EnumCurrency currency, // ETH || OSLIKI uint expires ) public; 

The client pays the bill:


 function pay( uint invoiceId, bytes32 depositHash //    keccak256     ,            ) public payable; //  ,              

Donkey fulfills the Order. For example, upon delivery, the Donkey scans the QR code of the Client, in which the Deposit Key is sewn:


 function fulfill( uint orderId, string depositKey //       depositHash,       ,         '' ) public; 

Donkey returns funds to the Client if he is not satisfied or something went wrong in the process:


 function refund( uint invoiceId ) public payable; 

Parties can add feedback about each other with an assessment of the impression of the Order:


 function addReview( uint orderId, uint8 rate, string text ) public; //     

Plus a bunch of data access functions. As a result, we have 2 contracts:



Examples


In fact, the Osliki contract is an open database of delivery orders that can be used by any individual, organization or any drone like Amazon Prime Air .


Imagine an entrepreneur with a fleet of cargo drones based on the roof of a building in your city. Drones monitor the database in the blockchain and if the order meets certain specific parameters (for example, an acceptable range and load dimensions), automatically send offers and invoices, and then fly to fulfill orders.


Imagine Mr. X from Craiglist, who grows a particularly fragrant cannabis variety in his country house (of course in countries where it is legal). He throws you a link where you can add an order to Osliki, indicating Mr. X’s ethereum address directly, so that other donkeys do not spam with offers. Further, the bill, payment and now the parcel you have on hand. And even if Mr. X is locked at Craigliste, fans of his gardening talent will always remember where to send orders.


You can imagine a marketplace where farmers sell their biological non-GMO vegetables “fresh from the garden”, fresh milk and meat. Delivery can be carried out, for example, by drivers plying from the suburbs to the city. Farmers will thus have unlimited access to retail customers, bypassing supermarkets.


Well, the original idea of ​​travelers carrying parcels between cities and countries also remains relevant.


Plans


As part of the platform, a decentralized marketplace (Osliki Marketplace) or Osliki Classifieds would fit in well here. Or perhaps use ready-made solutions.


Using the BigData and AI methods, it is possible to analyze more deeply the behavior and statistics of each user and give out results about his reliability. For example, you can identify users who have scored a rating.


At the moment, there is a task to implement the osliki-js front-end application (as one of the implementation options) on some GitHub Pages, so that you can work with contracts in the usual way in the browser. Plus a set of widgets for embedding on sites.


If the topic seemed interesting to you, join the development.


Links


Links to source code on GitHub:



Contracts are currently in the test network Ethereum Ropsten. Test broadcast to play, you can get here .


Addresses of contracts:



')

Source: https://habr.com/ru/post/351398/


All Articles