Skip to content

Commit

Permalink
Merge pull request #207 from VanGrx/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
VanGrx authored Feb 18, 2020
2 parents 967686f + 7fb2c4e commit cf9c105
Show file tree
Hide file tree
Showing 21 changed files with 583 additions and 116 deletions.
6 changes: 6 additions & 0 deletions src/blockchain_db/lmdb/db_lmdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5716,6 +5716,9 @@ bool BlockchainLMDB::is_valid_transaction_output_type(const txout_target_v &txou
offer.offer_id = offer_result.offer_id;
offer.active = offer_result.active;
offer.title = std::string{offer_result.title.begin(),offer_result.title.end()};
offer.price_peg_id = offer_result.price_peg_id;
offer.price_peg_used = offer_result.price_peg_used;
offer.min_sfx_price = offer_result.min_sfx_price;
}
else {
safex::create_offer_data offer_result;
Expand All @@ -5727,6 +5730,9 @@ bool BlockchainLMDB::is_valid_transaction_output_type(const txout_target_v &txou
offer.offer_id = offer_result.offer_id;
offer.active = offer_result.active;
offer.title = std::string{offer_result.title.begin(),offer_result.title.end()};
offer.price_peg_id = offer_result.price_peg_id;
offer.price_peg_used = offer_result.price_peg_used;
offer.min_sfx_price = offer_result.min_sfx_price;
}

}
Expand Down
7 changes: 0 additions & 7 deletions src/cryptonote_core/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3367,13 +3367,6 @@ bool Blockchain::check_safex_tx(const transaction &tx, tx_verification_context &
tvc.m_safex_invalid_input = true;
return false;
}
//check purchase total payment
if (total_payment > product_payment + network_fee)
{
MERROR("Not enough cash given for network fee and product payment ");
tvc.m_safex_invalid_input = true;
return false;
}
}
else if (command_type == safex::command_t::create_feedback)
{
Expand Down
4 changes: 2 additions & 2 deletions src/cryptonote_core/cryptonote_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ namespace cryptonote
//todo Atana optimize somehow key image validation, so many conversions
const crypto::key_image &k_image = *boost::apply_visitor(key_image_visitor(), in);
std::unique_ptr<safex::create_offer> cmd = safex::safex_command_serializer::parse_safex_command<safex::create_offer>(txin.script);
safex::create_offer_data offer(cmd->get_offerid(),cmd->get_seller(),cmd->get_title(),cmd->get_quantity(),cmd->get_price(),cmd->get_description(),cmd->get_active(),cmd->get_seller_address(),cmd->get_seller_private_view_key());
safex::create_offer_data offer(cmd->get_offerid(),cmd->get_seller(),cmd->get_title(),cmd->get_quantity(),cmd->get_price(),cmd->get_description(),cmd->get_active(),cmd->get_seller_address(),cmd->get_seller_private_view_key(),cmd->get_price_peg_id(),cmd->get_min_sfx_price(),cmd->get_price_peg_used());
crypto::hash cmd_hash{};
get_object_hash(offer, cmd_hash);
if (memcmp(cmd_hash.data, k_image.data, sizeof(k_image.data)) != 0)
Expand All @@ -1193,7 +1193,7 @@ namespace cryptonote
//todo Atana optimize somehow key image validation, so many conversions
const crypto::key_image &k_image = *boost::apply_visitor(key_image_visitor(), in);
std::unique_ptr<safex::edit_offer> cmd = safex::safex_command_serializer::parse_safex_command<safex::edit_offer>(txin.script);
safex::edit_offer_data offer(cmd->get_offerid(),cmd->get_seller(),cmd->get_title(),cmd->get_quantity(),cmd->get_price(),cmd->get_description(),cmd->get_active());
safex::edit_offer_data offer(cmd->get_offerid(),cmd->get_seller(),cmd->get_title(),cmd->get_quantity(),cmd->get_price(),cmd->get_description(),cmd->get_active(),cmd->get_price_peg_id(),cmd->get_min_sfx_price(),cmd->get_price_peg_used());
crypto::hash cmd_hash{};
get_object_hash(offer, cmd_hash);
if (memcmp(cmd_hash.data, k_image.data, sizeof(k_image.data)) != 0)
Expand Down
24 changes: 22 additions & 2 deletions src/rpc/core_rpc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,9 @@ namespace cryptonote
for(auto offer: offers) {
if(offer.active && offer.quantity > 0) {
std::string offer_id_str = epee::string_tools::pod_to_hex(offer.offer_id);
COMMAND_RPC_GET_SAFEX_OFFERS::entry ent{offer.title, offer.quantity, offer.price, offer.description,
offer.active, offer.shipping, offer_id_str, offer.seller,
std::string price_peg_id_str = epee::string_tools::pod_to_hex(offer.price_peg_id);
COMMAND_RPC_GET_SAFEX_OFFERS::entry ent{offer.title, offer.quantity, offer.price, offer.min_sfx_price, offer.description,
offer.active, offer.price_peg_used, offer.shipping, offer_id_str, price_peg_id_str, offer.seller,
offer.seller_address};
res.offers.push_back(ent);
}
Expand All @@ -195,6 +196,25 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
bool core_rpc_server::on_get_safex_price_pegs(const COMMAND_RPC_GET_SAFEX_PRICE_PEGS::request& req, COMMAND_RPC_GET_SAFEX_PRICE_PEGS::response& res)
{
PERF_TIMER(on_get_safex_price_pegs);
bool r;
if (use_bootstrap_daemon_if_necessary<COMMAND_RPC_GET_SAFEX_PRICE_PEGS>(invoke_http_mode::JON, "/get_safex_price_pegs", req, res, r))
return r;

std::vector<safex::safex_price_peg> price_pegs;
bool result = m_core.get_safex_price_pegs(price_pegs,req.currency);

for(auto price_peg: price_pegs) {
std::string price_peg_id_str = epee::string_tools::pod_to_hex(price_peg.price_peg_id);
COMMAND_RPC_GET_SAFEX_PRICE_PEGS::entry ent{price_peg.title,price_peg_id_str,price_peg.creator,price_peg.description,price_peg.currency,price_peg.rate};
res.price_pegs.push_back(ent);
}
res.status = CORE_RPC_STATUS_OK;
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
bool core_rpc_server::on_get_safex_ratings(const COMMAND_RPC_GET_SAFEX_RATINGS::request& req, COMMAND_RPC_GET_SAFEX_RATINGS::response& res)
{
PERF_TIMER(on_get_safex_ratings);
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/core_rpc_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ namespace cryptonote
MAP_URI_AUTO_JON2("/get_safex_accounts", on_get_safex_accounts, COMMAND_RPC_GET_SAFEX_ACCOUNTS)
MAP_URI_AUTO_JON2("/get_safex_offers", on_get_safex_offers, COMMAND_RPC_GET_SAFEX_OFFERS)
MAP_URI_AUTO_JON2("/get_safex_ratings", on_get_safex_ratings, COMMAND_RPC_GET_SAFEX_RATINGS)
MAP_URI_AUTO_JON2("/get_safex_price_pegs", on_get_safex_price_pegs, COMMAND_RPC_GET_SAFEX_PRICE_PEGS)
MAP_URI_AUTO_JON2("/get_limit", on_get_limit, COMMAND_RPC_GET_LIMIT)
MAP_URI_AUTO_JON2_IF("/set_limit", on_set_limit, COMMAND_RPC_SET_LIMIT, !m_restricted)
MAP_URI_AUTO_JON2_IF("/out_peers", on_out_peers, COMMAND_RPC_OUT_PEERS, !m_restricted)
Expand Down Expand Up @@ -178,6 +179,7 @@ namespace cryptonote
bool on_get_safex_accounts(const COMMAND_RPC_GET_SAFEX_ACCOUNTS::request& req, COMMAND_RPC_GET_SAFEX_ACCOUNTS::response& res);
bool on_get_safex_offers(const COMMAND_RPC_GET_SAFEX_OFFERS::request& req, COMMAND_RPC_GET_SAFEX_OFFERS::response& res);
bool on_get_safex_ratings(const COMMAND_RPC_GET_SAFEX_RATINGS::request& req, COMMAND_RPC_GET_SAFEX_RATINGS::response& res);
bool on_get_safex_price_pegs(const COMMAND_RPC_GET_SAFEX_PRICE_PEGS::request& req, COMMAND_RPC_GET_SAFEX_PRICE_PEGS::response& res);

bool on_get_output_histogram_protobuf(const COMMAND_RPC_GET_OUTPUT_HISTOGRAM_PROTOBUF::request& req, COMMAND_RPC_GET_OUTPUT_HISTOGRAM_PROTOBUF::response& res);
bool on_send_proto_raw_tx(const COMMAND_RPC_PROTO_SEND_RAW_TX::request& req, COMMAND_RPC_PROTO_SEND_RAW_TX::response& res);
Expand Down
52 changes: 52 additions & 0 deletions src/rpc/core_rpc_server_commands_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,27 @@ namespace cryptonote
std::string title;
uint64_t quantity;
uint64_t price;
uint64_t min_sfx_price;
std::vector<uint8_t> description;
bool active;
bool price_peg_used;
std::vector<uint8_t> shipping;
std::string offer_id;
std::string price_peg_id;
std::string seller;
cryptonote::account_public_address seller_address;

BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(title)
KV_SERIALIZE(quantity)
KV_SERIALIZE(price)
KV_SERIALIZE(min_sfx_price)
KV_SERIALIZE(description)
KV_SERIALIZE(active)
KV_SERIALIZE(price_peg_used)
KV_SERIALIZE(shipping)
KV_SERIALIZE(offer_id)
KV_SERIALIZE(price_peg_id)
KV_SERIALIZE(seller)
KV_SERIALIZE(seller_address)
END_KV_SERIALIZE_MAP()
Expand All @@ -164,6 +170,52 @@ namespace cryptonote
typedef epee::misc_utils::struct_init<response_t> response;
};

struct COMMAND_RPC_GET_SAFEX_PRICE_PEGS
{
struct request_t
{
std::string currency;

BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(currency)
END_KV_SERIALIZE_MAP()
};
typedef epee::misc_utils::struct_init<request_t> request;

struct entry
{
std::string title;
std::string price_peg_id;
std::string creator;
std::vector<uint8_t> description;
std::string currency;
uint64_t rate;

BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(title)
KV_SERIALIZE(price_peg_id)
KV_SERIALIZE(creator)
KV_SERIALIZE(description)
KV_SERIALIZE(currency)
KV_SERIALIZE(rate)
END_KV_SERIALIZE_MAP()
};

struct response_t
{
std::vector<entry> price_pegs;
std::string status;
bool untrusted;

BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(price_pegs)
KV_SERIALIZE(status)
KV_SERIALIZE(untrusted);
END_KV_SERIALIZE_MAP()
};
typedef epee::misc_utils::struct_init<response_t> response;
};

struct COMMAND_RPC_GET_SAFEX_RATINGS
{
struct request_t
Expand Down
30 changes: 30 additions & 0 deletions src/safex/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,28 @@ namespace safex
if(sfx_offer.quantity < cmd->quantity)
return execution_status::error_purchase_out_of_stock;

uint64_t sfx_price = sfx_offer.min_sfx_price;

if(sfx_offer.price_peg_used){
safex::safex_price_peg sfx_price_peg;
if (!blokchainDB.get_safex_price_peg(sfx_offer.price_peg_id,sfx_price_peg)) {
return execution_status::error_offer_price_peg_not_existant;
}
std::string rate_str = cryptonote::print_money(sfx_price_peg.rate);
double rate = stod(rate_str);

std::string price_str = cryptonote::print_money(sfx_offer.price);
double price_dbl = stod(price_str);

uint64_t pegged_price = (price_dbl*rate)*SAFEX_CASH_COIN;

if(sfx_price < pegged_price)
sfx_price = pegged_price;
}

if(sfx_price * cmd->quantity > cmd->price)
return execution_status::error_purchase_not_enough_funds;


SAFEX_COMMAND_CHECK_AND_ASSERT_THROW_MES((txin.amount > 0), "Purchase amount must be greater than zero ", this->get_command_type());
SAFEX_COMMAND_CHECK_AND_ASSERT_THROW_MES((txin.token_amount == 0), "Could not purchase with tokens ", this->get_command_type());
Expand Down Expand Up @@ -311,6 +333,11 @@ namespace safex
result = execution_status::error_offer_price_too_big;
}

safex::safex_price_peg sfx_price_peg{};
if(cmd->get_price_peg_used() && !blokchainDB.get_safex_price_peg(cmd->get_price_peg_id(),sfx_price_peg)){
result = execution_status::error_offer_price_peg_not_existant;
}

return result;
};

Expand Down Expand Up @@ -389,6 +416,9 @@ namespace safex
execution_status result = execution_status::ok;
std::unique_ptr<safex::create_price_peg> cmd = safex::safex_command_serializer::parse_safex_command<safex::create_price_peg>(txin.script);

if(cmd->get_currency().size()!=3)
result = execution_status::error_price_peg_bad_currency_format;

return result;
};

Expand Down
Loading

0 comments on commit cf9c105

Please sign in to comment.