Skip to content

Commit

Permalink
Add ICX Taker Fee logging (#3108)
Browse files Browse the repository at this point in the history
* Add ICX Taker Fee logging

* Rename keys
  • Loading branch information
Bushstar authored Nov 2, 2024
1 parent 0ca2fba commit e29655e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
38 changes: 31 additions & 7 deletions src/dfi/consensus/icxorders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,29 @@ static CAmount GetDFIperBTC(const CPoolPair &BTCDFIPoolPair) {
return DivideAmounts(BTCDFIPoolPair.reserveB, BTCDFIPoolPair.reserveA);
}

CAmount CICXOrdersConsensus::CalculateTakerFee(CAmount amount) const {
CAmount CICXOrdersConsensus::CalculateTakerFee(const CAmount amount,
const std::string &txType,
const std::string &txid) const {
auto &mnview = blockCtx.GetView();
auto tokenBTC = mnview.GetToken(CICXOrder::TOKEN_BTC);
assert(tokenBTC);
auto pair = mnview.GetPoolPair(tokenBTC->first, DCT_ID{0});
assert(pair);
return (arith_uint256(amount) * mnview.ICXGetTakerFeePerBTC() / COIN * GetDFIperBTC(pair->second) / COIN)
.GetLow64();
auto takerFee =
(arith_uint256(amount) * mnview.ICXGetTakerFeePerBTC() / COIN * GetDFIperBTC(pair->second) / COIN).GetLow64();
if (LogAcceptCategory(BCLog::ICXBUG)) {
UniValue result(UniValue::VOBJ);
result.pushKV("calc_type", txType);
result.pushKV("calc_tx", txid);
result.pushKV("calc_start_amount", GetDecimalString(amount));
result.pushKV("calc_fee_per_btc", GetDecimalString(mnview.ICXGetTakerFeePerBTC()));
result.pushKV("calc_pool_dfi_per_btc", GetDecimalString(GetDFIperBTC(pair->second)));
result.pushKV("calc_taker_fee_in_btc",
GetDecimalString(((arith_uint256(amount) * mnview.ICXGetTakerFeePerBTC() / COIN).GetLow64())));
result.pushKV("calc_taker_fee_in_dfi", GetDecimalString(takerFee));
LogPrint(BCLog::ICXBUG, "%s\n", result.write(0));
}
return takerFee;
}

DCT_ID CICXOrdersConsensus::FindTokenByPartialSymbolName(const std::string &symbol) const {
Expand Down Expand Up @@ -140,7 +155,7 @@ Res CICXOrdersConsensus::operator()(const CICXMakeOfferMessage &obj) const {

if (order->orderType == CICXOrder::TYPE_INTERNAL) {
// calculating takerFee
makeoffer.takerFee = CalculateTakerFee(makeoffer.amount);
makeoffer.takerFee = CalculateTakerFee(makeoffer.amount, "CICXMakeOfferMessage", tx.GetHash().ToString());
} else if (order->orderType == CICXOrder::TYPE_EXTERNAL) {
if (!makeoffer.receivePubkey.IsFullyValid()) {
return Res::Err("receivePubkey must be valid pubkey");
Expand All @@ -149,7 +164,7 @@ Res CICXOrdersConsensus::operator()(const CICXMakeOfferMessage &obj) const {
// calculating takerFee
CAmount BTCAmount(static_cast<CAmount>(
(arith_uint256(makeoffer.amount) * arith_uint256(COIN) / arith_uint256(order->orderPrice)).GetLow64()));
makeoffer.takerFee = CalculateTakerFee(BTCAmount);
makeoffer.takerFee = CalculateTakerFee(BTCAmount, "CICXMakeOfferMessage", tx.GetHash().ToString());
}

// locking takerFee in offer txidaddr
Expand Down Expand Up @@ -237,7 +252,7 @@ Res CICXOrdersConsensus::operator()(const CICXSubmitDFCHTLCMessage &obj) const {
}
} else {
auto BTCAmount = MultiplyAmounts(submitdfchtlc.amount, order->orderPrice);
takerFee = CalculateTakerFee(BTCAmount);
takerFee = CalculateTakerFee(BTCAmount, "CICXSubmitDFCHTLCMessage", tx.GetHash().ToString());
}

// refund the rest of locked takerFee if there is difference
Expand Down Expand Up @@ -426,7 +441,7 @@ Res CICXOrdersConsensus::operator()(const CICXSubmitEXTHTLCMessage &obj) const {
takerFee = (arith_uint256(submitexthtlc.amount) * offer->takerFee / BTCAmount).GetLow64();
}
} else {
takerFee = CalculateTakerFee(submitexthtlc.amount);
takerFee = CalculateTakerFee(submitexthtlc.amount, "CICXSubmitEXTHTLCMessage", tx.GetHash().ToString());
}

// refund the rest of locked takerFee if there is difference
Expand Down Expand Up @@ -564,8 +579,17 @@ Res CICXOrdersConsensus::operator()(const CICXClaimDFCHTLCMessage &obj) const {
if (ExtractDestination(order->ownerAddress, dest)) {
UniValue result(UniValue::VOBJ);
result.pushKV("order_tx", order->creationTx.ToString());
result.pushKV("order_pubkey", HexStr(order->receivePubkey));
result.pushKV("offer_tx", dfchtlc->offerTx.ToString());
result.pushKV("offer_pubkey", HexStr(offer->receivePubkey));
result.pushKV("dfchtlc_tx", dfchtlc->creationTx.ToString());
if (exthtlc) {
if (auto extTx = mnview.GetICXSubmitEXTHTLCTXID(dfchtlc->offerTx)) {
result.pushKV("ext_htlc_tx", extTx->ToString());
}
result.pushKV("ext_htlc_address", exthtlc->htlcscriptAddress);
result.pushKV("ext_htlc_pubkey", HexStr(exthtlc->ownerPubkey));
}
result.pushKV("claim_tx", tx.GetHash().ToString());
result.pushKV("address", EncodeDestination(dest));
result.pushKV("amount", GetDecimalString(offer->takerFee * 50 / 100));
Expand Down
4 changes: 3 additions & 1 deletion src/dfi/consensus/icxorders.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ struct CICXCloseOrderMessage;
struct CICXCloseOfferMessage;

class CICXOrdersConsensus : public CCustomTxVisitor {
[[nodiscard]] CAmount CalculateTakerFee(CAmount amount) const;
[[nodiscard]] CAmount CalculateTakerFee(const CAmount amount,
const std::string &txType,
const std::string &txid) const;
[[nodiscard]] DCT_ID FindTokenByPartialSymbolName(const std::string &symbol) const;

public:
Expand Down
11 changes: 9 additions & 2 deletions src/dfi/icxorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,17 @@ void CICXOrderView::ForEachICXSubmitEXTHTLCExpire(std::function<bool(const Statu
ForEach<ICXSubmitEXTHTLCStatus, StatusKey, uint8_t>(callback, StatusKey{height, {}});
}

std::unique_ptr<CICXOrderView::CICXSubmitEXTHTLCImpl> CICXOrderView::HasICXSubmitEXTHTLCOpen(const uint256 &offertxid) {
std::optional<uint256> CICXOrderView::GetICXSubmitEXTHTLCTXID(const uint256 &offertxid) {
auto it = LowerBound<ICXSubmitEXTHTLCOpenKey>(TxidPairKey{offertxid, {}});
if (it.Valid() && it.Key().first == offertxid) {
return GetICXSubmitEXTHTLCByCreationTx(it.Key().second);
return it.Key().second;
}
return {};
}

std::unique_ptr<CICXOrderView::CICXSubmitEXTHTLCImpl> CICXOrderView::HasICXSubmitEXTHTLCOpen(const uint256 &offertxid) {
if (const auto extTx = GetICXSubmitEXTHTLCTXID(offertxid)) {
return GetICXSubmitEXTHTLCByCreationTx(*extTx);
}
return {};
}
Expand Down
1 change: 1 addition & 0 deletions src/dfi/icxorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ class CICXOrderView : public virtual CStorageView {
void ForEachICXMakeOfferExpire(std::function<bool(const StatusKey &, uint8_t)> callback,
const uint32_t &height = 0);
std::unique_ptr<CICXMakeOfferImpl> HasICXMakeOfferOpen(const uint256 &ordertxid, const uint256 &offertxid);
std::optional<uint256> GetICXSubmitEXTHTLCTXID(const uint256 &offertxid);

// SubmitDFCHTLC
std::unique_ptr<CICXSubmitDFCHTLCImpl> GetICXSubmitDFCHTLCByCreationTx(const uint256 &txid) const;
Expand Down

0 comments on commit e29655e

Please sign in to comment.