Using 1inch ChiGas tokens to reduce transaction costs
What are gas tokens and example usage for Uniswap v2

Gas prices have been occasionally above 1000 Gwei in the past in peak times. Given an ETH price of over 1000 USD, this can lead to insane real transaction costs. In particular this can be a pain when using onchain DEX's like Uniswap, resulting in hundreds of dollars transaction fees for a single trade.
People have been using gas tokens to combat this issue. Let's explore what those are, how to use them and how you would integrate them to trade on Uniswap.
What are Gas Tokens?
Gas tokens are an Ethereum ERC-20 token that allows users to tokenize gas on the Ethereum network, storing gas when it is cheap and using / deploying this gas when it is expensive.
Using GasToken is particularly useful for scenarios of arbitraging decentralized exchanges or buying into ICOs early. They further allow users to buy and sell gas directly, enabling long-term "banking" of gas that can help shield users from rising gas prices.
The gas tokens work due to the refund mechanism of the Ethereum network. Whenever you release storage, Ethereum will refund you some gas for it, because you are reducing the size of the blockchain. Zero-ing a 32 bytes storage variable releases 15000 gas, while the setting to 0 operation costs 5000, effectively giving you a 3x gas return. Alternatively one can self-destruct a contract. This would cost 700 for the the call to the contract + 5000 for the selfdestruct operation, but refunds quite a bit more: 24000 gas.
And this is what gas tokens are using. You can mint them and they will store data in the contract. Then you can burn the tokens inside a transaction which will release gas. Refunds can only give up to half of the gas used by the transaction.
There are currently three versions available:
- GST1
- GST2
- Chi-Gas
GST1 is using the mechanism of storage variables which is more efficient when the gas prices between minting and burning is less than 3.71x different. In our current times of crazy gas fluctuations you are probably better off going with the other alternative. GST2 relies on the contract self-destruct mechanism, as well as the Chi-Gas.
What are Chi Gas Tokens?
Chi Gas added three ways to improve the efficiency of GST2:
- Reducing the contract address size by mining a private key with Profanity address generator, which allowed to decrease size of the sub contracts by 1 byte.
- Using
CREATE2
instruction to deploy sub smart contracts for their efficient address discovery during burning process. - Fixing ERC20 incompatibilities of GST2.

You can use the the Chi-Gas in your contracts using the modifier on the right. A short explanation of the constant values:
21000
= initial cost of a transaction16 * msg.data.length
= calldata cost (see EIP-2028)gasStart - gasleft()
= the actual gas used by your code14154
= freeFromUpTo costs41947 = refund per burnt gas token * 2 (times two due to max refund of half of all gas costs)
modifier discountCHI {
uint256 gasStart = gasleft();
_;
uint256 initialGas = 21000 + 16 * msg.data.length;
uint256 gasSpent = initialGas + gasStart - gasleft();
uint256 freeUpValue = (gasSpent + 14154) / 41947;
chi.freeFromUpTo(msg.sender, freeUpValue);
}
Now all you need to to do is add the ChiToken interface with the address:
interface ChiToken {
function freeFromUpTo(address from, uint256 value) external;
}
ChiToken constant public chi = ChiToken(0x0000000000004946c0e9F43F4Dee607b0eF1fA1c);
and your modifier will be working. When you want to use it, make sure to mint and approve Chi tokens to your contract beforehand. Oh and you can also deploy new contracts using Chi-gas using deployer.eth.
Example: Adding Chi tokens into UniSwap v2 integration
We will be building on top of my previous Uniswap v2 integration. As a reminder it is build on top for the Kovan test network using DAI. All we need to no is add the discountCHI
modifier and add a new function with this modifier.
function convertEthToDaiWithGasRefund(uint daiAmount) external payable discountCHI {
_convertEthToDai(daiAmount);
}
Congratulations, our Unicorn is now releasing its gas!

Fully working Remix example
Here's a fully working example you can use directly on Remix, see below for instructions how to use it and a comparison.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.1;
import "https://github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/interfaces/IUniswapV2Router02.sol";
interface ChiToken {
function freeFromUpTo(address from, uint256 value) external;
}
contract UniswapExample {
ChiToken constant public chi = ChiToken(0x0000000000004946c0e9F43F4Dee607b0eF1fA1c);
IUniswapV2Router02 constant public uniRouter = IUniswapV2Router02(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);
address constant public multiDaiKovan = 0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa;
modifier discountCHI {
uint256 gasStart = gasleft();
_;
uint256 gasSpent = 21000 + gasStart - gasleft() + 16 * msg.data.length;
chi.freeFromUpTo(msg.sender, (gasSpent + 14154) / 41947);
}
function convertEthToDai(uint daiAmount) external payable {
_convertEthToDai(daiAmount);
}
function convertEthToDaiWithGasRefund(uint daiAmount) external payable discountCHI {
_convertEthToDai(daiAmount);
}
function getEstimatedETHforDAI(uint daiAmount) external view returns (uint256[] memory) {
return uniRouter.getAmountsIn(daiAmount, _getPathForETHtoDAI());
}
function _getPathForETHtoDAI() private pure returns (address[] memory) {
address[] memory path = new address[](2);
path[0] = uniRouter.WETH();
path[1] = multiDaiKovan;
return path;
}
function _convertEthToDai(uint daiAmount) private {
// using 'now' for convenience in Remix, for mainnet pass deadline from frontend!
uint deadline = block.timestamp + 15;
uniRouter.swapETHForExactTokens{ value: msg.value }(
daiAmount,
_getPathForETHtoDAI(),
address(this),
deadline
);
// refund leftover ETH to user
(bool success,) = msg.sender.call{ value: address(this).balance }("");
require(success, "refund failed");
}
// important to receive ETH
receive() payable external {}
}
Uniswap Example Walkthrough
- First we have to mint some Chi-gas tokens. Switch MetaMask to Kovan and get some Test ETH. You can now mint Chi-gas tokens via Etherscan. Click
mint
and mint at least 4 tokens. - Now have to deploy above contract to Kovan via Remix. Do this and copy the address.
- We can now approve at least 4 tokens to our contract again via Etherscan and
approve
. - Now you can call the
convertEthToDaiWithGasRefund
function, but don't forget to pass enough Wei along in the top left in Remix. As a comparison we can callconvertEthToDai
to see the difference. Make sure to set a higher gas price to reflect mainnet.
Here's a comparison of my trade without and with using Chi-gas:


Quite a bit saved assuming we minted the Chi-gas during periods of lower gas prices.
Gas tokens won't exist in the future
Vitalik just recently posted a new EIP-3298 which proposes to remove the refund mechanism. I let him explain it:
He goes further on specifically about gas tokens which in his words have 'downsides to the network, particularly in exacerbating state size (as state slots are effectively used as a “battery” to save up gas) and inefficiently clogging blockchain gas usage'.
Keep in mind this is a very early EIP and won't come into effect any time soon. But it's on the horizon that gas tokens won't exist forever.
Solidity Developer
More great blog posts from Markus Waas
How to use ChatGPT with Solidity
Using the Solidity Scholar and other GPT tips
How to integrate Uniswap 4 and create custom hooks
Let's dive into Uniswap v4's new features and integration
How to integrate Wormhole in your smart contracts
Entering a New Era of Blockchain Interoperability
Solidity Deep Dive: New Opcode 'Prevrandao'
All you need to know about the latest opcode addition
How Ethereum scales with Arbitrum Nitro and how to use it
A blockchain on a blockchain deep dive
The Ultimate Merkle Tree Guide in Solidity
Everything you need to know about Merkle trees and their future
The New Decentralized The Graph Network
What are the new features and how to use it
zkSync Guide - The future of Ethereum scaling
How the zero-knowledge tech works and how to use it
Exploring the Openzeppelin CrossChain Functionality
What is the new CrossChain support and how can you use it.
Deploying Solidity Contracts in Hedera
What is Hedera and how can you use it.
Writing ERC-20 Tests in Solidity with Foundry
Blazing fast tests, no more BigNumber.js, only Solidity
ERC-4626: Extending ERC-20 for Interest Management
How the newly finalized standard works and can help you with Defi
Advancing the NFT standard: ERC721-Permit
And how to avoid the two step approve + transferFrom with ERC721-Permit (EIP-4494)
Moonbeam: The EVM of Polkadot
Deploying and onboarding users to Moonbeam or Moonriver
Advanced MultiSwap: How to better arbitrage with Solidity
Making multiple swaps across different decentralized exchanges in a single transaction
Deploying Solidity Smart Contracts to Solana
What is Solana and how can you deploy Solidity smart contracts to it?
Smock 2: The powerful mocking tool for Hardhat
Features of smock v2 and how to use them with examples
How to deploy on Evmos: The first EVM chain on Cosmos
Deploying and onboarding users to Evmos
EIP-2535: A standard for organizing and upgrading a modular smart contract system.
Multi-Facet Proxies for full control over your upgrades
MultiSwap: How to arbitrage with Solidity
Making multiple swaps across different decentralized exchanges in a single transaction
The latest tech for scaling your contracts: Optimism
How the blockchain on a blockchain works and how to use it
Ultimate Performance: The Aurora Layer2 Network
Deploying and onboarding users to the Aurora Network powered by NEAR Protocol
What is ecrecover in Solidity?
A dive into the waters of signatures for smart contracts
How to use Binance Smart Chain in your Dapp
Deploying and onboarding users to the Binance Smart Chain (BSC)
Using the new Uniswap v3 in your contracts
What's new in Uniswap v3 and how to integrate Uniswap v3
What's coming in the London Hardfork?
Looking at all the details of the upcoming fork
Welcome to the Matrix of blockchain
How to get alerted *before* getting hacked and prevent it
The Ultimate Ethereum Mainnet Deployment Guide
All you need to know to deploy to the Ethereum mainnet
SushiSwap Explained!
Looking at the implementation details of SushiSwap
Solidity Fast Track 2: Continue Learning Solidity Fast
Continuing to learn Solidity fast with the advanced basics
What's coming in the Berlin Hardfork?
Looking at all the details of the upcoming fork
Openzeppelin Contracts v4 in Review
Taking a look at the new Openzeppelin v4 Release
EIP-3156: Creating a standard for Flash Loans
A new standard for flash loans unifying the interface + wrappers for existing ecosystems
Tornado.cash: A story of anonymity and zk-SNARKs
What is Tornado.cash, how to use it and the future
High Stakes Roulette on Ethereum
Learn by Example: Building a secure High Stakes Roulette
How to implement generalized meta transactions
We'll explore a powerful design for meta transactions based on 0x
Utilizing Bitmaps to dramatically save on Gas
A simple pattern which can save you a lot of money
Using the new Uniswap v2 as oracle in your contracts
How does the Uniswap v2 oracle function and how to integrate with it
Smock: The powerful mocking tool for Hardhat
Features of smock and how to use them with examples
How to build and use ERC-721 tokens in 2021
An intro for devs to the uniquely identifying token standard and its future
Trustless token management with Set Protocol
How to integrate token sets in your contracts
Exploring the new Solidity 0.8 Release
And how to upgrade your contracts to Solidity 0.8
How to build and use ERC-1155 tokens
An intro to the new standard for having many tokens in one
Leveraging the power of Bitcoins with RSK
Learn how RSK works and how to deploy your smart contracts to it
Solidity Fast Track: Learn Solidity Fast
'Learn X in Y minutes' this time with X = Solidity 0.7 and Y = 20
Sourcify: The future of a Decentralized Etherscan
Learn how to use the new Sourcify infrastructure today
Integrating the 0x API into your contracts
How to automatically get the best prices via 0x
How to build and use ERC-777 tokens
An intro to the new upgraded standard for ERC-20 tokens
COMP Governance Explained
How Compound's Decentralized Governance is working under the hood
How to prevent stuck tokens in contracts
And other use cases for the popular EIP-165
Understanding the World of Automated Smart Contract Analyzers
What are the best tools today and how can you use them?
A Long Way To Go: On Gasless Tokens and ERC20-Permit
And how to avoid the two step approve + transferFrom with ERC20-Permit (EIP-2612)!
Smart Contract Testing with Waffle 3
What are the features of Waffle and how to use them.
How to use xDai in your Dapp
Deploying and onboarding users to xDai to avoid the high gas costs
Stack Too Deep
Three words of horror
Integrating the new Chainlink contracts
How to use the new price feeder oracles
TheGraph: Fixing the Web3 data querying
Why we need TheGraph and how to use it
Adding Typescript to Truffle and Buidler
How to use TypeChain to utilize the powers of Typescript in your project
Integrating Balancer in your contracts
What is Balancer and how to use it
Navigating the pitfalls of securely interacting with ERC20 tokens
Figuring out how to securely interact might be harder than you think
Why you should automatically generate interests from user funds
How to integrate Aave and similar systems in your contracts
How to use Polygon (Matic) in your Dapp
Deploying and onboarding users to Polygon to avoid the high gas costs
Migrating from Truffle to Buidler
And why you should probably keep both.
Contract factories and clones
How to deploy contracts within contracts as easily and gas-efficient as possible
How to use IPFS in your Dapp?
Using the interplanetary file system in your frontend and contracts
Downsizing contracts to fight the contract size limit
What can you do to prevent your contracts from getting too large?
Using EXTCODEHASH to secure your systems
How to safely integrate anyone's smart contract
Using the new Uniswap v2 in your contracts
What's new in Uniswap v2 and how to integrate Uniswap v2
Solidity and Truffle Continuous Integration Setup
How to setup Travis or Circle CI for Truffle testing along with useful plugins.
Upcoming Devcon 2021 and other events
The Ethereum Foundation just announced the next Devcon in 2021 in Colombia
The Year of the 20: Creating an ERC20 in 2020
How to use the latest and best tools to create an ERC-20 token contract
How to get a Solidity developer job?
There are many ways to get a Solidity job and it might be easier than you think!
Design Pattern Solidity: Mock contracts for testing
Why you should make fun of your contracts
Kickstart your Dapp frontend development with create-eth-app
An overview on how to use the app and its features
The big picture of Solidity and Blockchain development in 2020
Overview of the most important technologies, services and tools that you need to know
Design Pattern Solidity: Free up unused storage
Why you should clean up after yourself
How to setup Solidity Developer Environment on Windows
What you need to know about developing on Windows
Avoiding out of gas for Truffle tests
How you do not have to worry about gas in tests anymore
Design Pattern Solidity: Stages
How you can design stages in your contract
Web3 1.2.5: Revert reason strings
How to use the new feature
Gaining back control of the internet
How Ocelot is decentralizing cloud computing
Devcon 5 - Review
Impressions from the conference
Devcon 5 - Information, Events, Links, Telegram
What you need to know
Design Pattern Solidity: Off-chain beats on-chain
Why you should do as much as possible off-chain
Design Pattern Solidity: Initialize Contract after Deployment
How to use the Initializable pattern
Consensys Blockchain Jobs Report
What the current blockchain job market looks like
Provable — Randomness Oracle
How the Oraclize random number generator works
Solidity Design Patterns: Multiply before Dividing
Why the correct order matters!
Devcon 5 Applications closing in one week
Devcon 5 Applications closing
Randomness and the Blockchain
How to achieve secure randomness for Solidity smart contracts?