Skip to content

Commit

Permalink
Merge branch 'update-to-latest' into 'dev'
Browse files Browse the repository at this point in the history
Fix scripts and contracts to be compatible with the latest changes

See merge request ergo/rosen-bridge/cleanup-service!3
  • Loading branch information
vorujack committed Oct 9, 2023
2 parents 46fa26a + 6ddaa7a commit ffd32f7
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 157 deletions.
8 changes: 4 additions & 4 deletions src/main/resources/application.conf
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
node = {
url = "http://10.10.9.3:9064/"
url = "https://node.ergopool.io"
apiKey = "hello"
networkType = "MAINNET"
}
explorer.url = "http://10.10.9.3:7000/"
explorer.url = "https://api.ergoplatform.com"

fee = {
default = "1100000"
max = "1100000"
}

box = {
min = "100000"
min = "1000000"
}

cleanup.confirm = 5040 // 720 * 7 (almost 7 days)
cleanup.confirm = 7200 // 720 * 10 (almost 10 days)

tokens = {
RSN = "change me"
Expand Down
24 changes: 12 additions & 12 deletions src/main/scala/models/Models.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ class TriggerEventBox(eventBox: InputBox) extends ErgoBox(eventBox) {
* @param txB transaction builder
*/
def createFraudBoxes(txB: UnsignedTransactionBuilder): Seq[OutBox] = {
val rwtCountPerFraud = eventBox.getTokens.get(0).getValue / getWatchersLen
getWIDs.map(WID => {
txB.outBoxBuilder()
.value(Configs.minBoxValue)
.contract(Contracts.Fraud)
.tokens(new ErgoToken(Configs.tokens.RWT, 1))
.tokens(new ErgoToken(Configs.tokens.RWT, rwtCountPerFraud))
.registers(ErgoValue.of(Seq(WID).map(item => JavaHelpers.collFrom(item)).toArray, ErgoType.collType(ErgoType.byteType())))
.build()
}).toSeq
Expand Down Expand Up @@ -138,29 +139,28 @@ class RWTRepoBox(repoBox: InputBox) extends ErgoBox(repoBox) {
*/
def getR6Values: Array[Long] = repoBox.getRegisters.get(2).getValue.asInstanceOf[Coll[Long]].toArray.clone()

/**
* returns price of RWT in RSN (first value of register R6)
*/
def getRSNFactor: Long = getR6Values.head

/**
* creates new repo box using current repo box with new WIDs and RWTs passed by as arguments
* @param txB transaction builder
* @param WIDs array of watchers' WIDs in register R4
* @param RWTs array of watchers' RWT count in register R5
* @param repoBox old repo box
* @param watcherIndex the slashed WID index in array of watcher WIDs
* @param rwtCount slashed rwt
*/
def createRepoBox(txB: UnsignedTransactionBuilder, WIDs: Array[Array[Byte]], RWTs: Array[Long], watcherIndex: Int): OutBox = {
def createRepoBox(txB: UnsignedTransactionBuilder, repoBox: RWTRepoBox, watcherIndex: Int, rwtCount: Long): OutBox = {
val WIDs = repoBox.getWIDs
val oldRWTs = repoBox.getRWTs
val RWTs = oldRWTs.slice(0, watcherIndex) ++ Seq(oldRWTs(watcherIndex) - rwtCount) ++ oldRWTs.slice(watcherIndex + 1, oldRWTs.length)

val R4 = WIDs.map(item => JavaHelpers.SigmaDsl.Colls.fromArray(item))
val R5 = JavaHelpers.SigmaDsl.Colls.fromArray(RWTs)
val R6 = JavaHelpers.SigmaDsl.Colls.fromArray(getR6Values)

txB.outBoxBuilder()
.value(Configs.minBoxValue)
.value(repoBox.getErgs)
.tokens(
new ErgoToken(Configs.tokens.RepoNFT, 1),
new ErgoToken(Configs.tokens.RWT, RWTAmount + 1),
new ErgoToken(Configs.tokens.RSN, RSNAmount - this.getRSNFactor)
new ErgoToken(Configs.tokens.RWT, RWTAmount + rwtCount),
new ErgoToken(Configs.tokens.RSN, RSNAmount - rwtCount)
)
.contract(Contracts.RWTRepo)
.registers(
Expand Down
26 changes: 14 additions & 12 deletions src/main/scala/network/Client.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,24 @@ class Client extends RosenLogging {
/**
* @return Last cleaner box (consider mempool)
*/
def getCleanerBox: InputBox = getUnspentBoxesFor(
Utils.generateAddress(Contracts.EventTrigger),
(1e9 * 1e8).toLong,
Seq(new ErgoToken(Configs.tokens.CleanupNFT, 1)),
considerMempool = true
).getBoxes.asScala.last
def getCleanerBox: InputBox =
getUnspentBoxesFor(
new Address(Utils.getAddress(Configs.cleaner.address)),
0,
Seq(new ErgoToken(Configs.tokens.CleanupNFT, 1)),
considerMempool = true
).getBoxes.asScala.last

/**
* @return Last repo box (consider mempool)
*/
def getRepoBox: InputBox = getUnspentBoxesFor(
Utils.generateAddress(Contracts.RWTRepo),
(1e9 * 1e8).toLong,
Seq(new ErgoToken(Configs.tokens.RepoNFT, 1)),
considerMempool = true
).getBoxes.asScala.last
def getRepoBox: InputBox =
getUnspentBoxesFor(
Utils.generateAddress(Contracts.RWTRepo),
0,
Seq(new ErgoToken(Configs.tokens.RepoNFT, 1)),
considerMempool = true
).getBoxes.asScala.last

/**
* @return List of input boxes owned by cleaner and doesn't contain CleanupNFT (does not consider mempool)
Expand Down
23 changes: 22 additions & 1 deletion src/main/scala/rosen/bridge/Contracts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ object Contracts {
lazy val Commitment: ErgoContract = generateCommitmentContract()
lazy val EventTrigger: ErgoContract = generateWatcherTriggerEventContract()
lazy val Fraud: ErgoContract = generateFraudContract()
lazy val WatcherCollateral: ErgoContract = generateWatcherCollateralContract()
lazy val Lock: ErgoContract = generateLockContract()

def getContractScriptHash(contract: ErgoContract): Digest32 = {
scorex.crypto.hash.Blake2b256(contract.getErgoTree.bytes)
Expand All @@ -23,10 +25,12 @@ object Contracts {
private def generateRWTRepoContract(): ErgoContract = {
ergoClient.execute(ctx => {
val watcherPermitHash = Base64.encode(getContractScriptHash(WatcherPermit))
val watcherCollateralHash = Base64.encode(getContractScriptHash(WatcherCollateral))
val RwtRepoScript = Scripts.RwtRepoScript
.replace("GUARD_NFT", Base64.encode(Base16.decode(Configs.tokens.GuardNFT).get))
.replace("RSN_TOKEN", Base64.encode(Base16.decode(Configs.tokens.RSN).get))
.replace("PERMIT_SCRIPT_HASH", watcherPermitHash)
.replace("WATCHER_COLLATERAL_SCRIPT_HASH", watcherCollateralHash)
ctx.compileContract(ConstantsBuilder.create().build(), RwtRepoScript)
})
}
Expand Down Expand Up @@ -54,9 +58,10 @@ object Contracts {
private def generateWatcherTriggerEventContract(): ErgoContract = {
ergoClient.execute(ctx => {
val fraud = Base64.encode(getContractScriptHash(Fraud))
val lock = Base64.encode(getContractScriptHash(Lock))
val triggerScript = Scripts.EventTriggerScript
.replace("CLEANUP_NFT", Base64.encode(Base16.decode(Configs.tokens.CleanupNFT).get))
.replace("GUARD_NFT", Base64.encode(Base16.decode(Configs.tokens.GuardNFT).get))
.replace("LOCK_SCRIPT_HASH", lock)
.replace("FRAUD_SCRIPT_HASH", fraud)
.replace("CLEANUP_CONFIRMATION", Configs.cleanupConfirm.toString)
ctx.compileContract(ConstantsBuilder.create().build(), triggerScript)
Expand All @@ -71,4 +76,20 @@ object Contracts {
ctx.compileContract(ConstantsBuilder.create().build(), fraudScript)
})
}

private def generateWatcherCollateralContract(): ErgoContract = {
ergoClient.execute(ctx => {
val watcherCollateralScript = Scripts.watcherCollateral
.replace("REPO_NFT", Base64.encode(Base16.decode(Configs.tokens.RepoNFT).get))
ctx.compileContract(ConstantsBuilder.create().build(), watcherCollateralScript)
})
}

private def generateLockContract(): ErgoContract = {
ergoClient.execute(ctx => {
val lockScript = Scripts.lockScript
.replace("GUARD_NFT", Base64.encode(Base16.decode(Configs.tokens.GuardNFT).get))
ctx.compileContract(ConstantsBuilder.create().build(), lockScript)
})
}
}
Loading

0 comments on commit ffd32f7

Please sign in to comment.