BitShares-Core  4.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 )
33 {
34  account = new_account;
35  target_type = new_target_type;
36  amount = new_amount;
37 
39  status = charging;
42 
43  update_value();
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 )
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 );
59 }
60 
61 void ticket_object::update_target_type( time_point_sec now, ticket_type new_target_type )
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();
93 }
94 
95 void ticket_object::adjust_amount( const asset& delta_amount )
96 {
97  amount += delta_amount;
98  update_value();
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  {
132  if( delta_value <= 0 )
133  delta_value = 1;
134  if( value <= delta_value )
135  {
136  value = 0;
137  status = stable;
139  }
140  else
141  {
142  value -= delta_value;
144  }
145  }
146  else if( current_type == target_type ) // finished downgrading
147  {
148  status = stable;
151  }
152  else // to downgrade
153  {
154  current_type = static_cast<ticket_type>( static_cast<uint8_t>(current_type) - 1 );
157  }
158  }
159 
160  update_value();
161 }
162 
163 void ticket_object::update_value()
164 {
165  if( current_type != lock_forever )
166  {
168  }
169  // else lock forever and to be updated, do nothing here
170 }
171 
173  (account)
174  (target_type)
175  (amount)
176  (current_type)
177  (status)
178  (value)
181  )
182 
ticket_type
Type of a ticket.
Definition: ticket.hpp:33
void init_split(time_point_sec now, const ticket_object &old_ticket, ticket_type new_target_type, const asset &new_amount)
Initialize member variables for a ticket split from another ticket.
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)
Initialize member variables for a ticket newly created from account balance.
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.
void auto_update()
Update the ticket when it&#39;s time.
#define GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION(type)
Definition: types.hpp:74
ticket_status status
The status of the ticket.
void update_target_type(time_point_sec now, ticket_type new_target_type)
Set a new target type and update member variables accordingly.
static constexpr uint32_t seconds_per_charging_step
static constexpr uint32_t seconds_to_cancel_charging
void adjust_amount(const asset &delta_amount)
Adjust amount and update member variables accordingly.
base for all database objects
Definition: object.hpp:62
account_id_type account
The account who owns the ticket.
static uint8_t value_multiplier(ticket_type i)
a ticket for governance voting
share_type value
The current value of the ticket.