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

compiler level util functions #1037

Open
bdkent opened this issue Nov 30, 2024 · 0 comments
Open

compiler level util functions #1037

bdkent opened this issue Nov 30, 2024 · 0 comments

Comments

@bdkent
Copy link

bdkent commented Nov 30, 2024

I find myself reusing these functions in my ergoscripts. It might be useful to add this kind of functionality to the global utils:

def verifyBoxHasMarkerToken(args: (Box, Coll[Byte])): Boolean = {
  val box = args._1
  val token = args._2
  box.tokens.exists({ (t: (Coll[Byte], Long)) => t._1 == token && t._2 >= 1L })
}

def verifyBoxHasNoMarkerToken(args: (Box, Coll[Byte])): Boolean = {
  val box = args._1
  val token = args._2
  box.tokens.forall({ (t: (Coll[Byte], Long)) => t._1 != token })
}

def verifyUsedAdditionalRegisters(args: (Box, Int)): Boolean = {
  val box = args._1
  val used = args._2
  (used >= 1 || box.R4[Any].isEmpty) &&
  (used >= 2 || box.R5[Any].isEmpty) &&
  (used >= 3 || box.R6[Any].isEmpty) &&
  (used >= 4 || box.R7[Any].isEmpty) &&
  (used >= 5 || box.R8[Any].isEmpty) &&
  (used >= 6 || box.R9[Any].isEmpty)
}

def verifySameForBasicRequiredRegisters(args: (Box, Box)): Boolean = {
  val inBox = args._1
  val outBox = args._2
  inBox.value == outBox.value &&
  inBox.propositionBytes == outBox.propositionBytes
}

def verifySameForRequiredRegisters(args: (Box, Box)): Boolean = {
  val inBox = args._1
  val outBox = args._2
  verifySameForBasicRequiredRegisters(args) &&
  inBox.tokens == outBox.tokens
}

def verifySpentToken(args: ((Box, Box), (Coll[Byte], Long))): Boolean = {
  val inBox = args._1._1
  val outBox = args._1._2
  val token = args._2._1
  val amount = args._2._2
  inBox.tokens.forall({ (it: (Coll[Byte], Long)) =>
    {
      if (it._1 == token) {
        outBox.tokens.exists({ (ot: (Coll[Byte], Long)) => it._1 == ot._1 && it._2 == ot._2 + amount })
      } else {
        outBox.tokens.exists({ (ot: (Coll[Byte], Long)) => it._1 == ot._1 && it._2 == ot._2 })
      }
    }
  })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant