symbol / symbol-docs

Open-source documentation of Symbol
https://docs.symbol.dev
Apache License 2.0
56 stars 91 forks source link

[Guide] Add Java code examples to guides #101

Open dgarcia360 opened 5 years ago

dgarcia360 commented 5 years ago

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:

Instructions

Deliverables

Dependencies

Resources

dgarcia360 commented 4 years ago

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

vlad-bondarenko commented 4 years ago

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

fboucquez commented 4 years ago

Task:

Revisit, add, or update examples or each section:

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

fboucquez commented 4 years ago

@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.

vlad-bondarenko commented 3 years ago

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 commented 3 years ago

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;

/**

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.