use of io.nem.symbol.sdk.model.mosaic.NetworkCurrencies in project nem2-sdk-java by nemtech.
the class RepositoryFactoryConfigurationExamplesIntegrationTest method bootAppUsingLegacyHardcodedCurrencies.
@Test
void bootAppUsingLegacyHardcodedCurrencies() throws ExecutionException, InterruptedException {
// Option 3) Client app boot time relaying on some of the rest configuration.
// User uses the
// legacy hardcoded sdk currencies
RepositoryFactoryConfiguration configuration = new RepositoryFactoryConfiguration("http://localhost:3000");
configuration.withNetworkCurrencies(new NetworkCurrencies(Currency.CAT_CURRENCY, Currency.CAT_HARVEST));
try (RepositoryFactory repositoryFactory = new RepositoryFactoryVertxImpl(configuration)) {
appDoSomeStuff(repositoryFactory);
}
}
use of io.nem.symbol.sdk.model.mosaic.NetworkCurrencies in project nem2-sdk-java by nemtech.
the class RepositoryFactoryConfigurationExamplesIntegrationTest method bootAppFullyOffline.
@Test
void bootAppFullyOffline() throws ExecutionException, InterruptedException {
// Option 1) Client app boot time. The clients defines the configuration to work
// offline.
RepositoryFactoryConfiguration configuration = new RepositoryFactoryConfiguration("http://localhost:3000");
configuration.withNetworkType(NetworkType.MAIN_NET);
configuration.withGenerationHash("abc");
Currency currency = new CurrencyBuilder(NamespaceId.createFromName("my.custom.currency"), 6).build();
Currency harvest = new CurrencyBuilder(NamespaceId.createFromName("my.custom.harvest"), 3).build();
configuration.withNetworkCurrencies(new NetworkCurrencies(currency, harvest));
try (RepositoryFactory repositoryFactory = new RepositoryFactoryVertxImpl(configuration)) {
appDoSomeStuff(repositoryFactory);
}
}
use of io.nem.symbol.sdk.model.mosaic.NetworkCurrencies in project nem2-sdk-java by nemtech.
the class RepositoryFactoryOkHttpImplTest method getRestProvidedNetworkCurrencies.
@Test
public void getRestProvidedNetworkCurrencies() throws Exception {
String baseUrl = "https://localhost:1934/path";
RepositoryFactoryConfiguration configuration = new RepositoryFactoryConfiguration(baseUrl);
configuration.withGenerationHash("abc");
configuration.withNetworkType(NetworkType.MAIN_NET);
RepositoryFactory factory = new RepositoryFactoryOkHttpImpl(configuration) {
@Override
protected Observable<NetworkCurrencies> loadNetworkCurrencies() {
return Observable.just(new NetworkCurrencies(Currency.CAT_CURRENCY, Currency.CAT_HARVEST));
}
};
Assertions.assertEquals(configuration.getNetworkType(), factory.getNetworkType().toFuture().get());
Assertions.assertEquals(configuration.getGenerationHash(), factory.getGenerationHash().toFuture().get());
Assertions.assertEquals(Currency.CAT_HARVEST, factory.getHarvestCurrency().toFuture().get());
Assertions.assertEquals(Currency.CAT_CURRENCY, factory.getNetworkCurrency().toFuture().get());
}
use of io.nem.symbol.sdk.model.mosaic.NetworkCurrencies in project nem2-sdk-java by nemtech.
the class CurrencyServiceTest method getNetworkCurrencies.
@Test
void getNetworkCurrencies() throws Exception {
NetworkConfiguration networkConfiguration = Mockito.mock(NetworkConfiguration.class);
ChainProperties chainProperties = Mockito.mock(ChainProperties.class);
String currencyMosaicIdHex = "0x62EF'46FD'6555'AAAA";
MosaicId currencyMosaicId = new MosaicId(FormatUtils.toSimpleHex(currencyMosaicIdHex));
Mockito.when(chainProperties.getCurrencyMosaicId()).thenReturn(currencyMosaicIdHex);
String harvestMosaicIdHex = "0x62EF'46FD'6555'BBBB";
MosaicId harvestMosaicId = new MosaicId(FormatUtils.toSimpleHex(harvestMosaicIdHex));
Mockito.when(chainProperties.getHarvestingMosaicId()).thenReturn(harvestMosaicIdHex);
Mockito.when(networkConfiguration.getChain()).thenReturn(chainProperties);
Mockito.when(networkRepository.getNetworkProperties()).thenReturn(Observable.just(networkConfiguration));
Address account = Address.generateRandom(NetworkType.MIJIN_TEST);
MosaicInfo currencyMosaicInfo = new MosaicInfo("abc", 1, currencyMosaicId, BigInteger.valueOf(100), BigInteger.ONE, account, 4L, MosaicFlags.create(false, true, false), 10, BigInteger.TEN);
MosaicInfo harvestMosaicInfo = new MosaicInfo("abc", 1, harvestMosaicId, BigInteger.valueOf(200), BigInteger.ONE, account, 4L, MosaicFlags.create(true, false, true), 3, BigInteger.TEN);
Mockito.when(mosaicRepository.getMosaics(Mockito.eq(Arrays.asList(currencyMosaicId, harvestMosaicId)))).thenReturn(Observable.just(Arrays.asList(currencyMosaicInfo, harvestMosaicInfo)));
Mockito.when(namespaceRepository.getMosaicsNames(Mockito.eq(Arrays.asList(currencyMosaicId, harvestMosaicId)))).thenReturn(Observable.just(Collections.emptyList()));
NetworkCurrencies networkCurrencies = service.getNetworkCurrencies().toFuture().get();
Assertions.assertEquals(currencyMosaicInfo.toCurrency(), networkCurrencies.getCurrency());
Assertions.assertEquals(harvestMosaicInfo.toCurrency(), networkCurrencies.getHarvest());
}
use of io.nem.symbol.sdk.model.mosaic.NetworkCurrencies 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);
});
});
}
Aggregations