xrpld
Loading...
Searching...
No Matches
VaultTestBase.h
1#pragma once
2
3#include <test/jtx/Account.h>
4#include <test/jtx/Env.h>
5#include <test/jtx/amount.h>
6#include <test/jtx/vault.h>
7
8#include <xrpl/basics/chrono.h>
9#include <xrpl/beast/unit_test/suite.h>
10#include <xrpl/protocol/Asset.h>
11#include <xrpl/protocol/Feature.h>
12#include <xrpl/protocol/Keylet.h>
13#include <xrpl/protocol/Protocol.h>
14#include <xrpl/protocol/STAmount.h>
15
16#include <chrono>
17#include <cstdint>
18#include <format>
19#include <source_location>
20#include <string>
21#include <utility>
22
23namespace xrpl {
24
38{
39protected:
42
43 static constexpr auto kNegativeAmount = [](PrettyAsset const& asset) -> PrettyAmount {
44 return {STAmount{asset.raw(), 1ul, 0, true, STAmount::Unchecked{}}, ""};
45 };
46
53 {
54 return env.current()->header().closeTimeResolution;
55 }
56
57 void
59 test::jtx::Env& env,
62 {
63 using namespace std::chrono_literals;
64 env.close(time - env.closed()->header().closeTimeResolution + 1s);
65 expect(
66 env.closed()->header().closeTime == time,
68 "current ledger time {} is not equal to the target ledger time {}",
69 env.closed()->header().closeTime.time_since_epoch(),
70 time.time_since_epoch()),
71 loc.file_name(),
72 loc.line());
73 }
74
77
78 // Vault holds an Env& so no default initializer is possible; the
79 // struct is always aggregate-initialized by makeClosedEndedVault.
80 // NOLINTBEGIN(cppcoreguidelines-pro-type-member-init)
88 // NOLINTEND(cppcoreguidelines-pro-type-member-init)
89
90 // Submit a VaultCreate for a closed-ended vault with SubscriptionDate at
91 // env.now() + subOffset and RedemptionDate at SubscriptionDate + gap, then
92 // close the ledger. Returns the Vault helper, the vault's keylet and the
93 // resolved sub/red timestamps.
94 static ClosedEndedSetup
96 test::jtx::Env& env,
97 test::jtx::Account const& owner,
98 Asset const& asset,
99 std::uint32_t subOffset,
100 std::uint32_t gap)
101 {
102 auto const sub = env.now().time_since_epoch().count() + subOffset;
103 auto const red = sub + gap;
104 test::jtx::Vault const vault{env};
105 auto [tx, keylet] = vault.create(
106 {.owner = owner,
107 .asset = asset,
108 .vaultKind = std::to_underlying(VaultKind::ClosedEnded),
109 .subscriptionDate = sub,
110 .redemptionDate = red});
111 env(tx);
112 env.close();
113 return {.vault = vault, .keylet = keylet, .sub = sub, .red = red};
114 }
115
118};
119
120} // namespace xrpl
A testsuite class.
Definition suite.h:52
bool expect(Condition const &shouldBeTrue)
Evaluate a test condition.
Definition suite.h:235
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
std::chrono::duration< rep, period > duration
Definition chrono.h:47
Shared base for the Vault*_test family under src/test/app/vault/.
NetClock::duration D
NetClock::time_point Tp
FeatureBitset const all_
std::string const iouCurrency_
static ClosedEndedSetup makeClosedEndedVault(test::jtx::Env &env, test::jtx::Account const &owner, Asset const &asset, std::uint32_t subOffset, std::uint32_t gap)
static constexpr auto kNegativeAmount
void closeToTime(test::jtx::Env &env, NetClock::time_point time, std::source_location const &loc=std::source_location::current())
static NetClock::duration getLedgerTimeResolution(test::jtx::Env &env)
Get the current ledger's close time resolution.
test::jtx::PrettyAmount PrettyAmount
test::jtx::PrettyAsset PrettyAsset
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
A transaction testing environment.
Definition Env.h:161
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:133
std::shared_ptr< ReadView const > closed()
Returns the last closed ledger.
Definition Env.cpp:127
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:377
NetClock::time_point now()
Returns the current network time.
Definition Env.h:326
T current(T... args)
T format(T... args)
Keylet computation functions.
Definition Indexes.h:40
FeatureBitset testableAmendments()
Definition Env.h:92
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
Represents an XRP, IOU, or MPT quantity This customizes the string conversion and supports XRP conver...
T time_since_epoch(T... args)