Skip to content

Commit 668f519

Browse files
committed
Work on #103
1 parent 307ad76 commit 668f519

File tree

1 file changed

+64
-34
lines changed

1 file changed

+64
-34
lines changed

sample/src/main/java/my/sample/project/SteemJUsageExample.java

Lines changed: 64 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,11 @@
1313
import eu.bittrade.libs.steemj.SteemJ;
1414
import eu.bittrade.libs.steemj.base.models.AccountName;
1515
import eu.bittrade.libs.steemj.base.models.AppliedOperation;
16-
import eu.bittrade.libs.steemj.base.models.GlobalProperties;
1716
import eu.bittrade.libs.steemj.base.models.Permlink;
18-
import eu.bittrade.libs.steemj.base.models.SignedTransaction;
1917
import eu.bittrade.libs.steemj.base.models.Vote;
2018
import eu.bittrade.libs.steemj.base.models.VoteState;
2119
import eu.bittrade.libs.steemj.base.models.operations.AccountCreateOperation;
22-
import eu.bittrade.libs.steemj.base.models.operations.Operation;
23-
import eu.bittrade.libs.steemj.base.models.operations.VoteOperation;
20+
import eu.bittrade.libs.steemj.base.models.operations.CommentOperation;
2421
import eu.bittrade.libs.steemj.configuration.SteemJConfig;
2522
import eu.bittrade.libs.steemj.enums.PrivateKeyType;
2623
import eu.bittrade.libs.steemj.exceptions.SteemCommunicationException;
@@ -45,8 +42,11 @@ public static void main(String args[]) {
4542
// Change the default settings if needed.
4643
SteemJConfig myConfig = SteemJConfig.getInstance();
4744
myConfig.setResponseTimeout(100000L);
45+
myConfig.setDefaultAccount(new AccountName("steemj"));
4846

4947
try {
48+
// SteemJ already comes with a configured EndPoint, but if you want
49+
// to connect to another one, you can simply configure it here:
5050
myConfig.setWebSocketEndpointURI(new URI("wss://seed.bitcoiner.me"), false);
5151
} catch (URISyntaxException e) {
5252
throw new RuntimeException("The given URI is not valid.", e);
@@ -57,10 +57,10 @@ public static void main(String args[]) {
5757
SteemJ steemJ = new SteemJ();
5858

5959
List<ImmutablePair<PrivateKeyType, String>> privateKeys = new ArrayList<>();
60-
privateKeys.add(new ImmutablePair<>(PrivateKeyType.POSTING, "YOURPRIVATEPOSTINGKEY"));
61-
privateKeys.add(new ImmutablePair<>(PrivateKeyType.ACTIVE, "YOURPRIVATEACTIVEKEY"));
60+
privateKeys.add(new ImmutablePair<>(PrivateKeyType.POSTING, "YOUR-PRIVATE-POSTING-KEY"));
61+
privateKeys.add(new ImmutablePair<>(PrivateKeyType.ACTIVE, "YOUR-PRIVATE-ACTIVE-KEY"));
6262

63-
myConfig.getPrivateKeyStorage().addAccount(new AccountName("dez1337"), privateKeys);
63+
myConfig.getPrivateKeyStorage().addAccount(myConfig.getDefaultAccount(), privateKeys);
6464

6565
// Let's have a look at the account history of dez1337
6666
Map<Integer, AppliedOperation> accountHistory = steemJ.getAccountHistory(new AccountName("dez1337"), 100,
@@ -71,32 +71,60 @@ public static void main(String args[]) {
7171
LOGGER.info("The account {} has been created by {}.", "dez1337", accountCreateOperation.getCreator());
7272
}
7373

74-
// Perform a transaction
75-
AccountName voter = new AccountName("dez1337");
76-
AccountName author = new AccountName("dez1337");
77-
Permlink permlinkOfAPost = new Permlink("steem-java-api-learned-to-speak-graphene-update-5");
78-
Short votingWeight = 10000;
79-
VoteOperation voteOperation = new VoteOperation(voter, author, permlinkOfAPost, votingWeight);
80-
81-
ArrayList<Operation> operations = new ArrayList<>();
82-
operations.add(voteOperation);
83-
84-
// Get the current RefBlockNum and RefBlockPrefix from the global
85-
// properties.
86-
GlobalProperties globalProperties = steemJ.getDynamicGlobalProperties();
87-
88-
SignedTransaction signedTransaction = new SignedTransaction(globalProperties.getHeadBlockId(), operations,
89-
null);
90-
91-
try {
92-
signedTransaction.sign();
93-
} catch (SteemInvalidTransactionException e) {
94-
LOGGER.error("A propblem occured while signing your Transaction.", e);
95-
}
96-
steemJ.broadcastTransaction(signedTransaction);
97-
98-
LOGGER.info("The HEX representation of this transaction it {}.",
99-
steemJ.getTransactionHex(signedTransaction));
74+
/*
75+
* Upvote the post
76+
* "steem-java-api-learned-to-speak-graphene-update-5" written by
77+
* "dez1337" using 100% of the defaultAccounts voting power.
78+
*/
79+
steemJ.vote(new AccountName("dez1337"), new Permlink("steem-java-api-learned-to-speak-graphene-update-5"),
80+
(short) 100);
81+
82+
/*
83+
* Remove the vote made earlier.
84+
*/
85+
steemJ.cancelVote(new AccountName("dez1337"),
86+
new Permlink("steem-java-api-learned-to-speak-graphene-update-5"));
87+
88+
// Let the default account ("steemj") follow "cyriana"
89+
steemJ.follow(new AccountName("cyriana"));
90+
91+
// Let the default account ("steemj") unfollow "cyriana"
92+
steemJ.unfollow(new AccountName("cyriana"));
93+
94+
/*
95+
* Write a new post.
96+
*
97+
* Title = "Test of SteemJ 0.4.0"
98+
* Content = "Test using SteemJ 0. ..... "
99+
* Tags = "test", "dontvote"
100+
*
101+
*/
102+
CommentOperation myNewPost = steemJ.createPost("Test of SteemJ 0.4.0",
103+
"Test using SteemJ 0.4.0 by @dez1337 with a link to "
104+
+ "https://github.com/marvin-we/steem-java-api-wrapper "
105+
+ "and an image ![SteemJV2Logo](https://imgur.com/bIhZlYT.png).",
106+
new String[] { "test", "dontvote" });
107+
LOGGER.info(
108+
"SteemJ has generated some additional values for my new post. One good example is the permlink {} that I may need later on.",
109+
myNewPost.getPermlink().getLink());
110+
111+
/*
112+
* Write a new comment.
113+
*
114+
* Author of the post to reply to = "steemj"
115+
* Permlink of the post to reply to = "testofsteemj040"
116+
* Title = "Test of SteemJ 0.4.0"
117+
* Content = "Test using SteemJ 0. ..... "
118+
* Tags = "test"
119+
*
120+
*/
121+
steemJ.createComment(new AccountName("steemj"), new Permlink("testofsteemj040"),
122+
"Example comment without a link but with a @user .", new String[] { "test" });
123+
124+
/*
125+
* Delete the newly created post.
126+
*/
127+
steemJ.deletePostOrComment(myNewPost.getParentPermlink());
100128

101129
// Get the current Price
102130
LOGGER.info("The current price in the internal market is {}.",
@@ -128,7 +156,9 @@ public static void main(String args[]) {
128156
e.getError().getSteemErrorDetails().getData().getCode(),
129157
e.getError().getSteemErrorDetails().getMessage());
130158
} catch (SteemCommunicationException e) {
131-
LOGGER.error("Error!", e);
159+
LOGGER.error("A communication error occured!", e);
160+
} catch (SteemInvalidTransactionException e) {
161+
LOGGER.error("There was a problem to sign a transaction.", e);
132162
}
133163
}
134164

0 commit comments

Comments
 (0)