Skip to content

Commit

Permalink
refactor: update action input encoding to make simpler. remove comple…
Browse files Browse the repository at this point in the history
…x way of finding param type
  • Loading branch information
martin-opensky committed Jan 30, 2025
1 parent f56f555 commit a1cbf0d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 68 deletions.
68 changes: 4 additions & 64 deletions src/transaction/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,10 @@ export class Action<T extends EnvironmentType> {
}
}

const encodedActionInputs = this.encodeActionInputs(selectedAction, actionInputs);
const encodedActionInputs: EncodedValue[][] = [];
for (const actionInput of actionInputs) {
encodedActionInputs.push(encodeActionInputs(Object.values(actionInput)));
}

return {
actionName: selectedAction.name,
Expand Down Expand Up @@ -390,69 +393,6 @@ export class Action<T extends EnvironmentType> {
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.');
Expand Down
8 changes: 4 additions & 4 deletions test-eth-app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a1cbf0d

Please sign in to comment.