diff --git a/src/transaction/action.ts b/src/transaction/action.ts index 5a5c239c..8b574321 100644 --- a/src/transaction/action.ts +++ b/src/transaction/action.ts @@ -300,7 +300,10 @@ export class Action { } } - const encodedActionInputs = this.encodeActionInputs(selectedAction, actionInputs); + const encodedActionInputs: EncodedValue[][] = []; + for (const actionInput of actionInputs) { + encodedActionInputs.push(encodeActionInputs(Object.values(actionInput))); + } return { actionName: selectedAction.name, @@ -390,69 +393,6 @@ export class Action { return true; } - /** - * Encodes the action inputs into the expected format depending on the parameter types for the action. - * - * @param {NamespaceAction} selectedAction - The schema of the action to be executed. - * @param {ActionInput[]} actionInputs - The values of the actions to be executed. - * @returns {EncodedValue[][]} - An array of arrays of encoded values. - */ - private encodeActionInputs( - selectedAction: NamespaceAction, - actionInputs: Entries[] - ): EncodedValue[][] { - const encodedActionInputs: EncodedValue[][] = []; - - for (let i = 0; i < actionInputs.length; i++) { - const actionObject = actionInputs[i]; - - for (const [parameterName, parameterValue] of Object.entries(actionObject)) { - // Find the array location the parameter name is at from the selectedAction - const parameterNameIndex = selectedAction.parameter_names.findIndex( - (name) => name === parameterName - ); - if (parameterNameIndex === -1) { - throw new Error(`Parameter ${parameterName} not found in action ${this.actionName}.`); - } - // We determine the parameter type from the action definition, instead of inferring it from the parameter value - let parameterType = selectedAction.parameter_types[parameterNameIndex] as VarType; - - // Initialize the encodedActionInputs array if it doesn't exist - encodedActionInputs[i] = encodedActionInputs[i] || []; - - // Set metadata for all types - let metadata: number[] = [0, 0]; - - // Set metadata for numeric types - // Decimal is now deprecated and should be replaced with numeric but keeping for compatibility right now - if (parameterType.includes('numeric') || parameterType.includes('decimal')) { - parameterType = VarType.NUMERIC; - const analysis = analyzeNumber(Number(parameterValue)); - metadata = [analysis.precision, analysis.scale]; - } - - // Validate parameter type against VarType enum - if (!Object.values(VarType).includes(parameterType)) { - // This shouldn't happen as we are using the parameter type from the action definition - throw new Error( - `Invalid parameter type '${parameterType}' for action '${this.actionName}' parameter '${parameterName}'. If you hit this error, please report it to the Kwil team.` - ); - } - - encodedActionInputs[i].push({ - type: { - name: parameterType, - is_array: false, // TODO: Need to update to handle arrays - metadata, - }, - data: [encodeValue(parameterValue)], - }); - } - } - - return encodedActionInputs; - } - private assertNotBuilding(): void { if (this.actionInputs === TXN_BUILD_IN_PROGRESS) { throw new Error('Cannot modify the builder while a transaction is being built.'); diff --git a/test-eth-app/src/App.tsx b/test-eth-app/src/App.tsx index d813baac..72fe101a 100644 --- a/test-eth-app/src/App.tsx +++ b/test-eth-app/src/App.tsx @@ -81,10 +81,10 @@ function App() { 'INSERT INTO saved_arrays (id, text_arr, int_arr) VALUES ($id, $text_arr, $int_arr)', { $id: '123e4567-e89b-12d3-a456-426614174000', - $text_arr: ['test', 'test2'], - $int_arr: [1, 2], - // $text_arr: 'test', - // $int_arr: 1, + // $text_arr: ['test', 'test2'], + // $int_arr: [1, 2], + $text_arr: 'test', + $int_arr: 1, }, kwilSigner, true