rippled
Loading...
Searching...
No Matches
AMMVoteTests.cpp
1// Auto-generated unit tests for transaction AMMVote
2
3
4#include <gtest/gtest.h>
5
6#include <protocol_autogen/TestHelpers.h>
7
8#include <xrpl/protocol/SecretKey.h>
9#include <xrpl/protocol/Seed.h>
10#include <xrpl/protocol/STTx.h>
11#include <xrpl/protocol_autogen/transactions/AMMVote.h>
12#include <xrpl/protocol_autogen/transactions/AccountSet.h>
13
14#include <string>
15
16namespace xrpl::transactions {
17
18// 1 & 4) Set fields via builder setters, build, then read them back via
19// wrapper getters. After build(), validate() should succeed.
20TEST(TransactionsAMMVoteTests, BuilderSettersRoundTrip)
21{
22 // Generate a deterministic keypair for signing
23 auto const [publicKey, secretKey] =
25
26 // Common transaction fields
27 auto const accountValue = calcAccountID(publicKey);
28 std::uint32_t const sequenceValue = 1;
29 auto const feeValue = canonical_AMOUNT();
30
31 // Transaction-specific field values
32 auto const assetValue = canonical_ISSUE();
33 auto const asset2Value = canonical_ISSUE();
34 auto const tradingFeeValue = canonical_UINT16();
35
36 AMMVoteBuilder builder{
37 accountValue,
38 assetValue,
39 asset2Value,
40 tradingFeeValue,
41 sequenceValue,
42 feeValue
43 };
44
45 // Set optional fields
46
47 auto tx = builder.build(publicKey, secretKey);
48
49 std::string reason;
50 EXPECT_TRUE(tx.validate(reason)) << reason;
51
52 // Verify signing was applied
53 EXPECT_FALSE(tx.getSigningPubKey().empty());
54 EXPECT_TRUE(tx.hasTxnSignature());
55
56 // Verify common fields
57 EXPECT_EQ(tx.getAccount(), accountValue);
58 EXPECT_EQ(tx.getSequence(), sequenceValue);
59 EXPECT_EQ(tx.getFee(), feeValue);
60
61 // Verify required fields
62 {
63 auto const& expected = assetValue;
64 auto const actual = tx.getAsset();
65 expectEqualField(expected, actual, "sfAsset");
66 }
67
68 {
69 auto const& expected = asset2Value;
70 auto const actual = tx.getAsset2();
71 expectEqualField(expected, actual, "sfAsset2");
72 }
73
74 {
75 auto const& expected = tradingFeeValue;
76 auto const actual = tx.getTradingFee();
77 expectEqualField(expected, actual, "sfTradingFee");
78 }
79
80 // Verify optional fields
81}
82
83// 2 & 4) Start from an STTx, construct a builder from it, build a new wrapper,
84// and verify all fields match.
85TEST(TransactionsAMMVoteTests, BuilderFromStTxRoundTrip)
86{
87 // Generate a deterministic keypair for signing
88 auto const [publicKey, secretKey] =
89 generateKeyPair(KeyType::secp256k1, generateSeed("testAMMVoteFromTx"));
90
91 // Common transaction fields
92 auto const accountValue = calcAccountID(publicKey);
93 std::uint32_t const sequenceValue = 2;
94 auto const feeValue = canonical_AMOUNT();
95
96 // Transaction-specific field values
97 auto const assetValue = canonical_ISSUE();
98 auto const asset2Value = canonical_ISSUE();
99 auto const tradingFeeValue = canonical_UINT16();
100
101 // Build an initial transaction
102 AMMVoteBuilder initialBuilder{
103 accountValue,
104 assetValue,
105 asset2Value,
106 tradingFeeValue,
107 sequenceValue,
108 feeValue
109 };
110
111
112 auto initialTx = initialBuilder.build(publicKey, secretKey);
113
114 // Create builder from existing STTx
115 AMMVoteBuilder builderFromTx{initialTx.getSTTx()};
116
117 auto rebuiltTx = builderFromTx.build(publicKey, secretKey);
118
119 std::string reason;
120 EXPECT_TRUE(rebuiltTx.validate(reason)) << reason;
121
122 // Verify common fields
123 EXPECT_EQ(rebuiltTx.getAccount(), accountValue);
124 EXPECT_EQ(rebuiltTx.getSequence(), sequenceValue);
125 EXPECT_EQ(rebuiltTx.getFee(), feeValue);
126
127 // Verify required fields
128 {
129 auto const& expected = assetValue;
130 auto const actual = rebuiltTx.getAsset();
131 expectEqualField(expected, actual, "sfAsset");
132 }
133
134 {
135 auto const& expected = asset2Value;
136 auto const actual = rebuiltTx.getAsset2();
137 expectEqualField(expected, actual, "sfAsset2");
138 }
139
140 {
141 auto const& expected = tradingFeeValue;
142 auto const actual = rebuiltTx.getTradingFee();
143 expectEqualField(expected, actual, "sfTradingFee");
144 }
145
146 // Verify optional fields
147}
148
149// 3) Verify wrapper throws when constructed from wrong transaction type.
150TEST(TransactionsAMMVoteTests, WrapperThrowsOnWrongTxType)
151{
152 // Build a valid transaction of a different type
153 auto const [pk, sk] =
155 auto const account = calcAccountID(pk);
156
157 AccountSetBuilder wrongBuilder{account, 1, canonical_AMOUNT()};
158 auto wrongTx = wrongBuilder.build(pk, sk);
159
160 EXPECT_THROW(AMMVote{wrongTx.getSTTx()}, std::runtime_error);
161}
162
163// 4) Verify builder throws when constructed from wrong transaction type.
164TEST(TransactionsAMMVoteTests, BuilderThrowsOnWrongTxType)
165{
166 // Build a valid transaction of a different type
167 auto const [pk, sk] =
168 generateKeyPair(KeyType::secp256k1, generateSeed("testWrongTypeBuilder"));
169 auto const account = calcAccountID(pk);
170
171 AccountSetBuilder wrongBuilder{account, 1, canonical_AMOUNT()};
172 auto wrongTx = wrongBuilder.build(pk, sk);
173
174 EXPECT_THROW(AMMVoteBuilder{wrongTx.getSTTx()}, std::runtime_error);
175}
176
177
178}
AMMVote build(PublicKey const &publicKey, SecretKey const &secretKey)
Build and return the AMMVote wrapper.
std::shared_ptr< STTx const > getSTTx() const
Get the underlying STTx object.
TEST(TransactionsAccountDeleteTests, BuilderSettersRoundTrip)
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Definition Seed.cpp:57
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
AccountID calcAccountID(PublicKey const &pk)
void expectEqualField(T const &expected, T const &actual, char const *fieldName)