Built in 2021
Blits Wallet | Filecoin Loans Integration
DeFi Wallet Integration
I collaborated on the integration of the DeFI wallet with the Filecoin Network in order to allow cross-chain loans.
Made with
In late 2020, only about 2% of Filecoin’s supply was in circulation. Miners needed FIL as collateral to close storage deals. People who held FIL had no way to lend it out and earn from the time they held it. Centralized lending services filled the gap, but they brought risks. Users could lose custody, lose access, or have their funds seized.
Filecoin also had no virtual machine for outside smart contracts. A smart contract is a program that runs on a blockchain. Filecoin only had its built in actors, a small set of fixed programs. So the usual DeFi (decentralized finance) approach would not work.
I joined the Blits Labs team to help build Filecoin Loans, the first decentralized, non custodial lending protocol native to Filecoin. Non custodial means nobody else holds your money.
Loans across two chains
Filecoin Loans makes collateralized loans between Filecoin and EVM compatible blockchains. EVM is the Ethereum Virtual Machine, so these are Ethereum and chains that run the same contracts. A user can lock FIL as collateral and borrow ERC20 stablecoins. A user can also do the reverse and lock ERC20 tokens to borrow FIL.
The protocol is peer to peer. No intermediaries, bridges or validators stand between the two people. Each one keeps custody of their assets the whole time.
The flow works like this:
- A borrower creates a loan request by locking collateral.
- A lender funds that request with the principal.
- When the borrower repays, their collateral unlocks.
- If the borrower defaults, the lender can take the collateral.
Built on Atomic Loans
The protocol builds on the Atomic Loans paper. It was designed for Bitcoin and Ethereum, and we adapted it for Filecoin.
Payment channels as locks
We used Filecoin’s Payment Channel actor as our Hash Time Locked Contract (HTLC). An HTLC is a lock with two conditions. Funds move only if someone reveals a secret, and only before a deadline. We used payment channels to coordinate actions across two separate blockchains.
Before the loan starts, both people create secrets and publish only their hashes. A hash is a short fingerprint of the secret that does not reveal it. When one person reveals a secret on one chain, it unlocks an action on the other chain. This is the atomic swap pattern. Neither side can cheat without losing their stake.
Blake2b-256 on Ethereum
Filecoin hashes data with Blake2b-256, which Ethereum does not support. But our Ethereum contracts had to check hashes of Filecoin secrets.
We updated a Solidity implementation of Blake2b from ConsenSys, first built for EIP-152. The changed library outputs the Blake2b-256 variant that Filecoin uses.
A signing library without WASM
The Filecoin signing tools of that time were written in Rust and compiled to WASM (WebAssembly). Using them meant loading WebAssembly binaries. That made React Native and web apps harder to build.
So we built Filecoin JS Signer, a pure TypeScript library for Filecoin’s built in actors. It creates and signs messages for Payment Channels. It has no WASM dependencies and runs directly in JavaScript.
We also changed it to support preimages and secret hashes when creating and redeeming vouchers. A voucher is a signed promise of payment inside a channel. Those changes are the building blocks of our HTLC pattern.
Two contracts on the EVM side
The first handles loan offers. A lender locks ERC20 tokens in it. It tracks the loan state: funded, approved, withdrawn, repaid or closed. A borrower accepts an offer by giving their Filecoin address and a secret hash. The contract releases funds when the right secrets are revealed.
The second holds collateral for borrowers who want FIL. It keeps their ERC20 tokens, and price oracles set the liquidation thresholds. An oracle is a service that brings outside prices onto the chain. A minimum collateral ratio of 110% protects lenders if the FIL price drops.
Both contracts have deadlines. If one person disappears, the other can recover their funds once the deadline passes.
One repository, four parts
The project lives in one monorepo with four main parts:
- Solidity contracts for EVM chains
- A Node.js backend API for loan coordination
- A React web application for browser-based access
- A React Native mobile wallet for iOS and Android
The mobile wallet connects directly to Blits Wallet. Users manage their cross chain loans from their phone.
Limits that made it simpler
We built DeFi on a blockchain without smart contracts. That pushed us to creative solutions. Payment Channels became our way to program the chain. Cryptographic secrets took the place of on chain logic.
The limit also forced simplicity. Every cross chain message had a real cost, so we designed the protocol with as few round trips as possible while keeping it secure.
Cross chain work means thinking in state machines that span more than one system. More things can fail, and timing matters. We learned to design for partial failures and to give every state a way to recover.
Interested in working together? Check out my services and pricing to see how we can collaborate on your next project. Let's Build