From e7548589aa657088161bfed98295ee32cba1e453 Mon Sep 17 00:00:00 2001 From: Cody Worsnop Date: Thu, 14 Jul 2022 08:30:11 -0700 Subject: [PATCH] Returning state from approval (#46) * Returning state from approval * name change * name change --- .../cee/approve/ApproveContractExecutionResponse.kt | 8 ++++++++ .../cee/approve/ApproveContractBatchExecution.kt | 12 ++++++++++-- .../usecase/cee/approve/ApproveContractExecution.kt | 9 +++++---- 3 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 models/src/main/kotlin/io/provenance/api/models/cee/approve/ApproveContractExecutionResponse.kt diff --git a/models/src/main/kotlin/io/provenance/api/models/cee/approve/ApproveContractExecutionResponse.kt b/models/src/main/kotlin/io/provenance/api/models/cee/approve/ApproveContractExecutionResponse.kt new file mode 100644 index 00000000..10eac133 --- /dev/null +++ b/models/src/main/kotlin/io/provenance/api/models/cee/approve/ApproveContractExecutionResponse.kt @@ -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 +) diff --git a/service/src/main/kotlin/io/provenance/api/domain/usecase/cee/approve/ApproveContractBatchExecution.kt b/service/src/main/kotlin/io/provenance/api/domain/usecase/cee/approve/ApproveContractBatchExecution.kt index 2b8e1eb1..6c62027d 100644 --- a/service/src/main/kotlin/io/provenance/api/domain/usecase/cee/approve/ApproveContractBatchExecution.kt +++ b/service/src/main/kotlin/io/provenance/api/domain/usecase/cee/approve/ApproveContractBatchExecution.kt @@ -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 @@ -21,11 +24,12 @@ class ApproveContractBatchExecution( private val createClient: CreateClient, private val provenance: Provenance, private val getSigner: GetSigner, -) : AbstractUseCase() { +) : AbstractUseCase>() { private val log = KotlinLogging.logger { } - override suspend fun execute(args: ApproveContractBatchRequestWrapper) { + override suspend fun execute(args: ApproveContractBatchRequestWrapper): List { val errors = mutableListOf() + val responses = mutableListOf() val executionResults = mutableListOf>>() val signer = getSigner.execute(GetSignerRequest(args.uuid, args.request.account)) createClient.execute(CreateClientRequest(args.uuid, args.request.account, args.request.client)).use { client -> @@ -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 = { @@ -61,8 +66,11 @@ class ApproveContractBatchExecution( ) } } + if (errors.any()) { throw ContractExecutionBatchException(errors.joinToString(limit = 20) { it.message.toString() }) } + + return responses } } diff --git a/service/src/main/kotlin/io/provenance/api/domain/usecase/cee/approve/ApproveContractExecution.kt b/service/src/main/kotlin/io/provenance/api/domain/usecase/cee/approve/ApproveContractExecution.kt index 230acaab..12fd6baa 100644 --- a/service/src/main/kotlin/io/provenance/api/domain/usecase/cee/approve/ApproveContractExecution.kt +++ b/service/src/main/kotlin/io/provenance/api/domain/usecase/cee/approve/ApproveContractExecution.kt @@ -11,9 +11,10 @@ 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 @@ -21,8 +22,8 @@ class ApproveContractExecution( private val createClient: CreateClient, private val provenance: Provenance, private val getSigner: GetSigner, -) : AbstractUseCase() { - override suspend fun execute(args: ApproveContractRequestWrapper): TxResponse { +) : AbstractUseCase() { + 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 -> @@ -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!") } }