vechain / vechain-sdk-js

The official JavaScript SDK for VeChain.
23 stars 9 forks source link

🐛 [BUG] - typing for methods is not available when loading a contract #1053

Closed akanoce closed 1 month ago

akanoce commented 2 months ago

Description

Looks like some methods of the class returned via

new Contract(....) are not typed, making it difficult to work with such methods, requiring different ts-ignore to silence ts.

Screenshot 2024-07-11 at 10 30 38 Screenshot 2024-07-11 at 10 30 45

As you can see in both screenshots, the Contract is just a generic Contract<Abi>. I tried with multiple abis and contracts achieving the same result

Reproduction URL

No response

Reproduction steps

  1. Copy this json and assign it to a variable in your local const abi = [...]
  2. Define the contract address const address = "0x89A00Bb0947a30FF95BEeF77a66AEdE3842Fe5B7"
  3. Instantiate the thorClient const thorClient = ThorClient.fromUrl("https://mainnet.vechain.org");
  4. Instantiate the contract class
    const allocationContract = new Contract(
    address,
    abi,
    thorClient
    );
  5. See the errors like attached. No method is available using .read and .clause

Screenshots

No response

Logs

No response

OS

Mac

Valazan commented 1 month ago

There is something odd in the ABI that was provided, if I load the one from the vebetterdao package, the typings works as expected.

Screenshot 2024-07-26 at 18 57 19

@akanoce can you confirm it's still a bug and not a problem with the ABI provided?

fabiorigam commented 1 month ago

Waiting for @akanoce feedback

akanoce commented 1 month ago

@Valazan @fabiorigam it's still not working on my side guys. I tried with lot of different abis, without luck (i.e https://github.com/vechain/b32/blob/master/ABIs/VeBetterDAO-b3tr.json)

Also, i really think we should infer the type instead of having the user manually declare it like

Contract<type of json.abi>

which, is trowing type errors in my case

Screenshot 2024-08-01 at 18 09 55 Screenshot 2024-08-01 at 18 09 47
Valazan commented 1 month ago

@akanoce we have to check it better because the type is directly imported from abitype https://abitype.dev/, so it's not something implemented by SDK.

As for the loading, the standard way to load a contract is:

        const loadedContract = thorSoloClient.contracts.load(
            contract.address,
            contract.abi
        );

In this case type will be inferred. But we can also see it implemented also for a new contract object instantiation

fabiorigam commented 1 month ago

Opened a new PR with a releted issue, this one is working