Stellar Escrow Smart Contract Development

Create your first escrow smart contract on the Stellar network.

Michiel Mulders
wearetheledger
4 min readApr 12, 2018

--

Source: gratisography.com

Before reading this tutorial, you should have a basic understanding of how Stellar works and how to create a simple account on the test network. Take a look at my previous article in this series to get you updated.

This article will explain to you how to develop an escrow smart contract using Stellar Lumens. I will as well highlight some extra features like retrieving a balance and a clean history log.

Extra note: This article is part of the Blockchaingers series. Together with TheLedger, we have won the ‘Digital Nations Infrastructure’ track at the largest blockchain hackathon. You can find out more about our idea here. Escrow smart contracts on the Stellar testnet are part of this prototype.

Use Case Description

We have two identities: a house and a contractor. The house can pay a contractor for delivering house related services like a check-up of your central heating. Once the house and contractor agree to deliver a service, the house will deposit the agreed amount (in XLM) into the escrow contract. Once the job is done, both the house and contractor have to sign to release the funds.

Preparation

First, we need a new, empty account on the testnet. Let’s create one.

We still define an owner account who is creating the escrow but is not able to perform any actions with it. We use a config file for retrieving our server, this piece of code is the same as: new Stellar.Server('https://horizon-testnet.stellar.org');.

Probably, you wonder why I’m sending 2.5 XLM to the escrow? Each account needs a starting balance of 1 XLM. In addition, we are adding two more signers besides the random escrow signer. For each additional signer, you have to add 0.5 XLM. So, …

This is defined in the Stellar documentation.

Building Stellar Escrow Transaction

Let’s retrieve the escrow account,

and build the escrow transaction.

As you can see, we add two signers to the escrow contract. We give both signers equal voting power (1) and set the threshold to two. Because we are not giving the escrow account itself an explicit weight, this is set to zero. This means both the house and contractor have to sign to release the funds. You can see an example of an escrow smart contract transaction creation here.

Stellar Escrow Contract

At last, we need to sign (with the random keypair) and send the transaction to the network.

Ideally, the house sends the agreed amount to the smart contract. The code for sending the transaction looks like this.

Release Funds

Releasing the funds is actually very simple. You create a new transfer transaction from the escrow account to the contractor. The only difference here is that both the house and contractor have to sign.

Additional Operations

Retrieve History For Account

This will give you a nice list of all payments a certain account has executed. We remove the first result (with shift())from the array as that’s the account creation (0 payment to itself).

You can use the HTTP API endpoint /accounts/<account-ID>/payments to retrieve this in your web browser as well. You’ll get something like:

One payment object for an account.

Get Account Balance

To retrieve the XLM balance of an account, you just have to load the account based on its public key. As an account can have multiple balances (native XLM and other coins deployed on the Stellar network), we will only look for the native balance.

Source: https://www.in3dc.com

What to read next

— — — — — — — — — — — — — — — — — — — — — — — — — — — — -

NEXT ARTICLE IN THIS SERIESThe tech-stack to win the worlds biggest blockchain hackathon of 2018!

--

--

Michiel Mulders
wearetheledger

Writing gists about marketing, lifestyle, self-help, finance, and UI/UX. Get me a Belgian beer please!