Hacking for Mina ZK Apps Builders Program
My experience building a DeFi lending app using Mina ZK Apps zero knowledge smart contracts
I have been curious and following the development of the Mina ecosystem from the time it was called Coda in 2019. I found out about it while reading about ZK-rollup use cases. What got me hooked is the promise of better usability and decentralization because of it being the world's lightest blockchain. The announcement of ZK apps added to the excitement, so while having a chill vacation in beautiful Lisbon last December (2021), I decided to participate in the Mina ZK Apps bootcamp. I built a simple checkers app as part of the bootcamp to understand the core concepts while also having some fun. You can find the code in this repository. I was thrilled later to get to know that I was selected to be part of the ZK Apps Builder program based on what I built.
🔥🔥🔥 ZK Apps Hype 🔥🔥🔥
Before we dive into the details of what I started building for the builders program, let me summarize what excites me as the potentials of what can be achieved with Mina ZK Apps. As mentioned before Mina is super lightweight in terms of its size - 2kB, even more so it will remain that way(constant size) thanks to ZK rollup technology. This means that a client can run an entire blockchain node inside their browser to run any dApps that are based on Mina. Because of this, compared to an Ethereum browser based dApp that always has to communicate with a centralised RPC node, Mina dApps would be more secure and decentralized. If that advantage wasn’t enough, the architecture of Mina dApps - ZK Apps, provides some further advantages. ZK Apps use zk-SNARK circuits to validate computations made off-chain, on-chain. This means that a computation needs only to be run once (prover) and generate a zk proof for it, relieving the network of having to run the same computation in every node, improving scalability of Mina. This also means that the data used for the computation can also be kept private, opening an entirely new application area in the blockchain space - privacy preserving dApps. This is super exciting to me, as this opens up the potential of off-chain data such as credit scores, medical records, bank transaction records etc., for on-chain applications without the users losing their privacy. To emphasize this potential, I decided to focus on building an application that uses private bank transaction records for an on-chain application for my ZK Apps Builders Program project.
👷🏽 The Buidl 🚧
Therefore in collaboration with Marek I started working on the Off-Chain Transaction Analytics(OCTA) project prototype. The idea was to build a ZK App library that allows for the calculation and proving of certain statistics like average monthly income, average monthly balance etc., based on a user’s private bank transaction history. The first obvious use case is the use of these statistics/proofs for obtaining a non(under)-collateralized loan on-chain. Following is an architecture overview diagram of what I came up with for the initial iteration of the app called LendApp.
In summary the components in the diagram serves the following functionality,
Transaction Data Repository: Represents a bank or another financial institution that stores a users private financial transaction data. LendApp obtains a user’s private transaction data to make off-chain ZK proofs using the API of the Transaction Data Repository.
Lender App: Is the UI used by the lenders to publish new lending contracts with parameters such as the interest rate and the loan term.
Borrower App: Is the UI used by Borrowers to borrow money by browsing and selecting a suitable lending contract. This app is also responsible for making proofs about the users private transactional data.
Loan Smart Contract: The loan smart contract deployed on the Mina blockchain.
If you are interested in reading and understanding more about the ideas behind the OCTA prototype and its design, please refer to the design doc. For now the tl;dr is that the app allows a lender to deploy a loan contract as a Mina ZK smart contract specifying parameters like the amount, interest, term and the required financial proofs such as minimum average monthly income. Then a borrower can use the borrower UI to browse those loans and apply for a loan that they require. Afterwards we started executing on the implementation plan with Marek focusing on the transaction-data-repository(data oracle) part of that app and me focusing on the on-chain smart contract interactions and the UI.
Challenges
As expected with bleeding edge technology we faced some interesting technical challenges while executing on this plan, here are a few of them.
ZK Apps have a limited storage capacity on-chain (8 Field elements/255 bits * 8). This means that pretty much all of your contracts data that is of larger size or that might grow needs to be off-chain. Unluckily for us it meant pretty much all the data about a loan smart contract needed to stay off-chain including the required proofs field which specifies the proofs required by the contract about the transactions. Also the indexing of loan contracts needed to be implemented off-chain as well(which we skipped for the initial phase).
As OCTA was supposed to be a library to be used by other contracts we wanted to split the on-chain code base into two modules, octa library and the loan application. Doing this resulted in some hard to debug issues on the browser. Which we hope will be resolved in the future. For the moment the implementation uses a single module for all code.
JSON Serialization/Deserialization issues in SnarkyJS which were reported and promptly resolved by the O1 Labs team.
We managed to work through these challenges and others, sometimes with workarounds and other times with fixes thanks to the help from the O1 Labs team. Finally we managed to finish the initially set milestone of making the end to end integration of financial-data-repostory + UI + Mina ZK App contract satisfactorily. You can view the deployed app here.
In addition to the work related to lending app, I also published a reusable react + MUI template based on the app UI above, which you can also use for your project if you are trying out Mina ZK Apps.
Future Directions for OCTA
Despite the challenges faced while building the app, it was a pleasant experience to work with the ZK Apps framework. Compared to other ZK circuit frameworks, SnarkyJS is super easy to get started with and to build a useful app. I’m super optimistic about ZK Apps because of this developer friendliness. Basing the circuit DSL on Typescript is an excellent design choice for adoption. To play our part in this exciting journey, we are hoping to build out OCTA and its use cases further in the coming quarters and hopefully launch the app on mainnet at ZK Apps mainnet launch. Following are some of the exciting directions we think of when building out the app further,
Solve data storage/availability for loan indexing, required proofs etc. This is a common problem to solve for possibly all ZK Apps in the future. Our idea is to possibly break the problem down into smaller pieces to tackle in stages. But we are hopeful that there exists a generalized solution down the line here.
Building OCTA as a library by solving the problems we are facing at the moment.
Explore ZK Oracles for transaction data with possible mechanism designs. ZK Oracles are a feature that the O1 Labs team is working on releasing in the future. Our idea is to integrate and explore this technology to replace our transaction-data-repository design.
🙏🏽🙏🏽🙏🏽 Shout out to Mina Foundation and the O1 Labs Team 🙏🏽🙏🏽🙏🏽
I believe the ZK Apps Builder program and the associated workshops that were initiated by the Mina Foundation and the O1 Labs team has helped a great deal in creating awareness about the potential of ZK Apps while also helping developers like me get onboarded into this exciting technology. Thanks a lot for building and promoting this technology and allowing me to participate in this program. Your assistance during the program is also much appreciated. I hope there would be further programs like this and more developers would join the Mina ecosystem to build more super cool apps.
Finally I have to say,
Mina + ZK Apps Rock!!!!