CoinMarketCap: Read what our contributors have to say. This content is provided by the community. DYOR!

Enabling CivTrade Limit Orders With Chainlink Keepers

Table of Contents

Tech Deep Dives

Enabling CivTrade Limit Orders With Chainlink Keepers


By CivFund

a year ago
9 mins read
Enabling CivTrade Limit Orders With Chainlink Keepers

How using Chainlink Keepers saved Civilization $250,000/year and 3 months’ development time while providing enhanced uptime and enabling on-chain monitoring for CivTrade

NEWS: Learn more at the upcoming London (UK) Chainlink Masterclass on January 21st 2022 @ 5pm — free registration!

Join us in showcasing the cryptocurrency revolution, one newsletter at a time. Subscribe now to get daily news and market updates right to your inbox, along with our millions of other subscribers (that’s right, millions love us!) — what are you waiting for?

Introducing CivTrade

CivTrade is an innovative solution to the key pain points of traders using decentralized exchanges: it removes slippage, liquidity fees, and the price impact of each trade. Learn more here.
CivTrade is now online with over $2.5 million traded in over 200 positions by 150 traders: it’s the audited, patent-pending limit trading solution for #DeFi. It significantly reduces DeFi trading costs and friction, improves the DeFi trading toolkit, and introduces the order book capability previously only available on Centralized Exchanges. CivTrade effectively enables traders to set limits and create an order book as they would on a traditional exchange, specifying trade volume and the price they are aiming to achieve.
You can swap any ERC20 token at your target price by simply connecting your wallet: welcome to 0% slippage, 0% price impact, 0 liquidity fees, 0 front-running, with 100% target price control. Now cross-blockchain, thanks to the help of Chainlink Keeper
The (huge) advantages of CivTrade versus any traditional DeFi swaps. More examples in the Appendix.

The problem to be solved

As a community-driven project, it’s sometimes difficult to guarantee high standards of service and technology delivery, as our team is not paid a traditional salary and our income relies on donations which are naturally uncertain. But it’s key to users that CivTrade is reliably live 24/7!

Without a keeper service, our users would have to constantly monitor the price of the assets they want to trade, hoping that the market will reach their desired price before manually closing the position. This would create a terrible user experience, so pushing the trade execution process onto users was a non-starter for the Civilization team.Limit orders enable traders to specify the price at which they wish to buy or sell an asset and automatically execute an order when the market price hits their limit price.

How Civilization saves more than $250,000/year with the help of Chainlink Keepers

One option would have been to run an off-chain server to trigger the trade-close process. However, this would have required developers to build, manage, and deploy a DevOps solution from scratch. Not only would this service have been centralized and prone to downtime, but it would also have added significant development time and costs that could instead be focused on building new products and features.When we ran the numbers, the in-house business case simply didn’t add up.
Comparing in-house DevOps automation with Chainlink Keepers
When we ran the numbers, we realized that building a comparable solution in-house to address the problem that Chainlink Keepers would immediately solve would require a substantial investment of time and money:
  • 2+ People. 1 developer to build and upgrade the system, plus 1 system administrator to monitor and maintain it — with substantial upfront budget lock-up: pay people on a monthly basis, but to run a professional operation guarantee enough run-rate is available from day 1, so you can pay for at least 1 year. Assuming $100,000/year per head for reasonably senior devs, plus contingency and social security, we estimated a $250,000 budget for the in-house solution.
  • 2+ Datacenters. 2 redundant locations for high availability, plus a third for disaster recovery, plus additional development and staging environments, would make the total server cost scale-up 5x. If production was run on a higher performing, higher cost stack, and disaster recovery/dev/staging on cheaper servers (which is not ideal), the cloud-computing bill to keep the in-house system up 99.9%/24/7 would still run up to $3,000+ per calendar month.
  • Downtime. Having just 2 people would mean support would be 9–5 on working days only. Forget about 99.99% availability, even with 5 datacenters, issues may and will happen at times when support teams are not around (sleeping and/or on holidays)! So we estimated up to 1% system downtime in-house, versus achieving 99.99% by leveraging Chainlink Keepers’ ability to spread the administration overhead over multiple projects and therefore guarantee (much) better service. And we can also sleep soundly at night: one less thing to worry about.
  • Investment. As a community-funded start-up, we prefer to pay as we scale, rather than having to fundraise money upfront. Since Chainlink Keepers don’t require ponying up any $$ upfront, we love them even more.
  • Time. We conservatively estimated it would take at least 3 months to set up a stable in-house solution. Building systems, even when you have the money to do it, takes time and distracts attention from other priorities. Since it only took us a couple of days to read the Chainlink docs and set up the code required to go live, the choice gave us a huge speed boost!
So, for us, running the numbers showed that the choice was a no-brainer, and therefore we immediately got down to integrating Chainlink Keepers. Here is how we did it.

The Chainlink Keepers solution

Civilization is able to greatly improve uptime through its integration with Chainlink Keepers: towards full decentralization, Chainlink Keepers enable CivTrade to offer traders automated limit order execution with enhanced security, reliability, and performance for near real-time trade execution.

Civilization chose Chainlink Keepers as our decentralized transaction automation service to help close trades when CivTrade-specific conditions are met. This provides CivTraders with confidence that all their limit order positions are continuously monitored and optimally executed without any manual interactions from users or the Civilization team.

The Chainlink Network is the most widely adopted and time-tested oracle network in the DeFi industry. It has a flawless track record of providing high-quality services to blockchain applications.- DexMan, Lead Dev

Decentralized exchanges allow anyone to trade. Civilization’s integration of Chainlink Keepers helps us open the door to people who want to experience the market without the need for the help of third parties.

Making the tech work

Chainlink Keepers have ample documentation, making them the easy-to-use solution for automated smart contract execution. We just had to write a dedicated smart contract with two main functions:
  1. Verify in real-time if our execution conditions have been met: “is any CivTrade ready to close?
  2. Trigger the execution of our smart-contract: “immediately close all the CivTrades that have just been filled by the market”.
Changes required to our core CivTrade contract? Zero. Technically, we only had to write the new contract, and add it to the authorized list of blockchain addresses with authority to close positions — alongside the trader and the multi-sign CivTrade admin.

Our simple dedicated Keeper smart contract automates what otherwise could have been done manually, had a team of analysts been available 24/7 to monitor each trade (which clearly would be impossible, or very impractical to say the least). For every new block mined by each of our CivTrade-supported blockchains, currently Ethereum + Polygon (besides our test chains Kovan and Mumbai), Chainlink Keepers check our trigger conditions, and when they are met, close the relevant trades.

Chainlink Keepers describe the standard for these two functions, called checkUpkeep and performUpkeep.


The CivTrade Keeper contract function: checkUpkeep
Within the checkUpkeep function, our implementation checks whether each trade within the range has been filled by the market. Ranges are a partitioning mechanism to load balance activity across different Keepers, helping avoid running out of gas if the number of trades to be monitored grows too large. Currently, we run up to 1,000 trades through each Keeper, but we estimate a max capacity load in excess of 10,000 CivTrades per Keeper, or even more. This will keep us running for quite a while, as there’s no limit to the number of Keepers we can switch on. See the above section on cost scaling: this system grows with us!


The triggered() function checks if a CivTrade is ready for closing: this means that the asset being sent for trading has been entirely swapped into the other asset, so the pair (initialAmount,0) has become (0,targetAmount). For example, for a 1 ETH -> 200 LINK trade, initially the trader sends (1 ETH,0); the trade will complete when the position has become (0, 200 LINK).
Whenever at least one CivTrade is ready for closing, the variable upkeepNeeded will be set to True by the checkUpkeep function, which will also write into performData (abi-encoded) the list of all CivTrades to be closed as soon as possible.
Chainlink nodes constantly call the checkUpkeep function, on each and every block. Whenever upkeepNeeded evaluates to True, then the nodes will call the performUpkeep function.


The CivTrade Keeper contract function: performUpkeep
performUpkeep checks again that each CivTrade from performData is ready to be closed, because anyone can call this function.
We can’t trust the input data at face value, but check it again before execution. Once the triggered() condition is validated again, the CivTrade is immediately closed. Whereas checkUpkeep doesn’t actually do anything per se, performUpkeep can close positions, therefore for additional security reasons we made it pausable.


To activate this new Chainlink Keepers-compatible contract, we simply registered it online, following the Chainlink Developer Documentation. We set a 1-million gas limit, in case our performUpkeep needs to execute multiple closes at the same block; and set checkData for the first 0–1000 trades. Voila; our Upkeep is up and running.
Our first Chainlink Keepers mainnet Upkeep

We had to repeat the registration process for Polygon, and ta-da, CivTrade went cross-chain!

For security and scalability reasons, we implemented the following checks:

  • Keeper contract is pausable by our multi-sign admin.
  • checkUpkeep waits for an interval of at least 5 blocks (about 1 minute) after the last performUpkeep was triggered, to avoid clogging or repeat closes.
  • 1,000 CivTrades can be monitored by each Upkeep, to prevent out-of-gas errors; we can register any number of new Upkeep as we scale.

World-class support

Civilization is a successful community-driven project with a dedicated and highly skilled dev team, to which the Chainlink Labs team provided truly world-class support around the clock: crucial to our success. A big shout out to Michael Stone and the whole Chainlink Labs team: not only do they promptly reply to any of our questions 24/7, but also they help with go-to-market technical support. We appreciate the incredibly high level of support that the Chainlink Labs team offers: it truly makes a difference!

While CivTrade continues to expand to all the blockchain networks supported by Uniswap v3, we look forward to Chainlink Keepers’ continued support of new networks, so that all traders can benefit from CivTrade’s reliable and decentralized trading toolkit.

Read here to learn more about the advantages of CivTrade and how to use it.
Don’t miss any CivTrade news: Join our official Telegram Announcements Channel

Happy CivTrading!!

PS: To learn more about Civilization, please read our vision blog post.

About Civilization

Civilization, also branded the Burning Man of Finance, is the world’s first Decentralized Investment Fund. CIV stems from the initials of Community Investment Vehicle: the world’s first hedge fund owned and managed directly by its own investors. The funds donated so far have reached $1million and the aim is for CIV to generate enough returns to become self-sufficient. It has already launched its first product, CivTrade, and aims to launch CivFarm and CivFund to make the benefits of Decentralized Finance accessible to everyone.
Learn more about Civilization by visiting and reading the blog at For any questions, please reach out via the Telegram group.

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 or read the documentation at To discuss an integration, reach out to an expert.

Appendix — examples of CivTrade business case

As the size of a CivTrade grows, its benefits multiply!

1 like

1 shared

Join the thousands already learning crypto!

Join our free newsletter for daily crypto updates!