Skip to content

Instantly share code, notes, and snippets.

@nau
Last active April 21, 2024 15:56
Show Gist options
  • Save nau/b8996fe3e51b0e21c20479c5d8548ec7 to your computer and use it in GitHub Desktop.
Save nau/b8996fe3e51b0e21c20479c5d8548ec7 to your computer and use it in GitHub Desktop.
Scalus UPLC script for Micah's challenge: https://twitter.com/MicahAlexKenda1/status/1755465000486076846
// Install scalus-cli with `brew install scalus-cli` then `scala-cli run contract.scala`
//> using scala 3.3.3
//> using plugin org.scalus:scalus-plugin_3:0.6.1
//> using dep org.scalus:scalus_3:0.6.1
package optsend
import scalus.*, Compiler.*, builtin.*, Builtins.*, uplc.*, ledger.api.v2.*
import scalus.uplc.eval.MachineParams
@main def main() =
val validator = compile:
(script_withdrawal_credential: Data, datum: Data, redeemer: Data, ctx: Data) =>
def list_has(list: List[Pair[Data, Data]]): Unit =
if list.head.fst == script_withdrawal_credential then ()
else list_has(list.tail) // fails on empty list
inline def withdrawal_from_ctx =
unMapData(fieldAsData[ScriptContext](_.txInfo.withdrawals)(ctx))
list_has(withdrawal_from_ctx)
import scalus.uplc.TermDSL.{*, given}
import scalus.prelude.List.Nil
import scalus.prelude.AssocMap
import scalus.ledger.api.v1.Credential.PubKeyCredential
import scalus.ledger.api.v1.PubKeyHash
import scalus.ledger.api.v1.StakingCredential.StakingHash
import scalus.ledger.api.v1.Value
import scalus.ledger.api.v2.{TxInfo, TxOutRef, TxId}
import scalus.ledger.api.v1.ToDataInstances.given
import scalus.ledger.api.v2.ToDataInstances.given
import scalus.sir.PrettyPrinter
import scalus.builtin.ByteString.given
import scalus.builtin.ToDataInstances.given
import scalus.builtin.Data.toData
val cred = new StakingHash(new PubKeyCredential(new PubKeyHash(hex"DEADBEEF")))
val credWrong = new StakingHash(new PubKeyCredential(new PubKeyHash(hex"BEEFDEAD")))
val ctx = ScriptContext(
TxInfo(
inputs = Nil,
referenceInputs = Nil,
outputs = Nil,
fee = Value.zero,
mint = Value.zero,
dcert = Nil,
withdrawals = AssocMap.singleton(cred, 2),
validRange = Interval.always,
signatories = Nil,
data = AssocMap.empty,
redeemers = AssocMap.empty,
id = TxId(hex"6465616462656566")
),
ScriptPurpose.Spending(TxOutRef(TxId(hex"6465616462656566"), 0))
).toData
val unitData = ().toData
val script_withdrawal_credential = cred.toData
val term = validator.toUplc(generateErrorTraces = false) // $ (!(!DefaultFun.TailList))
val appliedTerm = term $ script_withdrawal_credential $ unitData $ unitData $ ctx
val named_program = Program(version = (1, 0, 0), term)
val program = Program(version = (1, 0, 0), appliedTerm)
println(validator.prettyXTerm.render(80))
println(program.prettyXTerm.render(80))
println(named_program.doubleCborEncoded.length)
println(named_program.doubleCborHex)
println(eval.VM.evaluateScriptCounting(MachineParams.defaultParams, program.flatEncoded))
//92
//585A58580100003222223230013756646AE84D5D11ABA2357446AE88D5D11ABA235573C6EA8004D5D09AAB9E3754004600A44A666AE68CDD79AAB9D3574200200C293098011ABA200123230022330020020012300223300200200101
//CekResult(Const(Unit), ExBudget(4153484,9782), )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment