xrpld
Loading...
Searching...
No Matches
AMMInfo_test.cpp
1#include <test/jtx/AMM.h>
2#include <test/jtx/AMMTest.h>
3#include <test/jtx/Account.h>
4#include <test/jtx/Env.h>
5#include <test/jtx/amount.h>
6#include <test/jtx/flags.h>
7#include <test/jtx/mpt.h>
8
9#include <xrpl/beast/unit_test/suite.h>
10#include <xrpl/protocol/AccountID.h>
11#include <xrpl/protocol/ErrorCodes.h>
12#include <xrpl/protocol/Feature.h>
13#include <xrpl/protocol/Issue.h>
14#include <xrpl/protocol/TxFlags.h>
15#include <xrpl/protocol/UintTypes.h>
16#include <xrpl/protocol/XRPAmount.h>
17#include <xrpl/protocol/jss.h>
18
19#include <cstdint>
20#include <exception>
21#include <optional>
22#include <string>
23#include <tuple>
24#include <unordered_map>
25#include <unordered_set>
26#include <vector>
27
28namespace xrpl::test {
29
31{
32public:
33 void
35 {
36 testcase("Errors");
37
38 using namespace jtx;
39
40 Account const bogie("bogie");
41 enum class TestAccount { None, Alice, Bogie };
42 auto accountId = [&](AMM const& ammAlice, TestAccount v) -> std::optional<AccountID> {
43 if (v == TestAccount::Alice)
44 {
45 return ammAlice.ammAccount();
46 }
47 if (v == TestAccount::Bogie)
48 {
49 return bogie;
50 }
51
52 return std::nullopt;
53 };
54
55 // Invalid tokens pair
56 testAMM([&](AMM& ammAlice, Env&) {
57 Account const gw("gw");
58 auto const usd = gw["USD"];
59 auto const jv = ammAlice.ammRpcInfo({}, {}, usd, usd);
60 BEAST_EXPECT(jv[jss::error] == "actNotFound");
61 BEAST_EXPECT(jv[jss::error_code] == RpcActNotFound);
62 BEAST_EXPECT(jv[jss::error_message] == "Account not found.");
63 });
64
65 // Invalid LP account id
66 testAMM([&](AMM& ammAlice, Env&) {
67 auto const jv = ammAlice.ammRpcInfo(bogie.id());
68 BEAST_EXPECT(jv[jss::error] == "actMalformed");
69 BEAST_EXPECT(jv[jss::error_code] == RpcActMalformed);
70 BEAST_EXPECT(jv[jss::error_message] == "Account malformed.");
71 });
72
73 // Account is not a string
74 testAMM([&](AMM& ammAlice, Env&) {
75 auto const jv =
76 ammAlice.ammRpcInfo(json::Value{42}, std::nullopt, XRP, USD, std::nullopt, true, 3);
77 BEAST_EXPECT(jv[jss::error] == "actMalformed");
78 BEAST_EXPECT(jv[jss::error_code] == RpcActMalformed);
79 BEAST_EXPECT(jv[jss::error_message] == "Account malformed.");
80 });
81
82 // AMM Account is not a string
83 testAMM([&](AMM& ammAlice, Env&) {
84 auto const jv = ammAlice.ammRpcInfo(
85 json::Value{to_string(ammAlice.ammAccount())},
86 std::nullopt,
87 XRP,
88 USD,
89 json::Value{42},
90 false,
91 3);
92 BEAST_EXPECT(jv[jss::error] == "actMalformed");
93 BEAST_EXPECT(jv[jss::error_code] == RpcActMalformed);
94 BEAST_EXPECT(jv[jss::error_message] == "Account malformed.");
95 });
96
98 invalidParams = {
99 {xrpIssue(), std::nullopt, TestAccount::None, false},
100 {std::nullopt, USD, TestAccount::None, false},
101 {xrpIssue(), std::nullopt, TestAccount::Alice, false},
102 {std::nullopt, USD, TestAccount::Alice, false},
103 {xrpIssue(), USD, TestAccount::Alice, false},
104 {std::nullopt, std::nullopt, TestAccount::None, true},
105 };
106
107 // Invalid parameters
108 testAMM([&](AMM& ammAlice, Env&) {
109 for (auto const& [iss1, iss2, acct, ignoreParams] : invalidParams)
110 {
111 auto const jv = ammAlice.ammRpcInfo(
112 std::nullopt,
113 std::nullopt,
114 iss1,
115 iss2,
116 accountId(ammAlice, acct),
117 ignoreParams);
118 BEAST_EXPECT(jv[jss::error] == "invalidParams");
119 BEAST_EXPECT(jv[jss::error_code] == RpcInvalidParams);
120 BEAST_EXPECT(jv[jss::error_message] == "Invalid parameters.");
121 }
122 });
123
124 // Invalid parameters *and* invalid LP account, default API version
125 testAMM([&](AMM& ammAlice, Env&) {
126 for (auto const& [iss1, iss2, acct, ignoreParams] : invalidParams)
127 {
128 auto const jv = ammAlice.ammRpcInfo(
129 bogie, //
130 std::nullopt,
131 iss1,
132 iss2,
133 accountId(ammAlice, acct),
134 ignoreParams);
135 BEAST_EXPECT(jv[jss::error] == "invalidParams");
136 BEAST_EXPECT(jv[jss::error_code] == RpcInvalidParams);
137 BEAST_EXPECT(jv[jss::error_message] == "Invalid parameters.");
138 }
139 });
140
141 // Invalid parameters *and* invalid LP account, API version 3
142 testAMM([&](AMM& ammAlice, Env&) {
143 for (auto const& [iss1, iss2, acct, ignoreParams] : invalidParams)
144 {
145 auto const jv = ammAlice.ammRpcInfo(
146 bogie, //
147 std::nullopt,
148 iss1,
149 iss2,
150 accountId(ammAlice, acct),
151 ignoreParams,
152 3);
153 BEAST_EXPECT(jv[jss::error] == "actMalformed");
154 BEAST_EXPECT(jv[jss::error_code] == RpcActMalformed);
155 BEAST_EXPECT(jv[jss::error_message] == "Account malformed.");
156 }
157 });
158
159 // Invalid AMM account id
160 testAMM([&](AMM& ammAlice, Env&) {
161 auto const jv = ammAlice.ammRpcInfo(
162 std::nullopt, std::nullopt, std::nullopt, std::nullopt, bogie.id());
163 BEAST_EXPECT(jv[jss::error] == "actMalformed");
164 BEAST_EXPECT(jv[jss::error_code] == RpcActMalformed);
165 BEAST_EXPECT(jv[jss::error_message] == "Account malformed.");
166 });
167
169 invalidParamsBadAccount = {
170 {xrpIssue(), std::nullopt, TestAccount::None, false},
171 {std::nullopt, USD, TestAccount::None, false},
172 {xrpIssue(), std::nullopt, TestAccount::Bogie, false},
173 {std::nullopt, USD, TestAccount::Bogie, false},
174 {xrpIssue(), USD, TestAccount::Bogie, false},
175 {std::nullopt, std::nullopt, TestAccount::None, true},
176 };
177
178 // Invalid parameters *and* invalid AMM account, default API version
179 testAMM([&](AMM& ammAlice, Env&) {
180 for (auto const& [iss1, iss2, acct, ignoreParams] : invalidParamsBadAccount)
181 {
182 auto const jv = ammAlice.ammRpcInfo(
183 std::nullopt,
184 std::nullopt,
185 iss1,
186 iss2,
187 accountId(ammAlice, acct),
188 ignoreParams);
189 BEAST_EXPECT(jv[jss::error] == "invalidParams");
190 BEAST_EXPECT(jv[jss::error_code] == RpcInvalidParams);
191 BEAST_EXPECT(jv[jss::error_message] == "Invalid parameters.");
192 }
193 });
194
195 // Invalid parameters *and* invalid AMM account, API version 3
196 testAMM([&](AMM& ammAlice, Env&) {
197 for (auto const& [iss1, iss2, acct, ignoreParams] : invalidParamsBadAccount)
198 {
199 auto const jv = ammAlice.ammRpcInfo(
200 std::nullopt,
201 std::nullopt,
202 iss1,
203 iss2,
204 accountId(ammAlice, acct),
205 ignoreParams,
206 3);
207 BEAST_EXPECT(
208 jv[jss::error] ==
209 (acct == TestAccount::Bogie ? std::string("actMalformed")
210 : std::string("invalidParams")));
211 BEAST_EXPECT(
212 jv[jss::error_code] ==
213 (acct == TestAccount::Bogie ? RpcActMalformed : RpcInvalidParams));
214 BEAST_EXPECT(
215 jv[jss::error_message] ==
216 (acct == TestAccount::Bogie ? std::string("Account malformed.")
217 : std::string("Invalid parameters.")));
218 }
219 });
220 }
221
222 void
224 {
225 testcase("RPC simple");
226
227 using namespace jtx;
228 testAMM([&](AMM& ammAlice, Env&) {
229 BEAST_EXPECT(ammAlice.expectAmmRpcInfo(
230 XRP(10000),
231 USD(10000),
232 IOUAmount{10000000, 0},
233 std::nullopt,
234 std::nullopt,
235 ammAlice.ammAccount()));
236 });
237
238 {
239 Env env{*this};
240 env.fund(XRP(1'000), gw_);
241 MPTTester mptTester(env, gw_, {.fund = false});
242 mptTester.create({.flags = tfMPTCanTransfer | tfMPTCanTrade});
243 MPTTester mptTester1(env, gw_, {.fund = false});
244 mptTester1.create({.flags = tfMPTCanTransfer | tfMPTCanTrade});
245 auto const mpt = mptTester["MPT"];
246 auto const mpt1 = mptTester1["MPT"];
248 {XRP(100), mpt(100), IOUAmount{100'000}},
249 {USD(100), mpt(100), IOUAmount{100}},
250 {mpt(100), mpt1(100), IOUAmount{100}},
251 };
252 for (auto& pool : pools)
253 {
254 AMM const amm(env, gw_, std::get<0>(pool), std::get<1>(pool));
255 BEAST_EXPECT(amm.expectAmmRpcInfo(
256 std::get<0>(pool),
257 std::get<1>(pool),
258 std::get<2>(pool),
259 std::nullopt,
260 std::nullopt,
261 amm.ammAccount()));
262 }
263 }
264 }
265
266 void
268 {
269 testcase("Vote and Bid");
270
271 using namespace jtx;
272 testAMM(
273 [&](AMM& ammAlice, Env& env) {
274 BEAST_EXPECT(
275 ammAlice.expectAmmRpcInfo(XRP(10000), USD(10000), IOUAmount{10000000, 0}));
277 votes.insert({alice_.human(), 0});
278 for (int i = 0; i < 7; ++i)
279 {
281 votes.insert({a.human(), 50 * (i + 1)});
282 if (!features[fixAMMv1_3])
283 {
284 fund(env, gw_, {a}, {USD(10000)}, Fund::Acct);
285 }
286 else
287 {
288 fund(env, gw_, {a}, {USD(10001)}, Fund::Acct);
289 }
290 ammAlice.deposit(a, 10000000);
291 ammAlice.vote(a, 50 * (i + 1));
292 }
293 BEAST_EXPECT(ammAlice.expectTradingFee(175));
294 Account ed("ed");
295 Account bill("bill");
296 env.fund(XRP(1000), bob_, ed, bill);
297 env(ammAlice.bid({.bidMin = 100, .authAccounts = {carol_, bob_, ed, bill}}));
298 if (!features[fixAMMv1_3])
299 {
300 BEAST_EXPECT(ammAlice.expectAmmRpcInfo(
301 XRP(80000),
302 USD(80000),
303 IOUAmount{79994400},
304 std::nullopt,
305 std::nullopt,
306 ammAlice.ammAccount()));
307 }
308 else
309 {
310 BEAST_EXPECT(ammAlice.expectAmmRpcInfo(
311 XRPAmount(80000000005),
312 STAmount{USD, UINT64_C(80'000'00000000005), -11},
313 IOUAmount{79994400},
314 std::nullopt,
315 std::nullopt,
316 ammAlice.ammAccount()));
317 }
318 for (auto i = 0; i < 2; ++i)
319 {
320 std::unordered_set<std::string> authAccounts = {
321 carol_.human(),
322 bob_.human(),
323 ed.human(),
324 bill.human(),
325 };
326 auto const ammInfo = i ? ammAlice.ammRpcInfo()
327 : ammAlice.ammRpcInfo(
328 std::nullopt,
329 std::nullopt,
330 std::nullopt,
331 std::nullopt,
332 ammAlice.ammAccount());
333 auto const& amm = ammInfo[jss::amm];
334 try
335 {
336 // votes
337 auto const voteSlots = amm[jss::vote_slots];
338 auto const votesCopy = votes;
339 for (std::uint8_t i = 0; i < 8; ++i)
340 {
341 if (!BEAST_EXPECT(
342 votes[voteSlots[i][jss::account].asString()] ==
343 voteSlots[i][jss::trading_fee].asUInt() &&
344 voteSlots[i][jss::vote_weight].asUInt() == 12500))
345 return;
346 votes.erase(voteSlots[i][jss::account].asString());
347 }
348 if (!BEAST_EXPECT(votes.empty()))
349 return;
350 votes = votesCopy;
351
352 // bid
353 auto const auctionSlot = amm[jss::auction_slot];
354 for (std::uint8_t i = 0; i < 4; ++i)
355 {
356 if (!BEAST_EXPECT(authAccounts.contains(
357 auctionSlot[jss::auth_accounts][i][jss::account].asString())))
358 return;
359 authAccounts.erase(
360 auctionSlot[jss::auth_accounts][i][jss::account].asString());
361 }
362 if (!BEAST_EXPECT(authAccounts.empty()))
363 return;
364 BEAST_EXPECT(
365 auctionSlot[jss::account].asString() == alice_.human() &&
366 auctionSlot[jss::discounted_fee].asUInt() == 17 &&
367 auctionSlot[jss::price][jss::value].asString() == "5600" &&
368 auctionSlot[jss::price][jss::currency].asString() ==
369 to_string(ammAlice.lptIssue().currency) &&
370 auctionSlot[jss::price][jss::issuer].asString() ==
371 to_string(ammAlice.lptIssue().account));
372 }
373 catch (std::exception const& e)
374 {
375 fail(e.what(), __FILE__, __LINE__);
376 }
377 }
378 },
379 std::nullopt,
380 0,
381 std::nullopt,
382 {features});
383 }
384
385 void
387 {
388 using namespace jtx;
389 testAMM([&](AMM& ammAlice, Env& env) {
390 env(fset(gw_, asfGlobalFreeze));
391 env.close();
392 auto const test = [&](bool freeze) {
393 auto const info = ammAlice.ammRpcInfo();
394 BEAST_EXPECT(info[jss::amm][jss::asset2_frozen].asBool() == freeze);
395 };
396 test(true);
397 env(fclear(gw_, asfGlobalFreeze));
398 env.close();
399 test(false);
400 });
401 }
402
403 void
405 {
406 using namespace jtx;
407 testcase("Invalid amm field");
408
409 testAMM([&](AMM& amm, Env&) {
410 auto const resp = amm.ammRpcInfo(
411 std::nullopt, jss::validated.cStr(), std::nullopt, std::nullopt, gw_);
412 BEAST_EXPECT(resp[jss::error] == "actNotFound");
413 BEAST_EXPECT(resp[jss::error_code] == RpcActNotFound);
414 BEAST_EXPECT(resp[jss::error_message] == "Account not found.");
415 });
416 }
417
418 void
419 run() override
420 {
421 using namespace jtx;
422 auto const all = testableAmendments();
423 testErrors();
425 testVoteAndBid(all);
426 testVoteAndBid(all - fixAMMv1_3);
427 testFreeze();
429 }
430};
431
433
434} // namespace xrpl::test
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition suite.h:554
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Represents a JSON value.
Definition json_value.h:121
Floating point representation of amounts with high dynamic range.
Definition IOUAmount.h:26
Currency currency
Definition Issue.h:20
AccountID account
Definition Issue.h:21
void run() override
Runs the suite.
void testVoteAndBid(FeatureBitset features)
jtx::Account const alice_
Definition AMMTest.h:87
void testAMM(std::function< void(jtx::AMM &, jtx::Env &)> const &cb, std::optional< std::pair< STAmount, STAmount > > const &pool=std::nullopt, std::uint16_t tfee=0, std::optional< jtx::Ter > const &ter=std::nullopt, std::vector< FeatureBitset > const &features={testableAmendments()})
testAMM() funds 30,000XRP and 30,000IOU for each non-XRP asset to Alice and Carol
Definition AMMTest.cpp:120
jtx::Account const gw_
Definition AMMTest.h:85
jtx::Account const carol_
Definition AMMTest.h:86
jtx::Account const bob_
Definition AMMTest.h:88
Convenience class to test AMM functionality.
bool expectTradingFee(std::uint16_t fee) const
Definition AMM.cpp:337
bool expectAmmRpcInfo(STAmount const &asset1, STAmount const &asset2, IOUAmount const &balance, std::optional< AccountID > const &account=std::nullopt, std::optional< std::string > const &ledgerIndex=std::nullopt, std::optional< AccountID > const &ammAccount=std::nullopt) const
Definition AMM.cpp:351
json::Value bid(BidArg const &arg)
Definition AMM.cpp:735
IOUAmount deposit(std::optional< Account > const &account, LPToken tokens, std::optional< STAmount > const &asset1InDetails=std::nullopt, std::optional< std::uint32_t > const &flags=std::nullopt, std::optional< Ter > const &ter=std::nullopt)
Definition AMM.cpp:463
json::Value ammRpcInfo(std::optional< AccountID > const &account=std::nullopt, std::optional< std::string > const &ledgerIndex=std::nullopt, std::optional< Asset > const &asset1=std::nullopt, std::optional< Asset > const &asset2=std::nullopt, std::optional< AccountID > const &ammAccount=std::nullopt, bool ignoreParams=false, unsigned apiVersion=rpc::kApiInvalidVersion) const
Send amm_info RPC command.
Definition AMM.cpp:183
AccountID const & ammAccount() const
void vote(std::optional< Account > const &account, std::uint32_t feeVal, std::optional< std::uint32_t > const &flags=std::nullopt, std::optional< jtx::Seq > const &seq=std::nullopt, std::optional< std::pair< Asset, Asset > > const &assets=std::nullopt, std::optional< Ter > const &ter=std::nullopt)
Definition AMM.cpp:709
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
std::string const & human() const
Returns the human readable public key.
AccountID id() const
Returns the Account ID.
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
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:323
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
T contains(T... args)
T empty(T... args)
T erase(T... args)
T insert(T... args)
The api_version numbers this server serves.
Definition ApiVersion.h:26
std::vector< STAmount > fund(jtx::Env &env, jtx::Account const &gw, std::vector< jtx::Account > const &accounts, std::vector< STAmount > const &amts, Fund how)
Definition AMMTest.cpp:34
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
json::Value fclear(Account const &account, std::uint32_t off)
Remove account flag.
Definition flags.h:110
FeatureBitset testableAmendments()
Definition Env.h:92
json::Value fset(Account const &account, std::uint32_t on, std::uint32_t off=0)
Add and/or remove flag.
Definition flags.cpp:15
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
constexpr XRPAmount
Convert XRP to drops (integral types).
Definition TxTest.h:53
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:108
@ RpcActNotFound
Definition ErrorCodes.h:53
@ RpcActMalformed
Definition ErrorCodes.h:73
@ RpcInvalidParams
Definition ErrorCodes.h:67
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
T to_string(T... args)
T what(T... args)