DeFi

Why Is DeFi Expensive

In a previous article, I talked about gas prices, I talked a little bit about what determines how much gas you pay for a transaction. Earlier today, I was talking to a friend, who was complaining about needing to pay $5 in gas to withdraw from a position on Pancake Bunny, and I realized I didn’t really go very deep on why certain things take so much gas to do on the network. And I know all you Ethereum users just started laughing at me for complaining about that low in gas prices, but you’ll see why it’s great when ETH 2.0 comes out.

Just a word of warning, this one is going to draw on a lot of my software development background directly, so is going to be a little more technical than most of my other posts, and is going to include some code snippets. Sorry in advance if that is not your thing.

For the usual disclosure, I am not a financial advisor, I don’t even work in finance at all. My day job is as a telecommunications software engineer. Treat everything you read here as some educational resources and not financial advice.

What Is Gas

I did an entire article on gas and gas prices, but a quick refresher is in store for this article. Gas is basically an imaginary unit that determines how much work your request needs to do on the network. Every operation you do takes a certain amount of gas. A basic transfer of the base currency of the network costs 21,000 gas for example.

Where things start to add up is when you start interacting with smart contracts. Every single operation within the code flow of the smart contract, contribute to the gas needed for the transaction. We’ll go through some example code below to highlight it, but there is a lot of things that go into a smart contract transaction. Looking up balances, doing math operations, changing balances, minting or burning tokens, just so many different things that can be required to be done in what might seem like a simple transaction.

And of course then whatever gas is required to do for the transaction get multiplied out by the gas price which gives you the actual fee of the transaction.

Simple Token Transfer

So let’s first take a look at a simple token transfer from one wallet to another. We’re using the basic BEP20 standard code here, but it applies to any of the tokens as they all have essentially the same code involved. We’ll look at the transfer() function, which facilitates moving the tokens between the addresses.

function _transfer(address sender, address recipient, uint256 amount) internal {
	require(sender != address(0), 'Cannot transfer from 0 address');
	require(recipient != address(0), 'Cannot transfer to 0 address');
	_balances[sender] = _balances[sender].sub(amount, 'Cannot transfer more tokens than you have');
	_balances[recipient] = _balances[recipient].add(amount);
	emit Transfer(sender, recipient, amount);
}

Ok, so a few lines of code, not bad right? But unfortunately, for any of you non developers out there, there is a part you may be missing.

_balances[sender] = _balances[sender].sub(amount, ‘Cannot transfer more tokens than you have’);

See that little .sub() function call in there, and the .add() on in the line below it. Those call out library functions that actually do a little bit of work, so let me expand this function out and include what the libraries are doing. If you’re curious, it’s the standard SafeMath library.

function _transfer(address sender, address recipient, uint256 amount) internal {
	require(sender != address(0), 'Cannot transfer from 0 address');
	require(recipient != address(0), 'Cannot transfer to 0 address');
	require(amount <= _balances[sender], 'Cannot transfer more tokens than you have');
	_balances[sender] = _balances[sender] - amount;
	uint256 test = _balances[recipient] + amount;
	require(test >= _balances[recipient], 'Addition overflow');
	_balances[recipient] = test;
	emit Transfer(sender, recipient, amount);
}

Well, that looks a whole lot more complicated, doesn’t it. Let’s break it down line by line.

The first two lines just check to make sure that neither the sender nor receiver are the 0x0 address. For some reason the default code from Binance and is commonly used restricts sending to the address, and having people burn using a function call. I personally generally remove the sending to restriction here to save a little gas, and if people want to burn by sending to that address, all the power to them.

Next up we have a check to make sure the sender has enough tokens to cover the transfer, and then we handle actually removing the tokens from their balance. By subtracting the numbers and then assigning the result to their balance, which technically counts as 2 steps, the math, and then the assignment.

The next two lines go together, the first one adds the amount of tokens to the receiver’s current balance, and then follows that up with a check to make sure there was no overflow. For the non-programmers, and overflow is just when you hit the top limit of what a number unit can hold and it starts back at the lowest number, in this case would be a zero (could be negative with some variable types).

After we validate the number is good, we assign it to the balance of the receiver, saving ourselves one math step as we already did it to be able to handle the overflow check. Finally we send out a Transfer event to the blockchain.

That’s 9 steps, and all to simply transfer tokens from one BEP20 wallet to another, and this does not even involved any kind of Decentralized Application, this is as basic of a transaction as you can get, besides just transferring around the basic currency like ETH or BNB.

More Advanced Transactions

Let’s take what we just learned about how a simple transfer works and look at something a little more complicated, a swap through a Decentralized Exchange. I won’t get into all the code involved here, as the article would get just too long including all of that, but I think it’s unnecessary anyways. If you’re interested in the coding side of things, maybe want to be involved in a community built Decentralized Application, I may have something in the works coming up, so stay tuned.

Imagine you want to do a simple swap, some Binance Coin (BNB) for some PancakeSwap (CAKE). Let’s walk through all of the steps involved in this swap.

First, the BNB you send is going to get converted to WBNB by using the interface to that token and minting some by depositing your BNB there. Next, it going to transfer your WBNB to the liquidity pair contract. Finally it’s going to transfer the CAKE tokens from the pair contract to your wallet.

Two of the three steps there use the same 9 steps from the simple transfer, and the minting of WBNB uses a few more, so a little over 30 operations for that just that simple DEX swap.

Now think about what is involved in say providing to a liquidity pool. You wrap your BNB, you transfer both tokens, it mints your LP tokens, and it transfer them back to you. Withdrawing LP tokens you have staked? Now you’re also getting transfers of your rewards as well.

All of this can add up pretty quickly, leading to a lot of gas being needed and driving up the gas prices we all have to pay for our DeFi fun.

Conclusions

As you can see, quite a lot of things need to happen when you are interacting with more complex smart contracts, especially in the DeFi space where there is a lot of steps involved in each transaction, which involves a lot of checks and balances on top of the actual things it needs to do.

I hope this article helps shed a little light into the more technical side of it, and helps you to understand exactly where your gas money is going.

Socials And Other Links

Find me on social media on TwitterFacebookInstagramTelegram and noise.cash.

If you enjoyed this content, you can check me out every weekday. My posts start at my website, but you can also find them cross posted at Publish0xLeoFinancialHive, and read.cash.

I also post a weekly price update video every Saturday over on my YouTube channel, where I will be discussing the weekly price action for some of the major cryptos. You can also sign up for my newsletter which I send out every Friday with news and whatnot from the crypto space, delivered right to your inbox!

You can also find links to resources such as research and news sites over at this link.

Want some more content right now? Check out some of my previous posts:

Next Level Swaps: SwapSpaceExplained

DeFi Money Markets
DeFi Derivatives
Ampleforth

A few referral links, in case you are interested in the service, and it also helps me out.

Binance – large centralized exchange – referral link saves you 10% on trading fees
Coinbase – basic crypto exchange – referral link gets you bonus crypto on first deposit
Cointiply – very good crypto faucet and earning site – no bonus for you on this referral unfortunately