Reading enum values directly from a contract with JS/TS scripts

Currently, in order to read enum values in a JS script, one has to create a function to return the value for a certain ordering ID. An example can be found in the link below. Although enum values can be shared among smart contracts conveniently, there is no easy way to retrieve them directly from an smart contract. Wondering if this could be a potential language feature to support direct reading of enum values. Thanks.

More precisely, if it is possible to read an enum definition from a smart contract and initialize an enum in javascript/typescript, that would be very handy. I don’t see a quick way of establishing this link right now.

Enums are not part of the ABI (they’re just numbers from that perspective) so you can’t get the values with libraries like ethers.js or web3.js that get information from the ABI JSON.

Tools that have access to source can compile it and get enum values from the AST. Not sure any of them is doing that in practice though.

1 Like