Skip to content

Instantly share code, notes, and snippets.

@dexX7
Created August 21, 2015 15:44
Show Gist options
  • Save dexX7/10017991e4119712ae39 to your computer and use it in GitHub Desktop.
Save dexX7/10017991e4119712ae39 to your computer and use it in GitHub Desktop.
DEx payments with plain integers
diff --git a/src/omnicore/dex.cpp b/src/omnicore/dex.cpp
index ff4cb0e..74d7381 100644
--- a/src/omnicore/dex.cpp
+++ b/src/omnicore/dex.cpp
@@ -10,6 +10,7 @@
#include "omnicore/errors.h"
#include "omnicore/log.h"
#include "omnicore/omnicore.h"
+#include "omnicore/rules.h"
#include "main.h"
#include "tinyformat.h"
@@ -17,6 +18,7 @@
#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>
+#include <boost/multiprecision/cpp_int.hpp>
#include <openssl/sha.h>
@@ -349,6 +351,22 @@ static int64_t calculateDExTrade(int64_t amountOffered, int64_t amountDesired, i
}
/**
+ * Determine purchased amount for a DEx payment.
+ *
+ * The amount is rounded up, to avoid sell offers with 1 willet leftovers.
+ *
+ * @return The number of tokens that can be purchased
+ */
+static int64_t calculateDExTrade(int64_t amountOffered, int64_t amountDesired, int64_t amountPaid)
+{
+ using boost::multiprecision::int128_t;
+
+ int128_t amountPurchased = 1 + ((int128_t(amountPaid) * int128_t(amountOffered)) - 1) / int128_t(amountDesired);
+
+ return amountPurchased.convert_to<int64_t>();
+}
+
+/**
* Handles incoming BTC payment for the offer.
* TODO: change nAmended: uint64_t -> int64_t
*/
@@ -384,7 +402,12 @@ int DEx_payment(const uint256& txid, unsigned int vout, const std::string& addre
return (DEX_ERROR_PAYMENT -2);
}
- int64_t amountPurchased = legacy::calculateDExTrade(amountOffered, amountDesired, amountPaid);
+ int64_t amountPurchased = 0;
+ if (!IsFeatureActivated(FEATURE_DEXSELL, block)) {
+ amountPurchased = legacy::calculateDExTrade(amountOffered, amountDesired, amountPaid);
+ } else {
+ amountPurchased = calculateDExTrade(amountOffered, amountDesired, amountPaid);
+ }
// -------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment