Contractless/src/orphans/undo_transactions/undo_create_token.rs

69 lines
2.4 KiB
Rust

use crate::blocks::token::CreateTokenTransaction;
use crate::common::network_paths_and_settings::block_extension_and_paths;
use crate::decode;
use crate::records::balance_sheet::operations::balance_sheet_operation_with_db;
use crate::records::record_chain::token_provenance::clear_token_history;
use crate::sled::Db;
pub async fn undo_create_token_transaction(
transaction: CreateTokenTransaction,
mining_receiver: &str,
db: &Db,
) {
// remove the created token state and restore the creator balances
// when a create-token transaction is rolled back
let operand_subtraction = "subtraction";
let operand_addition = "addition";
let (
_network_name,
_padded_base_coin,
type_str,
_torrentpath,
_wallet_path,
_blockpath,
_db_path,
_balance_path,
_log_path,
) = block_extension_and_paths();
let (txfee, creator, ticker, number) = (
&transaction.unsigned_create_token.txfee,
&transaction.unsigned_create_token.creator,
&transaction.unsigned_create_token.ticker,
&transaction.unsigned_create_token.number,
);
// Remove the miner fee, refund the creator fee, and remove the created
// supply from the creator balance.
let _ = balance_sheet_operation_with_db(
db,
mining_receiver,
*txfee,
&type_str,
operand_subtraction,
);
let _ = balance_sheet_operation_with_db(db, creator, *txfee, &type_str, operand_addition);
let _ = balance_sheet_operation_with_db(db, creator, *number, ticker, operand_subtraction);
let ticker_binary = &transaction.unsigned_create_token.ticker.as_bytes();
let hash_binary = decode(&transaction.unsigned_create_token.hash().await).unwrap();
// Remove the create-token transaction lookup from the txid tree.
let tree = db.open_tree("txid").unwrap();
let key = hash_binary.clone();
tree.remove(key).unwrap();
// remove the token definition and origin entry
// created when the token was first saved
let tree = db.open_tree("tokens").unwrap();
let key = ticker_binary;
tree.remove(key).unwrap();
let origin_tree = db.open_tree("token_origins").unwrap();
origin_tree.remove(key).unwrap();
let limit_tree = db.open_tree("token_limits").unwrap();
limit_tree.remove(key).unwrap();
// Token history is cleared because the token itself no longer exists.
let _ = clear_token_history(db, ticker);
}