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

feat: add function selector to cli to make it easier for to call functions #3053

Merged
merged 1 commit into from
Oct 26, 2023
Merged
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
8 changes: 7 additions & 1 deletion docs/docs/dev_docs/cli/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,10 @@ The `call` command calls a read-only method on a contract, one that will not gen
- `--contract-artifact` - The artifact of the contract we are calling.
- `--contract-address` - The address of the deployed contract

As you can see from the result, this address has a public balance of 543, as expected.
As you can see from the result, this address has a public balance of 543, as expected.

## Compute Function Selector
`aztec-cli --compute-selector <signature e.g. foo(Field,Field)>` gives the function selector.

## Inspect Contract
`aztec-cli --compute-selector <json artifact file e.g. artifacts/token_contract.json>` gives the list of all callable functions along with their function signature and selector.
24 changes: 21 additions & 3 deletions yarn-project/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import {
getSchnorrAccount,
isContractDeployed,
} from '@aztec/aztec.js';
import { StructType, decodeFunctionSignatureWithParameterNames } from '@aztec/foundation/abi';
import {
FunctionSelector,
StructType,
decodeFunctionSignature,
decodeFunctionSignatureWithParameterNames,
} from '@aztec/foundation/abi';
import { JsonStringify } from '@aztec/foundation/json-rpc';
import { DebugLogger, LogFn } from '@aztec/foundation/log';
import { sleep } from '@aztec/foundation/sleep';
Expand Down Expand Up @@ -677,11 +682,24 @@ export function getProgram(log: LogFn, debugLogger: DebugLogger): Command {
log(`No external functions found for contract ${contractArtifact.name}`);
}
for (const fn of contractFns) {
const signature = decodeFunctionSignatureWithParameterNames(fn.name, fn.parameters);
log(`${fn.functionType} ${signature}`);
const signatureWithParameterNames = decodeFunctionSignatureWithParameterNames(fn.name, fn.parameters);
const signature = decodeFunctionSignature(fn.name, fn.parameters);
const selector = FunctionSelector.fromSignature(signature);
log(
`${fn.functionType} ${signatureWithParameterNames} \n\tfunction signature: ${signature}\n\tselector: ${selector}`,
);
}
});

program
.command('compute-selector')
.description('Given a function signature, it computes a selector')
.argument('<functionSignature>', 'Function signature to compute selector for e.g. foo(Field)')
.action((functionSignature: string) => {
const selector = FunctionSelector.fromSignature(functionSignature);
log(`${selector}`);
});

compileContract(program, 'compile', log);
generateTypescriptInterface(program, 'generate-typescript', log);
generateNoirInterface(program, 'generate-noir-interface', log);
Expand Down