Skip to content

Commit

Permalink
Returning state from approval (#46)
Browse files Browse the repository at this point in the history
* Returning state from approval

* name change

* name change
  • Loading branch information
cworsnop-figure authored Jul 14, 2022
1 parent c902536 commit e754858
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.provenance.api.models.cee.approve

import io.provenance.api.models.p8e.TxResponse

data class ApproveContractExecutionResponse(
val envelopeStateBase64EncodedByteArray: String,
val tx: TxResponse
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import io.provenance.api.domain.usecase.cee.common.client.model.CreateClientRequ
import io.provenance.api.domain.usecase.provenance.account.GetSigner
import io.provenance.api.domain.usecase.provenance.account.models.GetSignerRequest
import io.provenance.api.frameworks.provenance.exceptions.ContractExecutionBatchException
import io.provenance.api.frameworks.provenance.extensions.toTxResponse
import io.provenance.api.models.cee.approve.ApproveContractExecutionResponse
import io.provenance.scope.contract.proto.Envelopes
import io.provenance.scope.sdk.FragmentResult
import java.util.Base64
import mu.KotlinLogging
import org.springframework.stereotype.Component

Expand All @@ -21,11 +24,12 @@ class ApproveContractBatchExecution(
private val createClient: CreateClient,
private val provenance: Provenance,
private val getSigner: GetSigner,
) : AbstractUseCase<ApproveContractBatchRequestWrapper, Unit>() {
) : AbstractUseCase<ApproveContractBatchRequestWrapper, List<ApproveContractExecutionResponse>>() {
private val log = KotlinLogging.logger { }

override suspend fun execute(args: ApproveContractBatchRequestWrapper) {
override suspend fun execute(args: ApproveContractBatchRequestWrapper): List<ApproveContractExecutionResponse> {
val errors = mutableListOf<Throwable>()
val responses = mutableListOf<ApproveContractExecutionResponse>()
val executionResults = mutableListOf<Pair<Envelopes.EnvelopeState, List<Tx.MsgGrant>>>()
val signer = getSigner.execute(GetSignerRequest(args.uuid, args.request.account))
createClient.execute(CreateClientRequest(args.uuid, args.request.account, args.request.client)).use { client ->
Expand All @@ -50,6 +54,7 @@ class ApproveContractBatchExecution(

it.forEach { executions ->
client.respondWithApproval(executions.first, broadcast.txhash)
responses.add(ApproveContractExecutionResponse(Base64.getEncoder().encodeToString(executions.first.toByteArray()), broadcast.toTxResponse()))
}
}.fold(
onSuccess = {
Expand All @@ -61,8 +66,11 @@ class ApproveContractBatchExecution(
)
}
}

if (errors.any()) {
throw ContractExecutionBatchException(errors.joinToString(limit = 20) { it.message.toString() })
}

return responses
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ import io.provenance.api.domain.usecase.provenance.account.GetSigner
import io.provenance.api.domain.usecase.provenance.account.models.GetSignerRequest
import io.provenance.api.frameworks.provenance.exceptions.ContractExecutionException
import io.provenance.api.frameworks.provenance.extensions.toTxResponse
import io.provenance.api.models.p8e.TxResponse
import io.provenance.api.models.cee.approve.ApproveContractExecutionResponse
import io.provenance.scope.contract.proto.Envelopes
import io.provenance.scope.sdk.FragmentResult
import java.util.Base64
import org.springframework.stereotype.Component

@Component
class ApproveContractExecution(
private val createClient: CreateClient,
private val provenance: Provenance,
private val getSigner: GetSigner,
) : AbstractUseCase<ApproveContractRequestWrapper, TxResponse>() {
override suspend fun execute(args: ApproveContractRequestWrapper): TxResponse {
) : AbstractUseCase<ApproveContractRequestWrapper, ApproveContractExecutionResponse>() {
override suspend fun execute(args: ApproveContractRequestWrapper): ApproveContractExecutionResponse {
val envelope = Envelopes.Envelope.newBuilder().mergeFrom(args.request.approval.envelope).build()
createClient.execute(CreateClientRequest(args.uuid, args.request.account, args.request.client)).use { client ->

Expand All @@ -35,7 +36,7 @@ class ApproveContractExecution(
}

client.respondWithApproval(result.envelopeState, tx.txhash)
return tx.toTxResponse()
return ApproveContractExecutionResponse(Base64.getEncoder().encodeToString(result.envelopeState.toByteArray()), tx.toTxResponse())
} else throw ContractExecutionException("Attempted to approve an envelope that did not result in a fragment. Only non-approved envelopes should be sent!")
}
}
Expand Down

0 comments on commit e754858

Please sign in to comment.