io.provenance.client.gas.estimators.CosmosSimulation.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pb-grpc-client-kotlin Show documentation
Show all versions of pb-grpc-client-kotlin Show documentation
A GRPC client for communicating with the Provenance Blockchain
The newest version!
package io.provenance.client.gas.estimators
import cosmos.tx.v1beta1.ServiceOuterClass
import io.provenance.client.common.extensions.toCoin
import io.provenance.client.common.gas.GasEstimate
import io.provenance.client.common.gas.prices.GasPrices
import io.provenance.client.grpc.gasEstimator
import kotlin.math.ceil
/**
* Cosmos simulation gas estimation. Must be used when interacting with pbc 1.7 or lower.
* TODO - Remove once mainnet.version > 1.8
*/
internal fun cosmosSimulationGasEstimator(gasPrices: GasPrices) = gasEstimator { tx, adjustment ->
val price = gasPrices()
val sim = cosmosService.simulate(
ServiceOuterClass.SimulateRequest.newBuilder()
.setTxBytes(tx.toByteString())
.build()
)
val limit = ceil(sim.gasInfo.gasUsed * adjustment).toLong()
val feeAmount = ceil(limit * price.amount.toDouble()).toLong()
GasEstimate(limit, listOf(feeAmount.toCoin(price.denom)))
}