-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for CAP 35 operations when building transactions (#325)
This PR adds support for building transactions using the operations introduced in CAP 35
- Loading branch information
Showing
15 changed files
with
610 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/main/java/org/stellar/sdk/ClawbackClaimableBalanceOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package org.stellar.sdk; | ||
|
||
import com.google.common.base.Objects; | ||
import org.stellar.sdk.xdr.ClawbackClaimableBalanceOp; | ||
import org.stellar.sdk.xdr.OperationType; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
/** | ||
* | ||
* Represents a Clawback Claimable Balance operation. | ||
* @see <a href="https://www.stellar.org/developers/learn/concepts/list-of-operations.html" target="_blank">List of Operations</a> | ||
*/ | ||
public class ClawbackClaimableBalanceOperation extends Operation { | ||
private final String balanceId; | ||
|
||
private ClawbackClaimableBalanceOperation(String balanceId) { | ||
this.balanceId = checkNotNull(balanceId, "balanceId cannot be null"); | ||
} | ||
|
||
/** | ||
* The id of the claimable balance which will be clawed back. | ||
*/ | ||
public String getBalanceId() { | ||
return balanceId; | ||
} | ||
|
||
|
||
@Override | ||
org.stellar.sdk.xdr.Operation.OperationBody toOperationBody() { | ||
ClawbackClaimableBalanceOp op = new ClawbackClaimableBalanceOp(); | ||
|
||
op.setBalanceID(Util.claimableBalanceIdToXDR(balanceId)); | ||
|
||
|
||
org.stellar.sdk.xdr.Operation.OperationBody body = new org.stellar.sdk.xdr.Operation.OperationBody(); | ||
body.setDiscriminant(OperationType.CLAWBACK_CLAIMABLE_BALANCE); | ||
body.setClawbackClaimableBalanceOp(op); | ||
return body; | ||
} | ||
|
||
/** | ||
* Builds ClawbackClaimableBalanceOperation. | ||
* @see ClawbackClaimableBalanceOperation | ||
*/ | ||
public static class Builder { | ||
private final String balanceId; | ||
|
||
private String mSourceAccount; | ||
|
||
Builder(ClawbackClaimableBalanceOp op) { | ||
balanceId = Util.xdrToClaimableBalanceId(op.getBalanceID()); | ||
} | ||
|
||
/** | ||
* Creates a new ClawbackClaimableBalanceOperation builder. | ||
* @param balanceId The id of the claimable balance which will be clawed back. | ||
*/ | ||
public Builder(String balanceId) { | ||
this.balanceId = balanceId; | ||
} | ||
|
||
/** | ||
* Set source account of this operation | ||
* @param sourceAccount Source account | ||
* @return Builder object so you can chain methods. | ||
*/ | ||
public Builder setSourceAccount(String sourceAccount) { | ||
mSourceAccount = sourceAccount; | ||
return this; | ||
} | ||
|
||
/** | ||
* Builds an operation | ||
*/ | ||
public ClawbackClaimableBalanceOperation build() { | ||
ClawbackClaimableBalanceOperation operation = new ClawbackClaimableBalanceOperation( | ||
balanceId | ||
); | ||
if (mSourceAccount != null) { | ||
operation.setSourceAccount(mSourceAccount); | ||
} | ||
return operation; | ||
} | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode( | ||
this.getSourceAccount(), | ||
this.balanceId | ||
); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (object == null || !(object instanceof ClawbackClaimableBalanceOperation)) { | ||
return false; | ||
} | ||
|
||
ClawbackClaimableBalanceOperation other = (ClawbackClaimableBalanceOperation) object; | ||
return Objects.equal(this.balanceId, other.balanceId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package org.stellar.sdk; | ||
|
||
import com.google.common.base.Objects; | ||
import org.stellar.sdk.xdr.*; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
/** | ||
* | ||
* Represents a Clawback operation. | ||
* @see <a href="https://www.stellar.org/developers/learn/concepts/list-of-operations.html" target="_blank">List of Operations</a> | ||
*/ | ||
public class ClawbackOperation extends Operation { | ||
private final String from; | ||
private final AssetTypeCreditAlphaNum asset; | ||
private final String amount; | ||
|
||
private ClawbackOperation(String from, AssetTypeCreditAlphaNum asset, String amount) { | ||
this.from = checkNotNull(from, "from cannot be null"); | ||
this.asset = checkNotNull(asset, "asset cannot be null"); | ||
this.amount = checkNotNull(amount, "amount cannot be null"); | ||
} | ||
|
||
/** | ||
* The account owning of the trustline. | ||
*/ | ||
public String getFrom() { | ||
return from; | ||
} | ||
|
||
/** | ||
* The amount to be clawed back. | ||
*/ | ||
public String getAmount() { | ||
return amount; | ||
} | ||
|
||
/** | ||
* The asset to be clawed back. | ||
*/ | ||
public Asset getAsset() { | ||
return asset; | ||
} | ||
|
||
@Override | ||
org.stellar.sdk.xdr.Operation.OperationBody toOperationBody() { | ||
ClawbackOp op = new ClawbackOp(); | ||
|
||
// trustor | ||
op.setFrom(StrKey.encodeToXDRMuxedAccount(this.from)); | ||
|
||
Int64 amount = new Int64(); | ||
amount.setInt64(Operation.toXdrAmount(this.amount)); | ||
op.setAmount(amount); | ||
op.setAsset(asset.toXdr()); | ||
|
||
|
||
org.stellar.sdk.xdr.Operation.OperationBody body = new org.stellar.sdk.xdr.Operation.OperationBody(); | ||
body.setDiscriminant(OperationType.CLAWBACK); | ||
body.setClawbackOp(op); | ||
return body; | ||
} | ||
|
||
/** | ||
* Builds ClawbackOperation operation. | ||
* @see ClawbackOperation | ||
*/ | ||
public static class Builder { | ||
private final String from; | ||
private final AssetTypeCreditAlphaNum asset; | ||
private final String amount; | ||
|
||
private String mSourceAccount; | ||
|
||
Builder(ClawbackOp op) { | ||
from = StrKey.encodeStellarAccountId(StrKey.muxedAccountToAccountId(op.getFrom())); | ||
amount = Operation.fromXdrAmount(op.getAmount().getInt64().longValue()); | ||
asset = Util.assertNonNativeAsset(Asset.fromXdr(op.getAsset())); | ||
} | ||
|
||
/** | ||
* Creates a new ClawbackOperation builder. | ||
* @param from The account holding the trustline. | ||
* @param asset The asset held in the trustline. | ||
* @param amount The amount to be clawed back. | ||
*/ | ||
public Builder(String from, Asset asset, String amount) { | ||
this.from = from; | ||
this.asset = Util.assertNonNativeAsset(asset); | ||
this.amount = amount; | ||
} | ||
|
||
/** | ||
* Set source account of this operation | ||
* @param sourceAccount Source account | ||
* @return Builder object so you can chain methods. | ||
*/ | ||
public Builder setSourceAccount(String sourceAccount) { | ||
mSourceAccount = sourceAccount; | ||
return this; | ||
} | ||
|
||
/** | ||
* Builds an operation | ||
*/ | ||
public ClawbackOperation build() { | ||
ClawbackOperation operation = new ClawbackOperation( | ||
from, asset, amount | ||
); | ||
if (mSourceAccount != null) { | ||
operation.setSourceAccount(mSourceAccount); | ||
} | ||
return operation; | ||
} | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode( | ||
this.getSourceAccount(), | ||
this.from, | ||
this.asset, | ||
this.amount | ||
); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (object == null || !(object instanceof ClawbackOperation)) { | ||
return false; | ||
} | ||
|
||
ClawbackOperation other = (ClawbackOperation) object; | ||
return Objects.equal(this.from, other.from) && | ||
Objects.equal(this.asset, other.asset) && | ||
Objects.equal(this.amount, other.amount) && | ||
Objects.equal(this.getSourceAccount(), other.getSourceAccount()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.