예제 #1
0
 def to_comb(self) -> PairType:
     return PairType.from_comb(items=[
         AddressType(self.ticketer), self.item,
         NatType(self.amount)
     ])
예제 #2
0
def compile_view_fa2_token_metadata(tokens: List[TokenMetadata]):
    from pytezos.michelson.types.core import BytesType, NatType, StringType
    from pytezos.michelson.types.map import MapType
    from pytezos.michelson.types.pair import PairType

    # this map is of type:
    #   map nat (nat, map string bytes)
    elt = MapType.from_items([(
        NatType.from_value(token.id),
        PairType.from_comb([
            NatType.from_value(token.id),
            MapType.from_items([
                (StringType.from_value(key), BytesType.from_value(value))
                for key, value in sorted(token.attrs.items())
            ]),
        ]),
    ) for token in tokens])

    # Below code takes a '(nat, state)', looks up the 'nat' from the map above ('elt'),
    # and returns the result.
    code = [
        # get the 'fst' of the '(nat, state)' pair
        {
            "prim": "CAR"
        },
        # push the 'elt' to the stack
        {
            "prim": "PUSH",
            "args": [elt.as_micheline_expr(),
                     elt.to_micheline_value()]
        },
        # swap the top two elements (to match 'get's parameter order)
        {
            "prim": "SWAP"
        },
        # lookup the given 'nat' from the map
        {
            "prim": "GET"
        },
        # fail if we don't get something, return otherwise
        {
            "prim":
            "IF_NONE",
            "args": [  # if none
                [
                    {
                        "prim":
                        "PUSH",
                        "args": [{
                            "prim": "string"
                        }, {
                            "string": "FA2_UNKNOWN_TOKEN"
                        }],
                    },
                    {
                        "prim": "FAILWITH"
                    },
                ],
                # if not none
                [],
            ],
        },
    ]

    return {
        "name": "token_metadata",
        "code": code,
        "parameter": {
            "prim": "nat"
        },
        "returnType": {
            "prim":
            "pair",
            "args": [
                {
                    "prim": "nat"
                },
                {
                    "prim": "map",
                    "args": [{
                        "prim": "string"
                    }, {
                        "prim": "bytes"
                    }]
                },
            ],
        },
    }