1#include <test/jtx/Account.h>
2#include <test/jtx/Env.h>
3#include <test/jtx/WSClient.h>
4#include <test/jtx/amount.h>
5#include <test/jtx/escrow.h>
6#include <test/jtx/mpt.h>
7#include <test/jtx/pay.h>
8#include <test/jtx/trust.h>
10#include <xrpl/beast/unit_test/suite.h>
11#include <xrpl/json/json_value.h>
12#include <xrpl/protocol/ApiVersion.h>
13#include <xrpl/protocol/Feature.h>
14#include <xrpl/protocol/STAmount.h>
15#include <xrpl/protocol/TxFlags.h>
16#include <xrpl/protocol/jss.h>
26 using namespace std::chrono_literals;
28 Env env(*
this, features);
34 auto usd = alice[
"USD"];
35 auto cny = alice[
"CNY"];
36 auto jpy = alice[
"JPY"];
42 env(
trust(hw, usd(10000)));
43 env(
trust(hw, jpy(10000)));
44 env(
pay(alice, hw, usd(5000)));
45 env(
pay(alice, hw, jpy(5000)));
51 env(
trust(bob, usd(100)));
52 env(
trust(bob, cny(100)));
53 env(
pay(alice, bob, usd(50)));
55 Account const charley{
"charley"};
56 env.
fund(
XRP(10000),
"charley");
58 env(
trust(charley, cny(500)));
59 env(
trust(charley, jpy(500)));
60 env(
pay(alice, charley, cny(250)));
61 env(
pay(alice, charley, jpy(250)));
66 env(
trust(dave, cny(100)));
67 env(
pay(alice, dave, cny(30)));
70 env(
trust(alice, charley[
"USD"](50)));
71 env(
pay(charley, alice, usd(10)));
74 env(
trust(alice, dave[
"CNY"](0), dave, tfSetFreeze));
81 qry[jss::account] = alice.
human();
82 qry[jss::hotwallet] = hw.
human();
84 auto jv = wsc->invoke(
"gateway_balances", qry);
85 expect(jv[jss::status] ==
"success");
86 if (wsc->version() == 2)
88 expect(jv.isMember(jss::jsonrpc) && jv[jss::jsonrpc] ==
"2.0");
89 expect(jv.isMember(jss::ripplerpc) && jv[jss::ripplerpc] ==
"2.0");
90 expect(jv.isMember(jss::id) && jv[jss::id] == 5);
93 auto const& result = jv[jss::result];
95 expect(result[jss::status] ==
"success");
98 auto const& balances = result[jss::balances];
99 expect(balances.isObject(),
"balances is not an object");
100 expect(balances.size() == 1,
"balances size is not 1");
102 auto const& hwBalance = balances[hw.
human()];
103 expect(hwBalance.isArray(),
"hwBalance is not an array");
104 expect(hwBalance.size() == 2);
105 auto c1 = hwBalance[0u][jss::currency];
106 auto c2 = hwBalance[1u][jss::currency];
107 expect(c1 ==
"USD" || c2 ==
"USD");
108 expect(c1 ==
"JPY" || c2 ==
"JPY");
109 expect(hwBalance[0u][jss::value] ==
"5000" && hwBalance[1u][jss::value] ==
"5000");
113 auto const& fBalances = result[jss::frozen_balances];
114 expect(fBalances.isObject());
115 expect(fBalances.size() == 1);
117 auto const& fBal = fBalances[dave.
human()];
120 expect(fBal[0u].isObject());
121 expect(fBal[0u][jss::currency] ==
"CNY");
122 expect(fBal[0u][jss::value] ==
"30");
126 auto const& assets = result[jss::assets];
127 expect(assets.isObject(),
"assets it not an object");
128 expect(assets.size() == 1,
"assets size is not 1");
130 auto const& cAssets = assets[charley.
human()];
131 expect(cAssets.isArray());
132 expect(cAssets.size() == 1);
133 expect(cAssets[0u][jss::currency] ==
"USD");
134 expect(cAssets[0u][jss::value] ==
"10");
138 auto const& obligations = result[jss::obligations];
139 expect(obligations.isObject(),
"obligations is not an object");
140 expect(obligations.size() == 3);
141 expect(obligations[
"CNY"] ==
"250");
142 expect(obligations[
"JPY"] ==
"250");
143 expect(obligations[
"USD"] ==
"50");
151 using namespace std::chrono_literals;
153 Env env(*
this, features);
165 qry2[jss::account] = alice.
human();
166 qry2[jss::hotwallet] =
"asdf";
169 qry2[jss::api_version] = apiVersion;
170 auto jv = wsc->invoke(
"gateway_balances", qry2);
171 expect(jv[jss::status] ==
"error");
173 auto response = jv[jss::result];
174 auto const error = apiVersion < 2u ?
"invalidHotWallet" :
"invalidParams";
175 BEAST_EXPECT(response[jss::error] == error);
182 using namespace std::chrono_literals;
190 auto usd = alice[
"USD"];
199 env(
trust(hw, maxUSD));
201 env(
pay(alice, hw, maxUSD));
207 env(
trust(bob, maxUSD));
209 env(
pay(alice, bob, maxUSD));
211 Account const charley{
"charley"};
214 env(
trust(charley, maxUSD));
216 env(
pay(alice, charley, maxUSD));
223 query[jss::account] = alice.
human();
224 query[jss::hotwallet] = hw.
human();
230 auto jv = wsc->invoke(
"gateway_balances", query);
231 expect(jv[jss::status] ==
"success");
232 expect(jv[jss::result][jss::obligations][
"USD"] == maxUSD.
getText());
238 testcase(
"Gateway Balances with MPT Escrow");
239 using namespace std::chrono_literals;
244 Env env(*
this, features);
249 env.
fund(
XRP(10000), alice, bob);
253 MPTTester mpt(env, alice, {.holders = {bob}, .fund =
false});
254 mpt.
create({.flags = tfMPTCanEscrow});
257 mpt.authorize({.account = bob, .holderCount = 1});
258 mpt.pay(alice, bob, 1000);
261 auto const MPT = mpt[
"MPT"];
268 qry[jss::account] = bob.
human();
270 auto jv = wsc->invoke(
"gateway_balances", qry);
271 expect(jv[jss::status] ==
"success");
279 for (
auto feature : {sa - featurePermissionedDEX, sa})
bool expect(Condition const &shouldBeTrue)
Evaluate a test condition.
TestcaseT testcase
Memberspace for declaring test cases.
virtual Config & config()=0
std::string getText() const override
static constexpr std::uint64_t kMaxValue
static constexpr int kMaxOffset
void testGWB(FeatureBitset features)
void run() override
Runs the suite.
void testGWBApiVersions(FeatureBitset features)
Immutable cryptographic account descriptor.
std::string const & human() const
Returns the human readable public key.
A transaction testing environment.
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
NetClock::time_point now()
Returns the current network time.
Test helper for creating, mutating, and asserting MPT and confidential MPT ledger state.
void create(MPTCreate const &arg=MPTCreate{})
Converts to MPT Issue or STAmount.
json::Value create(AccountID const &account, AccountID const &to, STAmount const &amount)
auto const kFinishTime
Set the "FinishAfter" time tag on a JTx.
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
XrpT const XRP
Converts to XRP Issue or STAmount.
FeatureBitset testableAmendments()
json::Value trust(Account const &account, STAmount const &amount, std::uint32_t flags)
Modify a trust line.
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
std::unique_ptr< WSClient > makeWSClient(Config const &cfg, bool v2, unsigned rpcVersion, std::unordered_map< std::string, std::string > const &headers)
Returns a client operating through WebSockets/S.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
void forAllApiVersions(Fn const &fn, Args &&... args)