-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: aded transformActions utility and exposed all utils (#42)
feat: added transformActions utility and exposed all utils
- Loading branch information
1 parent
380d7f7
commit bfc9072
Showing
8 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './viewAccessKeyList'; | ||
export * from './viewFunction'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './transformActions'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}, | ||
}, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './ITransformedActions'; |