How To Code A Simple Ponzi in Ethereum’s Solidity

Dean Schmid
3 min readAug 4, 2019

--

At the peak of the 2017 bullmarket cryptocurrency achieved such notoriety it managed to travel back in time and feature on Australian breakfast television.

After half-listening to a 35-second over-simplification on digital currencies, revered financial expert David Koch announced to proltarian Australia that Bitcoin must be a giant Ponzi.

Koch burns another ignorant investor

How To Code A Ponzi In Solidity

A simple Ponzi will take new investor money and send it to the previous investors. As long as there are new investors, and there is always more money going into the contract, the previous investors will see a return on their investment. That is until our final investor get’s Koched.

pragma solidity ^0.5.0; 
contract Ponzi {

}

First set up your contract

contract Ponzi {

address public currentInvestor;
uint public currentInvestment = 0;
function () payable public {

The contract keeps track of the current investor and the current Investment. We will need to know the current investment later when we require the future investors send in more money.

function () payable public {

This is a solidity fallback function. When money is sent to the contract instead of to a specific function, in the contract, the fallback function is run by default.

function () payable public {
// new investments must be 10% larger than the last investment

1 uint minimumInvestment = currentInvestment * 11 / 10;

2 require(msg.value > minimumInvestment);

3 address previousInvestor = currentInvestor;

4 currentInvestor = msg.sender;

5 currentInvestment = msg.value;

6 previousInvestor.send(msg.value);
}

Break It Down Line By Line

function () payable public {}

This means our fallback function is payable and “investors” can send money to it.

uint minimumInvestment = currentInvestment * 11 / 10;
require(msg.value > minimumInvestment);

When the contract is created the minimum investment is 10% > 0.

On line 5, current investment is reset to the msg.value. The current investment will continue to increase by a minimum of 10% as more money is added to the contract.

Line 2 has a require statement which will exit the function is the msg.value isn’t greater than the minimum investment. The minimum investment is set on line 1 as shown below.

uint minimumInvestment = currentInvestment * 11 / 10;

There are no decimals in solidity. A fact you have to workaround.

x + 10% === x * 11 / 10.

require(msg.value > minimumInvestment);

When the require statement checks if msg.value is > minimum investment, it is really asking if the attempted new investment is 10% larger than the previous.

address previousInvestor = currentInvestor;

The first time this function is called the previous investor isn’t set to an address. Look at the next line then flick back to this.

currentInvestor = msg.sender;

The current investor will be set to the msg.sender the first time it is run, and in subsequent function calls this value will update the previous investor.

currentInvestment = msg.value;

Here the current investment is set to the value attached to the function call. Remember the first time the function is called, the current investment is set to 0. To satisfy the require statement on line 2 the first time the function is called any amount of ether can be sent into the contract.

previousInvestor.send(msg.value);

This will send the new investment to the old investor and keep the ponzi alive.

--

--

Dean Schmid
Dean Schmid

Written by Dean Schmid

Full-Stack Developer, Web Designer. I’m a Lover of the Internet and all the Opportunity it Brings.

No responses yet