mirror of
https://github.com/alexgo-io/clarity-codegen.git
synced 2026-01-12 14:34:34 +08:00
feat: add traitT
This commit is contained in:
@@ -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": {
|
||||
".": {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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}`;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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),
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user