Finding out how does HBD interest work by reviewing code

in LeoFinance4 years ago (edited)

image.png

I mentioned in yesterday's post, you now earn interest (currently 3%) on HBD & HBD savings.

There hasn't been any interest for HBD since I have joined, so I'm going to dive deep into how it works and what you need to know about it by reviewing the code associated with it.

Hived is the software behind Hive nodes and is what is responsible for calculating and distributing interest.

You can find the Github repo here.

Our goal is to answer a few questions to get a better understanding of how HBD interest works.

Some questions off the top of my head

  • Do you earn interest on just savings or both liquid and savings?
  • Can you buy a lot of HBD before interest payout?
  • How often is interest paid out?
  • Do I have to claim interest like rewards?
  • What if you don't do any HBD transactions in 30 days?

There are likely other questions, but let's focus on these for now.

These are great questions and are easily answered by looking at the code. I did a search for interest and looked for the portion of the code that handled paying out interest. It was pretty easy to find and answers our question well.

In the libraries/chain/database.cpp file we can see there is a section of code that does pretty much the same thing twice, once for HBD and once for HBD Savings. It is clear the interest is paid out on both.


Let's review the code presented above and see if we can't figure it out.

          if( acnt.hbd_seconds > 0 &&
              (acnt.hbd_seconds_last_update - acnt.hbd_last_interest_payment).to_seconds() > HIVE_HBD_INTEREST_COMPOUND_INTERVAL_SEC )
          {
            auto interest = acnt.hbd_seconds / HIVE_SECONDS_PER_YEAR;
            interest *= get_dynamic_global_properties().get_hbd_interest_rate();
            interest /= HIVE_100_PERCENT;
            asset interest_paid(interest.to_uint64(), HBD_SYMBOL);
            acnt.hbd_balance += interest_paid;
            acnt.hbd_seconds = 0;
            acnt.hbd_last_interest_payment = head_block_time();

            if(interest > 0)
              push_virtual_operation( interest_operation( a.name, interest_paid ) );

            modify( get_dynamic_global_properties(), [&]( dynamic_global_property_object& props)
            {
              props.current_hbd_supply += interest_paid;
              props.virtual_supply += interest_paid * get_feed_history().current_median_history;
            } );
          }

I'm not going to get too deep here, but I will quickly run through what's happening.

The code first sees if the account has a value greater than 0 in the account's hbd_seconds. If there is, it further checks if the hbd_seconds_last_update is greater than HIVE_HBD_INTEREST_COMPOUND_INTERVAL_SEC.

HIVE_HBD_INTEREST_COMPOUND_INTERVAL_SEC is a constant, this means it is not expected to change throughout the code and why it is in capitals. Let's find this constant in the code and this will answer another one of our questions.

In libraries/protocol/include/hive/protocol/config.hpp we can see HIVE_HBD_INTEREST_COMPOUND_INTERVAL_SEC is defined to 60*60*24*30. As this variable represents seconds, we can see we are multiplying by 60 to get minutes, then by 60 to get hours, then by 24 to get days, then finally 30 to get a total of 30 days worth of seconds.

The first statement requires both criteria to be true to continue. In other words, there has to be more than 0 hbd_seconds on the account and there hasn't been an HBD update in 30 days.

This logic is commonly referred to as a gate, if these two criteria are not met, no further progress in the code is made.

The next section divides the hbd_seconds by another constant that represents how many seconds in a year. At this point we don't know what hbd_seconds represents. Let's try to figure this out.

This portion of the code gives us a good understanding of what hbd_seconds represents. It is a little difficult to understand, but on line 5215 we can see hbd_seconds gets added to hbd_seconds + hbd balance * how many seconds since the last block update. This might be a little confusing, so I will use an example.

Let's say you have 100 HBD. The last update to your HBD balance was 1 hour ago. You now make another HBD transaction which forces an update to your hbd_seconds field.

Let's say you hbd_seconds is 0 to make things simple.

The code above will take your current hbd_seconds (0) and add to the result of 100 (your hbd balance) * seconds since your last update.

You can think of this as a timer that keeps track of how much HBD you own and how long you held. If you held 100 HBD for 29 days, then added 1000 HBD on day 29, your hbd_seconds would largely represent holding 100 HBD for 29 days. When it is time to pay interest on day 30, you would be paid interest on 100 HBD for 29 days and 1100 HBD for something around 1 day.

This means buying and selling right before an interest payout has little affect on your interest. We can also deduce that inactive accounts still are earning interest even if they do not do a HBD transaction within the 30 days, the interest will be paid since the last update once a transaction triggers an interest payout.

Continuing on from the main code, we take the interest variable, which represents the fraction of seconds of the year since your last interest, and multiply it by the current hbd interest rate set by witnesses.

This is then turned into a percentage and then an asset object. This asset (an object that represents a Hive currency) is added to your hbd balance. Your hbd_seconds counter is reset to 0 and the hbd_last_update time is stamped with the current head block timestamp.

Another gate is defined (also called conditional) to check if you are due any interest. If so, a virtual operation is created to payout the interest and broadcast it to the blockchain.

The code finally updates the blockchain hbd_supply and virtual supply counters.

Let's review our questions.

Do you earn interest on just savings or both?

As we discussed, interest is paid out on both.

Can you buy a lot of HBD before interest payout?

Because both the balance and time held is tracked, you cannot buy HBD before interest payouts and sell it after to game the system.

How often is interest paid out?

We can see in the code it is set to 30 days, but this can be easily changed with a hard fork.

Do I have to claim interest like rewards?

As you can see from the code, it is done automatically, what may not have been clear is this is handled whenever you transact with HBD. If you do not do any HBD transactions your interest will continue to build up and payout when a transaction is made.

What if you don't do any HBD transactions in 30 days?

All interest owed will be paid out when an HBD transaction is made. If it is longer than 30 days, you will receive your fair share based on the total time since the last payout.

You can look at hiveblocks.com to find out the current interest rate by looking in the right hand side. Peakd and Condenser will likely be updated soon to reflect the current interest rate on the wallet page.

You can review your hbd balance and interest fields for your account on hiveblocks.com as well.

I should receive an hbd interest in roughly 10 days.

If you have any other questions about HBD interest, feel free to drop it in the comment section. Hopefully this helped you understand this change and gave you a little insight in the process.

Posted Using LeoFinance Beta

Sort:  

I just checked my peakd wallet. Looks like a 3% interest is given on Hive power as well.

Yeah peakd.com just updated to show the APR for both tokens.
As far as hive power that has been there for a while as an incentive to turning your hive liquid tokens into staked hive power. But peakd simply now shares the info on the wallet page now

Cool... Great job to peakd team.. I hope to see more improvements on peakd.com during the course of the year.

That's why we started with 3% for HBD, it's roughly the same as vesting interest (3.22% last I checked).

Posted Using LeoFinance Beta

Oh thanks for clarifying

Thanks for sharing your content...

Hey @preparedwombat, I read your article and it was quite informative. Now, my question is will this interest on the HBD be real HBD or would it be accounting for inflation much like Hive Power? Would you know that?

AFAIK, actual interest in the case of HBD. Vests are not a factor as they are with Hive Power.

Ah ok. Thanks! :)

Thanks for the explanation. This certainly is a big move by the Witnesses because, you stated, since you joined, there never was interest paid on HBD.

Tangenting a bit, we see the effort to strengthen the peg of HBD to 1 USD through the use of DHF. Is this move being done as a part of that effort, to try and incentivize people to hold HBD rather than speculate on it?

It seems that we could really use a stablecoin on Hive, which, if my understanding is correct, was the original intention of HBD.

Posted Using LeoFinance Beta

Tangenting a bit, we see the effort to strengthen the peg of HBD to 1 USD through the use of DHF. Is this move being done as a part of that effort, to try and incentivize people to hold HBD rather than speculate on it?

I believe so, the main goal is to push the price action to the Hive token.

It seems that we could really use a stablecoin on Hive, which, if my understanding is correct, was the original intention of HBD.

HBD is a debt instrument (a promise you can trade it in for $1 worth of Hive on some future date of your choosing). It's goal was to be a stable coin, offering a solid solution for commerce related activities. For example if I sell a blue widget that I feel is worth $10, I know I can sell it for 10 HBD and don't need to worry about price. When HBD does not hold closely to $1, that goes out the window.

Posted Using LeoFinance Beta

Once again, Hive (steem) was ahead of its time with some of the ideas, but failed in the execution.

Hopefully the HBD and the new project will get the peg back in order.

It would help the establishment of commerce on Hive a great deal if we could peg and hold it.

Posted Using LeoFinance Beta

"Hive (steem) was ahead of its time with some of the ideas, but failed in the execution." unfortunately I couldn't agree more, we missed out big time on some major opportunities, but fortunately, time is still on our side :)

Posted Using LeoFinance Beta

@taskmaster4450le

It would help the establishment of commerce on Hive a great deal if we could peg and hold it.

that is so true sir,that will really make the HIVE to get a better value and stay more relevant...

Posted Using LeoFinance Beta

If people/groups are working to move/peg the price of various tokens and currencies, what happens if some of are holders (either openly or secretly) of those tokens and currencies? It appears to be a recipe for shenanigans, putting it lightly. There are no regulators here now, but everything is recorded for all time, and eventually, there will be regulators. Even AI regulators. And they'll be looking back in time and unraveling everybody's participation in, well, everything. Hopefully (although statistically it's very unlikely) there aren't any shenanigans going on.

I saw the rolled up bills but was disappointed this wasn't a shitpost about a cocaine party. :(

This seems to be a fait accompli. Have I not been paying attention to extensive community-wide discussions (beyond just witnesses) debating the pros and cons of this change or did something like that simply not happen?

Posted Using LeoFinance Beta

Edit: Ok to be fair, there was this post:
https://hive.blog/hbd/@smooth/why-i-set-my-witness-hbd-interest-rate-parameter-to-3

So was there enough discussion? Maybe not, but changing the APR is just a witness parameter, it didn't require coding, and could be changed back to 0 (or any other value) easily.


It didn't happen. There was a relatively brief discussion on the semi-private mattermost witness channel, Smooth brought up the idea, changed his witness and many others adapted quickly.

This feature has existed in the consensus rules since basically the beginning of Steem. It was just largely forgotten about after witnesses set Steem Dollar interest to 0 years ago at a time when they were perpetually overvalued.

It has always been the domain of witnesses to decide this value. They could be updating it much more regularly if they wanted to.

Okay, makes sense in that Hivelandia is effectively a republic rather than a democracy. 😅

But since it doesn’t need a hard fork, what happens to HBD interest if some witnesses set it to 3% and some don’t?

The median value is used.

Good to know we don’t have to set aside an “untouched balance” for interests.

Delving in the code, thanks never realized how the interest was calculated, very interesting knowing more, always learning thanks @themarkymark

@tipu curate

This is awesome, a risk-free rate of 3% on a stable coin is solid stuff.

What I'd like to know is how the interest rate changes when the peg breaks down or up? Will, there be an incentive to up interest if it breaks down, and reduce when it breaks up?

HBD Stabilizer will put pressure on keeping HBD at $1. This is due to the fact it has different actions based on the price.

Interest will encourage more HBD holding, and the HBD Stabilizer will attempt to counteract any shift.

That being said, outside conditions can put a lot of pressure up or down. For example, SBD spiked to nearly $20 because the Korean exchange Upbit listed STEEM & SBD and many non-steem users bought SBD raising the price, many saw this and fomo'd and jumped on board, this started to snowball until it ultimately hit just short of $20. At the time there was no SBD Potato or HBD Stabilizer and the HBD supply was only around 1M (I believe at the time). HBD supply is a lot higher now making this more unlikely, but considerably lower than the peak on Steem.

Posted Using LeoFinance Beta

I feel that the main driver for traders to treat HBD (or SBD for that matter) like any other coin and not as a stable coin is that exchanges list it in that way.

When you go to to trade HBD on exchanges it is priced in BTC and not the other way around. Take DAI for example, if you see it on Binance it does not display the price of it in BTC but rather the opposite. So traders see that and say "Oh this is a stablecoin worth one USD" and act accordingly.

Thank you for the information, I think this touches all the key points!
Of course I find out about this two days after selling some HBD 🤣
Guess I will be accumulating some HBD instead of selling and powering up every week!

Posted Using LeoFinance Beta

I was just asking my pal @nickyhavey some of these questions since he's my usual resource to help me understand in "normie" speak what's going on. Thanks for the summary at the end to help answer them. 😂

I would have pointed you to that summary at the end haha! A lot of technical stuff here that goes beyond my knowledge of blockchain tech so glad Marky summarised at the end!

Posted Using LeoFinance Beta

First Time to know that
I have to save my hbd and buy more
Thanks

Posted Using LeoFinance Beta

Thanks, that helped allot

Thank you for this, @themarkymark. A question: every 30 days as opposed to 1x per month on, say 1st or 30th? I ask because pf the power up initiative that @traciyork runs and what impact this could have on folk using HBD to buy Hive.

TIA

Thank you for this, @themarkymark. A question: every 30 days as opposed to 1x per month on, say 1st or 30th? I ask because pf the power up initiative that @traciyork runs and what impact this could have on folk using HBD to buy Hive.

It is 30 days as the code works on simple time but may be longer depending on how often you transact with HBD.

Posted Using LeoFinance Beta

Very grateful for this post ... thanks for going over these questions. You're awesome for doing it

Not as awesome as this new layout.

I love it!

Posted Using LeoFinance Beta

Yeah I love it so much I changed my peakd setting to not use the pop out anymore because I like having that side layout haha

is that 3% per year? and as far as I understand it, it's a compound interest right ?

Posted Using LeoFinance Beta

3% per year, compounded monthly. Both of these numbers may change in the future.

Posted Using LeoFinance Beta

that's double the inflation rate so I guess it's pretty decent, but a bit higher interest like 4% or 5% might get some outside attention because it's an almost risk free investment. Don't know if that would be feasible though

Posted Using LeoFinance Beta

I believe that is the plan, but we'll have to see how well everything else holds up in the 3% environment.

sounds great to me

I love that it is almost similar to how ALGO rewards work with rewards being give on transactions, I know its not completely the same but still. Love it

Im going to read this like 3 more times and try to understand it to be able to make a post in spanish about it 🤣
there is a reason i went to law school, we dont have math there

Are the HBDs printed out of thin air, or is it hive inflation being converted to HBD similar to post payouts?

What about the DAO rewards?

afaik, DAO rewards is 10% of hive inflation being converted to HBD at issuance date.

And I would guess that interest rates are printed out of thin air, without considering hive, am I correct?

Interest is out of thin air. However, if the price of HIVE increases over time, which one would normally expect, then HBD has a negative net effect on inflation (it takes more HIVE to create $1 worth of HBD then is returned by converting it, later, at a higher HIVE price). The interest would partially offset that reduced inflation, but likely less than completely.

Nice breakdown. I like being able to see it in the code. Thanks!

Posted Using LeoFinance Beta

Cheers Marky now I can sleep at night knowing am growing like head on my front yard.

If you squint your eyes hard enough you can see it grow. And if you wink while squinting you hear them cheer away.

20210303_070934.jpg

Wow, your post is as stupid as you look like. What can I say? Well, have a stupid day and have a happy nightmare.

Tell me more, this is fascinating.

Cleared up everything , great detailed explanation . Just one more thing , what all is considered as a HBD transaction ?

What I know -

  1. Converting HBD to Hive
  2. Selling Hive for HBD

What I am not sure of -

  1. I claim my rewards in HBD , is it a HBD transaction?
  2. Sending HBD to others ?

Posted Using LeoFinance Beta

anything that makes your HBD or HBD savings go up or down. I'm not 100% sure sending to yourself works, I can only assume it does.

Posted Using LeoFinance Beta

I see.

I'm not 100% sure sending to yourself works, I can only assume it does.

Ah after the first month we can pull some data and see what's the case with this .

Posted Using LeoFinance Beta

Thank you for exploring this, I recently increased my HBD holdings because I suspected it was a good move. When HBD hit $4 and was trading higher I still did not sell. Now that there is an HBD fix in play, is there reason to believe HBD will ever be worth more than $1 ?

Now that there is an HBD fix in play, is there reason to believe HBD will ever be worth more than $1 ?

Only time can tell, HBD Stabilizer will try to keep it around $1 hopefully moving the price action to the Hive token instead.

Posted Using LeoFinance Beta

Will claiming liquid rewards count as a HBD transaction?

I'm about 99% sure it does, but I haven't tested.

Thanks this was informative! It is good that you don´t have to transfer to savings to get the interest :)

Posted via neoxian.city | The City of Neoxian

Finding out how does English work by reading books!

I guess this explains why the price of HBD suddenly went up after months in the dumps a few weeks ago.

Awesome! Thank you for going through all the details. I'm sure I'm not alone in saying that there is a lot of complexity to this site and this could be a very useful place to "park" assets you want to keep liquid in the future. Really great stuff!

Posted Using LeoFinance Beta

Thanks for the info but I still have a question, I used to keep my HBD for sell on the market, will I get the reward?

@themarkymark do you know if the 3% paid out monthly or is the interest averaged at 3% APR?

3% yearly, compound monthly

Posted Using LeoFinance Beta

That's one hell of a fixed income investment.

Hey, @themarkymark (or anybody else). Can you do the reward pool next? I'm specifically interested in answering the following questions:

Does the reward pool totally exhausted each cycle, or could there be a surplus?

Does upvote value take into account the current size of the reward pool?

That's all can think of right now.

Posted Using LeoFinance Beta

Thank you for this comprehensive review.

I'm pleased to see that the witnesses are working to improve the dynamics of HBD, rather than getting rid of the stablecoin altogether.

The main problem with a mathematically pegged stablecoin is that debt obligations increase the amplitude of the underlying asset, in this case HIVE. When things are going well, the HBD makes them even better, while in hard times, the HBD puts additional downward pressure on the price of HIVE.

I think that paying this price (increased volatility of HIVE) in exchange for a stablecoin for e-commerce is the right choice for the long term success of Hive.

Thanks for the info. I never realized HBD collected interest. Your posts are always well written and easy to understand.
By the way, thanks for all your support on my posts. You have been there for me for quite a while and I really appreciate both your support and sharing your incredible knowledge.
Peace, my friend, hive on.

The HBD interest is very new, it was always possible but it was only recently the witnesses voted in an interest rate for HBD.

Posted Using LeoFinance Beta

Ok, that works for me. My son keeps reminding me about holding and skimming the cream off the top. He has us in so many positions it makes my head spin. De-lease has paid off with some really unbelievable interest rates. So we are just sitting on it.
There is so much money to be made in crypto, it's staggering to me.
So glad I took his advise way back, invested in Bitcoin, then alt coins. At that time, I was completely ignorant, didn't know jack about crypto, just said ok I 'm game. So little investment then, big payoff now. yikes.

Where does the interest come from?

HIVE!D

Appreciate this review. I've corrected a small oversight thanks to your dive... HIVE Regards!