Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse ContractTemplate from JSON string #999

Merged
merged 2 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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 = {
Expand Down
Loading