-
Notifications
You must be signed in to change notification settings - Fork 112
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]) => { | ||
const escapedMessage = abiError.message.replaceAll('"', '\\"'); | ||
w.append(` "${escapedMessage}": ${k},`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an indent that doesn't take |
||
}); | ||
} | ||
}); | ||
w.append(`}`); | ||
w.append(); | ||
|
||
//map <int, Error> to generate ABI later. | ||
w.append( | ||
`const ${abi.name}_errors: { [key: number]: { message: string } } = {`, | ||
); | ||
|
@@ -217,6 +231,23 @@ export function writeTypescript( | |
} | ||
}); | ||
w.append(`]`); | ||
|
||
//Opcodes | ||
//So user can use them in sandbox tests | ||
w.append("export abstract class Opcodes {"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TypeScript has |
||
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(); | ||
|
@@ -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,` + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 && | ||
|
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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?