symbol-docs icon indicating copy to clipboard operation
symbol-docs copied to clipboard

[Guide] Add Java code examples to guides

Open dgarcia360 opened this issue 7 years ago • 6 comments

The documentation comes with guides on how to use each built-in feature. These guides show developers how to use built-in Symbol features while following step-by-step use cases.

Previously, most guides had java code examples ready to be used, but they were dropped because the Java SDK was outdated for a while. As the SDK developer, writing code examples will help you compare how the SDK code contrasts with others. Also, you will be helping others to get started with less effort.

Some outdated guides include:

  • [ ] https://docs.symbolplatform.com/guides/aggregate/adding-cosginatures-aggregate-complete.html

Instructions

  • Fork and clone nem2-docs repository.
  • Create a new folder under source/resources/examples/ called java.
  • Translate each TypeScript code example to Java. If possible, use the same comments.

Deliverables

  • Code examples for each guide.

Dependencies

  • ~https://github.com/nemtech/nem2-sdk-java/ updated to be 0.3 (cow) compatible.~
  • Release java SDK in Maven.

Resources

dgarcia360 avatar Apr 03 '19 15:04 dgarcia360

We'll work on this issue after #102 since we will refactor some code examples.

dgarcia360 avatar Dec 03 '19 17:12 dgarcia360

Please update samples for java. Need https://docs.symbolplatform.com/guides/aggregate/adding-cosginatures-aggregate-complete.html implementation.

vlad-bondarenko avatar Sep 08 '20 05:09 vlad-bondarenko

Task:

Revisit, add, or update examples or each section:

  • [x] account
  • [ ] accountlink
  • [x] aggregate
  • [ ] blockchain
  • [ ] metadata
  • [ ] monitor
  • [ ] mosaic
  • [ ] multisig
  • [ ] namespace
  • [ ] restriction
  • [ ] secretlock
  • [ ] transfer

Once examples have been updated, link them into de docs.

fboucquez avatar Sep 08 '20 12:09 fboucquez

@vlad-bondarenko , we have added the aggregate examples.

It uses the unreleased version of the SDK for now (0.20.4-SNAPSHOT), we are going to release soon.

fboucquez avatar Sep 14 '20 15:09 fboucquez

Error of exec example for https://docs.symbolplatform.com/guides/aggregate/adding-cosginatures-aggregate-complete.html

java.lang.IllegalArgumentException: Transaction to cosign should be announced before being able to cosign it at io.nem.symbol.sdk.model.transaction.CosignatureTransaction.lambda$new$0(CosignatureTransaction.java:43) ~[symbol-sdk-core-0.21.0.jar:?] at java.util.Optional.orElseThrow(Optional.java:408) ~[?:?] at io.nem.symbol.sdk.model.transaction.CosignatureTransaction.(CosignatureTransaction.java:41) ~[symbol-sdk-core-0.21.0.jar:?] at io.nem.symbol.sdk.model.transaction.CosignatureTransaction.create(CosignatureTransaction.java:54) ~[symbol-sdk-core-0.21.0.jar:?]

Use http://api-01.us-east-1.0.10.0.x.symboldev.network:3000, java-sdk symbol-sdk-okhttp-client version 0.21.0

vlad-bondarenko avatar Oct 08 '20 13:10 vlad-bondarenko

Exec modified example for symbol-sdk-okhttp-client', version: '0.22.1'

package nem;

import io.nem.symbol.core.utils.ConvertUtils; import io.nem.symbol.sdk.api.BinarySerialization; import io.nem.symbol.sdk.api.RepositoryFactory; import io.nem.symbol.sdk.api.TransactionRepository; import io.nem.symbol.sdk.infrastructure.BinarySerializationImpl; import io.nem.symbol.sdk.infrastructure.okhttp.RepositoryFactoryOkHttpImpl; import io.nem.symbol.sdk.model.account.Account; import io.nem.symbol.sdk.model.account.PublicAccount; import io.nem.symbol.sdk.model.message.PlainMessage; import io.nem.symbol.sdk.model.mosaic.Currency; import io.nem.symbol.sdk.model.network.NetworkType; import io.nem.symbol.sdk.model.transaction.*; import org.junit.jupiter.api.Test;

import java.math.BigInteger; import java.time.Duration; import java.util.Arrays; import java.util.Collections;

/**

  • Created by vbondarenko on 15.12.2020. */

public class AddingCosignaturesAggregateComplete {

@Test

// @Disabled void example() throws Exception {

    try (final RepositoryFactory repositoryFactory = new RepositoryFactoryOkHttpImpl(
            "http://api-01.us-west-2.0.10.0.x.symboldev.network:3000")) {
        // replace with recipient address


        /* start block 01 */
        NetworkType networkType = repositoryFactory.getNetworkType().toFuture().get();
        Currency networkCurrency = repositoryFactory.getNetworkCurrency().toFuture()
                .get();

        // replace with alice private key
        String alicePrivatekey = "";
        Account aliceAccount = Account.createFromPrivateKey(alicePrivatekey, networkType);

        // replace with bob public key
        String bobPublicKey = "";
        PublicAccount bobPublicAccount = PublicAccount
                .createFromPublicKey(bobPublicKey, networkType);

        TransferTransaction aliceTransferTransaction = TransferTransactionFactory
                .create(networkType,
                        Deadline.create(Duration.ofHours(2)),
                        bobPublicAccount.getAddress(),
                        Collections.singletonList(networkCurrency.createRelative(BigInteger.valueOf(100))))
                .message(PlainMessage.create("payout"))
                .build();

        TransferTransaction bobTransferTransaction = TransferTransactionFactory
                .create(networkType,
                        Deadline.create(Duration.ofHours(2)),
                        aliceAccount.getAddress(),
                        Collections.singletonList(networkCurrency.createRelative(BigInteger.valueOf(90))))

// Collections.singletonList(new Mosaic(new NamespaceId("collectible"), BigInteger.valueOf(1)))) .message(PlainMessage.create("payout")) .build();

        AggregateTransaction aggregateTransaction = AggregateTransactionFactory
                .createComplete(networkType,
                        Deadline.create(Duration.ofHours(2)),
                        Arrays.asList(aliceTransferTransaction.toAggregate(aliceAccount.getPublicAccount()),
                                bobTransferTransaction.toAggregate(bobPublicAccount)))
                .maxFee(BigInteger.valueOf(2000000)).build();
        /* end block 01 */

        /* start block 02 */
        String generationHash = repositoryFactory.getGenerationHash().toFuture().get();

        SignedTransaction signedTransactionNotComplete = aliceAccount
                .sign(aggregateTransaction, generationHash);
        System.out.println(signedTransactionNotComplete.getPayload());
        /* end block 02 */

        /* start block 03 */
        // replace with bob private key
        String bobPrivateKey = "";
        Account bobAccount = Account.createFromPrivateKey(bobPrivateKey, networkType);
        CosignatureSignedTransaction cosignedTransactionBob = CosignatureTransaction
                .create(aggregateTransaction)
                .signWith(bobAccount);

        System.out.println(cosignedTransactionBob.getSignature());
        System.out.println(cosignedTransactionBob.getParentHash());
        /* end block 03 */

        /* start block 04 */
        BinarySerialization serialization = BinarySerializationImpl.INSTANCE;

        AggregateTransactionFactory rectreatedAggregateTransactionFromPayload = (AggregateTransactionFactory) serialization
                .deserializeToFactory(
                        ConvertUtils.getBytes(signedTransactionNotComplete.getPayload()));

        //Added a new cosignature.
        rectreatedAggregateTransactionFromPayload.addCosignatures(cosignedTransactionBob);

        SignedTransaction signedTransactionComplete = aliceAccount
                .sign(rectreatedAggregateTransactionFromPayload.build(), generationHash);
        System.out.println(signedTransactionComplete.getHash());

        TransactionRepository transactionHttp = repositoryFactory.createTransactionRepository();

        transactionHttp.announce(signedTransactionComplete).toFuture().get();
        /* end block 04 */

    }
}

}

throw

java.lang.IllegalArgumentException: Transaction to cosign should be announced before being able to cosign it

at io.nem.symbol.sdk.model.transaction.CosignatureTransaction.lambda$new$0(CosignatureTransaction.java:43)
at java.base/java.util.Optional.orElseThrow(Optional.java:408)
at io.nem.symbol.sdk.model.transaction.CosignatureTransaction.<init>(CosignatureTransaction.java:41)
at io.nem.symbol.sdk.model.transaction.CosignatureTransaction.create(CosignatureTransaction.java:54)
at nem.AddingCosignaturesAggregateComplete.example(AddingCosignaturesAggregateComplete.java:92)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)

This it not fixed for symbol-sdk-okhttp-client.

vlad-bondarenko avatar Dec 26 '20 15:12 vlad-bondarenko