|
| 1 | +import { encodeFunctionData } from "viem"; // Adjust the import path according to your project structure |
1 | 2 | import { promises as fs } from "fs";
|
2 | 3 |
|
| 4 | +interface PrepareAndLogTransactionParams { |
| 5 | + contractInstance: any; // Replace `any` with the correct contract type if available |
| 6 | + functionName: string; |
| 7 | + args: any[]; |
| 8 | + description: string; |
| 9 | + inputs: { |
| 10 | + internalType: string; |
| 11 | + name: string; |
| 12 | + type: string; |
| 13 | + }[]; |
| 14 | +} |
| 15 | + |
3 | 16 | export const logTransaction = (description: string, data: string) => {
|
4 | 17 | console.log(`Transaction: ${description}`);
|
5 | 18 | console.log(`Data: ${data}`);
|
6 | 19 | };
|
7 | 20 |
|
8 | 21 | const transactions: any[] = [];
|
9 | 22 |
|
10 |
| -export const addTransaction = (tx: any) => { |
| 23 | +export const addTransaction = async (tx: any) => { |
11 | 24 | transactions.push(tx);
|
| 25 | + await writeSingleTransactionToFile(tx); |
| 26 | +}; |
| 27 | + |
| 28 | +const writeSingleTransactionToFile = async (tx: any) => { |
| 29 | + const filePath = "./transactions.json"; |
| 30 | + try { |
| 31 | + let batch; |
| 32 | + try { |
| 33 | + const fileContent = await fs.readFile(filePath, "utf8"); |
| 34 | + batch = JSON.parse(fileContent); |
| 35 | + } catch (error) { |
| 36 | + if (error.code === "ENOENT" || error.message === "Unexpected end of JSON input") { |
| 37 | + batch = { |
| 38 | + version: "1.0", |
| 39 | + chainId: "34443", |
| 40 | + createdAt: Math.floor(Date.now() / 1000), |
| 41 | + meta: { |
| 42 | + name: "Transactions Batch", |
| 43 | + description: "", |
| 44 | + txBuilderVersion: "1.16.5", |
| 45 | + createdFromSafeAddress: "0x8Fba84867Ba458E7c6E2c024D2DE3d0b5C3ea1C2", |
| 46 | + createdFromOwnerAddress: "", |
| 47 | + checksum: "0x" |
| 48 | + }, |
| 49 | + transactions: [] |
| 50 | + }; |
| 51 | + } else { |
| 52 | + throw error; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + batch.transactions.push(tx); |
| 57 | + |
| 58 | + await fs.writeFile(filePath, JSON.stringify(batch, null, 2)); |
| 59 | + console.log(`Transaction added and written to ${filePath}`); |
| 60 | + } catch (error) { |
| 61 | + console.error(`Failed to write transaction to ${filePath}`, error); |
| 62 | + } |
12 | 63 | };
|
13 | 64 |
|
14 |
| -export const writeTransactionsToFile = async () => { |
15 |
| - const batch = { |
16 |
| - version: "1.0", |
17 |
| - chainId: "34443", |
18 |
| - createdAt: Math.floor(Date.now() / 1000), |
19 |
| - meta: { |
20 |
| - name: "Transactions Batch", |
21 |
| - description: "", |
22 |
| - txBuilderVersion: "1.16.5", |
23 |
| - createdFromSafeAddress: "0x8Fba84867Ba458E7c6E2c024D2DE3d0b5C3ea1C2", |
24 |
| - createdFromOwnerAddress: "", |
25 |
| - checksum: "0x" |
| 65 | +export const prepareAndLogTransaction = async ({ |
| 66 | + contractInstance, |
| 67 | + functionName, |
| 68 | + args, |
| 69 | + description, |
| 70 | + inputs |
| 71 | +}: PrepareAndLogTransactionParams) => { |
| 72 | + const data = encodeFunctionData({ |
| 73 | + abi: contractInstance.abi, |
| 74 | + functionName, |
| 75 | + args |
| 76 | + }); |
| 77 | + |
| 78 | + await addTransaction({ |
| 79 | + to: contractInstance.address, |
| 80 | + value: "0", |
| 81 | + data, |
| 82 | + contractMethod: { |
| 83 | + inputs, |
| 84 | + name: functionName, |
| 85 | + payable: false |
26 | 86 | },
|
27 |
| - transactions |
28 |
| - }; |
| 87 | + contractInputsValues: args.reduce((acc: any, arg: any, index: number) => { |
| 88 | + acc[inputs[index].name] = arg; |
| 89 | + return acc; |
| 90 | + }, {}) |
| 91 | + }); |
29 | 92 |
|
30 |
| - const filePath = "./transactions.json"; |
31 |
| - await fs.writeFile(filePath, JSON.stringify(batch, null, 2)); |
32 |
| - console.log(`Transactions written to ${filePath}`); |
| 93 | + logTransaction(description, data); |
33 | 94 | };
|
0 commit comments