This time the blog gives an update on QRC20 token transactions, as an addition to my blog QRC20 Token Report (my most popular, 3.3k views, thank you!!!). This means a discussion of how Gas is used with smart contracts and understanding Gas Price and Gas Limit. We will use the new Explorer to look at the details for smart contract transactions.
I am an independent blockchain researcher, occasional blogger, and social media moderator who appreciates the technical guidance from the Qtum Team. I hope to chat with all of you soon on the social medias.
My calculations give a network weight of 23.1 million because big staking wallets with a stable total balance of 1.85 million won 8.0% of the block rewards in the past 10 days. This represents a 3.8% annual rate of return for Qtum staking nodes.
This calculation covers 6,002 blocks and 528 block rewards for the big staking wallets, and IMHO is more accurate than the network weight estimate calculated by the wallet.
You can only send tokens from their “tied” Q address if it holds enough QTUM for the transaction fee.
The smart contract storage for tokens ties each token balance to a specific Q address as shown in the table below. For a transaction to send tokens (in reality tokens are transferred between Q addresses in the smart contract storage table) you MUST have sufficient QTUM in that Q address to pay the gas fees.
For the example below, address Qabcdef… could send tokens because it has 2.0 QTUM to pay for the transaction fee, but address Qghjkmn… could never send tokens because it has no QTUM to pay for the transaction ☹. You can think of it this way: you can’t send a snail mail letter by attaching a stamp (transaction fee) on a different letter, you have to put a stamp on the letter you are sending. Likewise, to send tokens you must have sufficient QTUM in the tied Q address to pay the transaction fee (including “Gas” — will discuss below).
The Core wallet can have QRC20 tokens tied to multiple Q addresses (both addresses in the example above could be in the same wallet). Use the “listaddressgroupings” command to check the QTUM balance at each address or turn on coin control (Settings — Options — Wallet — Enable coin control features) to send QTUM from a specific Q address.
What to do if you are getting that error message from your Core wallet that you don’t have enough QTUM to pay the token transaction fee (“To send XXX you need QTUM on address Q…”)? The easiest thing if you have QTUM on other addresses in your Core wallet is a “payment to yourself”: in your wallet send a few QTUM to the Q address tied to the token. Otherwise add some QTUM to your wallet tied Q address — this same error for the Web wallet is:
Smart Contract Storage
The smart contract storage is the single source of truth about the token balance for every Q address. If an exchange, wallet, Explorer or anyone else has a different value for the token balance, they are wrong! To check the smart contract balance for a specific Q address, paste that Q address into the “BalanceOf” field and press “Query” on the Explorer READ SMART CONTRACT page, for example for the Bodhi BOT token:
Move the decimal point in the resulting balance value 8 places to the left to get the token value in units for BOT, “Decimals” to move over may be different for other tokens.
Gas Limit and Gas Price
TL; DR Every smart contract individual program step charges an amount of “Gas” (just QTUM really) to avoid spam and infinite loops. Smart contract transactions must specify the “Gas Limit”, or the total amount of gas they will commit to executing the contract transaction. If the contract runs out of gas before completing, the transaction is aborted, and all the gas is lost. A separate parameter “Gas Price” sets the price in QTUM for each unit of Gas and is used by the block reward winning wallet to decide which contract transactions to accept (obviously the ones with higher-priced gas).
Before we can look at QRC20 token transfer fees in more detail, we need to understand “Gas” and how it is used with Qtum smart contracts.
The staking wallet charges an amount of gas for each computation step executing the smart contract program. The amount of gas for each type of programming operation is set by a gas schedule (price list). For example, a simple operation would be 10 gas, a more complex operation would be 200 gas, and at the top end a contract creation operation would be 53,000 [Reference 1].
The important thing in setting the amount of gas to send with your transaction, which is called the “Gas Limit”, is to send enough gas so that the smart contract transaction can complete without “running out of gas”. If your transaction runs out of gas, the wallet keeps all your gas and cancels the transaction.
The other factor with gas is the Gas Price, which sets how many QTUM you are paying for each unit of gas. The Gas Price would be a factor if the Qtum blocks were becoming full because the block reward winning wallet chooses which transactions to include if a block, and (if the block is full) it will pick the transactions with higher-priced gas, and contract transactions with lower-priced gas will have to wait for later blocks.
Here are some examples, and then we will look at actual Gas Limits and Gas Prices on the network.
A standard Gas Limit for token transfers (sending QRC20 tokens to a Q address) is 250,000 units of gas.
Gas Prices are often given in Satoshis, where a Satoshi is 0.00000001 QTUM. The minimum Gas Price is 40 Satoshis or 0.00000040 QTUM per unit of gas. At this time, since the blocks are not full, there is no reason to pay more than this minimum Gas Price.
The transaction fee (just for the smart contract gas) is typically
40 Satoshis * 250,000 gas = 0.00000040 * 250,000 = 0.1 QTUM
There would be some additional fee added based on the size in bytes of the transaction.
Setting Gas Limit
How much gas should you use for a token transfer? Go to the new Explorer tokens page and select the transactions to see what gas other transfers are using. I looked at 100 recent transactions for BOT and found that while 42% were using the default Gas Limit of 250,000, there were some lower amounts, and 25% were using 60,000 gas:
The Explorer also tells us how much gas was actually used for each token transaction, and for these 100 transactions, there were 7 that used about 21,500 gas, 69 that used about 36,500 gas and 24 that used about 51,500 gas:
This chart shows sorted gas used for 100 BOT token transfers, with the 7 lowest at 21,500, etc. Why the different amounts of gas used? Remember gas is charged for each program step in the smart contract execution, and if there are more steps (e.g., setting up a new Q address in the storage table and then transferring an amount vs. transferring an amount to an existing Q address in the storage table) the gas charge will be higher. But notice something, for these 100 transactions the highest gas used was 51,678, so do you really need to send 250,000 gas?
We can look at the sorted Gas Limit and Gas Used charted together, and as long as these two lines don’t cross, no one ran out of gas:
For the 100 token transfers I examined, most of them used the minimum Gas Price of 40 Satoshis per unit of gas, but there was one transaction that set the Gas Price at 60 and one transaction that set the Gas Price to 71.
Setting the Gas Limit high is not so bad because you can get a gas refund for unused gas. The wallet reports the gas refund as a tiny amount “mined” which may come in the same block as the contract transaction or soon thereafter.
Setting Gas Limit and Gas Price in Qtum Web Wallet
Enough of this tutorial stuff, let’s send some QRC20 tokens using the Qtum Web wallet. I’m choosing the Web wallet because you can directly see how the fees are set, and because it has a convenient set of QRC20 tokens already added and ready to go, or click “More…” at the bottom of the list to add to add more token contract addresses.
If we are going to send some BOT tokens we know there must be sufficient QTUM in the tied address to pay the transaction fee.
To send tokens with the Web wallet, select “Send” and your token in the drop-down list. Based on the analysis we can currently set a Gas Limit of 60,000 and leave Gas Price set at the minimum of 40 (0.00000040) and leave the additional transaction fee set at 0.01. The screen would look like this:
These settings will give a total transaction fee:
0.00000040 * 60,000 + 0.01 = 0.034 QTUM
Token Send Failures
If your token transaction fails the new Explorer will tell you what the problem is if you look up your transaction:
You can get a bad instruction error by trying to send more tokens than you have for a Q address, and an out of gas error is caused by setting the Gas Limit too low.
The Explorer will show a successful token transfer like this, listing all the contract call data fields.
Bonus DAPP Topic
For the last topic, we see the gas for a smart contract creation and show how that contract appears on the Bodhi Prediction Market DAPP (Distributed Application). In the course of reviewing hundreds of contract transactions for the Bodhi smart contract, I found a contract creation for a Bodhi prediction market event. This transaction used a Gas Limit of 3,500,000, Gas Price of 40, consumed 2,909,900 Gas and paid an initial fee of 1.4 QTUM.
The contract creation sets up a bunch of stuff for the prediction market event, and you can see the contract data with the Explorer:
On the Bodhi App, this is an event for the NBA (USA National Basketball Association) championship, with about 29 QTUM bet, with leading votes for the Warriors (#3 at 27%) and Knicks (#5 at 28%):
There have been many of thousands of QRC20 token transactions on the Qtum blockchain (and millions for ERC20 which has the same specifications) for this humble token, implemented across many exchanges and wallets. This token standard is really a lynchpin in blockchain 2.0 smart contract ecosystems and with this update I hope you can understand and use this token to its full potential.
The blog finishes in the lovely city of Budapest, Hungary. According to the Qtum Nodemap, there are four nodes in Budapest, which combines the historical cities of Buda and Pest, but I will resist analogies to Qtum combining bitcoin and Ethereum, and instead show you some of the beautiful architecture.
Thanks for reading and stay safe online,
1. The Ethereum Yellow Paper lists gas prices, in Appendix G “Fee Schedule” on page 20.
2. Ethereum, Gas, Fuel and Fees (more basic).
3. Deep Dive into Gas Requirements (video, more advanced)
4. The Economics of Fees and Gas, Earlz blog (really advanced)
5. Please see my past blogs on Medium.