Skip to content

Commit

Permalink
Merge branch 'dev' into 'master'
Browse files Browse the repository at this point in the history
Dev

See merge request ergo/rosen-bridge/contract!98
  • Loading branch information
vorujack committed Jun 10, 2024
2 parents b1e9be5 + 7e72dd4 commit facdbf9
Show file tree
Hide file tree
Showing 11 changed files with 258 additions and 173 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ libraryDependencies ++= Seq(

name := "contract"
ThisBuild / scalaVersion := "2.12.7"
ThisBuild / version := "3.0.0"
ThisBuild / version := "3.2.0"
ThisBuild / organization := "rosen.bridge"
ThisBuild / organizationName := "rosen-bridge"
ThisBuild / publishMavenStyle := true
Expand Down
27 changes: 25 additions & 2 deletions src/main/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ networks = {
RepoConfigNFT="abde6d2b51f6e8b85f3040ecccb3087531f0773a6a1d3faaeef043e10390f3eb"
}
lock-address = "addr1v9kmp9flrq8gzh287q4kku8vmad3vkrw0rwqvjas6vyrf9s9at4dn"
cleanup-confirm = 7200
cleanup-confirm = 10
}
idKey = "tokenId"
}
Expand All @@ -78,7 +78,30 @@ networks = {
AwcNFT="852ccfce6b0b060f81153cd1994d5f42e5e59d7f092adbadab2b5e1ca2c34b63"
RepoConfigNFT="99016d570b66c7188957cb72acbf131ab0be7611e011ba47c783a3c3f5773daf"
}
cleanup-confirm = 7200
cleanup-confirm = 10
}
idKey = "tokenId"
}
bitcoin = {
public-launch = {
tokens = {
RWTId="30e4392fc439fce9948da124efddb8779fe179eef5a5d6196e249b75ee64defc"
CleanupNFT="dcf4e8398f01d9a54d07600fcbbdf486966a003ca364c99106d83eb12b9aa439"
AwcNFT="ec6105b9ee957f6ea700ffe4ce5eec06c9af478351175013d0c63b7370b1bc08"
RepoConfigNFT="92d61d0717853b1b6f85758c5e92c7aa946b24051323c12598df9a53f6684df6"
}
lock-address="bc1qs0852en99dfctv0egj2qxnmc79mhjgn9ap975t"
cleanup-confirm = 21600
}
loen = {
tokens = {
RWTId="72a9208a66c8a0d61d6b5abb2beb3afdd07a8bede0f54c0aebef2da8287fc247"
CleanupNFT="2708cc15be42eff988c7194c86912d305361c8c12cf9dae2b750dbf5c1519bd0"
AwcNFT="b32713f357fe0364cb9800e3499e456614a34bdaa1e3b902902f04fffd75ac92"
RepoConfigNFT="1c589bcb108a4408e65ef1d68766b102b7c2dedc11161c618e413225110ffc6a"
}
lock-address="bc1qt58qyyqw8yezp7g0lnjyshuqnhj07amuerhj93"
cleanup-confirm = 10
}
idKey = "tokenId"
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/helpers/Configs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ object Configs extends ConfigHelper {
})

private lazy val networkGeneralConfig = config.getObject("network-general")
var generalConfig = mutable.Map.empty[String, (ErgoNetwork, MainTokens)]
var generalConfig = mutable.Map.empty[String, NetworkGeneral]
networkGeneralConfig.keySet().forEach(networkTypeName => {
val mainNetworkConfig = networkGeneralConfig.get(networkTypeName).asInstanceOf[ConfigObject]
val mainTokensConfig = mainNetworkConfig.get("main-tokens").asInstanceOf[ConfigObject]
Expand All @@ -97,7 +97,7 @@ object Configs extends ConfigHelper {
val addressEncoder = new ErgoAddressEncoder(networkType.networkPrefix)
val ergoNetwork = ErgoNetwork(ergoClient, addressEncoder)

generalConfig(networkTypeName) = (ergoNetwork, mainTokens)
generalConfig(networkTypeName) = NetworkGeneral(ergoNetwork, mainTokens)
})

lazy val tokensMapDirPath: String = readKey("tokensMap.dirPath", "./tokensMap")
Expand Down
2 changes: 2 additions & 0 deletions src/main/scala/helpers/Models.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ case class Tokens(CleanupNFT: String, RWTId: String, AwcNFT: String, RepoConfigN
}

case class Network(tokens: Tokens, lockAddress: String, cleanupConfirm: Int)

case class NetworkGeneral(ergoNetwork: ErgoNetwork, mainTokens: MainTokens)
13 changes: 6 additions & 7 deletions src/main/scala/helpers/Utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import java.math.BigInteger
object Utils {
private val secureRandom = new java.security.SecureRandom

def selectConfig(networkName: String, networkType: String) : (ErgoNetwork, Network, MainTokens) = {
def selectConfig(networkName: String, networkType: String) : (NetworkGeneral, Network) = {
(
Configs.generalConfig(networkType)._1,
Configs.allNetworksToken((networkName, networkType)),
Configs.generalConfig(networkType)._2
Configs.generalConfig(networkType),
Configs.allNetworksToken((networkName, networkType))
)
}

Expand Down Expand Up @@ -44,8 +43,8 @@ object Utils {
*/
def createContracts(networkVersion: String, networkName: String = "", networkType: String = ""): Unit = {
if(networkName.nonEmpty) {
val networkConfig: (ErgoNetwork, Network, MainTokens) = selectConfig(networkName, networkType)
val contracts = new Contracts(networkConfig._1, (networkConfig._2, networkConfig._3))
val networkConfig: (NetworkGeneral, Network) = selectConfig(networkName, networkType)
val contracts = new Contracts(networkConfig._1, networkConfig._2)
contracts.createContractsJson(
networkName,
networkType,
Expand All @@ -61,7 +60,7 @@ object Utils {
if ((network._2 contains networkType) || networkType.isEmpty){
val networkObj = allNetworksToken(network)
val generalObj = generalConfig(network._2)
val contracts = new Contracts(generalObj._1, (networkObj, generalObj._2))
val contracts = new Contracts(generalObj, networkObj)
contracts.createContractsJson(
network._1,
network._2,
Expand Down
70 changes: 35 additions & 35 deletions src/main/scala/rosen/bridge/Contracts.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package rosen.bridge

import helpers.{ErgoNetwork, MainTokens, Network, Utils}
import helpers.{Network, NetworkGeneral, Utils}
import org.ergoplatform.appkit.{ConstantsBuilder, ErgoContract}
import scorex.util.encode.{Base16, Base64}
import io.circe.Json

import java.io.PrintWriter
import scala.io.{BufferedSource, Source}

class Contracts(ergoGeneralConfig: ErgoNetwork, networkConfig: (Network, MainTokens)) {
class Contracts(networkGeneral: NetworkGeneral, networkConfig: Network) {
lazy val WatcherCollateral: (ErgoContract, String) = generateWatcherCollateralContract()
lazy val RWTRepo: (ErgoContract, String) = generateRWTRepoContract()
lazy val WatcherPermit: (ErgoContract, String) = generateWatcherPermitContract()
Expand All @@ -27,7 +27,7 @@ class Contracts(ergoGeneralConfig: ErgoNetwork, networkConfig: (Network, MainTok
}

def toJsonAddresses(networkName: String): Json = {
val lockAddress = if (networkName != "ergo") networkConfig._1.lockAddress else Lock._2
val lockAddress = if (networkName != "ergo") networkConfig.lockAddress else Lock._2
Json.fromFields(List(
("RWTRepo", Json.fromString(RWTRepo._2)),
("WatcherPermit", Json.fromString(WatcherPermit._2)),
Expand All @@ -45,8 +45,8 @@ class Contracts(ergoGeneralConfig: ErgoNetwork, networkConfig: (Network, MainTok
val result = {
Json.fromFields(List(
("addresses", this.toJsonAddresses(networkName)),
("tokens", networkConfig._1.tokens.toJson().deepMerge(networkConfig._2.toJson())),
("cleanupConfirm", Json.fromInt(networkConfig._1.cleanupConfirm))
("tokens", networkConfig.tokens.toJson().deepMerge(networkGeneral.mainTokens.toJson())),
("cleanupConfirm", Json.fromInt(networkConfig.cleanupConfirm))
))
}

Expand All @@ -57,121 +57,121 @@ class Contracts(ergoGeneralConfig: ErgoNetwork, networkConfig: (Network, MainTok
}

private def generateWatcherCollateralContract(): (ErgoContract, String) = {
ergoGeneralConfig.ergoClient.execute(ctx => {
networkGeneral.ergoNetwork.ergoClient.execute(ctx => {
val watcherCollateralScript = readScript("Collateral.es")
.replace("REPO_NFT", Base64.encode(Base16.decode(networkConfig._2.RepoNFT).get))
.replace("REPO_NFT", Base64.encode(Base16.decode(networkGeneral.mainTokens.RepoNFT).get))
val contract = ctx.compileContract(ConstantsBuilder.create().build(), watcherCollateralScript)
val address = Utils.getContractAddress(contract, ergoGeneralConfig.addressEncoder)
val address = Utils.getContractAddress(contract, networkGeneral.ergoNetwork.addressEncoder)
println(s"Watcher collateral address is : \t\t\t$address")
(contract, address)
})
}

private def generateRWTRepoContract(): (ErgoContract, String) = {
ergoGeneralConfig.ergoClient.execute(ctx => {
networkGeneral.ergoNetwork.ergoClient.execute(ctx => {
val watcherPermitHash = Base64.encode(Utils.getContractScriptHash(WatcherPermit._1))
val watcherCollateralHash = Base64.encode(Utils.getContractScriptHash(WatcherCollateral._1))
val RwtRepoScript = readScript("RwtRepo.es")
.replace("REPO_CONFIG_NFT", Base64.encode(Base16.decode(networkConfig._1.tokens.RepoConfigNFT).get))
.replace("RSN_TOKEN", Base64.encode(Base16.decode(networkConfig._2.RSN).get))
.replace("REPO_CONFIG_NFT", Base64.encode(Base16.decode(networkConfig.tokens.RepoConfigNFT).get))
.replace("RSN_TOKEN", Base64.encode(Base16.decode(networkGeneral.mainTokens.RSN).get))
.replace("PERMIT_SCRIPT_HASH", watcherPermitHash)
.replace("WATCHER_COLLATERAL_SCRIPT_HASH", watcherCollateralHash)
val contract = ctx.compileContract(ConstantsBuilder.create().build(), RwtRepoScript)
val address = Utils.getContractAddress(contract, ergoGeneralConfig.addressEncoder)
val address = Utils.getContractAddress(contract, networkGeneral.ergoNetwork.addressEncoder)
println(s"Watcher repo address is : \t\t\t$address")
(contract, address)
})
}

private def generateWatcherPermitContract(): (ErgoContract, String) = {
ergoGeneralConfig.ergoClient.execute(ctx => {
networkGeneral.ergoNetwork.ergoClient.execute(ctx => {
val commitmentHash = Base64.encode(Utils.getContractScriptHash(Commitment._1))
val watcherPermitScript = readScript("Permit.es")
.replace("REPO_NFT", Base64.encode(Base16.decode(networkConfig._2.RepoNFT).get))
.replace("REPO_NFT", Base64.encode(Base16.decode(networkGeneral.mainTokens.RepoNFT).get))
.replace("COMMITMENT_SCRIPT_HASH", commitmentHash)

val contract = ctx.compileContract(ConstantsBuilder.create().build(), watcherPermitScript)
val address = Utils.getContractAddress(contract, ergoGeneralConfig.addressEncoder)
val address = Utils.getContractAddress(contract, networkGeneral.ergoNetwork.addressEncoder)
println(s"Watcher permit address is : \t\t\t$address")
(contract, address)
})
}

private def generateCommitmentContract(): (ErgoContract, String) = {
ergoGeneralConfig.ergoClient.execute(ctx => {
networkGeneral.ergoNetwork.ergoClient.execute(ctx => {
val triggerEvent = Base64.encode(Utils.getContractScriptHash(WatcherTriggerEvent._1))
val commitmentScript = readScript("Commitment.es")
.replace("REPO_NFT", Base64.encode(Base16.decode(networkConfig._2.RepoNFT).get))
.replace("REPO_CONFIG_NFT", Base64.encode(Base16.decode(networkConfig._1.tokens.RepoConfigNFT).get))
.replace("REPO_NFT", Base64.encode(Base16.decode(networkGeneral.mainTokens.RepoNFT).get))
.replace("REPO_CONFIG_NFT", Base64.encode(Base16.decode(networkConfig.tokens.RepoConfigNFT).get))
.replace("EVENT_TRIGGER_SCRIPT_HASH", triggerEvent)

val contract = ctx.compileContract(ConstantsBuilder.create().build(), commitmentScript)
val address = Utils.getContractAddress(contract, ergoGeneralConfig.addressEncoder)
val address = Utils.getContractAddress(contract, networkGeneral.ergoNetwork.addressEncoder)
println(s"Commitment address is : \t\t\t$address")
(contract, address)
})
}

private def generateWatcherTriggerEventContract(): (ErgoContract, String) = {
ergoGeneralConfig.ergoClient.execute(ctx => {
networkGeneral.ergoNetwork.ergoClient.execute(ctx => {
val fraud = Base64.encode(Utils.getContractScriptHash(Fraud._1))
val lock = Base64.encode(Utils.getContractScriptHash(Lock._1))
val triggerScript = readScript("EventTrigger.es")
.replace("CLEANUP_NFT", Base64.encode(Base16.decode(networkConfig._1.tokens.CleanupNFT).get))
.replace("CLEANUP_NFT", Base64.encode(Base16.decode(networkConfig.tokens.CleanupNFT).get))
.replace("LOCK_SCRIPT_HASH", lock)
.replace("FRAUD_SCRIPT_HASH", fraud)
.replace("CLEANUP_CONFIRMATION", networkConfig._1.cleanupConfirm.toString)
.replace("CLEANUP_CONFIRMATION", networkConfig.cleanupConfirm.toString)

val contract = ctx.compileContract(ConstantsBuilder.create().build(), triggerScript)
val address = Utils.getContractAddress(contract, ergoGeneralConfig.addressEncoder)
val address = Utils.getContractAddress(contract, networkGeneral.ergoNetwork.addressEncoder)
println(s"Watcher trigger event address is : \t\t\t$address")
(contract, address)
})
}

private def generateFraudContract(): (ErgoContract, String) = {
ergoGeneralConfig.ergoClient.execute(ctx => {
networkGeneral.ergoNetwork.ergoClient.execute(ctx => {
val fraudScript = readScript("Fraud.es")
.replace("CLEANUP_NFT", Base64.encode(Base16.decode(networkConfig._1.tokens.CleanupNFT).get))
.replace("REPO_NFT", Base64.encode(Base16.decode(networkConfig._2.RepoNFT).get))
.replace("CLEANUP_NFT", Base64.encode(Base16.decode(networkConfig.tokens.CleanupNFT).get))
.replace("REPO_NFT", Base64.encode(Base16.decode(networkGeneral.mainTokens.RepoNFT).get))

val contract = ctx.compileContract(ConstantsBuilder.create().build(), fraudScript)
val address = Utils.getContractAddress(contract, ergoGeneralConfig.addressEncoder)
val address = Utils.getContractAddress(contract, networkGeneral.ergoNetwork.addressEncoder)
println(s"Fraud address is : \t\t\t$address")
(contract, address)
})
}

private def generateLockContract(): (ErgoContract, String) = {
ergoGeneralConfig.ergoClient.execute(ctx => {
networkGeneral.ergoNetwork.ergoClient.execute(ctx => {
val lockScript = readScript("Lock.es")
.replace("GUARD_NFT", Base64.encode(Base16.decode(networkConfig._2.GuardNFT).get))
.replace("GUARD_NFT", Base64.encode(Base16.decode(networkGeneral.mainTokens.GuardNFT).get))

val contract = ctx.compileContract(ConstantsBuilder.create().build(), lockScript)
val address = Utils.getContractAddress(contract, ergoGeneralConfig.addressEncoder)
val address = Utils.getContractAddress(contract, networkGeneral.ergoNetwork.addressEncoder)
println(s"lock address is : \t\t\t$address")
(contract, address)
})
}

private def generateGuardSignContract(): (ErgoContract, String) = {
ergoGeneralConfig.ergoClient.execute(ctx => {
networkGeneral.ergoNetwork.ergoClient.execute(ctx => {
val guardSignScript = readScript("GuardSign.es")

val contract = ctx.compileContract(ConstantsBuilder.create().build(), guardSignScript)
val address = Utils.getContractAddress(contract, ergoGeneralConfig.addressEncoder)
val address = Utils.getContractAddress(contract, networkGeneral.ergoNetwork.addressEncoder)
println(s"guard sign address is : \t\t\t$address")
(contract, address)
})
}

private def generateRepoConfigContract(): (ErgoContract, String) = {
ergoGeneralConfig.ergoClient.execute(ctx => {
networkGeneral.ergoNetwork.ergoClient.execute(ctx => {
val testScript = readScript("RepoConfig.es")
.replace("GUARD_NFT", Base64.encode(Base16.decode(networkConfig._2.GuardNFT).get))
.replace("GUARD_NFT", Base64.encode(Base16.decode(networkGeneral.mainTokens.GuardNFT).get))

val contract = ctx.compileContract(ConstantsBuilder.create().build(), testScript)
val address = Utils.getContractAddress(contract, ergoGeneralConfig.addressEncoder)
val address = Utils.getContractAddress(contract, networkGeneral.ergoNetwork.addressEncoder)
println(s"repo config address is : \t\t\t$address")
(contract, address)
})
Expand Down
Loading

0 comments on commit facdbf9

Please sign in to comment.