Skip to content

Commit

Permalink
Merge pull request #149 from philip-healy/claim-rewards-simplified-op…
Browse files Browse the repository at this point in the history
…eration

Claim rewards simplified operation
  • Loading branch information
marvin-we committed Nov 11, 2017
2 parents c34cd2d + 3b2a91f commit a2dd3a7
Showing 1 changed file with 110 additions and 18 deletions.
128 changes: 110 additions & 18 deletions core/src/main/java/eu/bittrade/libs/steemj/SteemJ.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.TimeZone;
import java.util.UUID;

import com.google.common.collect.Lists;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.Sha256Hash;
Expand Down Expand Up @@ -68,6 +69,7 @@
import eu.bittrade.libs.steemj.base.models.VoteState;
import eu.bittrade.libs.steemj.base.models.Witness;
import eu.bittrade.libs.steemj.base.models.WitnessSchedule;
import eu.bittrade.libs.steemj.base.models.operations.ClaimRewardBalanceOperation;
import eu.bittrade.libs.steemj.base.models.operations.CommentOperation;
import eu.bittrade.libs.steemj.base.models.operations.CommentOptionsOperation;
import eu.bittrade.libs.steemj.base.models.operations.CustomJsonOperation;
Expand Down Expand Up @@ -3618,17 +3620,16 @@ public void deletePostOrComment(AccountName postOrCommentAuthor, Permlink postOr
* <ul>
* <li>This method will write data on the blockchain. As all writing
* operations, a private key is required to sign the transaction. For a
* follow operation the private posting key of the
* transfer operation the private active key of the
* {@link SteemJConfig#getDefaultAccount() DefaultAccount} needs to be
* configured in the {@link SteemJConfig#getPrivateKeyStorage()
* PrivateKeyStorage}.</li>
* <li>This method will automatically use the
* {@link SteemJConfig#getDefaultAccount() DefaultAccount} as the account
* that will follow the <code>accountToFollow</code> - If no default account
* has been provided, this method will throw an error. If you do not want to
* configure the following account as a default account, please use the
* {@link #follow(AccountName, AccountName)} method and provide the
* following account separately.</li>
* to transfer from. If no default account has been provided, this method will throw an error. If you
* do not want to configure the following account as a default account, please use the
* {@link #transfer(AccountName, AccountName, AssetSymbolType, double, String)} method and provide the
* <code>from</code> account separately.</li>
* </ul>
*
* @param to
Expand Down Expand Up @@ -3681,21 +3682,12 @@ public TransferOperation transfer(AccountName to, AssetSymbolType assetType, dou
* <code>SteemJ.transfer(new AccountName("accounta"), new AccountName("accountb"), AssetSymbolType.SBD, 1.0, "My memo");</code>
*
* <b>Attention</b>
* <ul>
* <li>This method will write data on the blockchain. As all writing
* This method will write data on the blockchain. As all writing
* operations, a private key is required to sign the transaction. For a
* follow operation the private posting key of the
* transfer operation the private active key of the
* {@link SteemJConfig#getDefaultAccount() DefaultAccount} needs to be
* configured in the {@link SteemJConfig#getPrivateKeyStorage()
* PrivateKeyStorage}.</li>
* <li>This method will automatically use the
* {@link SteemJConfig#getDefaultAccount() DefaultAccount} as the account
* that will follow the <code>accountToFollow</code> - If no default account
* has been provided, this method will throw an error. If you do not want to
* configure the following account as a default account, please use the
* {@link #follow(AccountName, AccountName)} method and provide the
* following account separately.</li>
* </ul>
* PrivateKeyStorage}.
*
* @param from
* The account from which to transfer currency.
Expand Down Expand Up @@ -3749,4 +3741,104 @@ public TransferOperation transfer(AccountName from, AccountName to, AssetSymbolT
this.broadcastTransaction(signedTransaction);
return transferOperation;
}

/**
* Claim all available Steem, SDB and VEST (Steam Power) rewards for the default account.
*
* <b>Attention</b>
* <ul>
* <li>This method will write data on the blockchain if a reward balance is available to be claimed. As with all writing
* operations, a private key is required to sign the transaction. See {@link SteemJConfig#getPrivateKeyStorage()
* PrivateKeyStorage}.</li>
* <li>This method will automatically use the
* {@link SteemJConfig#getDefaultAccount() DefaultAccount} as the account
* that will follow the <code>accountToFollow</code> - If no default account
* has been provided, this method will throw an error. If you do not want to
* configure the following account as a default account, please use the
* {@link #follow(AccountName, AccountName)} method and provide the
* following account separately.</li>
* </ul>
*
* @return The ClaimOperation for reward balances found. This will only have been broadcast if one of the balances
* is non-zero.
* @throws SteemCommunicationException
* <ul>
* <li>If the server was not able to answer the request in the
* given time (see
* {@link eu.bittrade.libs.steemj.configuration.SteemJConfig#setResponseTimeout(int)
* setResponseTimeout}).</li>
* <li>If there is a connection problem.</li>
* </ul>
* @throws SteemResponseException
* <ul>
* <li>If the SteemJ is unable to transform the JSON response
* into a Java object.</li>
* <li>If the Server returned an error object.</li>
* </ul>
* @throws SteemInvalidTransactionException
* If there is a problem while signing the transaction.
*/
public ClaimRewardBalanceOperation claimRewards()
throws SteemCommunicationException, SteemResponseException, SteemInvalidTransactionException {
if (SteemJConfig.getInstance().getDefaultAccount().isEmpty()) {
throw new InvalidParameterException(NO_DEFAULT_ACCOUNT_ERROR_MESSAGE);
}

return claimRewards(SteemJConfig.getInstance().getDefaultAccount());
}

/**
* Claim all available Steem, SDB and VEST (Steam Power) rewards for the specified account.
*
* <b>Attention</b>
* This method will write data on the blockchain if a reward balance is available to be claimed. As with all writing
* operations, a private key is required to sign the transaction. See {@link SteemJConfig#getPrivateKeyStorage()
* PrivateKeyStorage}.
*
* @param accountName
* The account to claim rewards for.
* @return The ClaimOperation for reward balances found. This will only have been broadcast if one of the balances
* is non-zero.
* @throws SteemCommunicationException
* <ul>
* <li>If the server was not able to answer the request in the
* given time (see
* {@link eu.bittrade.libs.steemj.configuration.SteemJConfig#setResponseTimeout(int)
* setResponseTimeout}).</li>
* <li>If there is a connection problem.</li>
* </ul>
* @throws SteemResponseException
* <ul>
* <li>If the SteemJ is unable to transform the JSON response
* into a Java object.</li>
* <li>If the Server returned an error object.</li>
* </ul>
* @throws SteemInvalidTransactionException
* If there is a problem while signing the transaction.
*/
public ClaimRewardBalanceOperation claimRewards(AccountName accountName)
throws SteemCommunicationException, SteemResponseException, SteemInvalidTransactionException {
// Get extended account info to determine reward balances
ExtendedAccount extendedAccount = this.getAccounts(Lists.newArrayList(accountName)).get(0);
Asset steemReward = extendedAccount.getRewardSteemBalance();
Asset sbdReward = extendedAccount.getRewardSdbBalance();
Asset vestingReward = extendedAccount.getRewardVestingBalance();

// Create claim operation based on available reward balances
ClaimRewardBalanceOperation claimOperation = new ClaimRewardBalanceOperation(accountName,
steemReward, sbdReward, vestingReward);

// Broadcast claim operation if there are any balances available
if (steemReward.getAmount() > 0 || sbdReward.getAmount() > 0 || vestingReward.getAmount() > 0) {
ArrayList<Operation> operations = new ArrayList<>();
operations.add(claimOperation);
GlobalProperties globalProperties = this.getDynamicGlobalProperties();
SignedTransaction signedTransaction = new SignedTransaction(globalProperties.getHeadBlockId(), operations,
null);
signedTransaction.sign();
this.broadcastTransaction(signedTransaction);
}

return claimOperation;
}
}

0 comments on commit a2dd3a7

Please sign in to comment.