-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_example.ts
More file actions
99 lines (85 loc) · 2.95 KB
/
write_example.ts
File metadata and controls
99 lines (85 loc) · 2.95 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { createPublicClient, createWalletClient, http } from "@arkiv-network/sdk";
import { privateKeyToAccount } from "@arkiv-network/sdk/accounts";
import { ExpirationTime, jsonToPayload } from "@arkiv-network/sdk/utils";
import * as fs from "fs";
import * as path from "path";
// Define the custom Dev chain
const dev = {
id: 12345,
name: "Arkiv Dev",
nativeCurrency: {
name: "Ethereum",
symbol: "ETH",
decimals: 18,
},
rpcUrls: {
default: {
http: ["http://127.0.0.1:8545"],
webSocket: ["wss://127.0.0.1:8545"],
},
},
testnet: true,
network: "dev",
} as const;
// Define interface for the JSON structure
interface AccountData {
address: string;
privateKey: string;
}
async function main() {
// 1. Load accounts from keys.json
const keysFilePath = path.join(__dirname, "keys.json");
console.log(`Loading accounts from ${keysFilePath}...`);
const rawData = fs.readFileSync(keysFilePath, "utf-8");
const accounts: AccountData[] = JSON.parse(rawData);
// 2. Initialize the Public Client (shared for reading state)
const publicClient = createPublicClient({
chain: dev,
transport: http(),
});
console.log(`Found ${accounts.length} accounts. Starting transactions...\n`);
// 3. Iterate through each account and perform a transaction
for (const [index, acc] of accounts.entries()) {
try {
console.log(`--- Processing Account ${index + 1}: ${acc.address} ---`);
// Create Wallet Client for specific account
const account = privateKeyToAccount(("0x" + acc.privateKey) as `0x${string}`);
const client = createWalletClient({
chain: dev,
transport: http(),
account: account,
});
// Create a unique entity for this user
// We append the index to the ID to ensure uniqueness
const uniqueId = `doc-hackathon-${index + 1}`;
const { entityKey, txHash } = await client.createEntity({
payload: jsonToPayload({
entity: {
entityType: "document",
entityId: uniqueId,
entityContent: `Hello from DevConnect Hackathon! Account ${acc.address} says hi.`,
},
}),
contentType: "application/json",
attributes: [
{ key: "category", value: "documentation" },
{ key: "author", value: acc.address },
{ key: "batch", value: "1.0" },
],
expiresIn: ExpirationTime.fromDays(30),
});
console.log(`✅ Transaction sent!`);
console.log(` Tx Hash: ${txHash}`);
console.log(` Entity Key: ${entityKey}`);
// Optional: Verify creation
const newEntity = await publicClient.getEntity(entityKey);
console.log(` Verified On-Chain: ID matches '${newEntity?.payload?.entity?.entityId}'\n`);
} catch (error) {
console.error(`❌ Error processing account ${acc.address}:`, error);
}
}
}
main().catch((error) => {
console.error("Error executing main function:", error);
process.exit(1);
});