import { assertMichelsonInstruction, Expr, MichelsonCode } from '@taquito/michel-codec'; import { MichelsonMap } from '@taquito/taquito'; import { BigNumber } from 'bignumber.js'; export type Instruction = MichelsonCode; export type unit = (true | undefined) & { __type: 'unit' }; export type address = string & { __type: 'address' }; export type bytes = string & { __type: 'bytes' }; export type contract = string & { __type: 'contract' }; export type operation = string & { __type: 'operation' }; export type key = string & { __type: 'key' }; export type key_hash = string & { __type: 'key_hash' }; export type signature = string & { __type: 'signature' }; export type ticket = string & { __type: 'ticket' }; export type timestamp = string & { __type: 'timestamp' }; export type int = BigNumber & { __type: 'int' }; export type nat = BigNumber & { __type: 'nat' }; export type mutez = BigNumber & { __type: 'mutez' }; export type tez = BigNumber & { __type: 'tez' }; export type MapKey = Array | object | string | boolean | number; export type MMap = Omit, 'get'> & { get: (key: K) => V }; export type BigMap = Omit, 'get'> & { get: (key: K) => Promise }; export type chest = bytes & { __type: 'chest' }; export type chest_key = string & { __type: 'chest_key' }; export const createStringTypeTas = () => { return (value: string): T => value as T; }; export const createBigNumberTypeTas = () => { return (value: number | BigNumber | string): T => new BigNumber(value) as T; }; export type asMapParamOf = K extends string ? { [key: string]: V } | Array<{ key: K; value: V }> : K extends number ? { [key: number]: V } | Array<{ key: K; value: V }> : Array<{ key: K; value: V }>; export function asMap(value: asMapParamOf): MMap { const m = new MichelsonMap(); if (Array.isArray(value)) { const vArray = value as Array<{ key: K; value: V }>; vArray.forEach(x => m.set(x.key, x.value)); } else { const vObject = value as { [key: string]: V }; Object.keys(vObject).forEach(key => m.set(key as unknown as K, vObject[key])); } return m as MMap; } export const asBigMap = (value: asMapParamOf) => asMap(value) as unknown as BigMap; export function add(a: T, b: T): T { return a.plus(b) as T; } export function subtract(a: T, b: T): T { return a.minus(b) as T; } export function createLambdaTypeTas(expr: Expr): MichelsonCode { assertMichelsonInstruction(expr); return expr as MichelsonCode; } /** tas: Tezos 'as' casting for strict types */ export const tas = { address: createStringTypeTas
(), bytes: createStringTypeTas(), contract: createStringTypeTas(), chest: createStringTypeTas(), chest_key: createStringTypeTas(), timestamp: (value: string | Date): timestamp => new Date(value).toISOString() as timestamp, int: createBigNumberTypeTas(), nat: createBigNumberTypeTas(), mutez: createBigNumberTypeTas(), tez: createBigNumberTypeTas(), map: asMap, bigMap: asBigMap, // Operations add, subtract, lambda: createLambdaTypeTas, // To number number: (value: string | BigNumber) => Number(value + ''), unit: () => true as unit, };