Search in sources :

Example 16 with Mosaic

use of io.nem.symbol.sdk.model.mosaic.Mosaic in project nem2-sdk-java by nemtech.

the class TransactionServiceIntegrationTest method transferUsingAliasesAggregate.

private AggregateTransaction transferUsingAliasesAggregate(Account sender, RepositoryType type, String mosaicAlias, String recipientAlias, BigInteger amount) {
    NamespaceId recipientNamespace = NamespaceId.createFromName(recipientAlias);
    NamespaceId mosaicNamespace = NamespaceId.createFromName(mosaicAlias);
    System.out.println("Sending " + amount + " Mosaic to: " + mosaicAlias);
    TransferTransactionFactory factory = TransferTransactionFactory.create(getNetworkType(), getDeadline(), recipientNamespace, Collections.singletonList(new Mosaic(mosaicNamespace, amount))).message(new PlainMessage("E2ETest:TransactionServiceIntegrationTest"));
    factory.maxFee(maxFee);
    TransferTransaction transferTransaction = factory.build();
    Assertions.assertTrue(transferTransaction.getMosaics().get(0).getId() instanceof NamespaceId);
    Assertions.assertTrue(transferTransaction.getRecipient() instanceof NamespaceId);
    Pair<TransferTransaction, AggregateTransaction> pair = announceAggregateAndValidate(type, transferTransaction, sender);
    TransferTransaction processedTransferTransaction = pair.getLeft();
    Assertions.assertEquals(amount, processedTransferTransaction.getMosaics().get(0).getAmount());
    System.out.println(toJson(processedTransferTransaction));
    Assertions.assertTrue(processedTransferTransaction.getMosaics().get(0).getId() instanceof NamespaceId);
    Assertions.assertTrue(processedTransferTransaction.getRecipient() instanceof NamespaceId);
    return pair.getRight();
}
Also used : PlainMessage(io.nem.symbol.sdk.model.message.PlainMessage) TransferTransactionFactory(io.nem.symbol.sdk.model.transaction.TransferTransactionFactory) AggregateTransaction(io.nem.symbol.sdk.model.transaction.AggregateTransaction) NamespaceId(io.nem.symbol.sdk.model.namespace.NamespaceId) TransferTransaction(io.nem.symbol.sdk.model.transaction.TransferTransaction) Mosaic(io.nem.symbol.sdk.model.mosaic.Mosaic)

Example 17 with Mosaic

use of io.nem.symbol.sdk.model.mosaic.Mosaic in project nem2-sdk-java by nemtech.

the class TransferTransactionMapper method createFactory.

@Override
protected TransactionFactory<TransferTransaction> createFactory(NetworkType networkType, Deadline deadline, TransferTransactionDTO transaction) {
    List<Mosaic> mosaics = new ArrayList<>();
    if (transaction.getMosaics() != null) {
        mosaics = transaction.getMosaics().stream().map(mosaic -> new Mosaic(toUnresolvedMosaicId(mosaic.getId()), mosaic.getAmount())).collect(Collectors.toList());
    }
    TransferTransactionFactory transferTransactionFactory = TransferTransactionFactory.create(networkType, deadline, MapperUtils.toUnresolvedAddress(transaction.getRecipientAddress()), mosaics);
    Message.createFromHexPayload(transaction.getMessage()).ifPresent(transferTransactionFactory::message);
    return transferTransactionFactory;
}
Also used : TransferTransactionFactory(io.nem.symbol.sdk.model.transaction.TransferTransactionFactory) ArrayList(java.util.ArrayList) Mosaic(io.nem.symbol.sdk.model.mosaic.Mosaic) UnresolvedMosaic(io.nem.symbol.sdk.openapi.vertx.model.UnresolvedMosaic)

Example 18 with Mosaic

use of io.nem.symbol.sdk.model.mosaic.Mosaic in project nem2-sdk-java by nemtech.

the class TransactionServiceTest method aggregateTransferTransactionResolveAlias.

@Test
void aggregateTransferTransactionResolveAlias() throws ExecutionException, InterruptedException {
    ArrayList<Mosaic> mosaics = new ArrayList<>();
    mosaics.add(new Mosaic(mosaicId1, BigInteger.valueOf(1)));
    mosaics.add(new Mosaic(mosaicNamespace2, BigInteger.valueOf(2)));
    mosaics.add(new Mosaic(mosaicId3, BigInteger.valueOf(3)));
    UnresolvedAddress recipient = addressNamespace1;
    String transactionHash = "aaaa";
    TransactionFactory<TransferTransaction> factory = TransferTransactionFactory.create(NetworkType.MIJIN_TEST, new Deadline(BigInteger.ONE), recipient, mosaics).message(new PlainMessage("")).transactionInfo(TransactionInfo.create(height, 0, "ABC", "BBBB", ""));
    // Extra transfer not aliases
    TransactionFactory<TransferTransaction> extraTransaction = TransferTransactionFactory.create(NetworkType.MIJIN_TEST, new Deadline(BigInteger.ONE), address2, Collections.singletonList(new Mosaic(mosaicId1, BigInteger.valueOf(1)))).transactionInfo(TransactionInfo.create(height, 1, "ABC", "CCCC", ""));
    TransferTransaction transferTransaction = factory.build();
    transferTransaction.toAggregate(Account.generateNewAccount(networkType).getPublicAccount());
    TransactionFactory<AggregateTransaction> aggregateTransactionFactory = AggregateTransactionFactory.createComplete(NetworkType.MIJIN_TEST, new Deadline(BigInteger.ONE), Arrays.asList(transferTransaction, extraTransaction.build().toAggregate(Account.generateNewAccount(networkType).getPublicAccount()))).transactionInfo(TransactionInfo.create(height, 0, "ABC", transactionHash, ""));
    AggregateTransaction aggregateTransaction = aggregateTransactionFactory.build();
    simulateStatement(height, 1, 1);
    List<String> hashes = Collections.singletonList(transactionHash);
    Mockito.when(transactionRepositoryMock.getTransactions(Mockito.eq(TransactionGroup.CONFIRMED), Mockito.eq(hashes))).thenReturn(Observable.just(Collections.singletonList(aggregateTransaction)));
    AggregateTransaction aggregateResolvedTransaction = (AggregateTransaction) service.resolveAliases(hashes).toFuture().get().get(0);
    TransferTransaction resolvedTransaction = (TransferTransaction) aggregateResolvedTransaction.getInnerTransactions().get(0);
    Assertions.assertEquals(transferTransaction.getMosaics().size(), resolvedTransaction.getMosaics().size());
    Assertions.assertEquals(transferTransaction.getMosaics().get(0).getId(), resolvedTransaction.getMosaics().get(0).getId());
    Assertions.assertEquals(BigInteger.valueOf(1), resolvedTransaction.getMosaics().get(0).getAmount());
    Assertions.assertEquals(mosaicId2, resolvedTransaction.getMosaics().get(1).getId());
    Assertions.assertEquals(BigInteger.valueOf(2), resolvedTransaction.getMosaics().get(1).getAmount());
    Assertions.assertEquals(transferTransaction.getMosaics().get(2).getId(), resolvedTransaction.getMosaics().get(2).getId());
    Assertions.assertEquals(BigInteger.valueOf(3), resolvedTransaction.getMosaics().get(2).getAmount());
    Assertions.assertEquals(address1, resolvedTransaction.getRecipient());
}
Also used : UnresolvedAddress(io.nem.symbol.sdk.model.account.UnresolvedAddress) PlainMessage(io.nem.symbol.sdk.model.message.PlainMessage) Deadline(io.nem.symbol.sdk.model.transaction.Deadline) AggregateTransaction(io.nem.symbol.sdk.model.transaction.AggregateTransaction) ArrayList(java.util.ArrayList) TransferTransaction(io.nem.symbol.sdk.model.transaction.TransferTransaction) Mosaic(io.nem.symbol.sdk.model.mosaic.Mosaic) Test(org.junit.jupiter.api.Test)

Example 19 with Mosaic

use of io.nem.symbol.sdk.model.mosaic.Mosaic in project nem2-sdk-java by nemtech.

the class TransactionServiceTest method transferTransactionResolveAlias.

@Test
void transferTransactionResolveAlias() throws ExecutionException, InterruptedException {
    ArrayList<Mosaic> mosaics = new ArrayList<>();
    mosaics.add(new Mosaic(mosaicId1, BigInteger.valueOf(1)));
    mosaics.add(new Mosaic(mosaicNamespace2, BigInteger.valueOf(2)));
    mosaics.add(new Mosaic(mosaicId3, BigInteger.valueOf(3)));
    UnresolvedAddress recipient = addressNamespace1;
    String transactionHash = "aaaa";
    TransactionFactory<TransferTransaction> factory = TransferTransactionFactory.create(NetworkType.MIJIN_TEST, new Deadline(BigInteger.ONE), recipient, mosaics).transactionInfo(TransactionInfo.create(height, 0, "ABC", transactionHash, ""));
    simulateStatement(height, 1, 0);
    TransferTransaction transaction = factory.build();
    List<String> hashes = Collections.singletonList(transactionHash);
    Mockito.when(transactionRepositoryMock.getTransactions(Mockito.eq(TransactionGroup.CONFIRMED), Mockito.eq(hashes))).thenReturn(Observable.just(Collections.singletonList(transaction)));
    TransferTransaction resolvedTransaction = (TransferTransaction) service.resolveAliases(hashes).toFuture().get().get(0);
    Assertions.assertEquals(transaction.getMosaics().size(), resolvedTransaction.getMosaics().size());
    Assertions.assertEquals(transaction.getMosaics().get(0).getId(), resolvedTransaction.getMosaics().get(0).getId());
    Assertions.assertEquals(BigInteger.valueOf(1), resolvedTransaction.getMosaics().get(0).getAmount());
    Assertions.assertEquals(mosaicId2, resolvedTransaction.getMosaics().get(1).getId());
    Assertions.assertEquals(BigInteger.valueOf(2), resolvedTransaction.getMosaics().get(1).getAmount());
    Assertions.assertEquals(transaction.getMosaics().get(2).getId(), resolvedTransaction.getMosaics().get(2).getId());
    Assertions.assertEquals(BigInteger.valueOf(3), resolvedTransaction.getMosaics().get(2).getAmount());
    Assertions.assertEquals(address1, resolvedTransaction.getRecipient());
}
Also used : UnresolvedAddress(io.nem.symbol.sdk.model.account.UnresolvedAddress) Deadline(io.nem.symbol.sdk.model.transaction.Deadline) ArrayList(java.util.ArrayList) TransferTransaction(io.nem.symbol.sdk.model.transaction.TransferTransaction) Mosaic(io.nem.symbol.sdk.model.mosaic.Mosaic) Test(org.junit.jupiter.api.Test)

Example 20 with Mosaic

use of io.nem.symbol.sdk.model.mosaic.Mosaic in project nem2-sdk-java by nemtech.

the class TransactionServiceTest method aggregateTransferTransactionResolveAliasFailWhenNoTransactionInfoInner.

@Test
void aggregateTransferTransactionResolveAliasFailWhenNoTransactionInfoInner() {
    ArrayList<Mosaic> mosaics = new ArrayList<>();
    mosaics.add(new Mosaic(mosaicId1, BigInteger.valueOf(1)));
    mosaics.add(new Mosaic(mosaicNamespace2, BigInteger.valueOf(2)));
    mosaics.add(new Mosaic(mosaicId3, BigInteger.valueOf(3)));
    UnresolvedAddress recipient = addressNamespace1;
    String transactionHash = "aaaa";
    TransactionFactory<TransferTransaction> factory = TransferTransactionFactory.create(NetworkType.MIJIN_TEST, new Deadline(BigInteger.ONE), recipient, mosaics).message(new PlainMessage("")).transactionInfo(TransactionInfo.create(height, 0, "ABC", "BBBB", ""));
    // Extra transfer not aliases
    TransactionFactory<TransferTransaction> extraTransaction = TransferTransactionFactory.create(NetworkType.MIJIN_TEST, new Deadline(BigInteger.ONE), address2, Collections.singletonList(new Mosaic(mosaicId1, BigInteger.valueOf(1)))).message(new PlainMessage(""));
    TransferTransaction transferTransaction = factory.build();
    transferTransaction.toAggregate(Account.generateNewAccount(networkType).getPublicAccount());
    TransactionFactory<AggregateTransaction> aggregateTransactionFactory = AggregateTransactionFactory.createComplete(NetworkType.MIJIN_TEST, new Deadline(BigInteger.ONE), Arrays.asList(transferTransaction, extraTransaction.build().toAggregate(Account.generateNewAccount(networkType).getPublicAccount()))).transactionInfo(TransactionInfo.create(height, 0, "ABC", transactionHash, ""));
    AggregateTransaction aggregateTransaction = aggregateTransactionFactory.build();
    simulateStatement(height, 1, 1);
    List<String> hashes = Collections.singletonList(transactionHash);
    Mockito.when(transactionRepositoryMock.getTransactions(Mockito.eq(TransactionGroup.CONFIRMED), Mockito.eq(hashes))).thenReturn(Observable.just(Collections.singletonList(aggregateTransaction)));
    IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, () -> {
        ExceptionUtils.propagate(service.resolveAliases(hashes).toFuture()::get);
    });
    Assertions.assertEquals("TransactionIndex cannot be loaded from Transaction TRANSFER", exception.getMessage());
}
Also used : UnresolvedAddress(io.nem.symbol.sdk.model.account.UnresolvedAddress) Deadline(io.nem.symbol.sdk.model.transaction.Deadline) AggregateTransaction(io.nem.symbol.sdk.model.transaction.AggregateTransaction) ArrayList(java.util.ArrayList) PlainMessage(io.nem.symbol.sdk.model.message.PlainMessage) TransferTransaction(io.nem.symbol.sdk.model.transaction.TransferTransaction) Mosaic(io.nem.symbol.sdk.model.mosaic.Mosaic) Test(org.junit.jupiter.api.Test)

Aggregations

Mosaic (io.nem.symbol.sdk.model.mosaic.Mosaic)35 Test (org.junit.jupiter.api.Test)26 PlainMessage (io.nem.symbol.sdk.model.message.PlainMessage)24 MosaicId (io.nem.symbol.sdk.model.mosaic.MosaicId)19 TransferTransaction (io.nem.symbol.sdk.model.transaction.TransferTransaction)18 BigInteger (java.math.BigInteger)18 Deadline (io.nem.symbol.sdk.model.transaction.Deadline)15 Address (io.nem.symbol.sdk.model.account.Address)12 ArrayList (java.util.ArrayList)12 UnresolvedAddress (io.nem.symbol.sdk.model.account.UnresolvedAddress)8 AggregateTransaction (io.nem.symbol.sdk.model.transaction.AggregateTransaction)8 NamespaceId (io.nem.symbol.sdk.model.namespace.NamespaceId)6 SignedTransaction (io.nem.symbol.sdk.model.transaction.SignedTransaction)6 NetworkType (io.nem.symbol.sdk.model.network.NetworkType)5 TransferTransactionFactory (io.nem.symbol.sdk.model.transaction.TransferTransactionFactory)5 BinarySerializationImpl (io.nem.symbol.sdk.infrastructure.BinarySerializationImpl)4 Account (io.nem.symbol.sdk.model.account.Account)4 PublicAccount (io.nem.symbol.sdk.model.account.PublicAccount)4 Currency (io.nem.symbol.sdk.model.mosaic.Currency)4 HashLockTransaction (io.nem.symbol.sdk.model.transaction.HashLockTransaction)4