-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.ts
More file actions
175 lines (152 loc) · 5.76 KB
/
Copy pathexample.ts
File metadata and controls
175 lines (152 loc) · 5.76 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import {
indexStates,
Chain,
createContract,
PureHandler,
SideEffectHandler,
PureHandlerContext,
SideEffectHandlerContext,
BaseContractState,
createUpdates,
StateMigrations,
} from './chaindexing/src';
import { Config } from './chaindexing-config/src';
import { PostgresRepo } from './chaindexing-postgres/src';
// Example NFT state
class Nft extends BaseContractState {
public tokenId: number;
public ownerAddress: string;
constructor(tokenId: number, ownerAddress: string) {
super();
this.tokenId = tokenId;
this.ownerAddress = ownerAddress;
}
tableName(): string {
return 'nfts';
}
}
// State migrations for NFT
class NftMigrations implements StateMigrations {
migrations(): string[] {
return [
`CREATE TABLE IF NOT EXISTS nfts (
token_id INTEGER NOT NULL,
owner_address TEXT NOT NULL,
created_at TIMESTAMP DEFAULT NOW(),
updated_at TIMESTAMP DEFAULT NOW()
)`,
`CREATE UNIQUE INDEX IF NOT EXISTS nfts_token_id_index ON nfts(token_id)`,
];
}
}
// Transfer event handler (Pure Handler)
class TransferHandler implements PureHandler {
abi(): string {
return 'event Transfer(address indexed from, address indexed to, uint256 indexed tokenId)';
}
async handleEvent(context: PureHandlerContext): Promise<void> {
const eventParams = context.getEventParams();
const from = eventParams.getAddressString('from');
const to = eventParams.getAddressString('to');
const tokenId = eventParams.getU32('tokenId');
console.log(`Processing Transfer: ${from} -> ${to}, tokenId: ${tokenId}`);
// In a real implementation, this would query the database
// For now, we'll create a new NFT or update existing one
if (from === '0x0000000000000000000000000000000000000000') {
// Mint: create new NFT
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const newNft = new Nft(tokenId, to);
console.log(`Minted NFT ${tokenId} to ${to}`);
// In real implementation: await newNft.create(context);
} else {
// Transfer: update existing NFT
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const existingNft = new Nft(tokenId, from);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const updates = createUpdates('owner_address', to);
console.log(`Transferred NFT ${tokenId} from ${from} to ${to}`);
// In real implementation: await existingNft.update(updates, context);
}
}
}
// Notification handler (Side Effect Handler)
interface AppState {
notificationCount: number;
lastProcessedBlock: number;
}
class NotificationHandler implements SideEffectHandler<AppState> {
abi(): string {
return 'event Transfer(address indexed from, address indexed to, uint256 indexed tokenId)';
}
async handleEvent(context: SideEffectHandlerContext<AppState>): Promise<void> {
const eventParams = context.getEventParams();
const sharedState = await context.getSharedState();
const to = eventParams.getAddressString('to');
const tokenId = eventParams.getU32('tokenId');
// Send notification (side effect)
await this.sendNotification(to, tokenId);
// Update shared state
sharedState.notificationCount++;
sharedState.lastProcessedBlock = context.event.blockNumber;
console.log(`Sent notification for NFT ${tokenId} to ${to}`);
console.log(`Total notifications: ${sharedState.notificationCount}`);
}
private async sendNotification(address: string, tokenId: number): Promise<void> {
// Mock notification service
console.log(`📧 Notification: NFT ${tokenId} transferred to ${address}`);
}
}
// Main function demonstrating usage
async function main() {
try {
// Create PostgreSQL repository
const repo = new PostgresRepo('postgresql://username:password@localhost:5432/chaindexing');
// Create configuration
const config = new Config(repo)
// Add Ethereum mainnet
.addChain(Chain.Mainnet, 'https://eth-mainnet.g.alchemy.com/v2/your-api-key')
// Add Polygon
.addChain(Chain.Sepolia, 'https://eth-sepolia.g.alchemy.com/v2/your-api-key')
// Configure processing parameters
.withBlocksPerBatch(100)
.withHandlerRateMs(5000)
.withIngestionRateMs(10000)
.withMinConfirmationCount(12)
// Add shared state for side effects
.withInitialState<AppState>({
notificationCount: 0,
lastProcessedBlock: 0,
})
// Add contract with handlers
.addContract(
createContract<AppState>('ERC721Token')
// Add contract address on Ethereum mainnet
.addAddress('0x1234567890123456789012345678901234567890', Chain.Mainnet, 18000000)
// Add contract address on Sepolia testnet
.addAddress('0x1234567890123456789012345678901234567890', Chain.Sepolia, 4000000)
// Add event handlers
.addEventHandler(new TransferHandler())
.addSideEffectHandler(new NotificationHandler())
// Add state migrations
.addStateMigrations(new NftMigrations())
.build()
);
console.log('🚀 Starting Chaindexing TypeScript Implementation');
console.log('📊 Configuration:');
console.log(` - Chains: ${Array.from(config.chains.keys()).join(', ')}`);
console.log(` - Contracts: ${config.contracts.map((c) => c.name).join(', ')}`);
console.log(` - Blocks per batch: ${config.blocksPerBatch}`);
console.log(` - Handler rate: ${config.handlerRateMs}ms`);
console.log(` - Ingestion rate: ${config.ingestionRateMs}ms`);
// Start indexing
await indexStates(config);
} catch (error) {
console.error('❌ Error starting chaindexing:', error);
process.exit(1);
}
}
// Run the example
if (require.main === module) {
main().catch(console.error);
}
export { main };