Image for post
Image for post
Author: Fabio Jose Moraes | Digital Architecture

Building a Smart Contract to Sell Goods

Interested in learning how to work with the Ethereum blockchain? In this article, we look at how to get an Ethereum instance set up using Node.js, Solidity, and JS.

No doubt, Bitcoin has changed the way we see and understand money, value, and, recently, ownership due to smart contracts. It’s interesting because almost everyone has heard about it or cryptocurrency. People from many areas of business, it’s not just us, the IT workers.

Blockchain for Beginners

There are many concepts involving blockchain technology, but, in this section, I will try to explain this amazing and disruptive technology.

As the name implies, blockchain is made up of blocks (#1) of data chained (#3) together using it’s hash data, signeted by user’s private key (#2). These blocks, generally, are limited to the size of 1MB and store as many transactions as possible due to this limitation.

Image for post
Image for post
Image for post
Image for post
  1. Using the public key, everyone can decode the data, or better, check the signature. With this operation, the origin is what we are expecting.

Case Study

I chose a very common use case to make easy to understand, leaving all the complexity only with the implementation of smart contracts logic and technical details.

  • The shipment is made by a courier requested from the manufacturer and they have a trusted commercial relationship too.
  • The trusted commercial relationship between retailer and courier is transitive because of the common relation provided by the manufacturer.
  • A trusted commercial relationship is so important because the traditional market needs this. The payment from retailer to manufacturer happens days after the receipt of goods. And, the payment from the manufacturer to the courier happens days after the delivery confirmation. If there is no trust they would not believe each other and not make a deal. This doesn’t promote the competition for best prices, services, and products.

Generalization

The most fine-grained of commercial transactions is composed of three parts: buyer, goods, and seller. The most basic process is the transfer of ownership of the goods in exchange for cash, from the seller to the buyer. What do you think about this?

Image for post
Image for post
  • B2C — services: Maria is hungry, she goes to the Mama’s Pasta and orders a nice portion of spaghetti. The Chef takes her order details, prepares the dish and the waiter serves it. When Maria is satisfied, she pays the bill and goes home. In this example, “Maria” is the buyer, the “dish” is the goods, and “Mama’s Pasta” is the seller. After payout, the ownership of the “dish” is transferred from “Mama’s Pasta” to “Maria.”
  • C2C — goods: Joseph is selling his car. After some weeks Berta wants to buy the car from him. They close the deal, Berta pays the accorded value, and Joseph transfers the ownership to her. In this case, “Berta” is the buyer, “car” is the goods and “Joseph” is the seller. The ownership is transferred to “Berta” after payout.
  • B2B — goods: Jong, the buyer, is an electronics reseller and has a store, he orders goods from Beijing Inc., the seller. When the goods are ready to ship they are packed and a courier called Overseas takes the packages and ships them to Jong. When he receives the goods and the invoice the payment will be performed.

The (Smart) Solution

As we saw in the case study section, the trust between the parts is the main concern to traditional business. The objective with this proof of concept is to establish a trustless way to make deals between partners, using a smart contract on the Ethereum blockchain network.

Image for post
Image for post
  1. The Retailer orders ProductX with quantity equal to N at Manufacturer’s smart contract. Through an event, so-called order send, the manufacturers could receive the order data and process it.
  2. The Manufacturer looks for best shipping price on the Courier smart contract.
  3. The Manufacturer sends the order price and the Retailer receives this through the event called price sent.
  4. The Manufacturer sends the shipment price and the Retailer receives this through the event called price sent.
  5. The Retailer performs the safe payment of the grand total: order price + shipment price. These coins go to the smart contract account and waits there until the delivery.
  6. The Manufacturer sends the invoice with a delivery date and some other data. The Retailer receives the invoice data through the event called invoice sent.
  7. The Courier, after delivery the order to the Retailer, marks the order as delivered on the Smart Contract. The courier could be a robot, a drone. Think with me! Today we have many possibilities.
  8. The Smart Contract pays out the Manufacturer for the order.
  9. The Smart Contract pays out the Courier for the shipment.

The Development Tools

To accelerate our development, there is a good option called the Truffle Framework. This framework has a lot of stuff to help us to develop smart contracts in Solidity, the Ethereum programming language.

Truffle

Truffle is a tool to compile, test, and deploy smart contracts written in Solidity to the Ethereum Blockchain. To use it we need Node.js. Then, check the version of installed node tools.

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Setup

With this article, I do not pretend to show how to become a Solidity programmer, but if you want to start a greenfield project, proceed as follows.

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Build and Deploy

To execute truffle commands we need access to our private blockchain. In order to do this, edit the truffle.js file by adding the lines below.

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Test

Unit Testing: Yes! We can do unit testing with our smart contracts. After all, a simple error can cause us to lose all our coins.

Image for post
Image for post

Coding

To implement the solution I apply generalization and use these entities: Buyer, Seller, Goods, Order, Invoice, and a smart contract called Deal. You could see the implementation by cloning the source.

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Final Words

Now it’s the time to experience the capabilities of smart contracts, trying to solve real-world problems. Go, try it, and let me know! We could share our experiments.

About Patterns

At this point, we should think about some patterns. I noticed these two that are connected to the deployment approach.

  • One deploy, one use: in this approach we deploy a version of our smart contract, use it once time for all we need for a very specific goal and destroy at the end.

Exponential intelligence for exponential companies

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store