All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.provenance.client.common.extensions.CoinExtensions.kt Maven / Gradle / Ivy

The newest version!
package io.provenance.client.common.extensions

import cosmos.base.v1beta1.CoinOuterClass
import java.math.BigDecimal

fun Number.toCoin(denom: String): CoinOuterClass.Coin {
    return CoinOuterClass.Coin.newBuilder()
        .setAmount(BigDecimal(toString()).toPlainString())
        .setDenom(denom)
        .build()
}

fun String.toCoin(): CoinOuterClass.Coin {
    val split = indexOfFirst { it.isLetter() }
    require(split != 0) { "invalid amount for coin:$this" }
    require(split > 0) { "invalid denom for coin:$this" }

    return CoinOuterClass.Coin.newBuilder()
        .setAmount(BigDecimal(substring(0, split)).toPlainString())
        .setDenom(substring(split, length))
        .build()
}

internal operator fun List.times(other: BigDecimal): List = map { it * other }

internal operator fun CoinOuterClass.Coin.times(other: BigDecimal): CoinOuterClass.Coin {
    return CoinOuterClass.Coin
        .newBuilder()
        .mergeFrom(this)
        .setAmount(amount.toBigDecimal().times(other).toPlainString())
        .build()
}

internal fun List.discreteSum(): List =
    groupBy { it.denom }
        .toSortedMap()
        .map { it.key to it.value.sumOf { it.amount.toBigInteger() } }
        .map { "${it.second}${it.first}".toCoin() }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy