Chain Labs

A bridge that moved Toucan's carbon tokens between Polygon and Celo

Toucan Protocol wanted its carbon pool tokens on Celo as well as Polygon, as the same token on each chain. At Curve Labs, Mihir was the primary engineer of the bridge: a messenger contract that burns the tokens on one chain and mints them on the other.

Founder’s experience at Curve Labs

Client
Toucan Protocol
Employer
Curve Labs
Service
Blockchain
When
March to July 2022
Team
Mihir

The problem

Toucan Protocol brings carbon credits on-chain and pools them into tokens such as BCT and NCT. Toucan wanted holders to move those pool tokens between Polygon and Celo.

The common way to bridge a token locks it on one chain and mints a wrapped copy on the other. Toucan chose a different model. It wanted one real carbon token on each chain, never a wrapped copy. The pool token contract is the same on every chain, with the same interface, so a BCT bridged to Celo is a BCT there. The bridge burns tokens on the departing chain and mints them on arrival.

The risk is on the minting side. If the bridge acted on one transfer twice, it would mint carbon tokens with no burned tokens to match.

What Mihir built

Mihir worked on the bridge at Curve Labs from March to July 2022. He built it inside Toucan's existing protocol, and Toucan's engineers reviewed his work. He was the main author of the ToucanCrosschainMessenger contract, and he wrote its storage contract and the token interface. He added mint and burn functions to the pool token contracts, and only the messenger can call them.

A transfer starts when a holder calls the messenger on their chain. The messenger records the request under a hash of the sender, token, amount, destination and a global counter. It sends a mint message through the Abacus messaging network, and it burns the holder's tokens. The messenger on the other chain checks that it has never seen that hash and records the hash. It then mints the same amount of the pool token to the holder. The global counter gives two otherwise identical transfers different hashes, so the messenger never mistakes one for a repeat.

The burn-and-mint model keeps one real token per chain. The cost is that the bridge must be able to mint pool tokens itself. Mihir limited that power: the new functions accept calls only from the messenger address set on each pool. Setting that address to zero turns bridging off for the pool.

Mihir was also the main author of the bridge's tests and its deploy script. He wrote the first Hardhat tasks that connect the messengers on each network. He deployed the staging bridge to the Celo Alfajores and Polygon Mumbai testnets, and he co-wrote the bridge's documentation.

What was hard

Mihir first built the messenger on Optics, a cross-chain messaging protocol. In May 2022 the bridge moved from Optics to Abacus, a fork of Optics that relies on a quorum of validator signatures. Mihir rebuilt the messenger on Abacus's router contract and removed the Optics contracts. He moved the whole test suite onto Abacus's test harness. A holder still made one call to send, and the bridge still burned on one chain and minted on the other.

After that, the code review changed the contract. One global counter replaced the separate counters for each token. The review removed the cooldown period and the admin action to revert a request, both of which Mihir had built. The messenger became pausable and upgradeable. Its storage contract stays last in the inheritance order, as a precaution against an upgrade shifting its stored state.

The result

After Mihir's work ended in July 2022, another engineer on the project deployed the messenger to Celo and Polygon mainnet. The messenger has the same address on both chains. The facts panel links to each.

The bridge carried about $6M of cross-chain bridge volume. That figure counts every bridge transaction at the value of the tokens it transferred.

“Mihir is an exceptional smart contract engineer with whom I have had the privilege of working intensively for the past 3 years. His brilliance in the field of blockchain technology is truly remarkable.”

Renç Korzay, Lead PM

Built with

ContractsSolidity, with OpenZeppelin upgradeable (UUPS) and pausable contracts
Cross-chain messagingAbacus, moved from Optics during the build
Request hashesEIP-712 domain separator
TestsHardhat, Waffle and Chai, against Abacus's local test harness
Deploymenthardhat-deploy and Hardhat tasks
ChainsPolygon and Celo, with staging on Mumbai and Alfajores