diff --git a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/ContractTemplate.scala b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/ContractTemplate.scala index 7b3d1dd19a..941b181f46 100644 --- a/sdk/js/src/main/scala/org/ergoplatform/sdk/js/ContractTemplate.scala +++ b/sdk/js/src/main/scala/org/ergoplatform/sdk/js/ContractTemplate.scala @@ -112,12 +112,8 @@ object ContractTemplate extends js.Object { * @return a new contract template. */ def fromJsonString(json: String): ContractTemplate = { - io.circe.parser.parse(json) match { - case Left(err) => throw err - case Right(json) => - val ct = sdk.ContractTemplate.jsonEncoder.decoder(json.hcursor).toOption.get - ContractTemplate.isoToSdk.from(ct) - } + val ct = sdk.ContractTemplate.fromJsonString(json) + ContractTemplate.isoToSdk.from(ct) } private val constsIso = isoUndefOr(isoArrayToIndexed(isoUndefOr(sigma.ast.js.isoValueToConstant))) diff --git a/sdk/shared/src/main/scala/org/ergoplatform/sdk/ContractTemplate.scala b/sdk/shared/src/main/scala/org/ergoplatform/sdk/ContractTemplate.scala index 8da420bee6..60d906e31c 100644 --- a/sdk/shared/src/main/scala/org/ergoplatform/sdk/ContractTemplate.scala +++ b/sdk/shared/src/main/scala/org/ergoplatform/sdk/ContractTemplate.scala @@ -1,9 +1,10 @@ package org.ergoplatform.sdk -import cats.syntax.either._ // required for Scala 2.11 +import cats.syntax.either._ // required for Scala 2.11 import debox.cfor import io.circe._ import io.circe.syntax.EncoderOps +import org.ergoplatform.sdk import org.ergoplatform.sdk.utils.SerializationUtils.{parseString, serializeString} import org.ergoplatform.sdk.utils.Zero import sigma.Evaluation @@ -14,6 +15,7 @@ import sigma.ast.ErgoTree.{ZeroHeader, headerWithVersion, setConstantSegregation import sigma.ast._ import sigma.ast.syntax.SigmaPropValue import sigma.serialization._ + import java.util.Objects import scala.collection.mutable @@ -206,6 +208,21 @@ object ContractTemplate { new ContractTemplate(None, name, description, constTypes, constValues, parameters, expressionTree) } + /** Create a new contract template from a JSON string. + * + * @param json JSON string representing a contract template. + * @return a new contract template. + */ + def fromJsonString(json: String): ContractTemplate = { + io.circe.parser.parse(json) match { + case Left(err) => throw err + case Right(json) => + val ct = sdk.ContractTemplate.jsonEncoder.decoder(json.hcursor).toOption.get + ct + } + } + + object serializer extends SigmaSerializer[ContractTemplate, ContractTemplate] { override def serialize(data: ContractTemplate, w: SigmaByteWriter): Unit = {