From 1a04ece465a28ce62ca2c65e7605ee9be2634156 Mon Sep 17 00:00:00 2001 From: c4605 Date: Thu, 4 Apr 2024 12:42:45 +0100 Subject: [PATCH] feat: add traitT --- package.json | 2 +- src/generate/contractsGenerator.ts | 20 ++++++++++++++++++-- src/runtime/decoders.ts | 2 +- src/runtime/encoders.ts | 15 +++++++-------- src/runtime/transcoders.ts | 21 +++++++++++++++++---- 5 files changed, 44 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index ba07685..5053037 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ }, "scripts": { "prepare": "pnpm run build", - "build": "rm -rf lib && tsc -p tsconfig.json && tsc -p tsconfig.cjs.json" + "build": "rm -rf lib && rm -rf dist && tsc -p tsconfig.json && tsc -p tsconfig.cjs.json" }, "exports": { ".": { diff --git a/src/generate/contractsGenerator.ts b/src/generate/contractsGenerator.ts index f116a47..f0a0ac9 100644 --- a/src/generate/contractsGenerator.ts +++ b/src/generate/contractsGenerator.ts @@ -21,8 +21,22 @@ import { assertNever, mapValues } from "../utils/helpers"; import { asyncAutoRetry } from "../utils/asyncAutoRetry"; import axios from "axios"; +type TranscoderDefType = + | "uintT" + | "intT" + | "booleanT" + | "principalT" + | "traitT" + | "noneT" + | "stringT" + | "stringAsciiT" + | "bufferT" + | "optionalT" + | "responseSimpleT" + | "listT" + | "tupleT"; type TranscoderDefArgument = TranscoderDef | Record; -type TranscoderDef = [string, ...TranscoderDefArgument[]]; +type TranscoderDef = [TranscoderDefType, ...TranscoderDefArgument[]]; const toTranscoderDef = ({ name, @@ -38,8 +52,10 @@ const toTranscoderDef = ({ return { def: ["intT"] }; } else if (type === "bool") { return { def: ["booleanT"] }; - } else if (type === "principal" || type === "trait_reference") { + } else if (type === "principal") { return { def: ["principalT"] }; + } else if (type === "trait_reference") { + return { def: ["traitT"] }; } else if (type === "none") { return { def: ["noneT"] }; } else { diff --git a/src/runtime/decoders.ts b/src/runtime/decoders.ts index 17c56cf..5815a49 100644 --- a/src/runtime/decoders.ts +++ b/src/runtime/decoders.ts @@ -37,7 +37,7 @@ export const addressResult: Decoder = (result) => { throw new Error(`Expected principal, got ${result.type}`); }; -export const contractResult: Decoder = (result) => { +export const contractResult: Decoder<`${string}.${string}`> = (result) => { if (result.type === ClarityType.PrincipalContract) { return `${addressToString(result.address)}.${result.contractName.content}`; } diff --git a/src/runtime/encoders.ts b/src/runtime/encoders.ts index 02e5af2..2391f0b 100644 --- a/src/runtime/encoders.ts +++ b/src/runtime/encoders.ts @@ -64,13 +64,10 @@ export function principalCV(principal: string): PrincipalCV { } } -export const traitCV = (val: string): ContractPrincipalCV => { - const [addr, name] = val.split("."); - if (addr && name) { - return contractPrincipalCV(addr, name); - } - throw new Error(`can not parse val as trait: ${val}`); -}; +export function traitCV(principal: `${string}.${string}`): ContractPrincipalCV { + const [addr, name] = principal.split("."); + return contractPrincipalCV(addr, name); +} export const booleanCV = (value: boolean): BooleanCV => { if (value) { @@ -83,7 +80,9 @@ export const booleanCV = (value: boolean): BooleanCV => { export const uintCV: Encoder = (input) => _uintCV(input); export const intCV: Encoder = (input) => _intCV(input); -export function optional(encoder: Encoder): Encoder { +export function optionalEncoder( + encoder: Encoder +): Encoder { return (value) => { if (value === undefined) { return noneCV(); diff --git a/src/runtime/transcoders.ts b/src/runtime/transcoders.ts index 1d7bc3d..c4c339d 100644 --- a/src/runtime/transcoders.ts +++ b/src/runtime/transcoders.ts @@ -1,7 +1,13 @@ -import {bufferCV, noneCV, stringAsciiCV, stringUtf8CV} from "@stacks/transactions"; +import { + bufferCV, + noneCV, + stringAsciiCV, + stringUtf8CV, +} from "@stacks/transactions"; import { boolResult, bufferResult, + contractResult, intResult, listDecoder, noneResult, @@ -15,10 +21,12 @@ import { booleanCV, listEncoder, uintCV, - optional, + optionalEncoder, principalCV, responseSimpleEncoder, - tupleEncoder, intCV, + tupleEncoder, + intCV, + traitCV, } from "./encoders"; import { Decoder, @@ -71,6 +79,11 @@ export const principalT = transcoders({ decode: principalResult, }); +export const traitT = transcoders({ + encode: traitCV, + decode: contractResult, +}); + export const listT = ( listItemTranscoder: Transcoder ): Transcoder => { @@ -104,7 +117,7 @@ export const optionalT = ( someTranscoder: Transcoder ): Transcoder => { return transcoders({ - encode: optional(someTranscoder.encode), + encode: optionalEncoder(someTranscoder.encode), decode: optionalDecoder(someTranscoder.decode), }); };