Skip to content
This repository has been archived by the owner on Feb 6, 2019. It is now read-only.

Commit

Permalink
Amount parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekn committed Dec 3, 2015
1 parent e377c49 commit 51f7bf8
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 98 deletions.
21 changes: 11 additions & 10 deletions src/main/java/org/stellar/base/ChangeTrustOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
public class ChangeTrustOperation extends Operation {

private final Asset asset;
private final Long limit;
private final String limit;

private ChangeTrustOperation(Asset asset, Long limit) {
private ChangeTrustOperation(Asset asset, String limit) {
this.asset = checkNotNull(asset, "asset cannot be null");
this.limit = checkNotNull(limit, "limit cannot be null");
}
Expand All @@ -30,7 +30,7 @@ public Asset getAsset() {
/**
* The limit of the trustline. For example, if a gateway extends a trustline of up to 200 USD to a user, the limit is 200.
*/
public long getLimit() {
public String getLimit() {
return limit;
}

Expand All @@ -39,7 +39,7 @@ org.stellar.base.xdr.Operation.OperationBody toOperationBody() {
ChangeTrustOp op = new ChangeTrustOp();
op.setLine(asset.toXdr());
Int64 limit = new Int64();
limit.setInt64(this.limit);
limit.setInt64(Operation.toXdrAmount(this.limit));
op.setLimit(limit);

org.stellar.base.xdr.Operation.OperationBody body = new org.stellar.base.xdr.Operation.OperationBody();
Expand All @@ -54,23 +54,24 @@ org.stellar.base.xdr.Operation.OperationBody toOperationBody() {
*/
public static class Builder {
private final Asset asset;
private final Long limit;
private final String limit;

private Keypair mSourceAccount;

Builder(ChangeTrustOp op) {
asset = Asset.fromXdr(op.getLine());
limit = op.getLimit().getInt64();
limit = Operation.fromXdrAmount(op.getLimit().getInt64().longValue());
}

/**
* Creates a new ChangeTrust builder.
* @param asset The asset of the trustline. For example, if a gateway extends a trustline of up to 200 USD to a user, the line is USD.
* @param limit The limit of the trustline. For example, if a gateway extends a trustline of up to 200 USD to a user, the limit is 200.
* @throws ArithmeticException when limit has more than 7 decimal places.
*/
public Builder(Asset asset, Long limit) {
this.asset = asset;
this.limit = limit;
public Builder(Asset asset, String limit) {
this.asset = checkNotNull(asset, "asset cannot be null");
this.limit = checkNotNull(limit, "limit cannot be null");
}

/**
Expand All @@ -79,7 +80,7 @@ public Builder(Asset asset, Long limit) {
* @return Builder object so you can chain methods.
*/
public Builder setSourceAccount(Keypair sourceAccount) {
mSourceAccount = sourceAccount;
mSourceAccount = checkNotNull(sourceAccount, "sourceAccount cannot be null");
return this;
}

Expand Down
17 changes: 9 additions & 8 deletions src/main/java/org/stellar/base/CreateAccountOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
public class CreateAccountOperation extends Operation {

private final Keypair destination;
private final Long startingBalance;
private final String startingBalance;

private CreateAccountOperation(Keypair destination, Long startingBalance) {
private CreateAccountOperation(Keypair destination, String startingBalance) {
this.destination = checkNotNull(destination, "destination cannot be null");
this.startingBalance = checkNotNull(startingBalance, "startingBalance cannot be null");
}

/**
* Amount of XLM to send to the newly created account.
*/
public long getStartingBalance() {
public String getStartingBalance() {
return startingBalance;
}

Expand All @@ -42,7 +42,7 @@ org.stellar.base.xdr.Operation.OperationBody toOperationBody() {
destination.setAccountID(this.destination.getXdrPublicKey());
op.setDestination(destination);
Int64 startingBalance = new Int64();
startingBalance.setInt64(Long.valueOf(this.startingBalance));
startingBalance.setInt64(Operation.toXdrAmount(this.startingBalance));
op.setStartingBalance(startingBalance);

org.stellar.base.xdr.Operation.OperationBody body = new org.stellar.base.xdr.Operation.OperationBody();
Expand All @@ -57,7 +57,7 @@ org.stellar.base.xdr.Operation.OperationBody toOperationBody() {
*/
public static class Builder {
private final Keypair destination;
private final long startingBalance;
private final String startingBalance;

private Keypair mSourceAccount;

Expand All @@ -67,15 +67,16 @@ public static class Builder {
*/
Builder(CreateAccountOp op) {
destination = Keypair.fromXdrPublicKey(op.getDestination().getAccountID());
startingBalance = op.getStartingBalance().getInt64().longValue();
startingBalance = Operation.fromXdrAmount(op.getStartingBalance().getInt64().longValue());
}

/**
* Creates a new CreateAccount builder.
* @param destination The destination keypair (uses only the public key).
* @param startingBalance The initial balance to start with.
* @param startingBalance The initial balance to start with in lumens.
* @throws ArithmeticException when startingBalance has more than 7 decimal places.
*/
public Builder(Keypair destination, long startingBalance) {
public Builder(Keypair destination, String startingBalance) {
this.destination = destination;
this.startingBalance = startingBalance;
}
Expand Down
25 changes: 13 additions & 12 deletions src/main/java/org/stellar/base/CreatePassiveOfferOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
public class CreatePassiveOfferOperation extends Operation {
private final Asset selling;
private final Asset buying;
private final Long amount;
private final String amount;
private final String price;

private CreatePassiveOfferOperation(Asset selling, Asset buying, Long amount, String price) {
private CreatePassiveOfferOperation(Asset selling, Asset buying, String amount, String price) {
this.selling = checkNotNull(selling, "selling cannot be null");
this.buying = checkNotNull(buying, "buying cannot be null");
this.amount = checkNotNull(amount, "amount cannot be null");
Expand All @@ -42,7 +42,7 @@ public Asset getBuying() {
/**
* Amount of selling being sold.
*/
public long getAmount() {
public String getAmount() {
return amount;
}

Expand All @@ -59,7 +59,7 @@ org.stellar.base.xdr.Operation.OperationBody toOperationBody() {
op.setSelling(selling.toXdr());
op.setBuying(buying.toXdr());
Int64 amount = new Int64();
amount.setInt64(Long.valueOf(this.amount));
amount.setInt64(Operation.toXdrAmount(this.amount));
op.setAmount(amount);
Price price = Price.fromString(this.price);
op.setPrice(price.toXdr());
Expand All @@ -79,7 +79,7 @@ public static class Builder {

private final Asset selling;
private final Asset buying;
private final long amount;
private final String amount;
private final String price;

private Keypair mSourceAccount;
Expand All @@ -91,7 +91,7 @@ public static class Builder {
Builder(CreatePassiveOfferOp op) {
selling = Asset.fromXdr(op.getSelling());
buying = Asset.fromXdr(op.getBuying());
amount = op.getAmount().getInt64().longValue();
amount = Operation.fromXdrAmount(op.getAmount().getInt64().longValue());
int n = op.getPrice().getN().getInt32().intValue();
int d = op.getPrice().getD().getInt32().intValue();
price = new BigDecimal(n).divide(new BigDecimal(d)).toString();
Expand All @@ -103,12 +103,13 @@ public static class Builder {
* @param buying The asset being bought in this operation
* @param amount Amount of selling being sold.
* @param price Price of 1 unit of selling in terms of buying.
* @throws ArithmeticException when amount has more than 7 decimal places.
*/
public Builder(Asset selling, Asset buying, long amount, String price) {
this.selling = selling;
this.buying = buying;
this.amount = amount;
this.price = price;
public Builder(Asset selling, Asset buying, String amount, String price) {
this.selling = checkNotNull(selling, "selling cannot be null");
this.buying = checkNotNull(buying, "buying cannot be null");
this.amount = checkNotNull(amount, "amount cannot be null");
this.price = checkNotNull(price, "price cannot be null");
}

/**
Expand All @@ -117,7 +118,7 @@ public Builder(Asset selling, Asset buying, long amount, String price) {
* @return Builder object so you can chain methods.
*/
public Builder setSourceAccount(Keypair sourceAccount) {
mSourceAccount = sourceAccount;
mSourceAccount = checkNotNull(sourceAccount, "sourceAccount cannot be null");
return this;
}

Expand Down
29 changes: 15 additions & 14 deletions src/main/java/org/stellar/base/ManagerOfferOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public class ManagerOfferOperation extends Operation {

private final Asset selling;
private final Asset buying;
private final Long amount;
private final String amount;
private final String price;
private final long offerId;

private ManagerOfferOperation(Asset selling, Asset buying, Long amount, String price, long offerId) {
private ManagerOfferOperation(Asset selling, Asset buying, String amount, String price, long offerId) {
this.selling = checkNotNull(selling, "selling cannot be null");
this.buying = checkNotNull(buying, "buying cannot be null");
this.amount = checkNotNull(amount, "amount cannot be null");
Expand All @@ -48,7 +48,7 @@ public Asset getBuying() {
/**
* Amount of selling being sold.
*/
public long getAmount() {
public String getAmount() {
return amount;
}

Expand All @@ -72,7 +72,7 @@ org.stellar.base.xdr.Operation.OperationBody toOperationBody() {
op.setSelling(selling.toXdr());
op.setBuying(buying.toXdr());
Int64 amount = new Int64();
amount.setInt64(Long.valueOf(this.amount));
amount.setInt64(Operation.toXdrAmount(this.amount));
op.setAmount(amount);
Price price = Price.fromString(this.price);
op.setPrice(price.toXdr());
Expand All @@ -95,7 +95,7 @@ public static class Builder {

private final Asset selling;
private final Asset buying;
private final long amount;
private final String amount;
private final String price;
private long offerId = 0;

Expand All @@ -108,7 +108,7 @@ public static class Builder {
Builder(ManageOfferOp op) {
selling = Asset.fromXdr(op.getSelling());
buying = Asset.fromXdr(op.getBuying());
amount = op.getAmount().getInt64().longValue();
amount = Operation.fromXdrAmount(op.getAmount().getInt64().longValue());
int n = op.getPrice().getN().getInt32().intValue();
int d = op.getPrice().getD().getInt32().intValue();
price = new BigDecimal(n).divide(new BigDecimal(d)).toString();
Expand All @@ -121,12 +121,13 @@ public static class Builder {
* @param buying The asset being bought in this operation
* @param amount Amount of selling being sold.
* @param price Price of 1 unit of selling in terms of buying.
* @throws ArithmeticException when amount has more than 7 decimal places.
*/
public Builder(Asset selling, Asset buying, long amount, String price) {
this.selling = selling;
this.buying = buying;
this.amount = amount;
this.price = price;
public Builder(Asset selling, Asset buying, String amount, String price) {
this.selling = checkNotNull(selling, "selling cannot be null");
this.buying = checkNotNull(buying, "buying cannot be null");
this.amount = checkNotNull(amount, "amount cannot be null");
this.price = checkNotNull(price, "price cannot be null");
}

/**
Expand All @@ -140,11 +141,11 @@ public Builder setOfferId(long offerId) {

/**
* Sets the source account for this operation.
* @param account The operation's source account.
* @param sourceAccount The operation's source account.
* @return Builder object so you can chain methods.
*/
public Builder setSourceAccount(Keypair account) {
mSourceAccount = account;
public Builder setSourceAccount(Keypair sourceAccount) {
mSourceAccount = checkNotNull(sourceAccount, "sourceAccount cannot be null");
return this;
}

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/stellar/base/Operation.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;

import static com.google.common.base.Preconditions.checkNotNull;

Expand All @@ -14,6 +15,19 @@ public abstract class Operation {

private Keypair mSourceAccount;

private static final BigDecimal ONE = new BigDecimal(10).pow(7);

protected static long toXdrAmount(String value) {
value = checkNotNull(value, "value cannot be null");
BigDecimal amount = new BigDecimal(value).multiply(Operation.ONE);
return amount.longValueExact();
}

protected static String fromXdrAmount(long value) {
BigDecimal amount = new BigDecimal(value).divide(Operation.ONE);
return amount.toPlainString();
}

/**
* Generates Operation XDR object.
*/
Expand Down
Loading

0 comments on commit 51f7bf8

Please sign in to comment.