Building Ethrone using Chainlink Keepers on Polygon

“Let’s build a blockchain game!” I said.

Little did I know building blockchain applications comes with unique design and technical constraints. I jumped in head first and started building Ethrone, my first web3 project. I learned by doing and hitting problems face on, one after the other. I wanted to highlight a couple of them in this post so that you can avoid them in your own web3 projects!

What time is it right now? 🤔

The main game mechanic of Ethrone relies on timestamps. The winner is the player that stays the longest on the Ethrone for a given round which lasts 24 hours.

In other words, the smart contract powering the game engine needs to always know what time it is. I had taken for granted this incredibly handy ability for machines to always have access to the current time. So many software programs and algorithms depend on this critical piece of information. So imagine my surprise when I found out that there is no System.currentTimeMillis() equivalent in Solidity 😭

“But of course!” I thought, “my smart contract lives on a peer-to-peer network of nodes. It gets replicated and executed on individual machines running the network. So each machine - executing contract calls at their own pace - would return a different timestamp!”

Luckily, there is one way to get a time that all nodes agree on - the timestamp of the latest mined block - block.timestamp. Ok, let’s work with that.

Tweaking the logic a bit to work with this constraint, I was able to at least get a reliable time difference between two transactions, which I used to determine how long each player has captured the Ethrone for, and how long since the round has started.

Lesson learned: While you can’t get a traditionally accurate time from a smart contract, you can determine the time elapsed between two blocks. Your app or game will have to work with that!

Can you call me later? 📞

So I know how long since the round started, and I need the game to automatically award the winner’s prize and start the new round every 24h. Should be easy right? Wrong.

Here I was, coming from my traditional, single machine programming paradigm, thinking: “Surely there’s a way to schedule a function call to be executed at a later time from my smart contract… right?”

Well no, there isn’t. Not directly at least. Once again, I had taken threading, concurrency and schedulers for granted. There is no such thing in the EVM.

“But of course!”, I thought, “since all nodes need to execute every contract call to agree on the final state, imagine the mess it would be with multi threaded smart contracts!”

Luckily, I wasn’t the only one with this problem, and the Ethereum ecosystem had my back. This is when I stumbled upon Chainlink Keepers - a smart contract automation service run by professional DevOps teams that monitors smart contracts and triggers function calls at the desired time.

I LOVE Chainlink Keepers. It’s simple to implement, has a nice dashboard, and a straightforward promise: “We will call your smart contract function for you at the desired time, every time, without fail.” - Music to my ears.

After implementing a couple of extra functions in my smart contract, Chainlink Keepers now does the heavy lifting for me, allowing Ethrone to run completely autonomously by automatically awarding prizes to winners every day.

There was one missing piece to complete the puzzle though. I wanted Ethrone to run on Polygon rather than mainnet, to avoid gas fees that cost more than the price to capture the Ethrone. And until recently, Chainlink Keepers was only operating on mainnet.

Not anymore! Chainlink Keepers officially launched on Polygon, and I have been impressed with how reliable and affordable it is.

My Polygon Chainlink Keepers Dashboard
My Polygon Chainlink Keepers Dashboard

Ethrone on Polygon awards prizes and starts a new round pretty much exactly on time every day, with a drift of less than 10 secs per day. That’s impressive for a decentralized solution. And it’s doing this at a ridiculously low cost of 0.001 to 0.005 LINK per day depending on network conditions.

Ethrone only costs me ~1 LINK / year to operate on Polygon, and as a solo dev working on it on the side, that makes me very happy 🥰 Thanks Chainlink!

Wanna play? 👉👈

  1. Capture the Ethrone
  2. Hold it for as long as you can
  3. ???
  4. Earn some MATIC and a NFT Trophy!

Follow updates on the blue bird app @ethronegame

Join the community on the purple mask app:

GL HF!

About Chainlink
Chainlink is the industry standard for building, accessing, and selling oracle services needed to power hybrid smart contracts on any blockchain. Chainlink oracle networks provide smart contracts with a way to reliably connect to any external API and leverage secure off-chain computations for enabling feature-rich applications. Chainlink currently secures tens of billions of dollars across DeFi, insurance, gaming, and other major industries, and offers global enterprises and leading data providers a universal gateway to all blockchains.

Learn more about Chainlink by visiting chain.link or read the documentation at docs.chain.link. To discuss an integration, reach out to an expert.

Subscribe to Joaquim Verges
Receive the latest updates directly to your inbox.
Verification
This entry has been permanently stored onchain and signed by its creator.