Skip to content

How to perform operations

dez1337 edited this page Aug 30, 2017 · 16 revisions

Attention: The following examples have been created and tested with version 0.2.4 and may not work for older versions. 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

Create a new Transaction

Every write action, like writing a post or even voting, requires a Operation to be broadcastet to the nodes. Those Operations are embedded in so called Transactions. The following example shows how to create one.

[...]
Transaction transaction = new Transaction();
// The expiration date defines how long the transaction is valid. This field is optional and SteemJ will automatically use the current system time and an offset.
transaction.setExpirationDate("2016-04-06T08:29:27UTC");
// Define in which block the transaction should be added. In most cases this is the next available block which can be asked using the following lines:
GlobalProperties globalProperties = steemApiWrapper.getDynamicGlobalProperties();

transaction.setRefBlockPrefix(globalProperties.getHeadBlockId().getHashValue());
transaction.setRefBlockNum(globalProperties.getHeadBlockId().getNumberFromHash());

// There is also the option to add extensions to a transaction which are currently not used by Steem and therefore not supported by SteemJ.
// transaction.setExtensions(extensions);

// A Transaction accepts one or more Operations which have to be provided as an ArrayList.
ArrayList<Operation> operations = new ArrayList<>();
[...]

Now you can add one or more Operations to the operations-ArrayList.

Back to TOC

Add Operations

The following chapters show how to create and add the different operations to a transaction object.

Back to TOC

Account Create Operation

Since: This operation is available since v0.2.5
Description: Use this operation to create a new account. Your active privatekey and enough Steem to pay the creation fee are required to perform this action.

[...]
AccountCreateOperation accountCreateOperation = new AccountCreateOperation();
accountCreateOperation.setFee(new Asset(10000, AssetSymbolType.STEEM));
accountCreateOperation.setCreator(new AccountName("dez1337"));
accountCreateOperation.setJsonMetadata("");
accountCreateOperation.setMemoKey(new PublicKey("STM6zLNtyFVToBsBZDsgMhgjpwysYVbsQD6YhP3kRkQhANUB4w7Qp"));
accountCreateOperation.setNewAccountName(new AccountName("steemj"));

Authority posting = new Authority();
posting.setAccountAuths(new HashMap<>());
Map<PublicKey, Integer> postingKeyAuth = new HashMap<>();
postingKeyAuth.put(new PublicKey("STM8CemMDjdUWSV5wKotEimhK6c4dY7p2PdzC2qM1HpAP8aLtZfE7"), 1);
posting.setKeyAuths(postingKeyAuth);
posting.setWeightThreshold(1);

accountCreateOperation.setPosting(posting);

Authority active = new Authority();
active.setAccountAuths(new HashMap<>());
Map<PublicKey, Integer> activeKeyAuth = new HashMap<>();
activeKeyAuth.put(new PublicKey("STM6pbVDAjRFiw6fkiKYCrkz7PFeL7XNAfefrsREwg8MKpJ9VYV9x"), 1);
active.setKeyAuths(activeKeyAuth);
active.setWeightThreshold(1);

accountCreateOperation.setActive(active);

Authority owner = new Authority();
owner.setAccountAuths(new HashMap<>());
Map<PublicKey, Integer> ownerKeyAuth = new HashMap<>();
ownerKeyAuth.put(new PublicKey("STM5jYVokmZHdEpwo5oCG3ES2Ca4VYzy6tM8pWWkGdgVnwo2mFLFq"), 1);
owner.setKeyAuths(ownerKeyAuth);
owner.setWeightThreshold(1);

accountCreateOperation.setOwner(owner);

operations.add(accountCreateOperation);
[...]

Back to TOC

Account Create With Delegation Operation

Since: This operation is available since v0.2.5
Description: Use this operation to create a new account while this time you can also delegate a part of your VESTS to the new account. Your active privatekey and enough Steem to pay the creation fee are required to perform this action.

[...]
AccountCreateWithDelegationOperation accountCreateWithDelegationOperation = new AccountCreateWithDelegationOperation();
accountCreateWithDelegationOperation.setFee(new Asset(500, AssetSymbolType.STEEM));
accountCreateWithDelegationOperation.setDelegation(new Asset(155440933151L, AssetSymbolType.VESTS));
accountCreateWithDelegationOperation.setCreator(new AccountName("dez1337"));
accountCreateWithDelegationOperation.setJsonMetadata("");
accountCreateWithDelegationOperation.setMemoKey(new PublicKey("STM6zLNtyFVToBsBZDsgMhgjpwysYVbsQD6YhP3kRkQhANUB4w7Qp"));
accountCreateWithDelegationOperation.setNewAccountName(new AccountName("steemj"));

Authority posting = new Authority();
posting.setAccountAuths(new HashMap<>());
Map<PublicKey, Integer> postingKeyAuth = new HashMap<>();
postingKeyAuth.put(new PublicKey("STM8CemMDjdUWSV5wKotEimhK6c4dY7p2PdzC2qM1HpAP8aLtZfE7"), 1);
posting.setKeyAuths(postingKeyAuth);
posting.setWeightThreshold(1);

accountCreateWithDelegationOperation.setPosting(posting);

Authority active = new Authority();
active.setAccountAuths(new HashMap<>());
Map<PublicKey, Integer> activeKeyAuth = new HashMap<>();
activeKeyAuth.put(new PublicKey("STM6pbVDAjRFiw6fkiKYCrkz7PFeL7XNAfefrsREwg8MKpJ9VYV9x"), 1);
active.setKeyAuths(activeKeyAuth);
active.setWeightThreshold(1);

accountCreateWithDelegationOperation.setActive(active);

Authority owner = new Authority();
owner.setAccountAuths(new HashMap<>());
Map<PublicKey, Integer> ownerKeyAuth = new HashMap<>();
ownerKeyAuth.put(new PublicKey("STM5jYVokmZHdEpwo5oCG3ES2Ca4VYzy6tM8pWWkGdgVnwo2mFLFq"), 1);
owner.setKeyAuths(ownerKeyAuth);
owner.setWeightThreshold(1);

accountCreateWithDelegationOperation.setOwner(owner);

operations.add(accountCreateWithDelegationOperation);
[...]

Back to TOC

Account Witness Vote 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);
[...]

Back to TOC

Claim Reward Balance Operation

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);
[...]

Back to TOC

Comment Operation

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);
[...]

Back to TOC

Comment Options Operation

Since: This operation is available since v0.2.5
Description: Use this operation to change the default options of a comment. Your posting privatekey is required to perform this action.

[...]
CommentOptionsOperation commentOptionsOperation = new CommentOptionsOperation();
commentOptionsOperation.setAuthor(new AccountName("dez1337"));
commentOptionsOperation.setPermlink("steemj-v0-2-4-has-been-released-update-9");
commentOptionsOperation.setAllowVotes(true);
commentOptionsOperation.setAllowCurationRewards(true);
commentOptionsOperation.setPercentSteemDollars((short) 10000);

operations.add(commentOptionsOperation);
[...]

Back to TOC

Convert Operation

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);
[...]

Back to TOC

Delete Comment Operation

Since: This operation is available since v0.2.5
Description: Use this operation to delete a comment. Your posting privatekey is required to perform this action.

[...]
DeleteCommentOperation deleteCommentOperation = new DeleteCommentOperation();
deleteCommentOperation.setAuthor(new AccountName("dez1337"));
deleteCommentOperation.setPermlink("re-re-dez1337-part-3-20170527t161702118z");

operations.add(deleteCommentOperation);
[...]

Back to TOC

Feed Publish Operation

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);
[...]

Back to TOC

Limit Order Cancel Operation

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);
[...]

Back to TOC

Limit Order Create Operation

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);
[...]

Back to TOC

Limit Order Create 2 Operation

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);
[...]

Back to TOC

Transfer Operation

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);
[...]

Back to TOC

Transfer To Vesting Operation

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);
[...]

Back to TOC

Vote Operation

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(new AccountName("dez1337"));
voteOperation.setPermlink("steem-java-api-learned-to-speak-graphene-update-5");
voteOperation.setVoter(new AccountName("dez1337"));
try {
     voteOperation.setWeight((short) 10000);
} catch (InvalidActivityException e) {
     LOGGER.error("Weight was to high.", e);
}

operations.add(voteOperation);
[...]

Back to TOC

Withdraw Vesting Operation

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);
[...]

Back to TOC

Witness Update Operation

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);
[...]

Back to TOC