Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ot_otapi_ot.cpp File Reference
#include "ot_otapi_ot.hpp"
#include "ot_utility_ot.hpp"
#include "../core/script/OTVariable.hpp"
#include "../core/OTLog.hpp"
Include dependency graph for ot_otapi_ot.cpp:

Go to the source code of this file.

Functions

string GetVariable (const char *name)
 
OT_OTAPI_OT OTDB::OfferListNymloadNymOffers (const string &serverID, const string &nymID)
 
OT_OTAPI_OT MapOfMapsconvert_offerlist_to_maps (OTDB::OfferListNym &offerList)
 
OT_OTAPI_OT int32_t output_nymoffer_data (const OTDB::OfferDataNym &offer_data, int32_t nIndex, const MapOfMaps &, const SubMap &, the_lambda_struct &)
 
OT_OTAPI_OT int32_t find_strange_offers (const OTDB::OfferDataNym &offer_data, const int32_t, const MapOfMaps &, const SubMap &, the_lambda_struct &extra_vals)
 
OT_OTAPI_OT int32_t iterate_nymoffers_sub_map (const MapOfMaps &map_of_maps, SubMap &sub_map, LambdaFunc the_lambda)
 
OT_OTAPI_OT int32_t iterate_nymoffers_sub_map (const MapOfMaps &map_of_maps, SubMap &sub_map, LambdaFunc the_lambda, the_lambda_struct &extra_vals)
 
OT_OTAPI_OT int32_t iterate_nymoffers_maps (MapOfMaps &map_of_maps, LambdaFunc the_lambda)
 
OT_OTAPI_OT int32_t iterate_nymoffers_maps (MapOfMaps &map_of_maps, LambdaFunc the_lambda, the_lambda_struct &extra_vals)
 

Variables

string Args
 
string HisAcct
 
string HisNym
 
string HisPurse
 
string MyAcct
 
string MyNym
 
string MyPurse
 
string Server
 

Function Documentation

OT_OTAPI_OT MapOfMaps* convert_offerlist_to_maps ( OTDB::OfferListNym offerList)

Definition at line 1385 of file ot_otapi_ot.cpp.

1386 {
1387  string strLocation = "convert_offerlist_to_maps";
1388 
1389  MapOfMaps* map_of_maps = nullptr;
1390 
1391  // LOOP THROUGH THE OFFERS and sort them into a map_of_maps, key is:
1392  // scale-assetID-currencyID
1393  // the value for each key is a sub-map, with the key: transaction ID and
1394  // value: the offer data itself.
1395  //
1396  int32_t nCount = offerList.GetOfferDataNymCount();
1397  int32_t nTemp = nCount;
1398 
1399  if (nCount > 0) {
1400  for (int32_t nIndex = 0; nIndex < nCount; ++nIndex) {
1401  nTemp = nIndex;
1402  OTDB::OfferDataNym* offerDataPtr = offerList.GetOfferDataNym(nTemp);
1403 
1404  if (!offerDataPtr) {
1405  otOut << strLocation << ": Unable to reference (nym) offerData "
1406  "on offerList, at index: " << nIndex
1407  << "\n";
1408  return map_of_maps;
1409  }
1410 
1411  OTDB::OfferDataNym& offerData = *offerDataPtr;
1412  string strScale = offerData.scale;
1413  string strAssetTypeID = offerData.asset_type_id;
1414  string strCurrencyTypeID = offerData.currency_type_id;
1415  string strSellStatus = offerData.selling ? "SELL" : "BUY";
1416  string strTransactionID = offerData.transaction_id;
1417 
1418  string strMapKey =
1419  strScale + "-" + strAssetTypeID + "-" + strCurrencyTypeID;
1420 
1421  SubMap* sub_map = nullptr;
1422  if (nullptr != map_of_maps && !map_of_maps->empty() &&
1423  (map_of_maps->count(strMapKey) > 0)) {
1424  sub_map = (*map_of_maps)[strMapKey];
1425  }
1426 
1427  if (nullptr != sub_map) {
1428  otWarn << strLocation << ": The sub-map already exists!\n";
1429 
1430  // Let's just add this offer to the existing submap
1431  // (There must be other offers already there for the same
1432  // market,
1433  // since the submap already exists.)
1434  //
1435  // the sub_map for this market is mapped by BUY/SELL ==> the
1436  // actual
1437  // offerData.
1438  //
1439 
1440  (*sub_map)[strTransactionID] = &offerData;
1441  }
1442  else // submap does NOT already exist for this market. (Create
1443  // it...)
1444  {
1445  otWarn << strLocation
1446  << ": The sub-map does NOT already exist!\n";
1447  //
1448  // Let's create the submap with this new offer, and add it
1449  // to the main map.
1450  //
1451  sub_map = new SubMap();
1452  (*sub_map)[strTransactionID] = &offerData;
1453 
1454  if (nullptr != map_of_maps) {
1455  map_of_maps = new MapOfMaps;
1456  }
1457  (*map_of_maps)[strMapKey] = sub_map;
1458  }
1459 
1460  // Supposedly by this point I have constructed a map keyed by the
1461  // market, which returns a sub_map for each market. Each sub map
1462  // uses the key "BUY" or "SELL" and that points to the actual
1463  // offer data. (Like a Multimap.)
1464  //
1465  // Therefore we have sorted out all the buys and sells for each
1466  // market. Later on, we can loop through the main map, and for each
1467  // market, we can loop through all the buys and sells.
1468  } // for (constructing the map_of_maps and all the sub_maps, so that the
1469  // offers are sorted
1470  // by market and buy/sell status.
1471  }
1472 
1473  return map_of_maps;
1474 }
std::map< std::string, opentxs::OTDB::OfferDataNym * > SubMap
OTLOG_IMPORT OTLogStream otOut
OTLOG_IMPORT OTLogStream otWarn
std::map< std::string, SubMap * > MapOfMaps
OT_OTAPI_OT int32_t find_strange_offers ( const OTDB::OfferDataNym offer_data,
const int32_t  ,
const MapOfMaps ,
const SubMap ,
the_lambda_struct extra_vals 
)

Definition at line 1537 of file ot_otapi_ot.cpp.

1543 {
1544  string strLocation = "find_strange_offers";
1545  /*
1546  me: How about this — when you do "opentxs newoffer" I can alter that
1547  script to automatically cancel any sell offers for a lower amount
1548  than my new buy offer, if they're on the same market at the same scale.
1549  and vice versa. Vice versa meaning, cancel any bid offers for a higher
1550  amount than my new sell offer.
1551 
1552  knotwork: yeah that would work.
1553 
1554  So when placing a buy offer, check all the other offers I already have at
1555  the same scale,
1556  same asset and currency ID. (That is, the same "market" as denoted by
1557  strMapKey in "opentxs showmyoffers")
1558  For each, see if it's a sell offer and if so, if the amount is lower than
1559  the amount on
1560  the new buy offer, then cancel that sell offer from the market. (Because I
1561  don't want to buy-high, sell low.)
1562 
1563  Similarly, if placing a sell offer, then check all the other offers I
1564  already have at the
1565  same scale, same asset and currency ID, (the same "market" as denoted by
1566  strMapKey....) For
1567  each, see if it's a buy offer and if so, if the amount is higher than the
1568  amount of my new
1569  sell offer, then cancel that buy offer from the market. (Because I don't
1570  want some old buy offer
1571  for $10 laying around for the same stock that I'm SELLING for $8! If I dump
1572  100 shares, I'll receive
1573  $800--I don't want my software to automatically turn around and BUY those
1574  same shares again for $1000!
1575  That would be a $200 loss.)
1576 
1577  This is done here. This function gets called once for each offer that's
1578  active for this Nym.
1579  extra_vals contains the relevant info we're looking for, and offer_data
1580  contains the current
1581  offer (as we loop through ALL this Nym's offers, this function gets called
1582  for each one.)
1583  So here we just need to compare once, and add to the list if the comparison
1584  matches.
1585  */
1586  /*
1587  attr the_lambda_struct::the_vector // used for returning a list of
1588  something.
1589  attr the_lambda_struct::the_asset_acct // for newoffer, we want to remove
1590  existing offers for the same accounts in certain cases.
1591  attr the_lambda_struct::the_currency_acct // for newoffer, we want to remove
1592  existing offers for the same accounts in certain cases.
1593  attr the_lambda_struct::the_scale // for newoffer as well.
1594  attr the_lambda_struct::the_price // for newoffer as well.
1595  attr the_lambda_struct::bSelling // for newoffer as well.
1596  */
1597  otLog4 << strLocation << ": About to compare the new potential offer "
1598  "against one of the existing ones...";
1599 
1600  if ((extra_vals.the_asset_acct == offer_data.asset_acct_id) &&
1601  (extra_vals.the_currency_acct == offer_data.currency_acct_id) &&
1602  (extra_vals.the_scale == offer_data.scale)) {
1603  otLog4 << strLocation << ": the account IDs and the scale match...";
1604 
1605  // By this point we know the current offer_data has the same asset acct,
1606  // currency acct, and scale
1607  // as the offer we're comparing to all the rest.
1608  //
1609  // But that's not enough: we also need to compare some prices:
1610  //
1611  // So when placing a buy offer, check all the other offers I already
1612  // have.
1613  // For each, see if it's a sell offer and if so, if the amount is lower
1614  // than the amount on
1615  // the new buy offer, then cancel that sell offer from the market.
1616  // (Because I don't want to buy-high, sell low.)
1617  //
1618  if (!extra_vals.bSelling && offer_data.selling &&
1619  (stoll(offer_data.price_per_scale) < stoll(extra_vals.the_price))) {
1620  extra_vals.the_vector.push_back(offer_data.transaction_id);
1621  }
1622  // Similarly, when placing a sell offer, check all the other offers I
1623  // already have.
1624  // For each, see if it's a buy offer and if so, if the amount is higher
1625  // than the amount of my new
1626  // sell offer, then cancel that buy offer from the market.
1627  //
1628  else if (extra_vals.bSelling && !offer_data.selling &&
1629  (stoll(offer_data.price_per_scale) >
1630  stoll(extra_vals.the_price))) {
1631  extra_vals.the_vector.push_back(offer_data.transaction_id);
1632  }
1633  }
1634  // We don't actually do the removing here, since we are still looping
1635  // through the maps.
1636  // So we just add the IDs to a vector so that the caller can do the removing
1637  // once this loop is over.
1638 
1639  return 1;
1640 }
OTLOG_IMPORT OTLogStream otLog4
std::string the_scale
std::string the_price
std::string the_asset_acct
std::vector< std::string > the_vector
std::string the_currency_acct
string GetVariable ( const char *  name)

Definition at line 161 of file ot_otapi_ot.cpp.

162 {
163  OTVariable* pVar = OT_ME::FindVariable2(name);
164  return pVar == nullptr ? "" : pVar->GetValueString();
165 }
std::string & GetValueString()
Definition: OTVariable.hpp:267
OT_OTAPI_OT int32_t iterate_nymoffers_maps ( MapOfMaps map_of_maps,
LambdaFunc  the_lambda 
)

Definition at line 1720 of file ot_otapi_ot.cpp.

1724 {
1725  the_lambda_struct extra_vals;
1726  return iterate_nymoffers_maps(map_of_maps, the_lambda, extra_vals);
1727 }
EXPORT OT_OTAPI_OT int32_t iterate_nymoffers_maps(MapOfMaps &map_of_maps, LambdaFunc the_lambda)
OT_OTAPI_OT int32_t iterate_nymoffers_maps ( MapOfMaps map_of_maps,
LambdaFunc  the_lambda,
the_lambda_struct extra_vals 
)

Definition at line 1733 of file ot_otapi_ot.cpp.

1738 {
1739  // the_lambda must be good (assumed) and must have the parameter profile
1740  // like this sample:
1741  // def the_lambda(offer_data, nIndex, map_of_maps, sub_map, extra_vals)
1742  // //
1743  // if 10 offers are printed for the SAME market, nIndex will be 0..9
1744 
1745  string strLocation = "iterate_nymoffers_maps";
1746 
1747  // Next let's loop through the map_of_maps and output the offers for each
1748  // market therein...
1749  //
1750  // var range_map_of_maps = map_of_maps.range();
1751  MapOfMaps* map_of_mapsPtr = &map_of_maps;
1752  if (!map_of_mapsPtr) {
1753  otOut << strLocation << ": No range retrieved from map_of_maps.\n";
1754  return -1;
1755  }
1756  if (map_of_maps.empty()) {
1757  otOut << strLocation << ": A range was retrieved for the map_of_maps, "
1758  "but the range is empty.\n";
1759  return -1;
1760  }
1761 
1762  int32_t nMainIndex = -1;
1763  for (auto it = map_of_maps.begin(); it != map_of_maps.end(); ++it) {
1764  ++nMainIndex; // so we can output a header on the FIRST one only.
1765 
1766  // var sub_map_pair = range_map_of_maps.front();
1767  if (nullptr == it->second) {
1768  otOut << strLocation << ": Looping through map_of_maps range, and "
1769  "first sub_map_pair fails to verify.\n";
1770  return -1;
1771  }
1772 
1773  string strMapKey = it->first;
1774 
1775  SubMap& sub_map = *it->second;
1776  if (sub_map.empty()) {
1777  otOut << strLocation << ": Error: Sub_map is empty (Then how is it "
1778  "even here?? Submaps are only added based "
1779  "on existing offers.)\n";
1780  return -1;
1781  }
1782 
1783  if (0 == nMainIndex) // this is the first iteration of the main output
1784  // loop...
1785  {
1786  // otOut << "\n** MY OFFERS **\n\n")
1787  }
1788 
1789  int32_t nSubMap = iterate_nymoffers_sub_map(map_of_maps, sub_map,
1790  the_lambda, extra_vals);
1791  if (-1 == nSubMap) {
1792  otOut << strLocation
1793  << ": Error: while trying to iterate_nymoffers_sub_map.\n";
1794  return -1;
1795  }
1796  }
1797  map_of_maps.clear();
1798 
1799  return 1;
1800 }
std::map< std::string, opentxs::OTDB::OfferDataNym * > SubMap
OTLOG_IMPORT OTLogStream otOut
EXPORT OT_OTAPI_OT int32_t iterate_nymoffers_sub_map(const MapOfMaps &map_of_maps, SubMap &sub_map, LambdaFunc the_lambda)
std::map< std::string, SubMap * > MapOfMaps
OT_OTAPI_OT int32_t iterate_nymoffers_sub_map ( const MapOfMaps map_of_maps,
SubMap sub_map,
LambdaFunc  the_lambda 
)

Definition at line 1642 of file ot_otapi_ot.cpp.

1645 {
1646  the_lambda_struct extra_vals;
1647  return iterate_nymoffers_sub_map(map_of_maps, sub_map, the_lambda,
1648  extra_vals);
1649 }
EXPORT OT_OTAPI_OT int32_t iterate_nymoffers_sub_map(const MapOfMaps &map_of_maps, SubMap &sub_map, LambdaFunc the_lambda)
OT_OTAPI_OT int32_t iterate_nymoffers_sub_map ( const MapOfMaps map_of_maps,
SubMap sub_map,
LambdaFunc  the_lambda,
the_lambda_struct extra_vals 
)

Definition at line 1656 of file ot_otapi_ot.cpp.

1660 {
1661  // the_lambda must be good (assumed) and must have the parameter profile
1662  // like this sample:
1663  // def the_lambda(offer_data, nIndex, map_of_maps, sub_map, extra_vals)
1664  //
1665  // if 10 offers are printed for the SAME market, nIndex will be 0..9
1666 
1667  string strLocation = "iterate_nymoffers_sub_map";
1668 
1669  // Looping through the map_of_maps, we are now on a valid sub_map in this
1670  // iteration.
1671  // Therefore let's loop through the offers on that sub_map and output them!
1672  //
1673  // var range_sub_map = sub_map.range();
1674 
1675  SubMap* sub_mapPtr = &sub_map;
1676  if (!sub_mapPtr) {
1677  otOut << strLocation << ": No range retrieved from sub_map. It must be "
1678  "non-existent, I guess.\n";
1679  return -1;
1680  }
1681  if (sub_map.empty()) {
1682  // Should never happen since we already made sure all the sub_maps
1683  // have data on them. Therefore if this range is empty now, it's a
1684  // chaiscript
1685  // bug (extremely unlikely.)
1686  //
1687  otOut << strLocation << ": Error: A range was retrieved for the "
1688  "sub_map, but the range is empty.\n";
1689  return -1;
1690  }
1691 
1692  int32_t nIndex = -1;
1693  for (auto it = sub_map.begin(); it != sub_map.end(); ++it) {
1694  ++nIndex;
1695  // var offer_data_pair = range_sub_map.front();
1696 
1697  if (nullptr == it->second) {
1698  otOut << strLocation << ": Looping through range_sub_map range, "
1699  "and first offer_data_pair fails to "
1700  "verify.\n";
1701  return -1;
1702  }
1703 
1704  OTDB::OfferDataNym& offer_data = *it->second;
1705  int32_t nLambda =
1706  (*the_lambda)(offer_data, nIndex, map_of_maps, sub_map,
1707  extra_vals); // if 10 offers are printed for the SAME
1708  // market, nIndex will be 0..9;
1709  if (-1 == nLambda) {
1710  otOut << strLocation << ": Error: the_lambda failed.\n";
1711  return -1;
1712  }
1713  }
1714  sub_map.clear();
1715 
1716  return 1;
1717 }
std::map< std::string, opentxs::OTDB::OfferDataNym * > SubMap
OTLOG_IMPORT OTLogStream otOut
OT_OTAPI_OT OTDB::OfferListNym* loadNymOffers ( const string &  serverID,
const string &  nymID 
)

Definition at line 1354 of file ot_otapi_ot.cpp.

1356 {
1357  OTDB::OfferListNym* offerList = nullptr;
1358 
1359  if (OTDB::Exists("nyms", serverID, "offers", nymID + ".bin")) {
1360  otWarn << "Offers file exists... Querying nyms...\n";
1361  OTDB::Storable* storable =
1363  "offers", nymID + ".bin");
1364 
1365  if (nullptr == storable) {
1366  otOut << "Unable to verify storable object. Probably doesn't "
1367  "exist.\n";
1368  return nullptr;
1369  }
1370 
1371  otWarn << "QueryObject worked. Now dynamic casting from storable to a "
1372  "(nym) offerList...\n";
1373  offerList = dynamic_cast<OTDB::OfferListNym*>(storable);
1374 
1375  if (nullptr == offerList) {
1376  otOut
1377  << "Unable to dynamic cast a storable to a (nym) offerList.\n";
1378  return nullptr;
1379  }
1380  }
1381 
1382  return offerList;
1383 }
OTLOG_IMPORT OTLogStream otOut
EXPORT Storable * QueryObject(StoredObjectType theObjectType, std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
Definition: OTStorage.cpp:788
OTLOG_IMPORT OTLogStream otWarn
EXPORT bool Exists(std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
Definition: OTStorage.cpp:584
OT_OTAPI_OT int32_t output_nymoffer_data ( const OTDB::OfferDataNym offer_data,
int32_t  nIndex,
const MapOfMaps ,
const SubMap ,
the_lambda_struct  
)

Definition at line 1477 of file ot_otapi_ot.cpp.

1481 { // extra_vals unused in this function, but not in others that share this
1482  // parameter profile.
1483  // (It's used as a lambda.)
1484 
1485  string strScale = offer_data.scale;
1486  string strAssetTypeID = offer_data.asset_type_id;
1487  string strCurrencyTypeID = offer_data.currency_type_id;
1488  string strSellStatus = offer_data.selling ? "SELL" : "BUY";
1489  string strTransactionID = offer_data.transaction_id;
1490  string strAvailableAssets = to_string(stoll(offer_data.total_assets) -
1491  stoll(offer_data.finished_so_far));
1492 
1493  if (0 == nIndex) // first iteration! (Output a header.)
1494  {
1495  otOut << "Scale:\t\t" << strScale << "\n";
1496  otOut << "Asset:\t\t" << strAssetTypeID << "\n";
1497  otOut << "Currency:\t" << strCurrencyTypeID << "\n";
1498  otOut << "\nIndex\tTrans#\tType\tPrice\tAvailable\n";
1499  }
1500 
1501  //
1502  // Okay, we have the offer_data, so let's output it!
1503  //
1504  cout << (nIndex) << "\t" << offer_data.transaction_id << "\t"
1505  << strSellStatus << "\t" << offer_data.price_per_scale << "\t"
1506  << strAvailableAssets << "\n";
1507 
1508  return 1;
1509 }
std::string to_string(const T &t)
OTLOG_IMPORT OTLogStream otOut

Variable Documentation

string Args

Definition at line 141 of file ot_otapi_ot.cpp.

string HisAcct

Definition at line 142 of file ot_otapi_ot.cpp.

string HisNym

Definition at line 143 of file ot_otapi_ot.cpp.

string HisPurse

Definition at line 144 of file ot_otapi_ot.cpp.

string MyAcct

Definition at line 145 of file ot_otapi_ot.cpp.

string MyNym

Definition at line 146 of file ot_otapi_ot.cpp.

string MyPurse

Definition at line 147 of file ot_otapi_ot.cpp.

string Server

Definition at line 148 of file ot_otapi_ot.cpp.