Skip to content

Commit

Permalink
feat: aded transformActions utility and exposed all utils (#42)
Browse files Browse the repository at this point in the history
feat: added transformActions utility and exposed all utils
  • Loading branch information
jaswinder6991 authored Aug 5, 2024
1 parent 380d7f7 commit bfc9072
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './constants';
export * from './controllers';
export * from './enums';
export * from './types';
export * from './utils';
10 changes: 10 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export * from './calculateRequiredDeposit';
export * from './calculateSizeOfData';
export * from './convertNEARToYoctoNEAR';
export * from './convertYoctoNEARToNEAR';
export * from './isObject';
export * from './parseKeysFromData';
export * from './rpcQueries';
export * from './rpcURLFromNetworkID';
export * from './transformActions';
export * from './validateAccountId';
2 changes: 2 additions & 0 deletions src/utils/rpcQueries/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './viewAccessKeyList';
export * from './viewFunction';
1 change: 1 addition & 0 deletions src/utils/transformActions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './transformActions';
87 changes: 87 additions & 0 deletions src/utils/transformActions/transformActions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { transformActions } from './transformActions';
import { Action, FunctionCall } from '@near-js/transactions';

describe('transformActions', () => {
it('should transform a valid FunctionCall action', () => {
const mockFunctionCall: FunctionCall = {
methodName: 'testMethod',
args: new Uint8Array([1, 2, 3]),
gas: BigInt(1000000),
deposit: BigInt(0),
};

const mockAction: Action = {
enum: 'FunctionCall',
functionCall: mockFunctionCall,
};

const result = transformActions([mockAction]);

expect(result).toEqual([
{
type: 'FunctionCall',
params: {
methodName: 'testMethod',
args: new Uint8Array([1, 2, 3]),
gas: BigInt(1000000),
deposit: BigInt(0),
},
},
]);
});

it('should throw an error for unsupported action types', () => {
const mockUnsupportedAction: Action = {
enum: 'CreateAccount',
createAccount: {},
};

expect(() => transformActions([mockUnsupportedAction])).toThrow(
'Unsupported action type: CreateAccount'
);
});

it('should transform multiple actions', () => {
const mockFunctionCall1: FunctionCall = {
methodName: 'method1',
args: new Uint8Array([4, 5, 6]),
gas: BigInt(2000000),
deposit: BigInt(100),
};

const mockFunctionCall2: FunctionCall = {
methodName: 'method2',
args: new Uint8Array([7, 8, 9]),
gas: BigInt(3000000),
deposit: BigInt(200),
};

const mockActions: Action[] = [
{ enum: 'FunctionCall', functionCall: mockFunctionCall1 },
{ enum: 'FunctionCall', functionCall: mockFunctionCall2 },
];

const result = transformActions(mockActions);

expect(result).toEqual([
{
type: 'FunctionCall',
params: {
methodName: 'method1',
args: new Uint8Array([4, 5, 6]),
gas: BigInt(2000000),
deposit: BigInt(100),
},
},
{
type: 'FunctionCall',
params: {
methodName: 'method2',
args: new Uint8Array([7, 8, 9]),
gas: BigInt(3000000),
deposit: BigInt(200),
},
},
]);
});
});
22 changes: 22 additions & 0 deletions src/utils/transformActions/transformActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Action } from '@near-js/transactions';
import { TransformedAction } from './types';

// Helper function to transform Actions
export const transformActions = (actions: Action[]): TransformedAction[] => {
return actions.map((action) => {
if (!action.functionCall) {
throw new Error(`Unsupported action type: ${action.enum}`);
}

const functionCall = action.functionCall;
return {
type: 'FunctionCall',
params: {
methodName: functionCall.methodName,
args: functionCall.args,
gas: functionCall.gas,
deposit: functionCall.deposit,
},
};
});
};
9 changes: 9 additions & 0 deletions src/utils/transformActions/types/ITransformedActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface TransformedAction {
type: 'FunctionCall';
params: {
methodName: string;
args: Uint8Array;
gas: bigint;
deposit: bigint;
};
}
1 change: 1 addition & 0 deletions src/utils/transformActions/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ITransformedActions';

0 comments on commit bfc9072

Please sign in to comment.