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

chore: Remove "as unknown" casts for ABIs where possible #2331

Merged
merged 2 commits into from
Sep 15, 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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { BaseAccountContract } from './base_account_contract.js';
*/
export class EcdsaAccountContract extends BaseAccountContract {
constructor(private signingPrivateKey: Buffer) {
super(EcdsaAccountContractAbi as unknown as ContractAbi);
super(EcdsaAccountContractAbi as ContractAbi);
}

async getDeploymentArgs() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { BaseAccountContract } from './base_account_contract.js';
*/
export class SchnorrAccountContract extends BaseAccountContract {
constructor(private signingPrivateKey: GrumpkinPrivateKey) {
super(SchnorrAccountContractAbi as unknown as ContractAbi);
super(SchnorrAccountContractAbi as ContractAbi);
}

async getDeploymentArgs() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { BaseAccountContract } from './base_account_contract.js';
*/
export class SingleKeyAccountContract extends BaseAccountContract {
constructor(private encryptionPrivateKey: GrumpkinPrivateKey) {
super(SchnorrSingleKeyAccountContractAbi as unknown as ContractAbi);
super(SchnorrSingleKeyAccountContractAbi as ContractAbi);
}

getDeploymentArgs(): Promise<any[]> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@ export class DefaultAccountEntrypoint implements EntrypointInterface {
},
],
returnTypes: [],
} as unknown as FunctionAbiHeader;
} as FunctionAbiHeader;
}
}
1 change: 1 addition & 0 deletions yarn-project/aztec.js/src/contract/checker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ describe('abiChecker', () => {
{
type: {
kind: 'struct',
path: 'mystruct',
fields: [
{
name: 'name',
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec.js/src/contract/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function abiParameterTypeChecker(type: ABIType): boolean {
case 'array':
return checkAttributes(type, { length: 'number', type: 'object' }) && abiParameterTypeChecker(type.type);
case 'struct':
return checkAttributes(type, { fields: 'object' }) && checkStruct(type);
return checkAttributes(type, { fields: 'object', path: 'string' }) && checkStruct(type);
default:
throw new Error('ABI function parameter has an unrecognised type');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { AztecRPC, PublicKey } from '@aztec/types';

import PrivateTokenContractAbiJson from './private_token_contract.json' assert { type: 'json' };

export const PrivateTokenContractAbi = PrivateTokenContractAbiJson as unknown as ContractAbi;
export const PrivateTokenContractAbi = PrivateTokenContractAbiJson as ContractAbi;

/**
* Type-safe interface for contract PrivateToken;
Expand Down
1 change: 1 addition & 0 deletions yarn-project/cli/src/test/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const mockContractAbi: ContractAbi = {
name: 'structParam',
type: {
kind: 'struct',
path: 'mystruct',
fields: [
{ name: 'subField1', type: { kind: 'field' } },
{ name: 'subField2', type: { kind: 'boolean' } },
Expand Down
4 changes: 4 additions & 0 deletions yarn-project/foundation/src/abi/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ export interface StructType extends BasicType<'struct'> {
* The fields of the struct.
*/
fields: ABIVariable[];
/**
* Fully qualified name of the struct.
*/
path: string;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions yarn-project/foundation/src/abi/decoder.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FunctionAbi } from './abi.js';
import { ABIParameterVisibility, FunctionAbi } from './abi.js';
import { decodeFunctionSignature, decodeFunctionSignatureWithParameterNames } from './decoder.js';

describe('abi/decoder', () => {
Expand All @@ -20,7 +20,7 @@ describe('abi/decoder', () => {
{ name: 'secretHash', type: { kind: 'field' } },
],
},
visibility: 'private',
visibility: 'private' as ABIParameterVisibility,
},
{
name: 'aDeepStruct',
Expand Down Expand Up @@ -58,10 +58,10 @@ describe('abi/decoder', () => {
},
],
},
visibility: 'private',
visibility: 'private' as ABIParameterVisibility,
},
],
} as unknown as Pick<FunctionAbi, 'name' | 'parameters'>;
} as Pick<FunctionAbi, 'name' | 'parameters'>;

it('decodes function signature', () => {
expect(decodeFunctionSignature(abi.name, abi.parameters)).toMatchInlineSnapshot(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ function generateAbiGetter(name: string) {
function generateAbiStatement(name: string, abiImportPath: string) {
const stmts = [
`import ${name}ContractAbiJson from '${abiImportPath}' assert { type: 'json' };`,
`export const ${name}ContractAbi = ${name}ContractAbiJson as unknown as ContractAbi;`,
`export const ${name}ContractAbi = ${name}ContractAbiJson as ContractAbi;`,
];
return stmts.join('\n');
}
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/noir-contracts/scripts/types.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ write_export() {


# artifacts
echo "export const ${NAME}ContractAbi = ${NAME}Json as unknown as ContractAbi;" >> "$artifacts_dir/index.ts";
echo "export const ${NAME}ContractAbi = ${NAME}Json as ContractAbi;" >> "$artifacts_dir/index.ts";
echo "Written typescript for $NAME"

# types
Expand Down