Adventures with Dumb Contracts

Photo by Henry Hustava on Unsplash

Adventureum

Rather than diving straight into some juicy code or abstract design concepts, I’ll introduce you to the DApp that brought us here today. It’s a friendly, simple little game I’m calling Adventureum, a clever portmanteau of Adventure and Ethereum, because it’s crypto so for some reason that kind of horrible naming style is acceptable.

Gas use and storing data

Anyone with a little experience developing for Ethereum will be well aware of the most expensive (by far!) operation on the EVM —writing to storage. The more data you write, the more it costs to store, if I’m going to be asking my users to write big chunks of text for a text-based game, that could be a problem. We don’t want to punish our users for their enthusiasm or creativity.

Dumb contracts

Dumb contracts! I’ll be honest, there may be another name for this that I’m unaware of, but I heard this one once and I like it. A dumb contract is a smart contract pattern that takes advantage of Events, and their relative cheapness compared to writing to storage. It costs many times more gas to write to storage than it does to emit unindexed event parameters, and since event logs are fully and readily available, it seems like a perfect place to store game text.

Give me an example

Rather than abstractly talking about this, let’s use an example. Suppose you wanted to store Shakespeare’s Hamlet on the Ethereum blockchain, indexed by paragraph. I honestly have no idea why you’d want to do this, but let’s go ahead.

contract Hamlet_store{
string[] public paragraphs;
function writeText(string memory paragraphText) public {
paragraphs.push(paragraphText);
}
}
contract Hamlet_event{
event Paragraph(uint indexed num, string paragraphText);
uint paragraphs;
function writeParagraph(string memory paragraphText) public{
emit Paragraph(paragraphs,paragraphText);
paragraphs++;
}
}

Back to the game

Anyway to wrap up, as most of you may have inferred, I used this dumb contract pattern to keep the game situations in the Event logs. There’s a little bit of logic in the smart contract which keeps track of which choice leads to which situation, but that’s about it.

Caveat regarding bytes32

A small caveat with the bytes32 method is that it limits the length, the choices can’t be more than 32 characters each. For my game design, this is fine, I don’t want players putting an essay in their choice anyway.

Game on

I hope you found this post useful. The dumb contract pattern is one I read about some time last year and I only implemented it for the first time in this project, and the genesis of this project was only a week ago. So it’s been bouncing around my head for a while, and hopefully one of you will get inspired and use it to save some users’ gas some day.

--

--

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
Andrew Parker

Andrew Parker

Codeslinger. Melbourne based Solidity developer for hire.