Skip to content

Commit b305977

Browse files
authored
chore(builder): improvements [arduino] (#217)
1 parent d42acb6 commit b305977

15 files changed

+42
-37
lines changed

CHANGELOG.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010
### Added
1111
- added Delegate Resignation Builder ([#208])
1212

13+
### Changed
14+
- improved transaction builder ([#217])
15+
1316
## [1.0.0-arduino] - 2020-02-13
1417

18+
### Added
19+
- added AIP-11 support for Core v.2.6 Transactions ([#198])
20+
1521
### Changed
1622
- removed use of monolithic `arkCrypto.h` header ([#190])
1723
- break up unit tests to support platforms with limited RAM ([#172])
1824

19-
### Added
20-
- added AIP-11 support for Core v.2.6 Transactions ([#198])
21-
2225
### Fixed
2326
- fixed `transaction::to_json` tests on ESP8266 ([#180])
2427
- fixed `transaction::to_array` tests on ESP8266 ([#178])
@@ -112,4 +115,5 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
112115
[1.0.0-arduino]: https://github.com/ArkEcosystem/cpp-crypto/compare/0.7.0-arduino...1.0.0-arduino
113116
[1.0.0]: https://github.com/ArkEcosystem/cpp-crypto/compare/0.7.0-arduino...1.0.0
114117
[#208]: https://github.com/ArkEcosystem/cpp-crypto/pull/208
118+
[#217]: https://github.com/ArkEcosystem/cpp-crypto/pull/217
115119
[unreleased]: https://github.com/ArkEcosystem/cpp-crypto/compare/1.0.0-arduino...develop

examples/ESP32/ESP32.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,12 @@ void createBridgechainTransaction() {
181181
const Configuration cfg(BridgechainNetwork);
182182

183183
// Use the Transaction Builder to make a transaction.
184-
const auto bridgechainTransaction = builder::Transfer()
184+
const auto bridgechainTransaction = builder::Transfer(cfg)
185185
.recipientId("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib")
186186
.vendorField("this is a custom bridgechain transaction")
187187
.sign(Passphrase)
188188
.secondSign(SecondPassphrase)
189-
.build(cfg);
189+
.build();
190190

191191
// Create and Print the Json representation of the Transaction.
192192
const auto transactionJson = bridgechainTransaction.toJson();

examples/ESP8266/ESP8266.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,11 @@ void createBridgechainTransaction() {
160160
const Configuration cfg(BridgechainNetwork);
161161

162162
// Use the Transaction Builder to make a transaction.
163-
const auto bridgechainTransaction = builder::Transfer()
163+
const auto bridgechainTransaction = builder::Transfer(cfg)
164164
.recipientId("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib")
165165
.vendorField("this is a custom bridgechain transaction")
166166
.sign(Passphrase)
167-
.build(cfg);
167+
.build();
168168

169169
// Create and Print the Json representation of the Transaction.
170170
const auto transactionJson = bridgechainTransaction.toJson();

src/common/configuration.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class Configuration : public managers::NetworkManager,
2424
////////////////////////////////////////////////////////////////////////////
2525
// Default initialization: ARK Devnet w/StaticFees
2626
Configuration() = default;
27-
explicit Configuration(const Network &network);
28-
explicit Configuration(const FeePolicy &policy);
27+
Configuration(const Network &network);
28+
Configuration(const FeePolicy &policy);
2929
Configuration(const Network &network, const FeePolicy &policy);
3030

3131
////////////////////////////////////////////////////////////////////////////

src/transactions/builders/common.hpp

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -164,28 +164,19 @@ template<class T> class Common {
164164
}
165165

166166
////////////////////////////////////////////////////////////////////////////
167-
// Build
168-
//
169-
// If calling 'builder::sign()' before calling 'build()',
170-
// ensure the Fees and Network are properly configured first.
171-
//
172-
// ---
173-
Transaction &build(const Configuration &config = {}) {
174-
// if the transaction fee is the default of '0', use the config fees.
175-
if (this->transaction.data.fee == 0ULL) {
176-
this->transaction.data.fee = config.getFee(this->transaction.data.type);
177-
}
178-
179-
// Use the configuration network version if it's different.
180-
if (this->transaction.data.network == Devnet.version &&
181-
config.getNetwork().version != Devnet.version) {
182-
this->transaction.data.network = config.getNetwork().version;
183-
}
184-
167+
// Finish the build pattern
168+
// !! should always be the last call !!
169+
Transaction &build() {
185170
return this->transaction;
186171
}
187172

188173
protected:
174+
////////////////////////////////////////////////////////////////////////////
175+
void configure(const Configuration &config) {
176+
this->transaction.data.fee = config.getFee(this->transaction.data.type);
177+
this->transaction.data.network = config.getNetwork().version;
178+
}
179+
189180
////////////////////////////////////////////////////////////////////////////
190181
Transaction transaction;
191182
};

src/transactions/builders/delegate_registration.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ class DelegateRegistration : public Common<DelegateRegistration> {
6363
}
6464

6565
////////////////////////////////////////////////////////////////////////////
66-
DelegateRegistration() {
66+
DelegateRegistration(const Configuration &config = {}) {
6767
this->transaction.data.type = DELEGATE_REGISTRATION_TYPE;
68+
this->configure(config);
6869
}
6970
};
7071

src/transactions/builders/delegate_resignation.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ class DelegateResignation;
2424
////////////////////////////////////////////////////////////////////////////////
2525
class DelegateResignation : public Common<DelegateResignation> {
2626
public:
27-
DelegateResignation() {
27+
DelegateResignation(const Configuration &config = {}) {
2828
this->transaction.data.type = DELEGATE_RESIGNATION_TYPE;
29+
this->configure(config);
2930
}
3031
};
3132

src/transactions/builders/htlc_claim.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ class HtlcClaim : public Common<HtlcClaim> {
5050
}
5151

5252
////////////////////////////////////////////////////////////////////////////
53-
HtlcClaim() {
53+
HtlcClaim(const Configuration &config = {}) {
5454
this->transaction.data.type = HTLC_CLAIM_TYPE;
55+
this->configure(config);
5556
}
5657
};
5758

src/transactions/builders/htlc_lock.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ class HtlcLock : public Common<HtlcLock> {
8888
}
8989

9090
////////////////////////////////////////////////////////////////////////////
91-
HtlcLock() {
91+
HtlcLock(const Configuration &config = {}) {
9292
this->transaction.data.type = HTLC_LOCK_TYPE;
93+
this->configure(config);
9394
}
9495
};
9596

src/transactions/builders/htlc_refund.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ class HtlcRefund : public Common<HtlcRefund> {
4040
}
4141

4242
////////////////////////////////////////////////////////////////////////////
43-
HtlcRefund() {
43+
HtlcRefund(const Configuration &config = {}) {
4444
this->transaction.data.type = HTLC_REFUND_TYPE;
45+
this->configure(config);
4546
}
4647
};
4748

src/transactions/builders/ipfs.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ class Ipfs : public Common<Ipfs> {
4040
}
4141

4242
////////////////////////////////////////////////////////////////////////////
43-
Ipfs() {
43+
Ipfs(const Configuration &config = {}) {
4444
this->transaction.data.type = IPFS_TYPE;
45+
this->configure(config);
4546
}
4647
};
4748

src/transactions/builders/multi_payment.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ class MultiPayment : public Common<MultiPayment> {
6767
}
6868

6969
////////////////////////////////////////////////////////////////////////////
70-
MultiPayment() {
70+
MultiPayment(const Configuration &config = {}) {
7171
this->transaction.data.type = MULTI_PAYMENT_TYPE;
72+
this->configure(config);
7273
}
7374
};
7475

src/transactions/builders/second_signature.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ class SecondSignature : public Common<SecondSignature> {
4242
}
4343

4444
////////////////////////////////////////////////////////////////////////////
45-
SecondSignature() {
45+
SecondSignature(const Configuration &config = {}) {
4646
this->transaction.data.type = SECOND_SIGNATURE_TYPE;
47+
this->configure(config);
4748
}
4849
};
4950

src/transactions/builders/transfer.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ class Transfer : public Common<Transfer> {
7272
}
7373

7474
////////////////////////////////////////////////////////////////////////////
75-
Transfer() {
75+
Transfer(const Configuration &config = {}) {
7676
this->transaction.data.type = TRANSFER_TYPE;
77+
this->configure(config);
7778
}
7879
};
7980

src/transactions/builders/vote.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ class Vote : public Common<Vote> {
4242
}
4343

4444
////////////////////////////////////////////////////////////////////////////
45-
Vote() {
45+
Vote(const Configuration &config = {}) {
4646
this->transaction.data.type = VOTE_TYPE;
47+
this->configure(config);
4748
}
4849
};
4950

0 commit comments

Comments
 (0)