Earlier this year, Michael wanted to develop a DeFi asset management protocol that would implement simple systematic strategies at virtually zero cost. The trustless nature of public blockchain ecosystems would ensure that the funds are actually there (no Bernie Madoff type issues) and that they are being invested according to the stated portfolio rules.

But the public nature of the blockchain meant that sophisticated traders would be able to predict the protocol’s trades by analyzing the smart contract’s rebalancing logic. This issue is especially stark with the current iteration of DEX protocols that are mostly based on the AMM concept. In AMMs, a passive side stands ready to buy or sell at prices determined via a simple formulaic approach such as x*y=k. On Uniswap, for example, 25 basis points out of the 30 basis point fee paid by active traders go to compensate liquidity providers for systematic losses at the hand of arbitrageurs who adversely select the LP pool using publicly available information! The rise of Flashbots in 2021 means that front-running is now being offered as a service by miners to the highest bidder, so each and every order will now be squeezed for its maximal extractable value in sandwich attacks and other schemes. This leads to slippage losses amounting to another 50 basis points for a total cost of 80 basis points or 0.8%. The reduction in transaction fees due to rollups, sidechains, and the imminent arrival of Ethereum 2.0 means that ever smaller trades can be profitably front-run by sophisticated trading operations.

All of this meant that before Michael could realize his vision of a universally accessible, trustless, and cost-efficient asset management protocol, he would first have to develop a trading protocol that solves the issue of toxic trading. This is where Krypton comes in.

Krypton is a new way of trading that is resistant to toxic trading schemes such as front-running, adverse selection based on short-term information advantages, and miner extractable value.

To make it a reality, Michael wrote down his rationale for Krypton and its design in a white paper in October of 2021 and enlisted the help of his brother Andreas and his former student Nathan to implement the protocol during the Chainlink hackathon this Fall.

What it does

Krypton is a novel peer-to-peer DeFi exchange protocol that solves the issue of miner extractable value in the context of trading. It is fully decentralized, resistant to front-running and to short-term adverse selection. This is achieved by executing orders as continuous streams over time at finite trading speeds in contrast to concentrating trade execution to a distinct instant of time at infinite speed. The protocol features price discovery. Rather than relating price to quantity, traders submit demand and supply schedules relating price to trading speed. This change in perspective affords enormous conceptual clarity, predictability, and simplicity: The market clearing equilibrium is simply defined as the intersection of the aggregate demand and supply curves in price/trading-speed space. An actor in possession of short-term information that needs to be monetized prior to becoming public will need to request a high trading speed, which is limited by the maximum aggregate trading speed offered by the opposite side and increases the price. The unilateral price and trading speed jump induced by a fast trade signals the presence of asymmetric information and gives uninformed traders time to respond by adjusting their quotes. In infinite speed trading systems such as automated market makers (AMMs) and central limit order books (CLOBs), the maximum tradable quantity is exchanged in the very same moment the order that signals the presence of superior information arrives, making it too late for everyone else to react and opening the door for toxic trading schemes. These toxic trading schemes are responsible for the existence of bid-ask spreads in limit order books and price impact in AMMs.

How we built it

Krypton is built on top of a diverse open source software stack.

We are using Solidity to implement EVM code and Brownie for development and deployment.

The entire KryptonCompute off-chain framework is implemented in Python. PyCharm is Michael’s development platform of choice, whereas Nathan prefers to work in vim, and Andreas uses VSCode.

The oracle is built using Chainlink technology, which we run in a Docker container.

Krypton will be deployed on a high-performance smart contract ecosystem. We explain in the section on challenges below a blockchain's high performance and low cost nature works synergistically with our Chainlink-based scaling solution to improve capital efficiency on oracle nodes.

The web frontend is built as a single page application using Quasar, a Node.js framework based on Vue. We use Plotly.js to produce the order book graph. All of the REST APIs are implemented using Flask.

The communication with IPFS as a tamper-resistant software delivery platform for oracle nodes and keepers is accomplished using the ipfshttpclient package for Python.

We have used Linux as our primary development platform.

Challenges we ran into

The biggest challenge overcome is the severe limitation of on-chain computation available in today's smart contract ecosystems. We used a combination of keepers and Chainlink external adapters to minimize on-chain storage and compute requirements, thereby reducing cost and eliminating the barrier of computational complexity in decentralized systems. This was achieved by developing a KryptonComputable contract that manages update cycles and integrates on-chain with trustless off-chain computation. A KryptonComputable internally relies on the Chainlink oracle network where nodes run an external adapter implementing the Krypton Merkle tree framework (see the modules krypton.merkle_tree and krypton.calculation.calculation for details). The oracles determine the work that the smart contract needs to perform in each update cycle.In the present trading context, the work consists of ERC-20 token transfers for trade settlement, the update of order structures on-chain to reflect executed quantities, and the inactivation of orders that have finished executing. The work is divided into chunks small enough to fit the transaction gas limit of the blockchain. These chunks constitute the leaves of the Merkle tree. The oracle computes the hash of the Merkle tree root and returns it as 256 bit scalar to the KryptonComputable contract. A keeper carries out the exact same calculation which results in the exact same Merkle tree and submits leaves to the smart contract one by one along with the hashes of adjacent nodes. The smart contract computes the hash of each leaf and combines it with the adjacent node hashes to verify that it matches the root hash provided by the oracle network. After validation and implementation, the smart contract stores the hash of the leaf on-chain to prevent replay attacks.

Our framework is a general approach and can be used in other contexts to expand the computational envelope of decentralized systems using Chainlink technology. Protocols using it simply need to

  • Inherit from KryptonComputable.
  • Implement the methods construct_from_hashables, empty_batch_hashable_function, and solidity_keccak for each type of work to be done in a smart contract. See the Transfer class in the krypton.structs module for example.
  • Write a function that implements the work on the smart contract for each type of work. See the contract function implementTransfers in Krypton.sol for example.

While Krypton’s off-chain compute approach is compatible with every EVM compliant blockchain that integrates with Chainlink, a high-performance and cost efficient blockchain works synergistically with Krypton. The shorter the settlement window, the higher the number of update cycles an order is involved in. With a settlement window of 10 minutes, an order that continuously trades over 5 minutes will be part of at most two settlement cycles. Increasing the update frequency to once per minute will feature the order in up to 6 settlement cycles which results in a multiplication of on-chain work for token transfers, order structure updates, and order inactivations. A more cost efficient blockchain will allow more frequent settlement cycles while keeping the cost of on-chain transactions minimal. The obvious and direct effect is an improvement of user experience as it makes the protocol less expensive and shortens the wait time for token transfers for parts of orders that have been executed. A second, less obvious but more profound, effect is an improvement of the capital efficiency of nodes in the oracle network. Chainlink 2.0 features a staking model which guarantees a crypto-economic security level that is quadratic in the number of nodes. The level of security required for Krypton is dictated by peak trading volume (i.e. maximum amount of value transferred) in a single settlement window. A shorter settlement window enabled by the use of a high-performance blockchain ecosystem reduces the amount of crypto-economic security required to secure a given window at the exact same trading activity on Krypton. This improves capital employment on oracle nodes.

Accomplishments that we're proud of

On a technical level, we are thrilled to have developed a novel decentralized scaling approach that ultimately enables our economic vision and can be used to power the next generation of Web3 protocols.

On an economic level, we are excited to be presenting a fully working trading solution that is resistant to the toxic trading patterns that are pervasive in both traditional finance and crypto. These practices have become pertinent in DeFi with the rise of Flasbots over the last year. Since DeFi’s inception, the computational limitation of smart contract ecosystems has limited the protocol design space to simple peer-to-pool mechanisms such as AMMs which have suffered from severe economic inefficiencies as a passive side needed to be compensated via high fees for systematic losses to toxic traders. As such the economic efficiency of decentralized finance has lagged behind that of its centralized counterpart. Krypton makes DeFi significantly more economically efficient than any exchange mechanism available in CeFi or DeFi to this day. We hope that Krypton will create an incentive for unsophisticated market participants such as pension funds, mutual funds, and retail investors that have traditionally been on the losing end of market shenanigans practiced by high-frequency traders and hedge funds to move into DeFi.

What we learned

We have built Krypton using a number of technologies none of us have used before. We were impressed with the quality of the open source stack that we built upon and their developer friendliness. Kudos to the Chainlink organizers and sponsors for the hours of video content to get us started with their tech.

What's next for Krypton

We are thrilled to be part of the Chainlink hackathon and hope that this event will help publicize the idea that a financial ecosystem where the majority does not systematically lose to a small, technically well-versed, well-informed, and sophisticated minority is not only a remote possibility but feasible with technology available today as proven by our implementation. The next steps will be to conduct security audits and publicly launch the protocol next year.

Built With

+ 1 more
Share this project: