Today we will look at an approach to save on gas fees when writing smart contract. When Ether was worth 5$ no one was worried about spending a little more gas then they should. But with todays market, is it really a little?

Will take an example where we already considered removing a transaction cost. We will be in the context of a decentralized exchange where will execute a common procedure like creating storage for your orders.

Storage on Ethereum Network is one of the most expensive operation.

struct _Order{
uint256 id;
address maker;
address makerAsset;
uint256 makerAmount;
address takerAsset;
uint256 takerAmount;
uint256 filledAmount;
uint256 timestamp;
bool active;

This is an example. We could make it simpler, but this is what we will work with. Now we will need some function to create and cancel some orders. And we will also emit an event when creating an order to let traders now about it.

function createTokenOrderOnDex(
address _makerAsset,
uint256 _makerAmount,
address _takerAsset,
uint256 _takerAmount
) public {
orderCount += 1;
orders[orderCount] =_Order(orderCount, msg.sender ,_makerAsset, _makerAmount, _takerAsset, _takerAmount, 0, now, true);
emit Order(orderCount, msg.sender, _makerAsset, _makerAmount, _takerAsset, _takerAmount, 0, now, true);
function cancelTokenOrderOnDex(uint _id) public{
_Order storage _order = orders[_id]; = false;
function renewOrderToSaveOnGas(uint _id) public{
_Order storage _order = orders[_id]; = true;

This is a minimalist example. In a real time, smart contract, we would do a lot more verification. But let’s keep it simple. We also assume that the user already has some funds on the exchange. We are not looking at the transfer cost.

Now a lot of developer will add even more storage to this process to record trades. We will use a different approach here. So let’s evaluate the cost of creating an order. Calling the createTokenOrderOnDex function and fill all the data and emit an event. As per remix. To create an order would cost

213896 gas for transaction cost

For a total of 408 504 units of gas.

Now the gas as a price to. At the time of writing, the gas cost for a trader is 276 Gwei.

Let’s do some conversions

Ether has 18 decimals
1 Ether = 1.000000000000000000
The last 0, the smallest unit is called a Wei.

So the gas units are represented as Wei and have a price in Gwei.

1 Gwei = 0.000000001 Ether
The price is 276 Gwei * gas units in Wei

for a total of 0.06 Ether in transaction fees.
At the price of 1800 USD this is 108$ USD to create an order on the Ethereum Network with traders fees. Keep in mind that it is a minimalist example to. You get the picture.

Now what could we do to save on the cost. Well let’s say this order get filled. The users want to, for the sake of the example, create a second order that is the same. Bob decided that he would buy 100 $ of that token he just got, every Friday.

So since we are good developer’s, and we want what is best. We are going to help Bob Save Money on Transaction fees.

Now instead of creating another other on the blockchain and add data, we could simply reactivate the order that Bob got filled already by calling the renewOrderToSaveOnGas function and set the state of that order to active again.

The cost in Gas this time to create the same order;

42418 units of gas for the transaction

The cost in now 21$ USD To create the Orders

The saving are non-negligible. Let’s Recap;

We crated an order and inserted all the required data in the structure of our smart contract. When ever this order get’s filled. in orders to save on fees, we avoided the insertion of more data but reused the data that was there. and simply reactivate the order.

Saving almost 80$ in fees for Bob.Another thing worth mentioning. Seting the value of the active order proprety to false is cheaper then setting it to true. So cancelling an order is about half 10$.

You can see the contract that was use to create these test on Rinkeby

I hope you enjoyed.

Till Next time!

I can do anything, I’m the lizard king

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store