Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

getaddressasset support utxo #391

Merged
merged 5 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/metaverse/explorer/extensions/base_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ struct utxo_balance {
uint64_t output_height;
uint64_t unspent_balance;
uint64_t frozen_balance;
std::string symbol="";
};

struct balances {
Expand Down Expand Up @@ -232,6 +233,9 @@ void sync_fetch_deposited_balance(wallet::payment_address& address,
void sync_fetch_asset_balance(const std::string& address, bool sum_all,
bc::blockchain::block_chain_impl& blockchain,
std::shared_ptr<chain::asset_balances::list> sh_asset_vec);
void sync_fetch_asset_balance(const std::string& address, bool sum_all,
bc::blockchain::block_chain_impl& blockchain,
std::shared_ptr<utxo_balance::list> sh_asset_utxo_vec);

void sync_fetch_asset_deposited_balance(const std::string& address,
bc::blockchain::block_chain_impl& blockchain,
Expand Down
12 changes: 12 additions & 0 deletions include/metaverse/explorer/extensions/commands/getaddressasset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ class getaddressasset: public command_extension
value<bool>(&option_.deposited)->zero_tokens()->default_value(false),
"If specified, then only get deposited assets. Default is not specified."
)
(
"utxo,u",
value<bool>(&option_.utxo)->zero_tokens()->default_value(false),
"If specified, list all utxos. Default is not specified."
)
(
"range,r",
value<colon_delimited2_item<uint64_t, uint64_t>>(&option_.range),
"Pick utxo whose value is between this range [begin:end)."
)
(
"symbol,s",
value<std::string>(&option_.symbol)->default_value(""),
Expand Down Expand Up @@ -109,6 +119,8 @@ class getaddressasset: public command_extension

bool is_cert;
bool deposited;
bool utxo;
colon_delimited2_item<uint64_t, uint64_t> range = {0, 0};
std::string symbol;
} option_;

Expand Down
75 changes: 75 additions & 0 deletions src/lib/explorer/extensions/base_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,81 @@ void sync_fetch_asset_balance(const std::string& address, bool sum_all,
}
}

void sync_fetch_asset_balance(const std::string& address, bool sum_all,
bc::blockchain::block_chain_impl& blockchain,
std::shared_ptr<utxo_balance::list> sh_asset_utxo_vec)
{
auto&& rows = blockchain.get_address_history(wallet::payment_address(address));

chain::transaction tx_temp;
uint64_t tx_height;

uint64_t height = 0;
blockchain.get_last_height(height);

for (auto& row: rows)
{
uint64_t unspent_balance = 0;
uint64_t frozen_balance = 0;

// spend unconfirmed (or no spend attempted)
if ((row.spend.hash == null_hash)
&& blockchain.get_transaction(tx_temp, tx_height, row.output.hash))
{
BITCOIN_ASSERT(row.output.index < tx_temp.outputs.size());
const auto& output = tx_temp.outputs.at(row.output.index);
if (output.get_script_address() != address) {
continue;
}
if (output.is_asset())
{
const auto& symbol = output.get_asset_symbol();
if (bc::wallet::symbol::is_forbidden(symbol)) {
// swallow forbidden symbol
continue;
}

auto asset_amount = output.get_asset_amount();

if (asset_amount == 0) {
continue;
}

uint64_t locked_amount = 0;
if (asset_amount
&& operation::is_pay_key_hash_with_attenuation_model_pattern(output.script.operations)) {
const auto& attenuation_model_param = output.get_attenuation_model_param();
auto diff_height = row.output_height
? blockchain.calc_number_of_blocks(row.output_height, height)
: 0;
auto available_amount = attenuation_model::get_available_asset_amount(
asset_amount, diff_height, attenuation_model_param);
locked_amount = asset_amount - available_amount;
}
else if (asset_amount
&& chain::operation::is_pay_key_hash_with_sequence_lock_pattern(output.script.operations)) {
auto is_spendable = blockchain.is_utxo_spendable(tx_temp, row.output.index, tx_height, height);
if (!is_spendable) {
// utxo already in block but is locked with sequence and not mature
locked_amount = asset_amount;
}
}

sh_asset_utxo_vec->emplace_back(utxo_balance{
encode_hash(row.output.hash), row.output.index,
row.output_height, asset_amount, locked_amount, symbol});
}
}
}

if (sh_asset_utxo_vec->size() > 1) {
auto sort_by_amount_descend = [](const utxo_balance& b1, const utxo_balance& b2){
return b1.unspent_balance > b2.unspent_balance;
};
std::sort(sh_asset_utxo_vec->begin(), sh_asset_utxo_vec->end(), sort_by_amount_descend);
}
}

void sync_fetch_locked_balance(const std::string& address,
bc::blockchain::block_chain_impl& blockchain,
std::shared_ptr<locked_balance::list> sh_vec,
Expand Down
39 changes: 38 additions & 1 deletion src/lib/explorer/extensions/commands/getaddressasset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ using namespace bc::explorer::config;

/************************ getaddressasset *************************/

static Json::Value to_json_value(const utxo_balance& balance)
{
Json::Value json_balance;
json_balance["available"] = (balance.unspent_balance - balance.frozen_balance);
json_balance["balance"] = balance.unspent_balance;
json_balance["frozen"] = balance.frozen_balance;
json_balance["utxo_block"] = balance.output_height;
json_balance["utxo_hash"] = balance.output_hash;
json_balance["utxo_index"] = balance.output_index;
json_balance["symbol"] = balance.symbol;
return json_balance;
}

console_result getaddressasset::invoke(Json::Value& jv_output,
libbitcoin::server::server_node& node)
{
Expand Down Expand Up @@ -84,7 +97,7 @@ console_result getaddressasset::invoke(Json::Value& jv_output,
json_value.append(asset_data);
}
}
else {
else if (!option_.utxo) {
json_key = "assets";

auto sh_vec = std::make_shared<chain::asset_balances::list>();
Expand All @@ -103,6 +116,30 @@ console_result getaddressasset::invoke(Json::Value& jv_output,
json_value.append(asset_data);
}
}
else {
json_key = "assets";

// range check
if (!option_.range.is_valid()) {
throw argument_legality_exception("invalid range option! "
+ option_.range.encode_colon_delimited());
}
auto utxo_balances = std::make_shared<utxo_balance::list>();
sync_fetch_asset_balance(address, true, blockchain, utxo_balances);
for (const auto& balance : *utxo_balances) {
if (option_.range.is_in_range(balance.unspent_balance)) {
if (!option_.symbol.empty() && option_.symbol != balance.symbol)
continue;

auto issued_asset = blockchain.get_issued_asset(balance.symbol);
if (!issued_asset) {
continue;
}

json_value.append(to_json_value(balance));
}
}
}

if (get_api_version() == 1 && json_value.isNull()) { //compatible for v1
jv_output[json_key] = "";
Expand Down
2 changes: 1 addition & 1 deletion src/lib/explorer/extensions/commands/getaddressetp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Json::Value to_json_value(const balances& addr_balance, uint8_t api_version)
return json_balance;
}

Json::Value to_json_value(const utxo_balance& balance)
static Json::Value to_json_value(const utxo_balance& balance)
{
Json::Value json_balance;
json_balance["available"] = (balance.unspent_balance - balance.frozen_balance);
Expand Down