test
[VerusCoin.git] / src / komodo_interest.h
1 /******************************************************************************
2  * Copyright © 2014-2016 The SuperNET Developers.                             *
3  *                                                                            *
4  * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at                  *
5  * the top-level directory of this distribution for the individual copyright  *
6  * holder information and the developer policies on copyright and licensing.  *
7  *                                                                            *
8  * Unless otherwise agreed in a custom licensing agreement, no part of the    *
9  * SuperNET software, including this file may be copied, modified, propagated *
10  * or distributed except according to the terms contained in the LICENSE file *
11  *                                                                            *
12  * Removal or modification of this copyright notice is prohibited.            *
13  *                                                                            *
14  ******************************************************************************/
15
16 #define KOMODO_INTEREST ((uint64_t)(0.05 * COIN))   // 5%
17
18 uint64_t komodo_earned_interest(int32_t height,int64_t paidinterest)
19 {
20     static uint64_t *interests; static int32_t maxheight;
21     uint64_t total; int32_t ind,incr = 100000;
22     if ( height >= maxheight )
23     {
24         interests = (uint64_t *)realloc(interests,(maxheight + incr) * sizeof(*interests) * 2);
25         memset(&interests[maxheight << 1],0,incr * sizeof(*interests) * 2);
26         maxheight += incr;
27     }
28     ind = (height << 1);
29     if ( paidinterest < 0 ) // request
30         return(interests[ind]);
31     else
32     {
33         if ( interests[ind + 1] != paidinterest )
34         {
35             interests[ind + 1] = paidinterest;
36             if ( height == 0 )
37                 interests[ind] = interests[ind + 1];
38             else interests[ind] = interests[ind - 2] + interests[ind + 1];
39             total = interests[ind];
40             for (++height; height<maxheight; height++)
41             {
42                 ind = (height << 1);
43                 interests[ind] = total;
44                 interests[ind + 1] = 0;
45             }
46         }
47     }
48     return(0);
49 }
50
51 uint64_t komodo_moneysupply(int32_t height)
52 {
53     if ( height <= 1 || ASSETCHAINS_SYMBOL[0] == 0 )
54         return(0);
55     else return(COIN * 100000000 + (height-1) * 3 + komodo_earned_interest(height,-1));
56 }
57
58 uint64_t komodo_interest(int32_t txheight,uint64_t nValue,uint32_t nLockTime,uint32_t tiptime)
59 {
60     int32_t minutes; uint64_t numerator,denominator,interest = 0;
61     if ( ASSETCHAINS_SYMBOL[0] != 0 )
62         return(0);
63     if ( komodo_moneysupply(txheight) < MAX_MONEY && nLockTime >= LOCKTIME_THRESHOLD && tiptime != 0 && nLockTime < tiptime && nValue >= 10*COIN )
64     {
65         if ( (minutes= (tiptime - nLockTime) / 60) >= 60 )
66         {
67             numerator = (nValue * KOMODO_INTEREST);
68             denominator = (((uint64_t)365 * 24 * 60) / minutes);
69             if ( denominator == 0 )
70                 denominator = 1; // max KOMODO_INTEREST per transfer, do it at least annually!
71             interest = (numerator / denominator) / COIN;
72             //fprintf(stderr,"komodo_interest %lld %.8f nLockTime.%u tiptime.%u minutes.%d interest %lld %.8f (%llu / %llu)\n",(long long)nValue,(double)nValue/COIN,nLockTime,tiptime,minutes,(long long)interest,(double)interest/COIN,(long long)numerator,(long long)denominator);
73         }
74     }
75     return(interest);
76 }
77
This page took 0.027421 seconds and 4 git commands to generate.