butaneprotocol / translucent-compat

Deprecated in favour of blaze https://github.com/butaneprotocol/blaze-cardano
https://github.com/butaneprotocol/blaze-cardano
Other
15 stars 8 forks source link

Added initial support for recursive PlutusData types and fixed kupmiosv5 provider's script converting issue caused my previous PR #29

Closed ilap closed 7 months ago

ilap commented 7 months ago

Added initial support for recursive PlutusData types. It is very simple patch, but it's required for handling recursive datatypes with translucent. It still requires manual intervention see example below: Define the relevant types in aiken


pub type Node {
  Link { value: ByteArray, next: Node }
  Value { value: ByteArray}
}

pub type MintRedeemer {
  node: Node,
}

Modify the blueprint.ts generated plutus.ts

import { applyParamsToScript, Data, Validator } from 'translucent-cardano'

export type Node =
  | {
      Link: { value: string; next: Node }
    }
  | {
      Value: { value: string }
    }

export type MintRedeemer =
  | {
      Minting: [
        {
          node: Node
        },
      ]
    }
  | 'Burning'

export interface MyMinting {
  new (params: {
...
  }): Validator
  // It can be the MintRedeemer type too
  // MintRedeemer
  rdmr:
    | {
        Minting: [
          {
            node: Node
          },
        ]
      }
    | 'Burning'
}
...
                    {
                      title: 'node',
                      anyOf: [
                        {
                          title: 'Link',
                          dataType: 'constructor',
                          index: 0,
                          fields: [
                            { dataType: 'bytes', title: 'value' },
                            {
                              title: 'next',
                              $ref: '#/definitions/ilap~1example~1types~1Node',
                            }
                          ],
                        },
                        {
                          title: 'Value',
                          dataType: 'constructor',
                          index: 1,
                          fields: [{ dataType: 'bytes', title: 'value' }],
                        },
                      ],
                    },

Use it witg translucent

import * as P from './plutus.ts'
...

const node: P.Node = {
  Link: {
    value: "test1",
    next: {
      Link: {
        value: "test2",
        next: { Value: { value: "test3", } },
      },
    },
  },
}
...
    const rdmr: P.MintRedeemer = {
      Minting: [
        {
          Node: node,
        },
      ], // Use 'as const' to assert that Minting is a tuple with a single element
    }

    const rdmr = Data.to(mintRedeemer, P.MyMinting.rdmr, 'node')
   // pass this to the MyMinting script as redeemer.