BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface software
ticket_object.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Abit More, and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
26 
27 #include <fc/io/raw.hpp>
28 
29 using namespace graphene::chain;
30 
31 void ticket_object::init_new( time_point_sec now, account_id_type new_account,
32  ticket_type new_target_type, const asset& new_amount, ticket_version version )
33 {
34  account = new_account;
35  target_type = new_target_type;
36  amount = new_amount;
37 
39  status = charging;
42 
43  update_value( version );
44 }
45 
46 void ticket_object::init_split( time_point_sec now, const ticket_object& old_ticket,
47  ticket_type new_target_type, const asset& new_amount, ticket_version version )
48 {
49  account = old_ticket.account;
50  target_type = old_ticket.target_type;
51  amount = new_amount;
52 
53  current_type = old_ticket.current_type;
54  status = old_ticket.status;
57 
58  update_target_type( now, new_target_type, version );
59 }
60 
61 void ticket_object::update_target_type( time_point_sec now, ticket_type new_target_type, ticket_version version )
62 {
63  if( current_type < new_target_type )
64  {
65  if( status != charging )
66  {
67  status = charging;
69  }
70  // else do nothing here
71  }
72  else // current_type >= new_target_type
73  {
75  if( next_type_downgrade_time == time_point_sec::maximum() ) // no downgrade was started ago
76  {
77  if( current_type == new_target_type ) // was charging, to cancel
79  else // was stable or charging, to downgrade
80  {
81  current_type = static_cast<ticket_type>( static_cast<uint8_t>(current_type) - 1 );
83  }
84  }
85  // else a downgrade was started ago, keep the old value of `next_type_downgrade_time`
86  // Note: it's possible that `next_type_downgrade_time` is in the past,
87  // in this case, it will be processed in database::process_tickets().
89  }
90  target_type = new_target_type;
91 
92  update_value( version );
93 }
94 
95 void ticket_object::adjust_amount( const asset& delta_amount, ticket_version version )
96 {
97  amount += delta_amount;
98  update_value( version );
99 }
100 
102 {
103  if( status == charging )
104  {
105  current_type = static_cast<ticket_type>( static_cast<uint8_t>(current_type) + 1 );
107  if( current_type < target_type )
108  {
110  }
111  else // reached target, stop
112  {
113  if( current_type != lock_forever )
114  {
115  status = stable;
117  }
118  else // lock forever
119  {
123  }
124  }
125  }
126  else // status == withdrawing
127  {
128  // Note: current_type != liquid, guaranteed by the caller
129  if( current_type == lock_forever )
130  {
131  share_type delta_value = amount.amount * value_multiplier( current_type, version )
133  if( delta_value <= 0 )
134  delta_value = 1;
135  if( value <= delta_value )
136  {
137  value = 0;
138  status = stable;
140  }
141  else
142  {
143  value -= delta_value;
145  }
146  }
147  else if( current_type == target_type ) // finished downgrading
148  {
149  status = stable;
152  }
153  else // to downgrade
154  {
155  current_type = static_cast<ticket_type>( static_cast<uint8_t>(current_type) - 1 );
158  }
159  }
160 
161  update_value( version );
162 }
163 
164 void ticket_object::update_value( ticket_version version )
165 {
166  if( current_type != lock_forever )
167  {
169  }
170  // else lock forever and to be updated, do nothing here
171 }
172 
174  (account)
175  (target_type)
176  (amount)
177  (current_type)
178  (status)
179  (value)
182  )
183 
static uint8_t value_multiplier(ticket_type i, ticket_version version)
void adjust_amount(const asset &delta_amount, ticket_version version)
Adjust amount and update member variables accordingly.
ticket_type
Type of a ticket.
Definition: ticket.hpp:33
ticket_type target_type
The target type of the ticket.
static constexpr uint32_t seconds_per_lock_forever_update_step
static uint32_t seconds_to_downgrade(ticket_type i)
#define FC_REFLECT_DERIVED_NO_TYPENAME(TYPE, INHERITS, MEMBERS)
Definition: reflect.hpp:356
void init_new(time_point_sec now, account_id_type new_account, ticket_type new_target_type, const asset &new_amount, ticket_version version)
Initialize member variables for a ticket newly created from account balance.
void auto_update(ticket_version version)
Update the ticket when it&#39;s time.
void init_split(time_point_sec now, const ticket_object &old_ticket, ticket_type new_target_type, const asset &new_amount, ticket_version version)
Initialize member variables for a ticket split from another ticket.
time_point_sec next_type_downgrade_time
When the account has ever started a downgrade or withdrawal, the scheduled auto-update time is stored...
static time_point_sec maximum()
Definition: time.hpp:86
time_point_sec next_auto_update_time
The next time that the ticket will be automatically updated.
asset amount
The token type and amount in the ticket.
static constexpr uint32_t lock_forever_update_steps
ticket_type current_type
The current type of the ticket.
#define GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION(type)
Definition: types.hpp:74
ticket_status status
The status of the ticket.
static constexpr uint32_t seconds_per_charging_step
static constexpr uint32_t seconds_to_cancel_charging
void update_target_type(time_point_sec now, ticket_type new_target_type, ticket_version version)
Set a new target type and update member variables accordingly.
base for all database objects
Definition: object.hpp:62
ticket_version
Version of a ticket.
account_id_type account
The account who owns the ticket.
a ticket for governance voting
share_type value
The current value of the ticket.