2

When I have a object of type ContractAbstraction<ContractProvider>, I can get a key/value map of all the endpoints in the deployed contract like this:

const contract: ContractAbstraction<ContractProvider> = client.contract.at(this.state.contractAddress);
const methods = contract.methods;

where client has type TezosToolkit and handles the RPC interaction with a node.

And I can get a list of method (smart contract function) names like this

function getObjectMethodNames(obj: any): string[] {
  if (!obj) {
    return [];
  }

  return Object.getOwnPropertyNames(obj)
    .filter((p) => typeof obj[p] === 'function')
    .map((name) => name.toLowerCase());
}
const methodNames: string[] = getObjectMethodNames(contract.methods);

But how do I get information about how many arguments each function takes and what type the arguments have? Is that possible using Taquito?

I can see from the block explorer Better Call Dev that this should be possible since they show not only a list of functions for a smart contract but also each function's signature.

1

Yes, you can get the function signature, not just the function names. This can be done like this.

const contract: ContractAbstraction<ContractProvider> = client.contract.at(this.state.contractAddress);
const signatures: string[][] = contract.parameterSchema.ExtractSignatures();

signatures will then contain an array of an array of strings, where element 0 in each array is the function name, and the rest of the elements are the types from the function signature.

For example, for a basic FA1.2 contract, you would have.

signatures = [
["approve", "address", "nat"],
["get_allowance", "address", "address", "contract"],
["get_balance", "address", "contract"],
["get_total_supply", "unit", "contract"],
["transfer", "address", "address", "nat"]]

If you want a dict of the types that each function takes, with the function name as key, you can get it like this:

  const signatures: string[][] = contract.parameterSchema.ExtractSignatures();
  let signatureDict: StringDictionary<string[]> = {};
  for (let i = 0; i < signatures.length; i++) {

    // 0th element is key, the rest are values of the dict
    signatureDict[signatures[i][0]] = signatures[i];
    signatureDict[signatures[i][0]].shift();
  }
| improve this answer | |
  • But far as I can see, Taquito only supports basic the interpretation of basic types like nat, address, and int. For more complex types like a list of composite types it will just return list and not for example list({"sender": <address>, "amount":<nat>}. Optimally I would be able to inspect the function signatures as well as I can on the Better Call Dev block explorer. – Thorkil Værge Oct 31 at 14:00
0

I've been digging around Taquito source code and I cannot find any information about the arguments for each entrypoint.
However, after you created the contract abstraction, you can use the parameterSchema property on it to get a JSON representation of the value the contract expects as a parameter:

const contract: ContractAbstraction<ContractProvider> = client.contract.at(this.state.contractAddress);
const parameters = contract.parameterSchema;

This outputs: { "root": { "val": (JSON value) } }

That being said, I am not sure how this information would be useful as Taquito verifies internally that the provided arguments are correct before emitting a transaction, so you don't have to worry about it :)

| improve this answer | |
  • Why it is useful? You can make a pretty sweet frontend for contract interaction if you the function signatures :) – Thorkil Værge Oct 31 at 13:48
  • Have a look at the "Call function" here to see what this can be used for :) myxtzwallet.tokengate.io – Thorkil Værge Nov 2 at 11:46
  • It's a strongly and dynamically typed interface for making function calls on any deployed smart contract. – Thorkil Værge Nov 2 at 11:51
  • My comment meant "it is not so useful for most dapps" rather than "it is completely useless" :) For a dapp where users are asked to provide raw values that are forwarded to a smart contract, that may be useful, but for the majority of dapps where interactions are button clicks and input of strings/numbers, it is not necessary as Taquito takes care of all the formatting and validating. – Claude Barde Nov 2 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.