Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Opcodes and Errors classes in generated Typescript code #1100

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added

- Abstract classes in the generated Typescript code contain a list of opcodes and errors for easy use in tests. PR [#1100](https://github.com/tact-lang/tact/pull/1100)
- `&&=`, `||=`, `>>=` and `<<=` augmented assignment operators: PR [#853](https://github.com/tact-lang/tact/pull/853)
- New CSpell dictionaries: TVM instructions and adjusted list of Fift words: PR [#881](https://github.com/tact-lang/tact/pull/881)
- Docs: the `description` property to the frontmatter of the each page for better SEO: PR [#916](https://github.com/tact-lang/tact/pull/916)
Expand All @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Generated Typescript class with wrappers for smart contract makes it more convenient to inherit from it. PR [#1100](https://github.com/tact-lang/tact/pull/1100)
- The `parseImports` function now returns AST import nodes instead of raw strings: PR [#966](https://github.com/tact-lang/tact/pull/966)
- Optional types for `self` argument in `extends mutates` functions are now allowed: PR [#854](https://github.com/tact-lang/tact/pull/854)
- Docs: complete overhaul of the exit codes page: PR [#978](https://github.com/tact-lang/tact/pull/978)
Expand Down
61 changes: 50 additions & 11 deletions src/bindings/writeTypescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,20 @@ export function writeTypescript(
}

// Errors
// Map, so user can do something like ExitCodes["Stack Overflow"]
w.append(`const ${abi.name}_errorMessages: { [key: string]: number } = {`);
w.inIndent(() => {
if (abi.errors) {
Object.entries(abi.errors).forEach(([k, abiError]) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for (const [k, abiError] of abi.errors) would be more readable, concise and performant.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@verytactical there is a few of instances of Object.entries(foo).forEach(([k, v]) ... in the codebase. Could you please open an issue to refactor those?

const escapedMessage = abiError.message.replaceAll('"', '\\"');
w.append(` "${escapedMessage}": ${k},`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an indent that doesn't take .inIndent() state in consideration.

});
}
});
w.append(`}`);
w.append();

//map <int, Error> to generate ABI later.
w.append(
`const ${abi.name}_errors: { [key: number]: { message: string } } = {`,
);
Expand All @@ -217,6 +231,23 @@ export function writeTypescript(
}
});
w.append(`]`);

//Opcodes
//So user can use them in sandbox tests
w.append("export abstract class Opcodes {");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypeScript has enum syntax for this. abstract class implies user should inherit from it to get access to its fields, and that's definitely not the way in a language without multiple inheritance.

w.inIndent(() => {
if (abi.types) {
for (const t of abi.types) {
if (t.header) {
const hexString =
"0x" + t.header.toString(16).padStart(8, "0");
w.append("static " + t.name + " = " + hexString);
}
}
}
});
w.append(`}`);

w.append();

const getterNames: Map<string, string> = new Map();
Expand Down Expand Up @@ -272,58 +303,66 @@ export function writeTypescript(

if (init) {
w.append(
`static async init(${writeArguments(init.args).join(", ")}) {`,
`static async init<T extends ${abi.name}>(` +
`this: new (address: Address, init?: { code: Cell, data: Cell }) => T,` +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of types is meant to type legacy JS code, not to write or generate novel code.

`${writeArguments(init.args).join(", ")}) {`,
);
w.inIndent(() => {
w.append(
`return await ${abi.name}_init(${init!.args.map((v) => v.name).join(", ")});`,
`const init = await ${abi.name}_init(${init.args.map((v) => v.name).join(", ")});`,
);
w.append(`return new this(contractAddress(0, init), init);`);
});
w.append(`}`);
w.append();

w.append(
`static async fromInit(${writeArguments(init.args).join(", ")}) {`,
`static async fromInit<T extends ${abi.name}>(` +
`this: new (address: Address, init?: { code: Cell, data: Cell }) => T,` +
`${writeArguments(init.args).join(", ")}) {`,
);
w.inIndent(() => {
w.append(
`const init = await ${abi.name}_init(${init!.args.map((v) => v.name).join(", ")});`,
`const init = await ${abi.name}_init(${init.args.map((v) => v.name).join(", ")});`,
);
w.append(`const address = contractAddress(0, init);`);
w.append(`return new ${abi.name}(address, init);`);
w.append(`return new this(address, init);`);
});
w.append(`}`);
w.append();
}

w.append(`static fromAddress(address: Address) {`);
w.append(
`static fromAddress<T extends ${abi.name}>(` +
`this: new (address: Address, init?: { code: Cell, data: Cell }) => T, ` +
`address: Address) {`,
);
w.inIndent(() => {
w.append(`return new ${abi.name}(address);`);
w.append(`return new this(address);`);
});
w.append(`}`);
w.append();

w.append(`readonly address: Address; `);
w.append(`readonly address: Address;`);
w.append(`readonly init?: { code: Cell, data: Cell };`);
w.append(`readonly abi: ContractABI = {`);
w.inIndent(() => {
w.append(`types: ${abi.name}_types,`);
w.append(`types: ${abi.name}_types,`);
w.append(`getters: ${abi.name}_getters,`);
w.append(`receivers: ${abi.name}_receivers,`);
w.append(`errors: ${abi.name}_errors,`);
});
w.append(`};`);
w.append();
w.append(
`private constructor(address: Address, init?: { code: Cell, data: Cell }) {`,
`constructor(address: Address, init?: { code: Cell, data: Cell }) {`,
);
w.inIndent(() => {
w.append("this.address = address;");
w.append("this.init = init;");
});
w.append("}");
w.append();

// Internal receivers
if (
abi.receivers &&
Expand Down
Loading