Optimizing The Graph by 10x to handle the load of PancakeSwap contracts on BSC.
A Collaboration with Polar Fox: Optimizing “The Graph” by 10x to Handle Significant Transaction Volumes on BSC
PancakeSwap is easily the largest project on Binance Smart Chain today. It is a DEX that allows users to trade tokens, add liquidity to pools, and provide yield farming activities — all in a trustless manner. As a fork of Uniswap but deployed on the booming BSC network, PancakeSwap used “The Graph” (GRT) to index events from smart contracts. For context, The Graph is an indexing protocol used to query networks like Ethereum, Binance Smart Chain, IPFS, etc. Anyone can build and publish open APIs, called “subgraphs,” making data (or the indexing of events) easily accessible.
Events and logs are important in blockchains like Ethereum and BSC because they facilitate communication between smart contracts and their user interfaces, just like how a server response is provided in a callback to the frontend in the traditional web development.
But the BSC network generates blocks five times faster than Ethereum. Hence, after some height X, the speed of block indexation of The Graph became lower than the speed of the BSC block generation, causing the front-end/UI of many decentralized applications to fail or work improperly.
In an effort to contend with the glaring problem of surging transaction volumes on BSC, we were approached by the development team at Polar Fox, a world-class API solutions platform designed for decentralized applications (“dapps”). Polar Fox has been developing a uniquely tailored solution for dapps to quickly and easily query data in real-time on high throughput blockchains such as BSC and Polygon (Matic). Since The Graph is already an open-sourced tool built for dapps, the most logical path forward was to optimize an existing, functional solution on Ethereum to handle the transaction load of a major protocol like PancakeSwap on BSC.
In close collaboration with the Polar Fox development team, we helped to design, implement, test and deploy a fully robust solution capable of handling all the needs of a protocol like PancakeSwap. In summary here is what we achieved:
Optimize communication between the BSC node and The Graph by up to a ~50x improvement. We also streamlined the sync process to handle events from a batch of blocks with new sources without the process of re-requesting that is normally required.
Simplified the schemas of entities in Postgres and updated queries to accelerate insert, update and select operations by up to a ~20x improvement.
Optimized PancakeSwap’s schema on the typescript (TS) level (e.g. removed RPC calls, reduced count of save/load operations) and added new features to both graph-ts and graph-cli to reduce the count of heavy operations.
Optimized some moments on The Graph level to process events more quickly.
The combination of these optimizations drove such compelling yield improvements that event processing dropped from ~35 milliseconds to just ~2 milliseconds (~17.5x.).
In summary, our deep domain expertise, coupled with the insights and efforts of Polar Fox, helped to produce a fully robust solution capable of handling the heavy transaction load of a buzzing network like BSC. This is just one of many examples where Supercolony can add significant value in working with outside teams to foster innovation and collaborative solutions. We hope this helps your curiosity on how a leading indexing protocol can be optimised for the transaction load of a buzzing network like BSC.
SuperColony operates as a DAO venture studio designing and building companies by bringing together experienced blockchain talent, resources, and capital to compress time, iterate quickly and scale more ambitiously than would otherwise be possible.
We connect founders to all of the necessary partners to achieve success in today’s crypto ecosystem. We have aligned ourselves with partners in every major crypto geography, including traditional trading firms, VC’s, legal/compliance experts, marketing and pr firms, launchpads, and security auditors.