/
encoding.ts
50 lines (46 loc) · 1.12 KB
/
encoding.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import {
encodeAbiParameters,
parseAbiParameters,
encodeFunctionData,
} from 'viem';
import { ABI, ArgType } from '../types';
export const encodeValues = (
typesArray: string[],
valueArray: ArgType[]
): string => {
return encodeAbiParameters(
parseAbiParameters(typesArray.join(',')),
valueArray
);
};
export const encodeFunction = (
abi: ABI,
fnName: string,
functionArgs: ReadonlyArray<unknown>
): string | { error: true; message: string } => {
try {
if (!abi || !Array.isArray(functionArgs))
throw new Error(
'Incorrect params passed to safeEncodeHexFunction in abi.js'
);
return encodeFunctionData({
abi,
functionName: fnName,
args: functionArgs,
});
} catch (error) {
console.error('error', error);
return {
error: true,
message: 'Could not encode transaction data with the values provided',
};
}
};
export const getNonce = (length = 24) => {
let text = '';
const possible = '0123456789';
for (let i = 0; i < length; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
};