-
Notifications
You must be signed in to change notification settings - Fork 31
How to perform operations
Attention: The following examples have been created and tested with version 0.2.3 and may be outdated. If you find an example that is not working with the current version it would be great if you would create an issue for that.
TOC
-
Account Witness Vote Operation
-
Claim Reward Balance Operation
-
Comment Operation
-
Convert Operation
-
Feed Publish Operation
-
Limit Order Cancel Operation
-
Limit Order Create Operation
-
Limit Order Create 2 Operation
-
Transfer Operation
-
Transfer To Vesting Operation
-
Vote Operation
-
Withdraw Vesting Operation
-
Witness Update Operation
Since: This operation is available since v0.2.3
Description: Use this operation to vote for a witness. Your active privatekey is required to perform this action.
[...]
AccountWitnessVoteOperation accountWitnessVoteOperation = new AccountWitnessVoteOperation();
accountWitnessVoteOperation.setAccount(new AccountName("dez1337"));
accountWitnessVoteOperation.setWitness(new AccountName("good-karma"));
accountWitnessVoteOperation.setApprove(true);
operations.add(accountWitnessVoteOperation);
[...]
Since: This operation is available since v0.2.2
Description: Use this operation to collect the rewards for a specifc account. Your posting privatekey is required to perform this action.
[...]
ArrayList<AccountName> accountsToRequest = new ArrayList<>();
AccountName myAccountName = new AccountName("dez1337");
accountsToRequest.add(myAccountName);
ExtendedAccount myAccount = steemApiWrapper.getAccounts(accountsToRequest).get(0);
ClaimRewardBalanceOperation claimRewardBalanceOpeartion = new ClaimRewardBalanceOperation();
claimRewardBalanceOpeartion.setAccount(myAccountName);
claimRewardBalanceOpeartion.setRewardSbd(myAccount.getRewardSdbBalance());
claimRewardBalanceOpeartion.setRewardSteem(myAccount.getRewardSteemBalance());
claimRewardBalanceOpeartion.setRewardVests(myAccount.getRewardVestingBalance());
operations.add(claimRewardBalanceOperation);
[...]
Since: This operation is available since v0.2.1
Description: Use this operation to create a comment. Your posting privatekey is required to perform this action.
[...]
CommentOperation commentOperation = new CommentOperation();
commentOperation.setAuthor(new AccountName("dez1337"));
commentOperation.setBody("Test SteemJ");
commentOperation.setJsonMetadata("{}");
commentOperation.setParentAuthor(new AccountName("dez1337"));
commentOperation.setParentPermlink("steem-java-api-v0-2-0-has-been-released-update-6");
commentOperation.setPermlink("re-steem-java-api-v0-2-0-has-been-released-update-6");
commentOperation.setTitle("-");
operations.add(commentOperation);
[...]
Since: This operation is available since v0.2.4
Description: Use this operation to convert STEEM into SBD or the other way around. Your active privatekey is required to perform this action.
[...]
ConvertOperation convertOperation = new ConvertOperation();
Asset amount = new Asset();
amount.setAmount(1L);
amount.setSymbol(AssetSymbolType.SBD);
convertOperation.setAmount(amount);
convertOperation.setOwner(new AccountName("dez1337"));
convertOperation.setRequestId(1337L);
operations.add(convertOperation);
[...]
Since: This operation is available since v0.2.4
Description: Witnesses can use this operation to publish the current price feed. Your active privatekey is required to perform this action.
[...]
FeedPublishOperation feedPublishOperation = new FeedPublishOperation();
feedPublishOperation.setPublisher(new AccountName("dez1337"));
// 1 STEEM = 1.15 SBD
Asset base = new Asset();
base.setAmount(115);
base.setSymbol(AssetSymbolType.SBD);
Asset quote = new Asset();
quote.setAmount(100);
quote.setSymbol(AssetSymbolType.STEEM);
Price exchangeRate = new Price();
exchangeRate.setBase(base);
exchangeRate.setQuote(quote);
feedPublishOperation.setExchangeRate(exchangeRate);
operations.add(feedPublishOperation);
[...]
Since: This operation is available since v0.2.3
Description: Use this operation to cancel an order. The balance will be returned to the owner. Your active privatekey is required to perform this action.
[...]
LimitOrderCancelOperation limitOrderCancelOperation = new LimitOrderCancelOperation();
limitOrderCancelOperation.setOwner(new AccountName("dez1337"));
limitOrderCancelOperation.setOrderId(492995L);
operations.add(limitOrderCancelOperation);
[...]
Since: This operation is available since v0.2.3
Description: Use this operation to sell/buy STEEM or SBD on the internal market. Your active privatekey is required to perform this action.
[...]
// Sell 0,001 SDB for 0,010 STEEM.
LimitOrderCreateOperation limitOrderCreateOperation = new LimitOrderCreateOperation();
Asset amountToSell = new Asset();
amountToSell.setAmount(1L);
amountToSell.setSymbol(AssetSymbolType.SBD);
limitOrderCreateOperation.setAmountToSell(amountToSell);
limitOrderCreateOperation.setExpirationDate(System.currentTimeMillis()
+ SteemApiWrapperConfig.getInstance().getMaximumExpirationDateOffset() - 30000L);
limitOrderCreateOperation.setFillOrKill(false);
Asset minToReceive = new Asset();
minToReceive.setAmount(10L);
minToReceive.setSymbol(AssetSymbolType.STEEM);
limitOrderCreateOperation.setMinToReceive(minToReceive);
limitOrderCreateOperation.setOrderId(492995);
limitOrderCreateOperation.setOwner(new AccountName("dez1337"));
operations.add(limitOrderCreateOperation);
[...]
Since: This operation is available since v0.2.3
Description: Use this operation to sell/buy STEEM or SBD on the internal market. This operation is identical to the Limit Order Create Operation except that it serializes the price rather than calculating it from other fields.
[...]
limitOrderCreate2Operation = new LimitOrderCreate2Operation();
Asset base = new Asset();
base.setAmount(1L);
base.setSymbol(AssetSymbolType.SBD);
Asset quote = new Asset();
quote.setAmount(10L);
quote.setSymbol(AssetSymbolType.STEEM);
Price exchangeRate = new Price();
exchangeRate.setBase(base);
exchangeRate.setQuote(quote);
limitOrderCreate2Operation.setExchangeRate(exchangeRate);
Asset amountToSell = new Asset();
amountToSell.setAmount(1L);
amountToSell.setSymbol(AssetSymbolType.SBD);
limitOrderCreate2Operation.setAmountToSell(amountToSell);
limitOrderCreate2Operation.setExpirationDate(EXPIRATION_DATE);
limitOrderCreate2Operation.setFillOrKill(false);
limitOrderCreate2Operation.setOrderId(492991L);
limitOrderCreate2Operation.setOwner(new AccountName("dez1337"));
operations.add(limitOrderCreate2Operation);
[...]
Since: This operation is available since v0.2.3
Description: Use this operation to transfer STEEM or SBD to another account. Your active privatekey is required to perform this action.
[...]
TransferOperation transferOperation = new TransferOperation();
transferOperation.setFrom(new AccountName("dez1337"));
transferOperation.setTo(new AccountName("dez1337"));
Asset amount = new Asset();
amount.setAmount(1L);
amount.setSymbol(AssetSymbolType.STEEM);
transferOperation.setAmount(amount);
transferOperation.setMemo("Test 4 SteemJ 0.2.2");
operations.add(transferOperation);
[...]
Since: This operation is available since v0.2.4
Description: Use this operation to transfer SteemPower to another account. Your active privatekey is required to perform this action.
[...]
TransferToVestingOperation transferToVestingOperation = new TransferToVestingOperation();
transferToVestingOperation.setFrom(new AccountName("dez1337"));
transferToVestingOperation.setTo(new AccountName("dez1337"));
Asset amount = new Asset();
amount.setSymbol(AssetSymbolType.STEEM);
amount.setAmount(1L);
transferToVestingOperation.setAmount(amount);
operations.add(transferToVestingOperation);
[...]
Since: This operation is available since v0.2.0
Description: Use this operation to vote for a comment or a post. Your posting privatekey is required to perform this action.
[...]
VoteOperation voteOperation = new VoteOperation();
voteOperation.setAuthor("dez1337");
voteOperation.setPermlink("steem-java-api-learned-to-speak-graphene-update-5");
voteOperation.setVoter("dez1337");
try {
voteOperation.setWeight((short) 10000);
} catch (InvalidActivityException e) {
LOGGER.error("Weight was to high.", e);
}
operations.add(voteOperation);
[...]
Since: This operation is available since v0.2.4
Description: Use this operation to start powering down. Your active privatekey is required to perform this action.
[...]
WithdrawVestingOperation withdrawVestingOperation = new WithdrawVestingOperation();
withdrawVestingOperation.setAccount(new AccountName("dez1337"));
Asset vestingShares = new Asset();
vestingShares.setAmount(1000);
vestingShares.setSymbol(AssetSymbolType.VESTS);
withdrawVestingOperation.setVestingShares(vestingShares);
operations.add(withdrawVestingOperation);
[...]
Since: This operation is available since v0.2.4
Description: Use this operation to become a Witness or to update your Witness information. To generate a new key pair please have a look at the SteemJ KeyGenerator class. Your active privatekey is required to perform this action.
[...]
WitnessUpdateOperation witnessUpdateOperation = new WitnessUpdateOperation();
witnessUpdateOperation.setBlockSigningKey(new PublicKey("STM8gyvJtYyv5ZbT2ZxbAtgufQ5ovV2bq6EQp4YDTzQuSwyg7Ckry"));
Asset fee = new Asset();
fee.setAmount(0L);
fee.setSymbol(AssetSymbolType.STEEM);
witnessUpdateOperation.setFee(fee);
witnessUpdateOperation.setOwner(new AccountName("dez1337"));
ChainProperties chainProperties = new ChainProperties();
Asset accountCreationFee = new Asset();
accountCreationFee.setAmount(5000L);
accountCreationFee.setSymbol(AssetSymbolType.STEEM);
chainProperties.setAccountCreationFee(accountCreationFee);
chainProperties.setMaximumBlockSize(65536);
chainProperties.setSdbInterestRate(0);
witnessUpdateOperation.setProperties(chainProperties);
witnessUpdateOperation.setUrl("https://steemit.com/@dez1337");
operations.add(witnessUpdateOperation);
[...]
package eu.bittrade.libs.steem.api.wrapper;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
import javax.activity.InvalidActivityException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import eu.bittrade.libs.steem.api.wrapper.configuration.SteemApiWrapperConfig;
import eu.bittrade.libs.steem.api.wrapper.enums.PrivateKeyType;
import eu.bittrade.libs.steem.api.wrapper.exceptions.SteemCommunicationException;
import eu.bittrade.libs.steem.api.wrapper.exceptions.SteemInvalidTransactionException;
import eu.bittrade.libs.steem.api.wrapper.exceptions.SteemResponseError;
import eu.bittrade.libs.steem.api.wrapper.models.AccountActivity;
import eu.bittrade.libs.steem.api.wrapper.models.ActiveVote;
import eu.bittrade.libs.steem.api.wrapper.models.GlobalProperties;
import eu.bittrade.libs.steem.api.wrapper.models.Transaction;
import eu.bittrade.libs.steem.api.wrapper.models.Vote;
import eu.bittrade.libs.steem.api.wrapper.models.operations.AccountCreateOperation;
import eu.bittrade.libs.steem.api.wrapper.models.operations.Operation;
import eu.bittrade.libs.steem.api.wrapper.models.operations.VoteOperation;
public class SteemAPIUsageExample {
private static final Logger LOGGER = LogManager.getLogger(SteemAPIUsageExample.class);
public static void main(String args[]) {
// Change the default settings if needed.
SteemApiWrapperConfig myConfig = SteemApiWrapperConfig.getInstance();
myConfig.setTimeout(100000L);
try {
myConfig.setWebsocketEndpointURI(new URI("wss://this.piston.rocks"));
myConfig.setSslVerificationDisabled(true);
} catch (URISyntaxException e) {
throw new RuntimeException("The given URI is not valid.", e);
}
myConfig.setPrivateKey(PrivateKeyType.POSTING, "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3");
myConfig.setPrivateKey(PrivateKeyType.ACTIVE, "5KQasdf7ASD8weASdW37FSSsadfAImkwASd732QzDeyXtP79zk");
try {
// Create a new apiWrapper with your config object.
SteemApiWrapper steemApiWrapper = new SteemApiWrapper();
// Let's have a look at the account history of dez1337
Map<Integer, AccountActivity> accountHistory = steemApiWrapper.getAccountHistory("dez1337", 100, 100);
if (accountHistory.get(0).getOperations() instanceof AccountCreateOperation) {
AccountCreateOperation accountCreateOperation = (AccountCreateOperation) (accountHistory.get(0)
.getOperations());
LOGGER.info("The account {} has been created by {}.", "dez1337", accountCreateOperation.getCreator());
}
// Perform a transaction
VoteOperation voteOperation = new VoteOperation();
voteOperation.setAuthor("dez1337");
voteOperation.setPermlink("steem-java-api-learned-to-speak-graphene-update-5");
voteOperation.setVoter("dez1337");
try {
voteOperation.setWeight((short) 10000);
} catch (InvalidActivityException e) {
LOGGER.error("Weight was to high.", e);
}
operations.add(voteOperation);
// Get the current RefBlockNum and RefBlockPrefix from the global properties.
GlobalProperties globalProperties = steemApiWrapper.getDynamicGlobalProperties();
int refBlockNum = (globalProperties.getHeadBlockNumber() & 0xFFFF);
Transaction transaction = new Transaction();
transaction.setRefBlockNum(refBlockNum);
transaction.setRefBlockPrefix(globalProperties.getHeadBlockId());
transaction.setOperations(operations);
try {
transaction.sign();
} catch (SteemInvalidTransactionException e) {
LOGGER.error("A propblem occured while signing your Transaction.", e);
}
steemApiWrapper.broadcastTransaction(transaction);
LOGGER.info("The HEX representation of this transaction it {}.", steemApiWrapper.getTransactionHex(transaction));
// Get the current Price
LOGGER.info("The current price in the internal market is {}.", steemApiWrapper.getCurrentMedianHistoryPrice().getBase().getAmount());
// Get votes
List<Vote> votes = steemApiWrapper.getAccountVotes("dez1337");
LOGGER.info("The user dez1337 has done {} votes so far.", votes.size());
LOGGER.info("His last vote has been done on {}.", votes.get(votes.size() - 1).getTime());
int numberOfAccounts = steemApiWrapper.getAccountCount();
int numberOfWitnesses = steemApiWrapper.getWitnessCount();
LOGGER.info(
"dez1337 is one of {} accounts on steemit and may increase the number witnesses to {} in the near future.",
numberOfAccounts, numberOfWitnesses + 1);
List<ActiveVote> activeVotesForArticle = steemApiWrapper.getActiveVotes("dez1337",
"steem-api-wrapper-for-java-update1");
LOGGER.info("The last vote for a article of dez1337 has been done from {}.",
activeVotesForArticle.get(activeVotesForArticle.size() - 1).getVoter());
LOGGER.info(
"You may also want to vote for some posts to generate some Steem which is currently worth about {}.",
steemApiWrapper.getCurrentMedianHistoryPrice().getBase());
steemApiWrapper.getPotentialSignatures();
// Force an error response:
steemApiWrapper.getAccountVotes("thisAcountDoesNotExistYet");
} catch (SteemResponseError e) {
// The SteemResponseError contains the error response.
LOGGER.error("An error with code {} occured with the following message {}.",
e.getError().getSteemErrorDetails().getData().getCode(),
e.getError().getSteemErrorDetails().getMessage());
} catch (SteemCommunicationException e) {
LOGGER.error("Error!", e);
}
}
}
package eu.bittrade.libs.steem.api.wrapper;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import eu.bittrade.libs.steem.api.wrapper.configuration.SteemApiWrapperConfig;
import eu.bittrade.libs.steem.api.wrapper.exceptions.SteemConnectionException;
import eu.bittrade.libs.steem.api.wrapper.exceptions.SteemResponseError;
import eu.bittrade.libs.steem.api.wrapper.exceptions.SteemTimeoutException;
import eu.bittrade.libs.steem.api.wrapper.exceptions.SteemTransformationException;
import eu.bittrade.libs.steem.api.wrapper.models.ActiveVote;
import eu.bittrade.libs.steem.api.wrapper.models.Vote;
public class SteemAPIUsageExample {
private static final Logger LOGGER = LogManager.getLogger(SteemAPIUsageExample.class);
public static void main(String args[]) {
// Create a config object that already provides the default settings.
SteemApiWrapperConfig myConfig = new SteemApiWrapperConfig();
// Change the default settings if needed.
try {
myConfig.setWebsocketEndpointURI(new URI("wss://steemit.com/wspa"));
} catch (URISyntaxException e) {
throw new RuntimeException("The given URI is not valid.", e);
}
try {
// Create a new apiWrapper with your config object.
SteemApiWrapper steemApiWrapper = new SteemApiWrapper(myConfig);
// Get the votes done by the specified account:
List<Vote> votes = steemApiWrapper.getAccountVotes("dez1337");
LOGGER.info("The user dez1337 has done {} votes so far.", votes.size());
LOGGER.info("His last vote has been done on {}.", votes.get(votes.size() - 1).getTime());
int numberOfAccounts = steemApiWrapper.getAccountCount();
int numberOfWitnesses = steemApiWrapper.getWitnessCount();
LOGGER.info(
"dez1337 is one of {} accounts on steemit and may increase the number witnesses to {} in the near future.",
numberOfAccounts, numberOfWitnesses + 1);
List<ActiveVote> activeVotesForArticle = steemApiWrapper.getActiveVotes("dez1337",
"steem-api-wrapper-for-java-update1");
LOGGER.info("The last vote for a article of dez1337 has been done from {}.",
activeVotesForArticle.get(activeVotesForArticle.size() - 1).getVoter());
LOGGER.info(
"You may also want to vote for some posts to generate some Steem which is currently worth about {}.",
steemApiWrapper.getCurrentMedianHistoryPrice().getBase());
// Force an error response:
steemApiWrapper.getAccountVotes("thisAcountDoesNotExistYet");
} catch (SteemConnectionException | SteemTimeoutException | SteemTransformationException e) {
LOGGER.error("Error!", e);
} catch (SteemResponseError e) {
// The SteemResponseError contains the error response.
LOGGER.error("An error with code {} occured with the following message {}.", e.getError().getSteemErrorDetails().getData().getCode(), e.getError().getSteemErrorDetails().getMessage());
}
}
}
This project is developed by dez1337