diff --git a/.circleci/config.yml b/.circleci/config.yml index bc968b697fb..75ea67cb08d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -526,7 +526,7 @@ jobs: name: "Test via adhoc script" command: ./yarn-project/boxes/blank/run_tests - boxes-private-token: + boxes-token: machine: image: ubuntu-2204:2023.07.2 resource_class: large @@ -535,7 +535,7 @@ jobs: - *setup_env - run: name: "Test via adhoc script" - command: ./yarn-project/boxes/private-token/run_tests + command: ./yarn-project/boxes/token/run_tests canary: machine: @@ -1338,7 +1338,7 @@ workflows: - aztec-sandbox-x86_64 <<: *defaults - - boxes-private-token: + - boxes-token: requires: - aztec-sandbox-x86_64 <<: *defaults diff --git a/build_manifest.yml b/build_manifest.yml index f806ea421e3..0421afc6749 100644 --- a/build_manifest.yml +++ b/build_manifest.yml @@ -131,9 +131,9 @@ boxes-blank: dependencies: - aztec-sandbox -boxes-private-token: +boxes-token: buildDir: yarn-project - projectDir: yarn-project/boxes/private-token + projectDir: yarn-project/boxes/token dependencies: - aztec-sandbox diff --git a/yarn-project/boxes/private-token/src/app/components/contract_function_form.tsx b/yarn-project/boxes/private-token/src/app/components/contract_function_form.tsx deleted file mode 100644 index 7cdb4fa680e..00000000000 --- a/yarn-project/boxes/private-token/src/app/components/contract_function_form.tsx +++ /dev/null @@ -1,165 +0,0 @@ -import { CONTRACT_ADDRESS_PARAM_NAMES, pxe } from '../../config.js'; -import { callContractFunction, deployContract, viewContractFunction } from '../../scripts/index.js'; -import { convertArgs } from '../../scripts/util.js'; -import styles from './contract_function_form.module.scss'; -import { Button, Loader } from '@aztec/aztec-ui'; -import { AztecAddress, CompleteAddress, Fr } from '@aztec/aztec.js'; -import { ContractArtifact, FunctionArtifact } from '@aztec/foundation/abi'; -import { useFormik } from 'formik'; -import * as Yup from 'yup'; - -type NoirFunctionYupSchema = { - // hack: add `any` at the end to get the array schema to typecheck - // eslint-disable-next-line @typescript-eslint/no-explicit-any - [key: string]: Yup.NumberSchema | Yup.ArraySchema | Yup.BooleanSchema | any; -}; - -type NoirFunctionFormValues = { - [key: string]: string | number | number[] | boolean; -}; - -function generateYupSchema(functionAbi: FunctionArtifact, defaultAddress: string) { - const parameterSchema: NoirFunctionYupSchema = {}; - const initialValues: NoirFunctionFormValues = {}; - for (const param of functionAbi.parameters) { - if (CONTRACT_ADDRESS_PARAM_NAMES.includes(param.name)) { - // these are hex strings instead, but yup doesn't support bigint so we convert back to bigint on execution - parameterSchema[param.name] = Yup.string().required(); - initialValues[param.name] = defaultAddress; - continue; - } - switch (param.type.kind) { - case 'field': - parameterSchema[param.name] = Yup.number().required(); - initialValues[param.name] = 100; - break; - // not really needed for private token, since we hide the nullifier helper method which has the array input - case 'array': - // eslint-disable-next-line no-case-declarations - const arrayLength = param.type.length; - parameterSchema[param.name] = Yup.array() - .of(Yup.number()) - .min(arrayLength) - .max(arrayLength) - .transform(function (value: number[], originalValue: string) { - if (typeof originalValue === 'string') { - return originalValue.split(',').map(Number); - } - return value; - }); - initialValues[param.name] = Array(arrayLength).fill( - CONTRACT_ADDRESS_PARAM_NAMES.includes(param.name) ? defaultAddress : 200, - ); - break; - case 'boolean': - parameterSchema[param.name] = Yup.boolean().required(); - initialValues[param.name] = false; - break; - } - } - return { validationSchema: Yup.object().shape(parameterSchema), initialValues }; -} - -async function handleFunctionCall( - contractAddress: AztecAddress | undefined, - artifact: ContractArtifact, - functionName: string, - args: any, - wallet: CompleteAddress, -) { - const functionAbi = artifact.functions.find(f => f.name === functionName)!; - const typedArgs: any[] = convertArgs(functionAbi, args); - - if (functionName === 'constructor' && !!wallet) { - if (functionAbi === undefined) { - throw new Error('Cannot find constructor in the ABI.'); - } - // hack: addresses are stored as string in the form to avoid bigint compatibility issues with formik - // convert those back to bigints before sending - - // for now, dont let user change the salt. requires some change to the form generation if we want to let user choose one - // since everything is currently based on parsing the contractABI, and the salt parameter is not present there - const salt = Fr.random(); - return await deployContract(wallet, artifact, typedArgs, salt, pxe); - } - - if (functionAbi.functionType === 'unconstrained') { - return await viewContractFunction(contractAddress!, artifact, functionName, typedArgs, pxe, wallet); - } else { - const txnReceipt = await callContractFunction(contractAddress!, artifact, functionName, typedArgs, pxe, wallet); - return `Transaction ${txnReceipt.status} on block number ${txnReceipt.blockNumber}`; - } -} - -interface ContractFunctionFormProps { - wallet: CompleteAddress; - contractAddress?: AztecAddress; - artifact: ContractArtifact; - functionAbi: FunctionArtifact; - defaultAddress: string; - title?: string; - buttonText?: string; - isLoading: boolean; - disabled: boolean; - onSubmit: () => void; - onSuccess: (result: any) => void; - onError: (msg: string) => void; -} - -export function ContractFunctionForm({ - wallet, - contractAddress, - artifact, - functionAbi, - defaultAddress, - buttonText = 'Submit', - isLoading, - disabled, - onSubmit, - onSuccess, - onError, -}: ContractFunctionFormProps) { - const { validationSchema, initialValues } = generateYupSchema(functionAbi, defaultAddress); - const formik = useFormik({ - initialValues: initialValues, - validationSchema: validationSchema, - onSubmit: async (values: any) => { - onSubmit(); - try { - const result = await handleFunctionCall(contractAddress, artifact, functionAbi.name, values, wallet); - onSuccess(result); - } catch (e: any) { - onError(e.message); - } - }, - }); - - return ( -
- {functionAbi.parameters.map(input => ( -
- - - {formik.touched[input.name] && formik.errors[input.name] && ( -
{formik.errors[input.name]?.toString()}
- )} -
- ))} - {isLoading ? ( - - ) : ( -