Smart Contracts for the People

Alex Chepurnoy

October 19, 2018

A lot of blockchain-related news these days are saying us that a bank X or a corporation Y
is going to use a "blockchain" to "reduce costs". Basically it means that just another buzzword is
considered by big banksters as just another tool to extract value from everywhere in a more efficient way,
and also to save on cutting job positions.

While I am avoiding to say anything about "Satoshi's vision" (nothing but speculations could be dug there after
his exodus), a broad view I witnessed in the early years around online forums users is that a cryptocurrency
should provide tools to enrich the ordinary people behind their small businesses providing no much above making ends meet, not depersonalized big financial capital. The tools of
a cryptocurrency in the eyes of the original broad community vision should allow people to do economic activity dependless on business size, geographic location, interest rates set by big players, and so on.
The tools should allow people to do contracts (no paper contracts, but digital, self-enforcing, and reasonably smart contracts) dependless on differences in jurisdictions, traditions, followed business practices etc.

I hope Ergo would be useful here. Thousands of small cooperatives and individual entrepreneurs are more important to healthy and sustainable wealth growth around the globe than a couple of corporations hiding profits in offshore heavens.

As an example, let's consider a cooperative federation (such as Radical Routes) willing to provide financial help to an entrepreneur thousands kilometers away (say, in Rojava).

We need to assume some details now. In the first place, assume that there are four cooperatives in the network. They collectively lock 10,000 Ergs (in equal parts, so 2,500 Ergs each) in a contract, which says the following:

  1. The cooperatives are associated with public keys pubkeyA, pubkeyB, pubkeyC, pubkeyD. The entrepreneur is associated with a public key businessKey.
  2. The cooperatives are locking money in a coin protected by a funding contract, then doing due diligence and vote on whether to fund the entrepreneur or not. All the contract fund (10,000 Ergs) is going to the entrepreneur if 3 votes out of 4 are for that. Technically, the voting is done via 3-out-of-4 threshold signature. If voting is not successful (3 out of 4 signatures are not collected) before the block number 1,000, any cooperative (actually, anyone) can submit withdraw transaction, which is returning 2,500 Ergs (at least) to every cooperative. Further, the funding contract will also be called the voting contract.
  3. The investments could be spent on three goals, with some strict bounds. Namely, the entrepreneur must spend at least 5,000 Ergs on equipment, at least 2,000 Ergs on construction of a building needed, other funds the entrepreneur may spend arbitrarily.
  4. To be sure that equipment money will be spent on equipment, the cooperative federation is using public keys of known equipment sellers in the area of the entrepreneur. For example, consider that there are equipment sellers with public keys pubkeyTool1, pubkeyTool2, pubkeyTool3, pubkeyTool4 in the area. Technically, the transfer is organized as a collective signature of one equipment sellers (thus ring signature from equipment sellers ring AND entrepreneur's signature)
  5. Similarly, assume that there are 3 builders in the are cooperative federation is recognizing, associated with public keys pubkeyConstr1, pubkeyConstr2, and pubkeyConstr3.
  6. Similarly to the voting contract, if equipment and construction contracts are not co-signed before block number 5000, the federation cooperatives could withdraw funds.

There are different ways to define contracts in Ergo. A script in the low-level language, ErgoTree, is describing a (single) logical condition on whether a coin could be spent according it, and also a spending proof provided by a spending transaction. Internally, the condition is represented as a typed syntax tree thus the name. The structure is allowing us to do ahead-of-time cost analysis etc. Higher-level language called ErgoScript allows to have more traditional and readable description, use variables and break logic into subroutines.

Let's start with the main contract defined in (1-3) above:

{
 val votingSuccess  = atLeast(3, Array(pubkeyA, pubkeyB, pubkeyC, pubkeyD))
 val properSpending = OUTPUTS(0).value >= 5000L &&
                      blake2b256(OUTPUTS(0).propositionBytes) == spendingContract1Hash &&
                      OUTPUTS(1).value >= 2000L &&
                      blake2b256(OUTPUTS(1).propositionBytes) == spendingContract2Hash

 val withdrawCondition = HEIGHT >= 1000L &&
                         OUTPUTS(0).value >= 2500L && OUTPUTS(0).propositionBytes == pubkeyA.propBytes &&
                         OUTPUTS(1).value >= 2500L && OUTPUTS(1).propositionBytes == pubkeyB.propBytes &&
                         OUTPUTS(2).value >= 2500L && OUTPUTS(2).propositionBytes == pubkeyC.propBytes &&
                         OUTPUTS(3).value >= 2500L && OUTPUTS(3).propositionBytes == pubkeyD.propBytes 

 (votingSuccess && properSpending) || withdrawCondition
}

This script in the ErgoScript is to be compiled into a syntax tree (which is to be written into the blockchain in a serialized form) by binding it with concrete values for variables (pubkeyA, pubkeyB, pubkeyC, pubkeyD, spendingContract1Hash, spendingContract2Hash). spendingContract1Hash is a hash of (serialized) equipment spending script which will be provided below, spendingContract2Hash is a hash of construction spending script.

The equipment spending script is below:

{
 val spendingSuccess = (pubkeyTool1 || pubkeyTool2 || pubkeyTool3 || pubkeyTool4) && businessKey

 val withdrawCondition = HEIGHT > 5000L &&
                         OUTPUTS(0).value >= 1250L && OUTPUTS(0).propositionBytes == pubkeyA.propBytes &&
                         OUTPUTS(1).value >= 1250L && OUTPUTS(1).propositionBytes == pubkeyB.propBytes &&
                         OUTPUTS(2).value >= 1250L && OUTPUTS(2).propositionBytes == pubkeyC.propBytes &&
                         OUTPUTS(3).value >= 1250L && OUTPUTS(3).propositionBytes == pubkeyD.propBytes 

 spendingSuccess || withdrawCondition
}

And the construction script is:

{
 val spendingSuccess = (pubkeyConstr1 || pubkeyConstr2 || pubkeyConstr3) && businessKey

 val withdrawCondition = HEIGHT > 5000L &&
                         OUTPUTS(0).value >= 500L && OUTPUTS(0).propositionBytes == pubkeyA.propBytes &&
                         OUTPUTS(1).value >= 500L && OUTPUTS(1).propositionBytes == pubkeyB.propBytes &&
                         OUTPUTS(2).value >= 500L && OUTPUTS(2).propositionBytes == pubkeyC.propBytes &&
                         OUTPUTS(3).value >= 500L && OUTPUTS(3).propositionBytes == pubkeyD.propBytes 

 spendingSuccess || withdrawCondition
}

Now assume that the cooperative federation has created a coin protected by the voting contract. Below is how the entrepreneur can do his business:

  1. Create a transaction which consumes the coin and create at least three coins, one with equipment spending contract, another with construction spending contract, third is creating a coin protected by entrepreneur's public key.
  2. Send the transaction to the cooperatives, wait for the transaction with the threshold-signed input being published on the blockchain.
  3. Consider a contract with an equipment seller, co-sign a spending transaction.
  4. Consider a contract with a builder, co-sign a spending transaction.

The entrepreneur can easily run away without doing any business, but with no more than 3,000 Ergs. This can be fixed, e.g. this money could be made spendable only after block number 5,000, and before that money could be withdrawn by the cooperatives. For equipment and construction spendings, the entrepreneur is working along with some, presumably already reputable, business. Thus the investor is reducing its risks.

You can find code and example transactions online. Please note that we have more examples of complex signature schemes, multi-step contracts (with on-the-go execution paths revealing, like MAST in Bitcoin, but with cycles allowed), oracles, crowdfunding and so on. Please check our examples repository.

Share post

Bene V2 is Here: Multi-Asset Fundraising, Expanded Wallet Support, and Enhanced UI

Bene V2 is Here: Multi-Asset Fundraising, Expanded Wallet Support, and Enhanced UI

The ecosystem of decentralized applications (dApps) on Ergo is constantly evolving.

Ergo Platform

November 28, 2025

Ecosystem Update: Duckpools Rolls Out V2 Site Preview

Ecosystem Update: Duckpools Rolls Out V2 Site Preview

The team behind Duckpools, a prominent lending and borrowing protocol on the Ergo blockchain, has released a comprehensive develop.

Ergo Platform

November 26, 2025

Ecosystem Spotlight: USE, a Universal Stablecoin for Ergo

Ecosystem Spotlight: USE, a Universal Stablecoin for Ergo

With the protocol nearing deployment, the Community Liquidity Bootstrapping (CLB) event is set to begin on November 25th, allowing.

Ergo Platform

November 24, 2025

Rosen Bridge Nears Bitcoin Runes Launch

Rosen Bridge Nears Bitcoin Runes Launch

Rosen Bridge has entered the final phase for its Bitcoin Runes integration. You can now set up your Bitcoin-Runes watcher.

Ergo Platform

November 23, 2025

GitCircles and Ergo: Fair Rewards for Open Source Contributions

GitCircles and Ergo: Fair Rewards for Open Source Contributions

Open source software powers much of the digital world — from the apps on your phone to the infrastructure of the internet itself.

Ergo Platform

October 12, 2025

Bitcoin Runes + Rosen Bridge: A Practical Path for Multi-Chain Fungible Assets

Bitcoin Runes + Rosen Bridge: A Practical Path for Multi-Chain Fungible Assets

The big idea Bitcoin finally has a clean way to represent fungible tokens.

Ergo Platform

September 30, 2025

ChainCash: Money That Carries Its Own Story

ChainCash: Money That Carries Its Own Story

ChainCash records reserves and signatures for each note on Ergo.

Ergo Platform

September 23, 2025

Braiding Lunarpunk and Solarpunk through Merged Mining

Braiding Lunarpunk and Solarpunk through Merged Mining

The question is simple.

Ergo Platform

September 2, 2025

Machina Finance: Off-Chain Execution, On-Chain Trust

Machina Finance: Off-Chain Execution, On-Chain Trust

Machina Finance is an innovative, bot-driven decentralized exchange (DEX) being developed on the Ergo blockchain.

Ergo Platform

September 1, 2025

Ergo Infrastructure DAO: Decentralizing the Backbone of the Ergo Ecosystem

Ergo Infrastructure DAO: Decentralizing the Backbone of the Ergo Ecosystem

Ergo’s mission has always been rooted in decentralization, not just at the consensus layer, but across the entire stack.

Ergo Platform

August 13, 2025

Mew Finance: A Playful DeFi Toolkit for the Ergo Ecosystem

Mew Finance: A Playful DeFi Toolkit for the Ergo Ecosystem

Mew Finance is a decentralized application suite on the Ergo Blockchain.

Ergo Platform

August 12, 2025

Lithos: Decentralizing Mining with On-Chain Pools

Lithos: Decentralizing Mining with On-Chain Pools

Lithos is a new protocol designed to overhaul how mining pools work by moving them on-chain, giving miners full control, and elimi.

Ergo Platform

July 24, 2025

Sigma 6.0: A Smarter, More Flexible Ergo

Sigma 6.0: A Smarter, More Flexible Ergo

Sigma 6.0 is a major proposed upgrade to the Ergo blockchain.

Ergo Platform

July 23, 2025

Shaping Rosen's Future: A Community Call on Five Key Treasury Proposals

Shaping Rosen's Future: A Community Call on Five Key Treasury Proposals

Rosen co-founder, Armeanio, has submitted five new proposals to the Rosen Treasury.

Ergo Platform

July 9, 2025

Ergo's Extended UTXO and the Rise of Artificial Economic Intelligence

Ergo's Extended UTXO and the Rise of Artificial Economic Intelligence

A Practical Vision for Autonomous Economic Agents Autonomous economic agents on the Ergo blockchain perform useful work in a real.

Ergo Platform

May 12, 2025

ErgoHACK X: Artificial Intelligence on the Ergo Blockchain

ErgoHACK X: Artificial Intelligence on the Ergo Blockchain

Celebrating a Decade of Decentralized Innovation Join the 10th anniversary ErgoHACK and be at the forefront of the AI revolution o.

Ergo Platform

April 10, 2025

Ergohack 9: Innovations in UI/UX and Mining – Meet the Visionary Winners!

Ergohack 9: Innovations in UI/UX and Mining – Meet the Visionary Winners!

The latest annual Ergo hackathon, ErgoHack IX, was a six-day event held at the end of October.

Ergo Platform

December 9, 2024

ErgoHack IX: Next Steps Towards Increased Adoption

ErgoHack IX: Next Steps Towards Increased Adoption

We’ve come a long way since the early days of Bitcoin, and cryptocurrencies have boomed into an industry with thousands and thousa.

Ergo Platform

October 20, 2024

Ergo Vs Other Blockchain Platforms: What’s The Difference?

Ergo Vs Other Blockchain Platforms: What’s The Difference?

Ergo offers a series of unique features that set it apart from other blockchain platforms.

Ergo Platform

August 19, 2024

Earning With The Rosen Bridge

Earning With The Rosen Bridge

Bitcoin has officially bridged to the Ergo ecosystem via Rosen Bridge! This newly built, decentralized infrastructure enables trus.

Ergo Platform

August 8, 2024

How Sigma Chains Will Bring Bitcoin To Ergo

How Sigma Chains Will Bring Bitcoin To Ergo

Ergo’s powerful, flexible, and secure smart contract functionality opens the door to a whole host of new use cases for Bitcoin DeF.

Ergo Platform

July 15, 2024