Skip to content

Commit

Permalink
Track Add/SubVaultCollateral in vault history (#1715)
Browse files Browse the repository at this point in the history
Co-authored-by: Prasanna Loganathar <pvl@prasannavl.com>
  • Loading branch information
Bushstar and prasannavl authored Feb 6, 2023
1 parent a8d147f commit 50d877a
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 73 deletions.
16 changes: 16 additions & 0 deletions src/masternodes/accountshistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,22 @@ Res CAccountsHistoryWriter::SubBalance(const CScript &owner, CTokenAmount amount
return res;
}

Res CAccountsHistoryWriter::AddVaultCollateral(const CVaultId &vaultId, CTokenAmount amount) {
auto res = CCustomCSView::AddVaultCollateral(vaultId, amount);
if (writers && res && amount.nValue) {
writers->AddVaultCollateral(amount, vaultID);
}
return res;
}

Res CAccountsHistoryWriter::SubVaultCollateral(const CVaultId &vaultId, CTokenAmount amount) {
auto res = CCustomCSView::SubVaultCollateral(vaultId, amount);
if (writers && res && amount.nValue) {
writers->SubVaultCollateral(amount, vaultID);
}
return res;
}

bool CAccountsHistoryWriter::Flush() {
writers.Flush(height, txid, txn, type, vaultID);
return CCustomCSView::Flush();
Expand Down
2 changes: 2 additions & 0 deletions src/masternodes/accountshistory.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class CAccountsHistoryWriter : public CCustomCSView {
~CAccountsHistoryWriter() override;
Res AddBalance(const CScript &owner, CTokenAmount amount) override;
Res SubBalance(const CScript &owner, CTokenAmount amount) override;
Res AddVaultCollateral(const CVaultId &vaultId, CTokenAmount amount) override;
Res SubVaultCollateral(const CVaultId &vaultId, CTokenAmount amount) override;
bool Flush() override;
CHistoryWriters& GetHistoryWriters() override { return writers; }
};
Expand Down
12 changes: 11 additions & 1 deletion src/masternodes/rpc_vault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,11 @@ UniValue historyToJSON(VaultHistoryKey const & key, VaultHistoryValue const & va
key.txn, value.txid.ToString(), value.diff);
}

UniValue collateralToJSON(VaultHistoryKey const & key, VaultHistoryValue const & value) {
return vaultToJSON(key.vaultID, "vaultCollateral", key.blockHeight, ToString(CustomTxCodeToType(value.category)),
key.txn, value.txid.ToString(), value.diff);
}

UniValue schemeToJSON(VaultSchemeKey const & key, const VaultGlobalSchemeValue& value) {
auto obj = vaultToJSON(key.vaultID, "", key.blockHeight, ToString(CustomTxCodeToType(value.category)), 0, value.txid.ToString(), {});

Expand Down Expand Up @@ -1469,7 +1474,12 @@ UniValue listvaulthistory(const JSONRPCRequest& request) {
}

auto& array = ret.emplace(key.blockHeight, UniValue::VARR).first->second;
array.push_back(historyToJSON(key, value));

if (key.address.empty()) {
array.push_back(collateralToJSON(key, value));
} else {
array.push_back(historyToJSON(key, value));
}

return --count != 0;
};
Expand Down
4 changes: 2 additions & 2 deletions src/masternodes/vault.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ class CVaultView : public virtual CStorageView {
const CVaultId &start = {},
const CScript &ownerAddress = {});

Res AddVaultCollateral(const CVaultId &vaultId, CTokenAmount amount);
Res SubVaultCollateral(const CVaultId &vaultId, CTokenAmount amount);
virtual Res AddVaultCollateral(const CVaultId &vaultId, CTokenAmount amount);
virtual Res SubVaultCollateral(const CVaultId &vaultId, CTokenAmount amount);
std::optional<CBalances> GetVaultCollaterals(const CVaultId &vaultId);
void ForEachVaultCollateral(std::function<bool(const CVaultId &, const CBalances &)> callback);

Expand Down
6 changes: 3 additions & 3 deletions src/masternodes/vaulthistory.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ class CVaultHistoryView : public virtual CStorageView {
void EraseVaultHistory(const uint32_t height);

void ForEachVaultHistory(std::function<bool(const VaultHistoryKey &, CLazySerialize<VaultHistoryValue>)> callback,
const VaultHistoryKey &start = {});
const VaultHistoryKey &start = {std::numeric_limits<uint32_t>::max(), {}, std::numeric_limits<uint32_t>::max(), {}});
void ForEachVaultScheme(std::function<bool(const VaultSchemeKey &, CLazySerialize<VaultSchemeValue>)> callback,
const VaultSchemeKey &start = {});
const VaultSchemeKey &start = {{}, std::numeric_limits<uint32_t>::max()});
void ForEachVaultState(std::function<bool(const VaultStateKey &, CLazySerialize<VaultStateValue>)> callback,
const VaultStateKey &start = {});
const VaultStateKey &start = {{}, std::numeric_limits<uint32_t>::max()});

// Loan Scheme storage
void WriteGlobalScheme(const VaultGlobalSchemeKey &key, const VaultGlobalSchemeValue &value);
Expand Down
Loading

0 comments on commit 50d877a

Please sign in to comment.