A boat in a container shipyard

Tech Talks: An Elegant Solution to Batched Gas Reimbursements

Shipyard’s development team came up with an elegant solution to an issue we encountered following Clipper’s first community liquidity program.

Written by

Hrithik Datta

Published on

February 25, 2022

Skip the empty chatter with Tech Talks, an ongoing crypto series that covers specific technical challenges and how we’ve solved them. Tech Talks is written for (and by) galaxy brains who love digging into the details.

We’d like to shine a spotlight on Shipyard’s development team for coming up with an elegant solution to an issue we encountered following Clipper’s first community liquidity program (CLP). Our developers created a custom smart contract that streamlined operations and significantly reduced transaction costs. See the details below.

Issue

During Clipper’s inaugural CLP, some participants racked up significant gas fees when they tried to provide liquidity for pools that were already full. This was due to a bug in the CLP’s front-end interface, and the Shipyard team immediately set out to compensate the 70+ wallets who lost ETH as a result of this bug.

Solution

To avoid the costly process (in gas) of sending 70+  individual transactions through the Ethereum mainnet, we came up with a better solution. A custom smart contract designed to safely distribute all 70+ reimbursements to Clipper’s affected community members simultaneously via a single on-chain transaction. This smart contract deployment significantly reduced gas expenditures while creating a single on-chain transaction record for the entire reimbursement process. 

Approach

  1. Forking Clipper’s mainnet through Hardhat to create a smart contract test environment. The first step was simulating Ethereum’s mainnet using Hardhat, a development environment used to test, debug, and deploy Ethereum-based dApps. This was done using the tools in Hardhat’s configuration file, which allowed us to fork the network and create an ethereum archive node testing environment using the Alchemy API. 
  1. Creating a Python macro to compose address-specific Solidity code. We collected each failed transaction that occurred during the CLP as a .csv file. We used the csv Python library to parse the data from this address list and output a safeEthSend string with the appropriate recipient address and reimbursement amount into the constructor of the Solidity contract. These operations were explicitly written out instead of having a loop. In other words, our Python macro wrote new smart contract code out for each address instead of having a command repeat with different addresses. This was a necessary workaround due to Solidity’s coding constraints regarding external data.
  1. Creating a “safe send” Solidity function and javascript test to ensure correct reimbursement parameters. Our custom Solidity smart contract was designed to send out all the reimbursements by using a “safe send” function which executed each ETH transaction before checking that the send was actually executed. This process of checking the status of each ETH send, after each transaction execution is generally considered a best practice. Furthermore, we set up a “self destruct” function at the end of the smart contract constructor to send any leftover ETH to a designated address after the users received their reimbursements.
  1. Testing the smart contract and distributing ETH reimbursements. To simulate the functionality of the smart contract, we then used the ethers.js library to run a test deployment of our custom smart contract in the forked Ethereum mainnet on Hardhat. Specifically, we used Hardhat to create an account on the forked mainnet with enough test ETH to trial run our custom reimbursement distribution solution.

From there, a JavaScript test written using the ethers.js library cross-checked the old and new ETH balances of each address by referencing the data in the .csv file. This was done to verify that all the correct ETH reimbursement amounts would be sent to the respective recipients once the contract was deployed on mainnet. Once testing was completed and all results were verified on Hardhat’s forked network, we deployed the smart contract on the Ethereum mainnet and successfully reimbursed all those who lost gas when trying to deposit liquidity on Clipper. 

We’ll continue to shine a spotlight on future challenges and our creative attempts to solve them.


Written by

Hrithik Datta

Published on

February 25, 2022

September 29, 2022

Among the US Congress' proposed bills, the Digital Commodities Consumer Protection Act takes the most practical approach to digital asset oversight.

September 13, 2022

New DEXs are pushing beyond simple spot trades for common crypto assets, and Clipper users can now become a Sponsor and earn among the highest yields in DeFi!

Keep up with DeFi by subscribing to Shipyard’s industry newsletter

We’ll send you the latest news about DEXs, stats on our products, interesting articles, and CEO Mark Lurie’s thoughts on it all.

Thank you! Your submission has been received!
Oops! Something went wrong. :-( Please try again.