Skip to content

Commit f720257

Browse files
committed
Fix #164 and #163
1 parent eaff1ba commit f720257

31 files changed

+126
-285
lines changed

core/src/main/java/eu/bittrade/libs/steemj/SteemJ.java

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3636,11 +3636,12 @@ public void deletePostOrComment(AccountName postOrCommentAuthor, Permlink postOr
36363636
}
36373637

36383638
/**
3639-
* Transfer currency from default account to recipient. Amount is
3640-
* automatically converted from normalized representation to base
3639+
* Transfer the currency of your choice from
3640+
* {@link SteemJConfig#getDefaultAccount() DefaultAccount} to recipient.
3641+
* Amount is automatically converted from normalized representation to base
36413642
* representation. For example, to transfer 1.00 SBD to another account,
36423643
* simply use:
3643-
* <code>SteemJ.transfer(new AccountName("accountb"), AssetSymbolType.SBD, 1.0, "My memo");</code>
3644+
* <code>SteemJ.transfer(new AccountName("accountb"), new Asset(1.0, AssetSymbolType.SBD), "My memo");</code>
36443645
*
36453646
* <b>Attention</b>
36463647
* <ul>
@@ -3655,20 +3656,18 @@ public void deletePostOrComment(AccountName postOrCommentAuthor, Permlink postOr
36553656
* transfer from. If no default account has been provided, this method will
36563657
* throw an error. If you do not want to configure the following account as
36573658
* a default account, please use the
3658-
* {@link #transfer(AccountName, AccountName, AssetSymbolType, double, String)}
3659-
* method and provide the <code>from</code> account separately.</li>
3659+
* {@link #transfer(AccountName, AccountName, Asset, String)} method and
3660+
* provide the <code>from</code> account separately.</li>
36603661
* </ul>
36613662
*
36623663
* @param to
36633664
* The account name of the account the
36643665
* {@link SteemJConfig#getDefaultAccount() DefaultAccount} should
36653666
* transfer currency to.
3666-
* @param assetType
3667-
* Asset type, see
3668-
* {@link eu.bittrade.libs.steemj.enums.AssetSymbolType}.
36693667
* @param amount
3670-
* Normalized amount of asset to transfer. For example, use 1.0
3671-
* to transfer 1 SBD.
3668+
* An {@link Asset} object containing the Asset type (see
3669+
* {@link eu.bittrade.libs.steemj.enums.AssetSymbolType} and the
3670+
* amount to transfer.
36723671
* @param memo
36733672
* Message include with transfer (255 char max)
36743673
* @return The TransferOperation broadcast.
@@ -3692,13 +3691,13 @@ public void deletePostOrComment(AccountName postOrCommentAuthor, Permlink postOr
36923691
* If one of the provided parameters does not fulfill the
36933692
* requirements described above.
36943693
*/
3695-
public TransferOperation transfer(AccountName to, AssetSymbolType assetType, double amount, String memo)
3694+
public TransferOperation transfer(AccountName to, Asset amount, String memo)
36963695
throws SteemCommunicationException, SteemResponseException, SteemInvalidTransactionException {
36973696
if (SteemJConfig.getInstance().getDefaultAccount().isEmpty()) {
36983697
throw new InvalidParameterException(NO_DEFAULT_ACCOUNT_ERROR_MESSAGE);
36993698
}
37003699

3701-
return transfer(SteemJConfig.getInstance().getDefaultAccount(), to, assetType, amount, memo);
3700+
return transfer(SteemJConfig.getInstance().getDefaultAccount(), to, amount, memo);
37023701
}
37033702

37043703
/**
@@ -3719,11 +3718,10 @@ public TransferOperation transfer(AccountName to, AssetSymbolType assetType, dou
37193718
* The account from which to transfer currency.
37203719
* @param to
37213720
* The account to which to transfer currency.
3722-
* @param assetType
3723-
* Asset type, see
3724-
* {@link eu.bittrade.libs.steemj.enums.AssetSymbolType}.
37253721
* @param amount
3726-
* The amount to transfer.
3722+
* An {@link Asset} object containing the Asset type (see
3723+
* {@link eu.bittrade.libs.steemj.enums.AssetSymbolType} and the
3724+
* amount to transfer.
37273725
* @param memo
37283726
* Message include with transfer (255 char max)
37293727
* @return The TransferOperation broadcast.
@@ -3747,19 +3745,9 @@ public TransferOperation transfer(AccountName to, AssetSymbolType assetType, dou
37473745
* If one of the provided parameters does not fulfill the
37483746
* requirements described above.
37493747
*/
3750-
public TransferOperation transfer(AccountName from, AccountName to, AssetSymbolType assetType, double amount,
3751-
String memo) throws SteemCommunicationException, SteemResponseException, SteemInvalidTransactionException {
3752-
// Convert amount to long asset value. Conversion factor depends on
3753-
// asset type
3754-
Asset asset = new Asset();
3755-
asset.setSymbol(assetType);
3756-
asset.setAmount(Double.valueOf(amount * Math.pow(10.0, asset.getPrecision())).longValue());
3757-
if (!asset.toReal().equals(amount)) {
3758-
throw new IllegalStateException("Amount conversion mismatch: " + amount + " -> " + asset.toReal());
3759-
}
3760-
3761-
// Create & broadcast transfer operation
3762-
TransferOperation transferOperation = new TransferOperation(from, to, asset, memo);
3748+
public TransferOperation transfer(AccountName from, AccountName to, Asset amount, String memo)
3749+
throws SteemCommunicationException, SteemResponseException, SteemInvalidTransactionException {
3750+
TransferOperation transferOperation = new TransferOperation(from, to, amount, memo);
37633751
ArrayList<Operation> operations = new ArrayList<>();
37643752
operations.add(transferOperation);
37653753
DynamicGlobalProperty globalProperties = this.getDynamicGlobalProperties();

core/src/main/java/eu/bittrade/libs/steemj/base/models/Asset.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,18 @@ public class Asset implements ByteTransformable {
3333
private byte precision;
3434

3535
/**
36-
* Create an empty Asset object.
36+
* Create a new asset object by providing all required fields.
37+
*
38+
* @param amount
39+
* The amount.
40+
* @param symbol
41+
* One type of
42+
* {@link eu.bittrade.libs.steemj.enums.AssetSymbolType
43+
* AssetSymbolType}.
3744
*/
38-
public Asset() {
39-
45+
public Asset(double amount, AssetSymbolType symbol) {
46+
this.setSymbol(symbol);
47+
this.setAmount(amount);
4048
}
4149

4250
/**
@@ -50,8 +58,8 @@ public Asset() {
5058
* AssetSymbolType}.
5159
*/
5260
public Asset(long amount, AssetSymbolType symbol) {
53-
this.setAmount(amount);
5461
this.setSymbol(symbol);
62+
this.setAmount(amount);
5563
}
5664

5765
/**
@@ -92,6 +100,16 @@ public void setAmount(long amount) {
92100
this.amount = amount;
93101
}
94102

103+
/**
104+
* Set the amount of this asset.
105+
*
106+
* @param amount
107+
* The amount.
108+
*/
109+
public void setAmount(double amount) {
110+
this.amount = (long) (amount * Math.pow(10.0, this.getPrecision()));
111+
}
112+
95113
/**
96114
* Set the symbol of this asset.
97115
*

core/src/main/java/eu/bittrade/libs/steemj/base/models/deserializer/AssetDeserializer.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,15 @@ public Asset deserialize(JsonParser jasonParser, DeserializationContext deserial
2121
String[] assetFields = jasonParser.getText().split(" ");
2222

2323
if (assetFields.length == 2) {
24-
Asset asset = new Asset();
25-
// Set the symbol first which calculates the precision
26-
// internally.
27-
asset.setSymbol(AssetSymbolType.valueOf(assetFields[1]));
28-
// The amount is provided as a double value while we need a long
29-
// value for the byte representation so we transform the amount
30-
// into a long value here.
31-
Double assetAmount = Double.valueOf(assetFields[0]) * Math.pow(10.0, asset.getPrecision());
32-
asset.setAmount(assetAmount.longValue());
33-
return asset;
24+
/*
25+
* Set the symbol first which calculates the precision
26+
* internally.
27+
*
28+
* The amount is provided as a double value while we need a long
29+
* value for the byte representation so we transform the amount
30+
* into a long value here.
31+
*/
32+
return new Asset(Double.valueOf(assetFields[0]), AssetSymbolType.valueOf(assetFields[1]));
3433
}
3534
}
3635

core/src/test/java/eu/bittrade/libs/steemj/SteemJIT.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,7 @@ public void testVerifyAuthority() throws Exception {
461461

462462
config.getPrivateKeyStorage().addAccount(new AccountName("dez1337"), privateKeys);
463463

464-
Asset steemAmount = new Asset();
465-
steemAmount.setAmount(1L);
466-
steemAmount.setSymbol(AssetSymbolType.STEEM);
464+
Asset steemAmount = new Asset(1L, AssetSymbolType.STEEM);
467465

468466
AccountName from = new AccountName("dez1337");
469467
AccountName to = new AccountName("steemj");

core/src/test/java/eu/bittrade/libs/steemj/base/models/AssetTest.java

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ public class AssetTest {
3434
*/
3535
@Test
3636
public void testVestsAssetToByteArray() throws Exception {
37-
Asset vestsAsset = new Asset();
38-
vestsAsset.setSymbol(AssetSymbolType.VESTS);
39-
vestsAsset.setAmount(1230);
37+
Asset vestsAsset = new Asset(1230, AssetSymbolType.VESTS);
4038

4139
assertThat(vestsAsset.getPrecision(), equalTo(6));
4240
assertThat("Expect that the asset object has the given byte representation.",
@@ -52,9 +50,7 @@ public void testVestsAssetToByteArray() throws Exception {
5250
*/
5351
@Test
5452
public void testSteemAssetToByteArray() throws Exception {
55-
Asset steemAsset = new Asset();
56-
steemAsset.setSymbol(AssetSymbolType.STEEM);
57-
steemAsset.setAmount(123000);
53+
Asset steemAsset = new Asset(123000, AssetSymbolType.STEEM);
5854

5955
assertThat(steemAsset.getPrecision(), equalTo(3));
6056
assertThat("Expect that the asset object has the given byte representation.",
@@ -70,9 +66,7 @@ public void testSteemAssetToByteArray() throws Exception {
7066
*/
7167
@Test
7268
public void testSbdAssetToByteArray() throws Exception {
73-
Asset sbdAsset = new Asset();
74-
sbdAsset.setSymbol(AssetSymbolType.SBD);
75-
sbdAsset.setAmount(123);
69+
Asset sbdAsset = new Asset(123, AssetSymbolType.SBD);
7670

7771
assertThat(sbdAsset.getPrecision(), equalTo(3));
7872
assertThat("Expect that the asset object has the given byte representation.",
@@ -88,9 +82,7 @@ public void testSbdAssetToByteArray() throws Exception {
8882
*/
8983
@Test
9084
public void testStmdAssetToByteArray() throws Exception {
91-
Asset sbdAsset = new Asset();
92-
sbdAsset.setSymbol(AssetSymbolType.STMD);
93-
sbdAsset.setAmount(56844);
85+
Asset sbdAsset = new Asset(56844, AssetSymbolType.STMD);
9486

9587
assertThat(sbdAsset.getPrecision(), equalTo(3));
9688
assertThat("Expect that the asset object has the given byte representation.",
@@ -106,9 +98,7 @@ public void testStmdAssetToByteArray() throws Exception {
10698
*/
10799
@Test
108100
public void testTestsAssetToByteArray() throws Exception {
109-
Asset sbdAsset = new Asset();
110-
sbdAsset.setSymbol(AssetSymbolType.TESTS);
111-
sbdAsset.setAmount(36741);
101+
Asset sbdAsset = new Asset(36741, AssetSymbolType.TESTS);
112102

113103
assertThat(sbdAsset.getPrecision(), equalTo(3));
114104
assertThat("Expect that the asset object has the given byte representation.",
@@ -124,9 +114,7 @@ public void testTestsAssetToByteArray() throws Exception {
124114
*/
125115
@Test
126116
public void testTbdAssetToByteArray() throws Exception {
127-
Asset sbdAsset = new Asset();
128-
sbdAsset.setSymbol(AssetSymbolType.TBD);
129-
sbdAsset.setAmount(4547);
117+
Asset sbdAsset = new Asset(4547, AssetSymbolType.TBD);
130118

131119
assertThat(sbdAsset.getPrecision(), equalTo(3));
132120
assertThat("Expect that the asset object has the given byte representation.",
@@ -142,9 +130,7 @@ public void testTbdAssetToByteArray() throws Exception {
142130
*/
143131
@Test
144132
public void testTstdAssetToByteArray() throws Exception {
145-
Asset sbdAsset = new Asset();
146-
sbdAsset.setSymbol(AssetSymbolType.TSTD);
147-
sbdAsset.setAmount(78945214);
133+
Asset sbdAsset = new Asset(78945214, AssetSymbolType.TSTD);
148134

149135
assertThat(sbdAsset.getPrecision(), equalTo(3));
150136
assertThat("Expect that the asset object has the given byte representation.",
@@ -157,18 +143,11 @@ public void testTstdAssetToByteArray() throws Exception {
157143
*/
158144
@Test
159145
public void testAssetEqualsMethod() {
146+
Asset asset = new Asset(115, AssetSymbolType.SBD);
160147

161-
Asset asset = new Asset();
162-
asset.setAmount(115);
163-
asset.setSymbol(AssetSymbolType.SBD);
148+
Asset sameAsset = new Asset(0.115, AssetSymbolType.SBD);
164149

165-
Asset sameAsset = new Asset();
166-
sameAsset.setAmount(115);
167-
sameAsset.setSymbol(AssetSymbolType.SBD);
168-
169-
Asset differentAsset = new Asset();
170-
differentAsset.setAmount(100);
171-
differentAsset.setSymbol(AssetSymbolType.STEEM);
150+
Asset differentAsset = new Asset(100, AssetSymbolType.STEEM);
172151

173152
assertThat(asset.equals(sameAsset), equalTo(true));
174153
assertThat(sameAsset.equals(differentAsset), equalTo(false));

core/src/test/java/eu/bittrade/libs/steemj/base/models/ChainPropertiesTest.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ public class ChainPropertiesTest {
2626
*/
2727
@Test
2828
public void testSteemChainPropertiesToByteArray() throws Exception {
29-
Asset accountCreationFee = new Asset();
30-
accountCreationFee.setAmount(5000);
31-
accountCreationFee.setSymbol(AssetSymbolType.STEEM);
29+
Asset accountCreationFee = new Asset(5000, AssetSymbolType.STEEM);
3230

3331
long maximumBlockSize = 65536;
3432
int sbdInterestRate = 0;
@@ -46,9 +44,7 @@ public void testSteemChainPropertiesToByteArray() throws Exception {
4644
*/
4745
@Test(expected = InvalidParameterException.class)
4846
public void testChainPropertiesValidationInterestRate() {
49-
Asset accountCreationFee = new Asset();
50-
accountCreationFee.setAmount(5000);
51-
accountCreationFee.setSymbol(AssetSymbolType.STEEM);
47+
Asset accountCreationFee = new Asset(5000, AssetSymbolType.STEEM);
5248

5349
long maximumBlockSize = 65570;
5450
int sbdInterestRate = -1;
@@ -62,9 +58,7 @@ public void testChainPropertiesValidationInterestRate() {
6258
*/
6359
@Test(expected = InvalidParameterException.class)
6460
public void testChainPropertiesValidationBlockSize() {
65-
Asset accountCreationFee = new Asset();
66-
accountCreationFee.setAmount(5000);
67-
accountCreationFee.setSymbol(AssetSymbolType.STEEM);
61+
Asset accountCreationFee = new Asset(5000, AssetSymbolType.STEEM);
6862

6963
long maximumBlockSize = 5215;
7064
int sbdInterestRate = 0;
@@ -78,9 +72,7 @@ public void testChainPropertiesValidationBlockSize() {
7872
*/
7973
@Test(expected = InvalidParameterException.class)
8074
public void testChainPropertiesValidationAccountCreationFee() {
81-
Asset accountCreationFee = new Asset();
82-
accountCreationFee.setAmount(-1);
83-
accountCreationFee.setSymbol(AssetSymbolType.STEEM);
75+
Asset accountCreationFee = new Asset(5000, AssetSymbolType.STEEM);
8476

8577
long maximumBlockSize = 65535;
8678
int sbdInterestRate = 5;
@@ -99,9 +91,7 @@ public void testChainPropertiesValidation() {
9991
new AccountName("dez");
10092
new AccountName("dez1337-steemj");
10193

102-
Asset accountCreationFee = new Asset();
103-
accountCreationFee.setAmount(3);
104-
accountCreationFee.setSymbol(AssetSymbolType.STEEM);
94+
Asset accountCreationFee = new Asset(3, AssetSymbolType.STEEM);
10595

10696
long maximumBlockSize = 65536;
10797
int sbdInterestRate = 5;

0 commit comments

Comments
 (0)