use of com.github.rosklyar.client.mosaic.domain.MosaicMetaData in project nem-library by rosklyar.
the class DefaultFeeCalculator method mosaicFee.
private long mosaicFee(MosaicTransfer mosaicTransfer, int times) {
String namespaceId = mosaicTransfer.mosaicId.namespaceId;
String mosaicName = mosaicTransfer.mosaicId.name;
MosaicsMetaDataResponse mosaicsMetaDataResponse = mosaicClient.mosaics(namespaceId, null, 100);
MosaicMetaData mosaicMetaData = mosaicsMetaDataResponse.data.stream().filter(mmd -> mmd.mosaic.id.name.equals(mosaicName)).findFirst().orElseThrow(() -> new RuntimeException(String.format("No mosaic definition with name=%s found in namespace=%s", mosaicName, namespaceId)));
int divisibility = mosaicMetaData.mosaic.properties.stream().filter(pr -> pr.name.equals("divisibility")).findFirst().map(pr -> Integer.valueOf(pr.value)).orElseThrow(RuntimeException::new);
OwnedMosaicsResponse ownedMosaicsResponse = accountClient.ownedMosaics(accountClient.getFromPublicKey(mosaicMetaData.mosaic.creator).account.address);
long mosaicQuantity = ownedMosaicsResponse.data.stream().filter(ownedMosaic -> ownedMosaic.mosaicId.name.equals(mosaicName)).findFirst().map(ownedMosaic -> ownedMosaic.quantity).orElseThrow(RuntimeException::new);
if (isSmallBusinessMosaic(divisibility, mosaicQuantity)) {
return 1L;
}
long supplyRelatedAdjustment = roundToLong(0.8 * log(valueOf(maxMosaicQuantity).divide(valueOf(mosaicQuantity)).doubleValue()), DOWN);
long xemEquivalent = roundToLong(valueOf(8999999999L * mosaicTransfer.quantity * times).divide(valueOf(mosaicQuantity)).doubleValue(), DOWN);
long microXemEquivalent = xemEquivalent * TEN.pow(nemDivisibility).longValue();
long xemFee = calculateXemTransferFee(microXemEquivalent);
return max(1L, xemFee - supplyRelatedAdjustment);
}
Aggregations