Search in sources :

Example 6 with Currency

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

the class CurrencyServiceImpl method getNetworkCurrencies.

@Override
public Observable<NetworkCurrencies> getNetworkCurrencies() {
    return this.networkRepository.getNetworkProperties().flatMap(properties -> {
        if (properties.getChain() == null || properties.getChain().getCurrencyMosaicId() == null) {
            return Observable.error(new IllegalArgumentException("CurrencyMosaicId could not be loaded from network properties!!!"));
        }
        if (properties.getChain() == null || properties.getChain().getHarvestingMosaicId() == null) {
            return Observable.error(new IllegalArgumentException("HarvestingMosaicId could not be loaded from network properties!!"));
        }
        MosaicId currencyMosaicId = new MosaicId(FormatUtils.toSimpleHex(properties.getChain().getCurrencyMosaicId()));
        MosaicId harvestingMosaicId = new MosaicId(FormatUtils.toSimpleHex(properties.getChain().getHarvestingMosaicId()));
        List<MosaicId> mosaicIds = currencyMosaicId.equals(harvestingMosaicId) ? Collections.singletonList(currencyMosaicId) : Arrays.asList(currencyMosaicId, harvestingMosaicId);
        return this.getCurrencies(mosaicIds).map(currencies -> {
            Currency currency = currencies.stream().filter(c -> c.getMosaicId().filter(mosaicId -> mosaicId.equals(currencyMosaicId)).isPresent()).findFirst().orElseThrow(() -> new IllegalArgumentException("There is no Main Currency with id " + currencyMosaicId));
            Currency harvest = currencies.stream().filter(c -> c.getMosaicId().filter(mosaicId -> mosaicId.equals(harvestingMosaicId)).isPresent()).findFirst().orElseThrow(() -> new IllegalArgumentException("There is no Harvest Currency with id " + harvestingMosaicId));
            return new NetworkCurrencies(currency, harvest);
        });
    });
}
Also used : UnresolvedMosaicId(io.nem.symbol.sdk.model.mosaic.UnresolvedMosaicId) MosaicId(io.nem.symbol.sdk.model.mosaic.MosaicId) Currency(io.nem.symbol.sdk.model.mosaic.Currency) NetworkCurrencies(io.nem.symbol.sdk.model.mosaic.NetworkCurrencies)

Example 7 with Currency

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

the class SecretLockIntegrationTest method secretLockAndProofTransaction.

@ParameterizedTest
@MethodSource("provider")
void secretLockAndProofTransaction(RepositoryType type, LockHashAlgorithm lockHashAlgorithm) {
    RepositoryFactory repositoryFactory = getRepositoryFactory(type);
    byte[] secretSeed = RandomUtils.generateRandomBytes(20);
    String secret = ConvertUtils.toHex(lockHashAlgorithm.hash(secretSeed));
    String storedSecret = ConvertUtils.padHex(secret, LockHashAlgorithm.DEFAULT_SECRET_HEX_SIZE);
    if (lockHashAlgorithm == LockHashAlgorithm.HASH_160) {
        Assertions.assertEquals(LockHashAlgorithm.DEFAULT_SECRET_HEX_SIZE, storedSecret.length());
        Assertions.assertEquals(40, secret.length());
    } else {
        Assertions.assertEquals(LockHashAlgorithm.DEFAULT_SECRET_HEX_SIZE, storedSecret.length());
        Assertions.assertEquals(LockHashAlgorithm.DEFAULT_SECRET_HEX_SIZE, secret.length());
    }
    String proof = ConvertUtils.toHex(secretSeed);
    Account account = config().getNemesisAccount1();
    Account account2 = config().getNemesisAccount2();
    Currency currency = get(repositoryFactory.getNetworkCurrency());
    Mosaic mosaic = currency.createAbsolute(BigInteger.valueOf(1));
    BigInteger amount = mosaic.getAmount();
    BigInteger duration = BigInteger.valueOf(10000);
    SecretLockTransaction secretLockTransaction = SecretLockTransactionFactory.create(getNetworkType(), getDeadline(), mosaic, duration, lockHashAlgorithm, secret, account2.getAddress()).maxFee(maxFee).build();
    announceAndValidate(type, account, secretLockTransaction);
    SecretProofTransaction secretProofTransaction = SecretProofTransactionFactory.create(getNetworkType(), getDeadline(), lockHashAlgorithm, account2.getAddress(), secret, proof).maxFee(maxFee).build();
    SecretProofTransaction secretProofTransactionAnnounced = announceAndValidate(type, account, secretProofTransaction);
    sleep(500);
    Assertions.assertEquals(lockHashAlgorithm, secretProofTransactionAnnounced.getHashType());
    Assertions.assertEquals(account2.getAddress(), secretProofTransactionAnnounced.getRecipient());
    Assertions.assertEquals(storedSecret, secretProofTransactionAnnounced.getSecret());
    Assertions.assertEquals(proof, secretProofTransactionAnnounced.getProof());
    SecretLockRepository hashLockRepository = getRepositoryFactory(type).createSecretLockRepository();
    SecretLockInfo info = get(hashLockRepository.search(new SecretLockSearchCriteria().address(account.getAddress()).secret(storedSecret))).getData().get(0);
    Assertions.assertNotNull(info);
    Assertions.assertEquals(account.getAddress(), info.getOwnerAddress());
    Assertions.assertEquals(account2.getAddress(), info.getRecipientAddress());
    Assertions.assertEquals(amount, info.getAmount());
    Assertions.assertEquals(storedSecret, info.getSecret());
    Assertions.assertEquals(lockHashAlgorithm, info.getHashAlgorithm());
    Assertions.assertEquals(LockStatus.USED, info.getStatus());
    Page<SecretLockInfo> page = get(hashLockRepository.search(new SecretLockSearchCriteria().address(account.getAddress()).order(OrderBy.DESC)));
    Assertions.assertTrue(page.getData().stream().anyMatch(m -> m.getSecret().equals(storedSecret)));
    Assertions.assertEquals(20, page.getPageSize());
    SecretLockInfo infoSearch = page.getData().stream().filter(m -> m.getSecret().equals(storedSecret)).findFirst().get();
    Assertions.assertNotNull(infoSearch);
    Assertions.assertEquals(account.getAddress(), infoSearch.getOwnerAddress());
    Assertions.assertEquals(account2.getAddress(), infoSearch.getRecipientAddress());
    Assertions.assertEquals(amount, infoSearch.getAmount());
    Assertions.assertEquals(lockHashAlgorithm, infoSearch.getHashAlgorithm());
    Assertions.assertEquals(LockStatus.USED, infoSearch.getStatus());
    Assertions.assertEquals(storedSecret, infoSearch.getSecret());
}
Also used : SecretLockTransactionFactory(io.nem.symbol.sdk.model.transaction.SecretLockTransactionFactory) SecretProofTransaction(io.nem.symbol.sdk.model.transaction.SecretProofTransaction) SecretLockInfo(io.nem.symbol.sdk.model.transaction.SecretLockInfo) LockHashAlgorithm(io.nem.symbol.sdk.model.transaction.LockHashAlgorithm) Mosaic(io.nem.symbol.sdk.model.mosaic.Mosaic) Account(io.nem.symbol.sdk.model.account.Account) ConvertUtils(io.nem.symbol.core.utils.ConvertUtils) LockStatus(io.nem.symbol.sdk.model.transaction.LockStatus) Arguments(org.junit.jupiter.params.provider.Arguments) RepositoryFactory(io.nem.symbol.sdk.api.RepositoryFactory) Currency(io.nem.symbol.sdk.model.mosaic.Currency) ArrayList(java.util.ArrayList) SecretLockSearchCriteria(io.nem.symbol.sdk.api.SecretLockSearchCriteria) OrderBy(io.nem.symbol.sdk.api.OrderBy) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) TestInstance(org.junit.jupiter.api.TestInstance) Assertions(org.junit.jupiter.api.Assertions) BigInteger(java.math.BigInteger) SecretLockRepository(io.nem.symbol.sdk.api.SecretLockRepository) Page(io.nem.symbol.sdk.api.Page) SecretProofTransactionFactory(io.nem.symbol.sdk.model.transaction.SecretProofTransactionFactory) SecretLockTransaction(io.nem.symbol.sdk.model.transaction.SecretLockTransaction) MethodSource(org.junit.jupiter.params.provider.MethodSource) Account(io.nem.symbol.sdk.model.account.Account) SecretLockRepository(io.nem.symbol.sdk.api.SecretLockRepository) SecretLockInfo(io.nem.symbol.sdk.model.transaction.SecretLockInfo) SecretProofTransaction(io.nem.symbol.sdk.model.transaction.SecretProofTransaction) SecretLockTransaction(io.nem.symbol.sdk.model.transaction.SecretLockTransaction) SecretLockSearchCriteria(io.nem.symbol.sdk.api.SecretLockSearchCriteria) Currency(io.nem.symbol.sdk.model.mosaic.Currency) BigInteger(java.math.BigInteger) RepositoryFactory(io.nem.symbol.sdk.api.RepositoryFactory) Mosaic(io.nem.symbol.sdk.model.mosaic.Mosaic) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 8 with Currency

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

the class RepositoryFactoryConfigurationExamplesIntegrationTest method appDoSomeStuff.

public void appDoSomeStuff(RepositoryFactory repositoryFactory) throws ExecutionException, InterruptedException {
    // The application logic is exactly the same regardless of how the repository
    // factory was
    // set
    // Note: if rest is used, these values are cached form rest
    Currency currency = repositoryFactory.getNetworkCurrency().toFuture().get();
    String generationHash = repositoryFactory.getGenerationHash().toFuture().get();
    NetworkType networkType = repositoryFactory.getNetworkType().toFuture().get();
    Account sender = Account.generateNewAccount(networkType);
    Account recipient = Account.generateNewAccount(networkType);
    Duration epochAdjustment = repositoryFactory.getEpochAdjustment().toFuture().get();
    TransferTransaction transferTransaction = TransferTransactionFactory.create(networkType, Deadline.create(epochAdjustment), recipient.getAddress(), Collections.singletonList(currency.createRelative(BigInteger.TEN))).message(new PlainMessage("")).build();
    SignedTransaction signedTransaction = transferTransaction.signWith(sender, generationHash);
// Announce or store somewhere....
}
Also used : Account(io.nem.symbol.sdk.model.account.Account) PlainMessage(io.nem.symbol.sdk.model.message.PlainMessage) NetworkType(io.nem.symbol.sdk.model.network.NetworkType) Currency(io.nem.symbol.sdk.model.mosaic.Currency) Duration(java.time.Duration) TransferTransaction(io.nem.symbol.sdk.model.transaction.TransferTransaction) SignedTransaction(io.nem.symbol.sdk.model.transaction.SignedTransaction)

Example 9 with Currency

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

the class CurrencyServiceTest method getNetworkCurrencyFromNamespaceId.

@Test
void getNetworkCurrencyFromNamespaceId() throws Exception {
    MosaicId mosaicId = new MosaicId(BigInteger.TEN);
    Account account = Account.generateNewAccount(NetworkType.MAIN_NET);
    BigInteger supply = BigInteger.valueOf(12);
    MosaicInfo mosaicInfo = new MosaicInfo("abc", 1, mosaicId, supply, BigInteger.ONE, account.getAddress(), 4L, MosaicFlags.create(true, true, true), 10, BigInteger.TEN);
    Mockito.when(mosaicRepository.getMosaic(Mockito.eq(mosaicId))).thenReturn(Observable.just(mosaicInfo));
    String name = "some.alias";
    NamespaceId namespaceId = NamespaceId.createFromName(name);
    MosaicNames mosaicNames = new MosaicNames(mosaicId, Arrays.asList(new NamespaceName(name), new NamespaceName("some.alias2")));
    Mockito.when(namespaceRepository.getLinkedMosaicId(Mockito.eq(namespaceId))).thenReturn(Observable.just(mosaicId));
    Mockito.when(mosaicRepository.getMosaics(Mockito.eq(Collections.singletonList(mosaicId)))).thenReturn(Observable.just(Collections.singletonList(mosaicInfo)));
    Mockito.when(namespaceRepository.getMosaicsNames(Mockito.eq(Collections.singletonList(mosaicId)))).thenReturn(Observable.just(Collections.singletonList(mosaicNames)));
    Currency currency = service.getCurrencyFromNamespaceId(namespaceId).toFuture().get();
    Assertions.assertEquals(10, currency.getDivisibility());
    Assertions.assertEquals(mosaicId, currency.getUnresolvedMosaicId());
    Assertions.assertEquals(mosaicId, currency.getMosaicId().get());
    Assertions.assertEquals(namespaceId, currency.getNamespaceId().get());
    Assertions.assertEquals("some.alias", currency.getNamespaceId().get().getFullName().get());
    Assertions.assertTrue(currency.isTransferable());
    Assertions.assertTrue(currency.isSupplyMutable());
}
Also used : Account(io.nem.symbol.sdk.model.account.Account) MosaicInfo(io.nem.symbol.sdk.model.mosaic.MosaicInfo) NamespaceName(io.nem.symbol.sdk.model.namespace.NamespaceName) MosaicId(io.nem.symbol.sdk.model.mosaic.MosaicId) MosaicNames(io.nem.symbol.sdk.model.mosaic.MosaicNames) Currency(io.nem.symbol.sdk.model.mosaic.Currency) BigInteger(java.math.BigInteger) NamespaceId(io.nem.symbol.sdk.model.namespace.NamespaceId) Test(org.junit.jupiter.api.Test)

Example 10 with Currency

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

the class CurrencyServiceTest method getNetworkCurrencyFromMosaicIdWhenNoNamespaceError.

@Test
void getNetworkCurrencyFromMosaicIdWhenNoNamespaceError() throws Exception {
    MosaicId mosaicId = new MosaicId(BigInteger.TEN);
    Account account = Account.generateNewAccount(NetworkType.MAIN_NET);
    BigInteger supply = BigInteger.valueOf(12);
    MosaicInfo mosaicInfo = new MosaicInfo("abc", 1, mosaicId, supply, BigInteger.ONE, account.getAddress(), 4L, MosaicFlags.create(true, true, true), 10, BigInteger.TEN);
    Mockito.when(mosaicRepository.getMosaics(Mockito.eq(Collections.singletonList(mosaicId)))).thenReturn(Observable.just(Collections.singletonList(mosaicInfo)));
    Mockito.when(namespaceRepository.getMosaicsNames(Mockito.eq(Collections.singletonList(mosaicId)))).thenReturn(Observable.error(new RepositoryCallException("Not found", 404, null)));
    Currency currency = service.getCurrency(mosaicId).toFuture().get();
    Assertions.assertEquals(10, currency.getDivisibility());
    Assertions.assertEquals(mosaicId, currency.getUnresolvedMosaicId());
    Assertions.assertEquals(mosaicId, currency.getMosaicId().get());
    Assertions.assertFalse(currency.getNamespaceId().isPresent());
    Assertions.assertTrue(currency.isTransferable());
    Assertions.assertTrue(currency.isSupplyMutable());
}
Also used : Account(io.nem.symbol.sdk.model.account.Account) MosaicInfo(io.nem.symbol.sdk.model.mosaic.MosaicInfo) MosaicId(io.nem.symbol.sdk.model.mosaic.MosaicId) RepositoryCallException(io.nem.symbol.sdk.api.RepositoryCallException) Currency(io.nem.symbol.sdk.model.mosaic.Currency) BigInteger(java.math.BigInteger) Test(org.junit.jupiter.api.Test)

Aggregations

Currency (io.nem.symbol.sdk.model.mosaic.Currency)13 Test (org.junit.jupiter.api.Test)8 Account (io.nem.symbol.sdk.model.account.Account)7 MosaicId (io.nem.symbol.sdk.model.mosaic.MosaicId)6 BigInteger (java.math.BigInteger)6 MosaicInfo (io.nem.symbol.sdk.model.mosaic.MosaicInfo)5 NetworkCurrencies (io.nem.symbol.sdk.model.mosaic.NetworkCurrencies)4 PlainMessage (io.nem.symbol.sdk.model.message.PlainMessage)3 CurrencyBuilder (io.nem.symbol.sdk.model.mosaic.CurrencyBuilder)3 Mosaic (io.nem.symbol.sdk.model.mosaic.Mosaic)3 MosaicNames (io.nem.symbol.sdk.model.mosaic.MosaicNames)3 NamespaceId (io.nem.symbol.sdk.model.namespace.NamespaceId)3 TransferTransaction (io.nem.symbol.sdk.model.transaction.TransferTransaction)3 Duration (java.time.Duration)3 RepositoryFactory (io.nem.symbol.sdk.api.RepositoryFactory)2 NamespaceName (io.nem.symbol.sdk.model.namespace.NamespaceName)2 ArrayList (java.util.ArrayList)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 KeyPair (io.nem.symbol.core.crypto.KeyPair)1 ConvertUtils (io.nem.symbol.core.utils.ConvertUtils)1