Skip to content

Commit

Permalink
Merge branch 'feature/WTCH-78' into 'dev'
Browse files Browse the repository at this point in the history
[WTCH-78] Fix builder by comments

Closes WTCH-78

See merge request waves-enterprise/java-sdk/we-node-client!11
  • Loading branch information
Daniil Georgiev committed Aug 30, 2022
2 parents 99d1e25 + 83b71e4 commit a2f62f9
Showing 1 changed file with 38 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,32 @@ import com.wavesenterprise.sdk.node.domain.sign.CallContractSignRequest
import com.wavesenterprise.sdk.node.domain.sign.ContractSignRequest
import com.wavesenterprise.sdk.node.domain.sign.CreateContractSignRequest
import com.wavesenterprise.sdk.node.domain.tx.ContractTx
import kotlin.reflect.KMutableProperty0

class ContractSignRequestBuilder {
private var builderProperties: BuilderProperties = BuilderProperties()

val NOT_NULLABLE_FOR_CREATE = with(builderProperties) {
listOf(
::senderAddress,
::fee,
::image,
::imageHash,
::contractName,
::params,
)
}

val NOT_NULLABLE_FOR_CALL = with(builderProperties) {
listOf(
::senderAddress,
::fee,
::params,
::contractId,
::contractVersion,
)
}

fun version(version: TxVersion) = this.apply { builderProperties.version = version }

fun senderAddress(senderAddress: Address) = this.apply { builderProperties.senderAddress = senderAddress }
Expand Down Expand Up @@ -62,25 +84,8 @@ class ContractSignRequestBuilder {
fun build(txType: TxType): ContractSignRequest<out ContractTx> {
return when (txType) {
TxType.CREATE_CONTRACT -> {
val notNullableProperties =
with(builderProperties) {
listOf(
::senderAddress,
::fee,
::image,
::imageHash,
::contractName,
::params,
)
}
val variablesNotNullable = buildList {
notNullableProperties.forEach {
if (it.get() == null) {
this.add(it.name)
}
}
}
if (variablesNotNullable.isEmpty()) {
val variablesIsEqualsNull = getVariablesIsEqualsNull(NOT_NULLABLE_FOR_CREATE)
if (variablesIsEqualsNull.isEmpty()) {
with(builderProperties) {
CreateContractSignRequest(
version = version,
Expand All @@ -99,31 +104,15 @@ class ContractSignRequestBuilder {
}
} else {
throw IllegalStateException(
"Fields: " + variablesNotNullable.toString() +
"Fields: " + variablesIsEqualsNull.toString() +
" can not be null - for CreateContractSignRequest"
)
}
}

TxType.CALL_CONTRACT -> {
val notNullableProperties =
with(builderProperties) {
listOf(
::senderAddress,
::fee,
::params,
::contractId,
::contractVersion,
)
}
val variablesNotNullable = buildList {
notNullableProperties.forEach {
if ((it.get() == null)) {
this.add(it.name)
}
}
}
if (variablesNotNullable.isEmpty()) {
val variablesIsEqualsNull = getVariablesIsEqualsNull(NOT_NULLABLE_FOR_CALL)
if (variablesIsEqualsNull.isEmpty()) {
with(builderProperties) {
CallContractSignRequest(
version = version,
Expand All @@ -139,15 +128,25 @@ class ContractSignRequestBuilder {
}
} else {
throw IllegalStateException(
"Fields: " + variablesNotNullable.toString() +
"Fields: " + variablesIsEqualsNull.toString() +
" can not be null - for CallContractSignRequest"
)
}
}

else -> throw IllegalStateException("Shouldn't be here")
}
}

private fun getVariablesIsEqualsNull(notNullableProperties: List<KMutableProperty0<out Any?>>) =
buildList {
notNullableProperties.forEach {
if ((it.get() == null)) {
this.add(it.name)
}
}
}

class BuilderProperties {
var version: TxVersion? = null
var senderAddress: Address? = null
Expand Down

0 comments on commit a2f62f9

Please sign in to comment.