xrpld
Loading...
Searching...
No Matches
GatewayBalances_test.cpp
1#include <test/jtx/Account.h>
2#include <test/jtx/Env.h>
3#include <test/jtx/TestHelpers.h>
4#include <test/jtx/WSClient.h>
5#include <test/jtx/amount.h>
6#include <test/jtx/escrow.h>
7#include <test/jtx/mpt.h>
8#include <test/jtx/pay.h>
9#include <test/jtx/trust.h>
10
11#include <xrpl/beast/unit_test/suite.h>
12#include <xrpl/json/json_value.h>
13#include <xrpl/protocol/ApiVersion.h>
14#include <xrpl/protocol/ErrorCodes.h>
15#include <xrpl/protocol/Feature.h>
16#include <xrpl/protocol/STAmount.h>
17#include <xrpl/protocol/TxFlags.h>
18#include <xrpl/protocol/jss.h>
19
20namespace xrpl::test {
21
23{
24public:
25 void
27 {
28 using namespace std::chrono_literals;
29 using namespace jtx;
30 Env env(*this, features);
31
32 {
33 // Gateway account and assets
34 Account const alice{"alice"};
35 env.fund(XRP(10000), "alice");
36 auto const usd = alice["USD"];
37 auto const cny = alice["CNY"];
38 auto const jpy = alice["JPY"];
39
40 // Create a hotwallet
41 Account const hw{"hw"};
42 env.fund(XRP(10000), "hw");
43 env.close();
44 env(trust(hw, usd(10000)));
45 env(trust(hw, jpy(10000)));
46 env(pay(alice, hw, usd(5000)));
47 env(pay(alice, hw, jpy(5000)));
48
49 // Create some clients
50 Account const bob{"bob"};
51 env.fund(XRP(10000), "bob");
52 env.close();
53 env(trust(bob, usd(100)));
54 env(trust(bob, cny(100)));
55 env(pay(alice, bob, usd(50)));
56
57 Account const charley{"charley"};
58 env.fund(XRP(10000), "charley");
59 env.close();
60 env(trust(charley, cny(500)));
61 env(trust(charley, jpy(500)));
62 env(pay(alice, charley, cny(250)));
63 env(pay(alice, charley, jpy(250)));
64
65 Account const dave{"dave"};
66 env.fund(XRP(10000), "dave");
67 env.close();
68 env(trust(dave, cny(100)));
69 env(pay(alice, dave, cny(30)));
70
71 // give the gateway an asset
72 env(trust(alice, charley["USD"](50)));
73 env(pay(charley, alice, usd(10)));
74
75 // freeze dave
76 env(trust(alice, dave["CNY"](0), dave, tfSetFreeze));
77
78 env.close();
79
80 auto wsc = makeWSClient(env.app().config());
81
82 json::Value qry;
83 qry[jss::account] = alice.human();
84 qry[jss::hotwallet] = hw.human();
85
86 auto jv = wsc->invoke("gateway_balances", qry);
87 expect(jv[jss::status] == "success");
88 if (wsc->version() == 2)
89 {
91 expect(jv.isMember(jss::id) && jv[jss::id] == 5);
92 }
93
94 auto const& result = jv[jss::result];
95 expect(result[jss::account] == alice.human());
96 expect(result[jss::status] == "success");
97
98 {
99 auto const& balances = result[jss::balances];
100 expect(balances.isObject(), "balances is not an object");
101 expect(balances.size() == 1, "balances size is not 1");
102
103 auto const& hwBalance = balances[hw.human()];
104 expect(hwBalance.isArray(), "hwBalance is not an array");
105 expect(hwBalance.size() == 2);
106 auto const c1 = hwBalance[0u][jss::currency];
107 auto const c2 = hwBalance[1u][jss::currency];
108 expect(c1 == "USD" || c2 == "USD");
109 expect(c1 == "JPY" || c2 == "JPY");
110 expect(hwBalance[0u][jss::value] == "5000" && hwBalance[1u][jss::value] == "5000");
111 }
112
113 {
114 auto const& fBalances = result[jss::frozen_balances];
115 expect(fBalances.isObject());
116 expect(fBalances.size() == 1);
117
118 auto const& fBal = fBalances[dave.human()];
119 expect(fBal.isArray());
120 expect(fBal.size() == 1);
121 expect(fBal[0u].isObject());
122 expect(fBal[0u][jss::currency] == "CNY");
123 expect(fBal[0u][jss::value] == "30");
124 }
125
126 {
127 auto const& assets = result[jss::assets];
128 expect(assets.isObject(), "assets it not an object");
129 expect(assets.size() == 1, "assets size is not 1");
130
131 auto const& cAssets = assets[charley.human()];
132 expect(cAssets.isArray());
133 expect(cAssets.size() == 1);
134 expect(cAssets[0u][jss::currency] == "USD");
135 expect(cAssets[0u][jss::value] == "10");
136 }
137
138 {
139 auto const& obligations = result[jss::obligations];
140 expect(obligations.isObject(), "obligations is not an object");
141 expect(obligations.size() == 3);
142 expect(obligations["CNY"] == "250");
143 expect(obligations["JPY"] == "250");
144 expect(obligations["USD"] == "50");
145 }
146 }
147 }
148
149 void
151 {
152 using namespace std::chrono_literals;
153 using namespace jtx;
154 Env env(*this, features);
155
156 // Gateway account and assets
157 Account const alice{"alice"};
158 env.fund(XRP(10000), alice);
159 Account const hw{"hw"};
160 env.fund(XRP(10000), hw);
161 env.close();
162
163 auto wsc = makeWSClient(env.app().config());
164
165 json::Value qry2;
166 qry2[jss::account] = alice.human();
167 qry2[jss::hotwallet] = "asdf";
168
169 forAllApiVersions([&, this](unsigned apiVersion) {
170 qry2[jss::api_version] = apiVersion;
171 auto jv = wsc->invoke("gateway_balances", qry2);
172 expect(jv[jss::status] == "error");
173
174 auto response = jv[jss::result];
175 auto const error = apiVersion < 2u ? "invalidHotWallet" : "invalidParams";
176 auto const errorCode = apiVersion < 2u ? RpcInvalidHotwallet : RpcInvalidParams;
177 auto const errorMessage =
178 apiVersion < 2u ? "Invalid hotwallet." : "Invalid parameters.";
179 BEAST_EXPECT(response[jss::error] == error);
180 BEAST_EXPECT(response[jss::error_code] == errorCode);
181 BEAST_EXPECT(response[jss::error_message] == errorMessage);
182 });
183 }
184
185 void
187 {
188 testcase("Gateway Balances with non-string account/ident");
189 using namespace std::chrono_literals;
190 using namespace jtx;
191 Env env(*this, features);
192
193 Account const alice{"alice"};
194 env.fund(XRP(10000), alice);
195 env.close();
196
197 auto wsc = makeWSClient(env.app().config());
198
199 // A non-string "account" must be rejected cleanly with invalidParams
200 // rather than throwing a Json::LogicError that surfaces as internal.
201 json::Value qry;
202 qry[jss::account] = 42;
203 qry[jss::hotwallet] = alice.human();
204
205 forAllApiVersions([&, this](unsigned apiVersion) {
206 qry[jss::api_version] = apiVersion;
207 auto jv = wsc->invoke("gateway_balances", qry);
208 expect(jv[jss::status] == "error");
209 BEAST_EXPECT(jv[jss::result][jss::error] == "invalidParams");
210 BEAST_EXPECT(jv[jss::result][jss::error_code] == RpcInvalidParams);
211 BEAST_EXPECT(jv[jss::result][jss::error_message] == "Invalid field 'account'.");
212 });
213
214 // The same applies to a non-string "ident".
215 json::Value qry2;
216 qry2[jss::ident] = 42;
217
218 forAllApiVersions([&, this](unsigned apiVersion) {
219 qry2[jss::api_version] = apiVersion;
220 auto jv = wsc->invoke("gateway_balances", qry2);
221 expect(jv[jss::status] == "error");
222 BEAST_EXPECT(jv[jss::result][jss::error] == "invalidParams");
223 BEAST_EXPECT(jv[jss::result][jss::error_code] == RpcInvalidParams);
224 BEAST_EXPECT(jv[jss::result][jss::error_message] == "Invalid field 'ident'.");
225 });
226 }
227
228 void
230 {
231 using namespace std::chrono_literals;
232 using namespace jtx;
233 Env env(*this);
234
235 // Gateway account and assets
236 Account const alice{"alice"};
237 env.fund(XRP(10000), alice);
238 env.close();
239 auto const usd = alice["USD"];
240
241 // The largest valid STAmount of USD:
243
244 // Create a hotwallet
245 Account const hw{"hw"};
246 env.fund(XRP(10000), hw);
247 env.close();
248 env(trust(hw, maxUSD));
249 env.close();
250 env(pay(alice, hw, maxUSD));
251
252 // Create some clients
253 Account const bob{"bob"};
254 env.fund(XRP(10000), bob);
255 env.close();
256 env(trust(bob, maxUSD));
257 env.close();
258 env(pay(alice, bob, maxUSD));
259
260 Account const charley{"charley"};
261 env.fund(XRP(10000), charley);
262 env.close();
263 env(trust(charley, maxUSD));
264 env.close();
265 env(pay(alice, charley, maxUSD));
266
267 env.close();
268
269 auto wsc = makeWSClient(env.app().config());
270
271 json::Value query;
272 query[jss::account] = alice.human();
273 query[jss::hotwallet] = hw.human();
274
275 // Note that the sum of bob's and charley's USD balances exceeds
276 // the amount that can be represented in an STAmount. Nevertheless
277 // we get a valid "obligations" that shows the maximum valid
278 // STAmount.
279 auto jv = wsc->invoke("gateway_balances", query);
280 expect(jv[jss::status] == "success");
281 expect(jv[jss::result][jss::obligations]["USD"] == maxUSD.getText());
282 }
283
284 void
286 {
287 testcase("Gateway Balances with MPT Escrow");
288 using namespace std::chrono_literals;
289 using namespace jtx;
290
291 // testableAmendments() includes MPT
292 FeatureBitset const features = testableAmendments();
293 Env env(*this, features);
294
295 Account const alice{"alice"};
296 Account const bob{"bob"};
297
298 env.fund(XRP(10000), alice, bob);
299 env.close();
300
301 // Create MPT issuance (Alice) with Escrow capability
302 MPTTester mpt(env, alice, {.holders = {bob}, .fund = false});
303 mpt.create({.flags = tfMPTCanEscrow});
304
305 // Authorize Bob and fund him
306 mpt.authorize({.account = bob, .holderCount = 1});
307 mpt.pay(alice, bob, 1000);
308
309 // Bob creates an escrow of MPT to Alice.
310 auto const MPT = mpt["MPT"]; // NOLINT(readability-identifier-naming)
311 env(escrow::create(bob, alice, MPT(100)), escrow::kFinishTime(env.now() + 10s));
312 env.close();
313
314 // Query gateway_balances for Bob.
315 auto wsc = makeWSClient(env.app().config());
316 json::Value qry;
317 qry[jss::account] = bob.human();
318
319 auto jv = wsc->invoke("gateway_balances", qry);
320 expect(jv[jss::status] == "success");
321 }
322
323 void
324 run() override
325 {
326 using namespace jtx;
327 auto const sa = testableAmendments();
328 for (auto const feature : {sa - featurePermissionedDEX, sa})
329 {
330 testGWB(feature);
331 testGWBApiVersions(feature);
332 testGWBInvalidAccount(feature);
333 }
336 }
337};
338
339BEAST_DEFINE_TESTSUITE(GatewayBalances, rpc, xrpl);
340
341} // namespace xrpl::test
A testsuite class.
Definition suite.h:52
bool expect(Condition const &shouldBeTrue)
Evaluate a test condition.
Definition suite.h:235
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Represents a JSON value.
Definition json_value.h:121
virtual Config & config()=0
std::string getText() const override
static constexpr std::uint64_t kMaxValue
Definition STAmount.h:67
static constexpr int kMaxOffset
Definition STAmount.h:62
void testGWB(FeatureBitset features)
void run() override
Runs the suite.
void testGWBApiVersions(FeatureBitset features)
void testGWBInvalidAccount(FeatureBitset features)
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
std::string const & human() const
Returns the human readable public key.
A transaction testing environment.
Definition Env.h:161
Application & app()
Definition Env.h:300
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:133
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:323
NetClock::time_point now()
Returns the current network time.
Definition Env.h:326
Test helper for creating, mutating, and asserting MPT and confidential MPT ledger state.
Definition mpt.h:512
void create(MPTCreate const &arg=MPTCreate{}, std::source_location const &loc=std::source_location::current())
Definition mpt.cpp:346
Converts to MPT Issue or STAmount.
The api_version numbers this server serves.
Definition ApiVersion.h:26
json::Value create(AccountID const &account, AccountID const &to, STAmount const &amount)
Definition escrow.cpp:24
auto const kFinishTime
Set the "FinishAfter" time tag on a JTx.
Definition escrow.h:93
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition pay.cpp:14
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
bool hasEnvelope2(json::Value const &reply)
Reports whether reply carries the version 2 envelope: jsonrpc equal to rpc::kJsonRpcVersion and rippl...
FeatureBitset testableAmendments()
Definition Env.h:92
json::Value trust(Account const &account, STAmount const &amount, std::uint32_t flags)
Modify a trust line.
Definition trust.cpp:18
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.
Definition WSClient.cpp:373
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcInvalidHotwallet
Definition ErrorCodes.h:64
@ RpcInvalidParams
Definition ErrorCodes.h:67
void forAllApiVersions(Fn const &fn, Args &&... args)
Definition ApiVersion.h:159