Skip to content

Commit

Permalink
Add existing extension methods to Provenance coroutine-based client f…
Browse files Browse the repository at this point in the history
…or feature parity (#1665)
  • Loading branch information
mwoods-figure authored Aug 30, 2023
1 parent 7664651 commit 045bca8
Show file tree
Hide file tree
Showing 13 changed files with 425 additions and 40 deletions.
1 change: 1 addition & 0 deletions protoBindings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ Checkout `./bindings` for all supported language bindings.
```bash
./gradlew clean :kotlin:jar
```

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package io.provenance.client.protobuf.extensions
import io.provenance.attribute.v1.QueryAttributesRequest
import io.provenance.client.protobuf.paginationBuilder
import java.util.concurrent.TimeUnit
import io.provenance.attribute.v1.QueryGrpc.QueryBlockingStub as Attribute
import io.provenance.attribute.v1.QueryGrpc.QueryBlockingStub as BlockingAttribute
import io.provenance.attribute.v1.QueryGrpcKt.QueryCoroutineStub as CoroutineAttribute

/**
* Given an address, fetch all [io.provenance.attribute.v1.Attribute] instances associated with it.
Expand All @@ -14,9 +15,40 @@ import io.provenance.attribute.v1.QueryGrpc.QueryBlockingStub as Attribute
* @param deadlineInSeconds
* @return A list of [io.provenance.attribute.v1.Attribute]
*/
fun Attribute.getAllAttributes(
fun BlockingAttribute.getAllAttributes(
address: String,
deadlineInSeconds: Long? = null
deadlineInSeconds: Long? = null,
): List<io.provenance.attribute.v1.Attribute> {
var offset = 0
var total: Long
val limit = 100
val attributes = mutableListOf<io.provenance.attribute.v1.Attribute>()

do {
val request = QueryAttributesRequest.newBuilder()
.setAccount(address)
.setPagination(paginationBuilder(offset, limit))
.build()

val client = if (deadlineInSeconds == null) {
this
} else {
this.withDeadlineAfter(deadlineInSeconds, TimeUnit.SECONDS)
}

val results = client.attributes(request)

total = results.pagination?.total ?: results.attributesCount.toLong()
offset += limit
attributes.addAll(results.attributesList)
} while (attributes.count() < total)

return attributes
}

suspend fun CoroutineAttribute.getAllAttributes(
address: String,
deadlineInSeconds: Long? = null,
): List<io.provenance.attribute.v1.Attribute> {
var offset = 0
var total: Long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package io.provenance.client.protobuf.extensions

import cosmos.auth.v1beta1.QueryOuterClass
import io.provenance.marker.v1.MarkerAccount
import cosmos.auth.v1beta1.QueryGrpc.QueryBlockingStub as Auth
import cosmos.auth.v1beta1.QueryGrpc.QueryBlockingStub as BlockingAuth
import cosmos.auth.v1beta1.QueryGrpcKt.QueryCoroutineStub as CoroutineAuth

/**
* Given an address, get the base account associated with it.
Expand All @@ -12,14 +13,46 @@ import cosmos.auth.v1beta1.QueryGrpc.QueryBlockingStub as Auth
* @param bech32Address The bech32 address to fetch.
* @return [cosmos.auth.v1beta1.Auth.BaseAccount] or throw [IllegalArgumentException] if the account type is not supported.
*/
fun Auth.getBaseAccount(bech32Address: String): cosmos.auth.v1beta1.Auth.BaseAccount =
fun BlockingAuth.getBaseAccount(bech32Address: String): cosmos.auth.v1beta1.Auth.BaseAccount =
account(QueryOuterClass.QueryAccountRequest.newBuilder().setAddress(bech32Address).build()).account.run {
when {
this.`is`(cosmos.auth.v1beta1.Auth.BaseAccount::class.java) -> unpack(cosmos.auth.v1beta1.Auth.BaseAccount::class.java)
else -> throw IllegalArgumentException("Account type not handled:$typeUrl")
}
}

/**
* Given an address, get the base account associated with it.
*
* See [Accounts](https://github.com/FigureTechnologies/service-wallet/blob/v45/pb-client/src/main/kotlin/com/figure/wallet/pbclient/client/grpc/Accounts.kt#L18).
*
* @param bech32Address The bech32 address to fetch.
* @return [cosmos.auth.v1beta1.Auth.BaseAccount] or throw [IllegalArgumentException] if the account type is not supported.
*/
suspend fun CoroutineAuth.getBaseAccount(bech32Address: String): cosmos.auth.v1beta1.Auth.BaseAccount =
account(QueryOuterClass.QueryAccountRequest.newBuilder().setAddress(bech32Address).build()).account.run {
when {
this.`is`(cosmos.auth.v1beta1.Auth.BaseAccount::class.java) -> unpack(cosmos.auth.v1beta1.Auth.BaseAccount::class.java)
else -> throw IllegalArgumentException("Account type not handled:$typeUrl")
}
}

/**
* Given an address, get the marker account associated with it.
*
* See [Accounts](https://github.com/FigureTechnologies/service-wallet/blob/v45/pb-client/src/main/kotlin/com/figure/wallet/pbclient/client/grpc/Accounts.kt#L26).
*
* @param bech32Address The bech32 address to fetch.
* @return [MarkerAccount] or throw [IllegalArgumentException] if the account type is not supported.
*/
fun BlockingAuth.getMarkerAccount(bech32Address: String): MarkerAccount =
account(QueryOuterClass.QueryAccountRequest.newBuilder().setAddress(bech32Address).build()).account.run {
when {
this.`is`(MarkerAccount::class.java) -> unpack(MarkerAccount::class.java)
else -> throw IllegalArgumentException("Account type not handled:$typeUrl")
}
}

/**
* Given an address, get the marker account associated with it.
*
Expand All @@ -28,7 +61,7 @@ fun Auth.getBaseAccount(bech32Address: String): cosmos.auth.v1beta1.Auth.BaseAcc
* @param bech32Address The bech32 address to fetch.
* @return [MarkerAccount] or throw [IllegalArgumentException] if the account type is not supported.
*/
fun Auth.getMarkerAccount(bech32Address: String): MarkerAccount =
suspend fun CoroutineAuth.getMarkerAccount(bech32Address: String): MarkerAccount =
account(QueryOuterClass.QueryAccountRequest.newBuilder().setAddress(bech32Address).build()).account.run {
when {
this.`is`(MarkerAccount::class.java) -> unpack(MarkerAccount::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package io.provenance.client.protobuf.extensions

import cosmos.bank.v1beta1.QueryOuterClass
import cosmos.base.v1beta1.CoinOuterClass
import cosmos.bank.v1beta1.QueryGrpc.QueryBlockingStub as Bank
import cosmos.bank.v1beta1.QueryGrpc.QueryBlockingStub as BlockingBank
import cosmos.bank.v1beta1.QueryGrpcKt.QueryCoroutineStub as CoroutineBank

/**
* Get a coin balance in the account at the supplied address.
Expand All @@ -13,7 +14,19 @@ import cosmos.bank.v1beta1.QueryGrpc.QueryBlockingStub as Bank
* @param denom The denomination of the coin.
* @return [CoinOuterClass.Coin]
*/
fun Bank.getAccountBalance(bech32Address: String, denom: String): CoinOuterClass.Coin =
fun BlockingBank.getAccountBalance(bech32Address: String, denom: String): CoinOuterClass.Coin =
balance(QueryOuterClass.QueryBalanceRequest.newBuilder().setAddress(bech32Address).setDenom(denom).build()).balance

/**
* Get a coin balance in the account at the supplied address.
*
* See [Accounts](https://github.com/FigureTechnologies/service-wallet/blob/v45/pb-client/src/main/kotlin/com/figure/wallet/pbclient/client/grpc/Accounts.kt#L34).
*
* @param bech32Address The bech32 address to query.
* @param denom The denomination of the coin.
* @return [CoinOuterClass.Coin]
*/
suspend fun CoroutineBank.getAccountBalance(bech32Address: String, denom: String): CoinOuterClass.Coin =
balance(QueryOuterClass.QueryBalanceRequest.newBuilder().setAddress(bech32Address).setDenom(denom).build()).balance

/**
Expand All @@ -24,9 +37,35 @@ fun Bank.getAccountBalance(bech32Address: String, denom: String): CoinOuterClass
* @param bech32Address The bech32 address to query.
* @return A list of [CoinOuterClass.Coin] associated with the account.
*/
fun Bank.getAccountCoins(bech32Address: String): List<CoinOuterClass.Coin> =
fun BlockingBank.getAccountCoins(bech32Address: String): List<CoinOuterClass.Coin> =
allBalances(QueryOuterClass.QueryAllBalancesRequest.newBuilder().setAddress(bech32Address).build()).balancesList

/**
* Get a list of coin balances in the account at the supplied address.
*
* See [Accounts](https://github.com/FigureTechnologies/service-wallet/blob/v45/pb-client/src/main/kotlin/com/figure/wallet/pbclient/client/grpc/Accounts.kt#L34).
*
* @param bech32Address The bech32 address to query.
* @return A list of [CoinOuterClass.Coin] associated with the account.
*/
suspend fun CoroutineBank.getAccountCoins(bech32Address: String): List<CoinOuterClass.Coin> =
allBalances(QueryOuterClass.QueryAllBalancesRequest.newBuilder().setAddress(bech32Address).build()).balancesList

/**
* Queries the supply of a single coin.
*
* See [Accounts](https://github.com/FigureTechnologies/service-wallet/blob/v45/pb-client/src/main/kotlin/com/figure/wallet/pbclient/client/grpc/Accounts.kt#L40).
*
* @param denom The denomination of the coin.
* @return [CoinOuterClass.Coin]
*/
fun BlockingBank.getSupply(denom: String): CoinOuterClass.Coin =
supplyOf(
QueryOuterClass.QuerySupplyOfRequest.newBuilder()
.setDenom(denom)
.build()
).amount

/**
* Queries the supply of a single coin.
*
Expand All @@ -35,7 +74,7 @@ fun Bank.getAccountCoins(bech32Address: String): List<CoinOuterClass.Coin> =
* @param denom The denomination of the coin.
* @return [CoinOuterClass.Coin]
*/
fun Bank.getSupply(denom: String): CoinOuterClass.Coin =
suspend fun CoroutineBank.getSupply(denom: String): CoinOuterClass.Coin =
supplyOf(
QueryOuterClass.QuerySupplyOfRequest.newBuilder()
.setDenom(denom)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package io.provenance.client.protobuf.extensions

import cosmos.feegrant.v1beta1.Feegrant
import cosmos.feegrant.v1beta1.QueryOuterClass
import cosmos.feegrant.v1beta1.QueryGrpc.QueryBlockingStub as FeeGrants
import cosmos.feegrant.v1beta1.QueryGrpc.QueryBlockingStub as BlockingFeeGrants
import cosmos.feegrant.v1beta1.QueryGrpcKt.QueryCoroutineStub as CoroutineFeeGrants

/**
* Get the fee grants for (granter, grantee) addresses.
Expand All @@ -13,7 +14,26 @@ import cosmos.feegrant.v1beta1.QueryGrpc.QueryBlockingStub as FeeGrants
* @param granteeAddress The bech32 address of the fee grantee.
* @return [Feegrant.Grant]
*/
fun FeeGrants.getFeeGrant(granterAddress: String, granteeAddress: String): Feegrant.Grant =
fun BlockingFeeGrants.getFeeGrant(granterAddress: String, granteeAddress: String): Feegrant.Grant =
allowance(
QueryOuterClass.QueryAllowanceRequest
.newBuilder()
.setGranter(granterAddress)
.setGrantee(granteeAddress)
.build()
)
.allowance

/**
* Get the fee grants for (granter, grantee) addresses.
*
* See [FeeGrants](https://github.com/FigureTechnologies/service-wallet/blob/v45/pb-client/src/main/kotlin/com/figure/wallet/pbclient/client/grpc/FeeGrants.kt#L11).
*
* @param granterAddress The bech32 address of the fee granter.
* @param granteeAddress The bech32 address of the fee grantee.
* @return [Feegrant.Grant]
*/
suspend fun CoroutineFeeGrants.getFeeGrant(granterAddress: String, granteeAddress: String): Feegrant.Grant =
allowance(
QueryOuterClass.QueryAllowanceRequest
.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package io.provenance.client.protobuf.extensions

import cosmos.gov.v1beta1.Gov
import cosmos.gov.v1beta1.QueryGrpc.QueryBlockingStub as Governance
import cosmos.gov.v1beta1.QueryGrpc.QueryBlockingStub as BlockingGovernance
import cosmos.gov.v1beta1.QueryGrpcKt.QueryCoroutineStub as CoroutineGovernance

/**
* Get a coin balance in the account at the supplied address.
*
* @return A list of [Gov.Proposal]
*/
fun Governance.getAllProposals(): List<Gov.Proposal> =
fun BlockingGovernance.getAllProposals(): List<Gov.Proposal> =
proposals(cosmos.gov.v1beta1.QueryOuterClass.QueryProposalsRequest.getDefaultInstance()).proposalsList

/**
* Get a coin balance in the account at the supplied address.
*
* @return A list of [Gov.Proposal]
*/
suspend fun CoroutineGovernance.getAllProposals(): List<Gov.Proposal> =
proposals(cosmos.gov.v1beta1.QueryOuterClass.QueryProposalsRequest.getDefaultInstance()).proposalsList
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import io.provenance.marker.v1.QueryEscrowRequest
import io.provenance.marker.v1.QueryHoldingRequest
import io.provenance.marker.v1.QueryHoldingResponse
import io.provenance.marker.v1.QueryMarkerRequest
import io.provenance.marker.v1.QueryGrpc.QueryBlockingStub as Markers
import io.provenance.marker.v1.QueryGrpc.QueryBlockingStub as BlockingMarkers
import io.provenance.marker.v1.QueryGrpcKt.QueryCoroutineStub as CoroutineMarkers

/**
* Get a marker account by ID.
Expand All @@ -19,7 +20,23 @@ import io.provenance.marker.v1.QueryGrpc.QueryBlockingStub as Markers
* @param id The identifier of the marker account.
* @return [MarkerAccount]
*/
fun Markers.getMarkerAccount(id: String): MarkerAccount =
fun BlockingMarkers.getMarkerAccount(id: String): MarkerAccount =
marker(QueryMarkerRequest.newBuilder().setId(id).build()).marker.run {
when {
this.`is`(MarkerAccount::class.java) -> unpack(MarkerAccount::class.java)
else -> throw IllegalArgumentException("Marker type not handled:$typeUrl")
}
}

/**
* Get a marker account by ID.
*
* See [Markers](https://github.com/FigureTechnologies/service-wallet/blob/v45/pb-client/src/main/kotlin/com/figure/wallet/pbclient/client/grpc/Markers.kt#L17).
*
* @param id The identifier of the marker account.
* @return [MarkerAccount]
*/
suspend fun CoroutineMarkers.getMarkerAccount(id: String): MarkerAccount =
marker(QueryMarkerRequest.newBuilder().setId(id).build()).marker.run {
when {
this.`is`(MarkerAccount::class.java) -> unpack(MarkerAccount::class.java)
Expand All @@ -35,7 +52,18 @@ fun Markers.getMarkerAccount(id: String): MarkerAccount =
* @param denom The denomination.
* @return [Bank.Metadata]
*/
fun Markers.getMarkerMetadata(denom: String): Bank.Metadata =
fun BlockingMarkers.getMarkerMetadata(denom: String): Bank.Metadata =
denomMetadata(QueryDenomMetadataRequest.newBuilder().setDenom(denom).build()).metadata

/**
* Get the metadata associated with a given marker.
*
* See [Markers](https://github.com/FigureTechnologies/service-wallet/blob/v45/pb-client/src/main/kotlin/com/figure/wallet/pbclient/client/grpc/Markers.kt#L25).
*
* @param denom The denomination.
* @return [Bank.Metadata]
*/
suspend fun CoroutineMarkers.getMarkerMetadata(denom: String): Bank.Metadata =
denomMetadata(QueryDenomMetadataRequest.newBuilder().setDenom(denom).build()).metadata

/**
Expand All @@ -48,22 +76,56 @@ fun Markers.getMarkerMetadata(denom: String): Bank.Metadata =
* @param limit
* @return [QueryHoldingResponse]
*/
fun Markers.getMarkerHolders(denom: String, offset: Int = 0, limit: Int = 200): QueryHoldingResponse =
fun BlockingMarkers.getMarkerHolders(denom: String, offset: Int = 0, limit: Int = 200): QueryHoldingResponse =
holding(
QueryHoldingRequest.newBuilder()
.setId(denom)
.setPagination(paginationBuilder(offset, limit))
.build()
)

/**
* List all accounts holding the given marker.
*
* See [Markers](https://github.com/FigureTechnologies/service-wallet/blob/v45/pb-client/src/main/kotlin/com/figure/wallet/pbclient/client/grpc/Markers.kt#L31).
*
* @param denom The denomination.
* @param offset
* @param limit
* @return [QueryHoldingResponse]
*/
suspend fun CoroutineMarkers.getMarkerHolders(denom: String, offset: Int = 0, limit: Int = 200): QueryHoldingResponse =
holding(
QueryHoldingRequest.newBuilder()
.setId(denom)
.setPagination(paginationBuilder(offset, limit))
.build()
)

/**
* TODO: Description for getMarkerEscrow
*
* @param id The identifier of the marker.
* @param escrowDenom The escrow denomination.
* @return [CoinOuterClass.Coin?]
*/
fun BlockingMarkers.getMarkerEscrow(id: String, escrowDenom: String): CoinOuterClass.Coin? =
escrow(
QueryEscrowRequest
.newBuilder()
.setId(id)
.build()
).escrowList
.firstOrNull { it.denom == escrowDenom }

/**
* TODO: Description for getMarkerEscrow
*
* @param id The identifier of the marker.
* @param escrowDenom The escrow denomination.
* @return [CoinOuterClass.Coin?]
*/
fun Markers.getMarkerEscrow(id: String, escrowDenom: String): CoinOuterClass.Coin? =
suspend fun CoroutineMarkers.getMarkerEscrow(id: String, escrowDenom: String): CoinOuterClass.Coin? =
escrow(
QueryEscrowRequest
.newBuilder()
Expand Down
Loading

0 comments on commit 045bca8

Please sign in to comment.