Skip to content

Commit 14bdee2

Browse files
committed
implement handy serde utils
1 parent ef6cd36 commit 14bdee2

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/bcs/simple.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { EntryFunctionArgumentTypes } from "../transactions";
2+
import { SimpleEntryFunctionArgumentTypes } from "../transactions";
3+
import { TypeTag } from "../transactions/typeTag";
4+
import { Bool } from "./serializable/movePrimitives";
5+
6+
export function serialize(typeTag: TypeTag, arg: SimpleEntryFunctionArgumentTypes): EntryFunctionArgumentTypes {
7+
if (typeTag.isBool()) {
8+
if (typeof arg === "boolean") {
9+
return new Bool(arg);
10+
}
11+
// Accept number 1/0
12+
if (typeof arg === "number" && (arg === 1 || arg === 0)) {
13+
return new Bool(arg === 1);
14+
}
15+
// Accept string '1'/'0'
16+
if (typeof arg === "string" && (arg === "1" || arg === "0")) {
17+
return new Bool(arg === "1");
18+
}
19+
throw new Error(`Cannot serialize argument as bool: ${arg}`);
20+
}
21+
// TODO: Implement other types
22+
throw new Error(`Cannot serialize argument for typeTag: ${typeTag}`);
23+
}
24+
25+
export function deserialize(typeTag: TypeTag, arg: EntryFunctionArgumentTypes): SimpleEntryFunctionArgumentTypes {
26+
if (typeTag.isBool()) {
27+
if (arg instanceof Bool) {
28+
return arg.value;
29+
}
30+
throw new Error(`Cannot deserialize argument as bool: ${arg}`);
31+
}
32+
// TODO: Implement other types
33+
throw new Error(`Cannot deserialize argument for typeTag: ${typeTag}`);
34+
}

0 commit comments

Comments
 (0)