feat: add traitT

This commit is contained in:
c4605
2024-04-04 12:42:45 +01:00
parent 63efea7d8a
commit 1a04ece465
5 changed files with 44 additions and 16 deletions

View File

@@ -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": {
".": {

View File

@@ -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<string, TranscoderDef>;
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 {

View File

@@ -37,7 +37,7 @@ export const addressResult: Decoder<string> = (result) => {
throw new Error(`Expected principal, got ${result.type}`);
};
export const contractResult: Decoder<string> = (result) => {
export const contractResult: Decoder<`${string}.${string}`> = (result) => {
if (result.type === ClarityType.PrincipalContract) {
return `${addressToString(result.address)}.${result.contractName.content}`;
}

View File

@@ -64,13 +64,10 @@ export function principalCV(principal: string): PrincipalCV {
}
}
export const traitCV = (val: string): ContractPrincipalCV => {
const [addr, name] = val.split(".");
if (addr && name) {
export function traitCV(principal: `${string}.${string}`): ContractPrincipalCV {
const [addr, name] = principal.split(".");
return contractPrincipalCV(addr, name);
}
throw new Error(`can not parse val as trait: ${val}`);
};
}
export const booleanCV = (value: boolean): BooleanCV => {
if (value) {
@@ -83,7 +80,9 @@ export const booleanCV = (value: boolean): BooleanCV => {
export const uintCV: Encoder<bigint> = (input) => _uintCV(input);
export const intCV: Encoder<bigint> = (input) => _intCV(input);
export function optional<T>(encoder: Encoder<T>): Encoder<T | undefined> {
export function optionalEncoder<T>(
encoder: Encoder<T>
): Encoder<T | undefined> {
return (value) => {
if (value === undefined) {
return noneCV();

View File

@@ -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 = <T>(
listItemTranscoder: Transcoder<T>
): Transcoder<T[]> => {
@@ -104,7 +117,7 @@ export const optionalT = <T>(
someTranscoder: Transcoder<T>
): Transcoder<undefined | T> => {
return transcoders({
encode: optional(someTranscoder.encode),
encode: optionalEncoder(someTranscoder.encode),
decode: optionalDecoder(someTranscoder.decode),
});
};