Repository navigation
Expand file tree
/
Copy pathsync-database-to-contract.js
More file actions
executable file
·308 lines (275 loc) · 9.49 KB
/
Copy pathsync-database-to-contract.js
File metadata and controls
executable file
·308 lines (275 loc) · 9.49 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env node
const { createPublicClient, createWalletClient, http, privateKeyToAccount } = require('viem');
// Somnia network configuration
const chain = {
id: 50312,
name: 'Somnia',
nativeCurrency: {
decimals: 18,
name: 'ETH',
symbol: 'ETH',
},
rpcUrls: {
default: {
http: ['https://dream-rpc.somnia.network/']
}
}
};
const CORRECT_CONTRACT = '0x70D7D101641c72b8254Ab45Ff2a5CED9b0ad0E75';
// Get private key from environment
const PRIVATE_KEY = process.env.PRIVATE_KEY || process.env.BOT_PRIVATE_KEY;
if (!PRIVATE_KEY) {
console.error('❌ Private key not found. Set PRIVATE_KEY or BOT_PRIVATE_KEY environment variable.');
process.exit(1);
}
const account = privateKeyToAccount(PRIVATE_KEY);
const publicClient = createPublicClient({
chain,
transport: http('https://dream-rpc.somnia.network/')
});
const walletClient = createWalletClient({
account,
chain,
transport: http('https://dream-rpc.somnia.network/')
});
// Contract ABI
const ODDYSSEY_ABI = [
{
"inputs": [],
"name": "getCurrentCycle",
"outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"components": [
{"internalType": "uint256", "name": "id", "type": "uint256"},
{"internalType": "uint256", "name": "startTime", "type": "uint256"},
{"internalType": "uint256", "name": "oddsHome", "type": "uint256"},
{"internalType": "uint256", "name": "oddsDraw", "type": "uint256"},
{"internalType": "uint256", "name": "oddsAway", "type": "uint256"},
{"internalType": "uint256", "name": "oddsOver", "type": "uint256"},
{"internalType": "uint256", "name": "oddsUnder", "type": "uint256"},
{
"components": [
{"internalType": "uint8", "name": "moneyline", "type": "uint8"},
{"internalType": "uint8", "name": "overUnder", "type": "uint8"}
],
"internalType": "struct IOddyssey.Result",
"name": "result",
"type": "tuple"
}
],
"internalType": "struct IOddyssey.Match[10]",
"name": "_matches",
"type": "tuple[10]"
}
],
"name": "startDailyCycle",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
];
// Database matches from the current cycle (from the earlier check)
const databaseMatches = [
{
id: BigInt('19568530'),
startTime: BigInt(1758049200),
oddsHome: BigInt(1950),
oddsDraw: BigInt(3500),
oddsAway: BigInt(3900),
oddsOver: BigInt(1800),
oddsUnder: BigInt(2000),
result: { moneyline: 0, overUnder: 0 }
},
{
id: BigInt('19556654'),
startTime: BigInt(1758046500),
oddsHome: BigInt(3100),
oddsDraw: BigInt(3250),
oddsAway: BigInt(2100),
oddsOver: BigInt(2300),
oddsUnder: BigInt(1600),
result: { moneyline: 0, overUnder: 0 }
},
{
id: BigInt('19568595'),
startTime: BigInt(1758049200),
oddsHome: BigInt(1850),
oddsDraw: BigInt(3750),
oddsAway: BigInt(4000),
oddsOver: BigInt(1800),
oddsUnder: BigInt(2000),
result: { moneyline: 0, overUnder: 0 }
},
{
id: BigInt('19556609'),
startTime: BigInt(1758038400),
oddsHome: BigInt(1750),
oddsDraw: BigInt(3300),
oddsAway: BigInt(4100),
oddsOver: BigInt(2050),
oddsUnder: BigInt(1750),
result: { moneyline: 0, overUnder: 0 }
},
{
id: BigInt('19568573'),
startTime: BigInt(1758041100),
oddsHome: BigInt(1800),
oddsDraw: BigInt(3700),
oddsAway: BigInt(4330),
oddsOver: BigInt(1610),
oddsUnder: BigInt(2300),
result: { moneyline: 0, overUnder: 0 }
},
{
id: BigInt('19568475'),
startTime: BigInt(1758041100),
oddsHome: BigInt(4500),
oddsDraw: BigInt(3500),
oddsAway: BigInt(1830),
oddsOver: BigInt(2100),
oddsUnder: BigInt(1720),
result: { moneyline: 0, overUnder: 0 }
},
{
id: BigInt('19510860'),
startTime: BigInt(1758043800),
oddsHome: BigInt(2500),
oddsDraw: BigInt(2870),
oddsAway: BigInt(3300),
oddsOver: BigInt(3250),
oddsUnder: BigInt(1330),
result: { moneyline: 0, overUnder: 0 }
},
{
id: BigInt('19556564'),
startTime: BigInt(1758024900),
oddsHome: BigInt(2300),
oddsDraw: BigInt(3700),
oddsAway: BigInt(3150),
oddsOver: BigInt(1750),
oddsUnder: BigInt(2050),
result: { moneyline: 0, overUnder: 0 }
},
{
id: BigInt('19510859'),
startTime: BigInt(1758043800),
oddsHome: BigInt(2200),
oddsDraw: BigInt(2800),
oddsAway: BigInt(4000),
oddsOver: BigInt(3400),
oddsUnder: BigInt(1300),
result: { moneyline: 0, overUnder: 0 }
},
{
id: BigInt('19568580'),
startTime: BigInt(1758049200),
oddsHome: BigInt(1400),
oddsDraw: BigInt(5000),
oddsAway: BigInt(7000),
oddsOver: BigInt(1440),
oddsUnder: BigInt(2750),
result: { moneyline: 0, overUnder: 0 }
}
];
async function syncDatabaseToContract() {
try {
console.log('🔄 Synchronizing Database to Contract...');
console.log('📍 Contract Address:', CORRECT_CONTRACT);
console.log('👤 Account:', account.address);
console.log('=' .repeat(80));
// Check current contract state
console.log('\n1️⃣ Checking current contract state...');
const currentCycle = await publicClient.readContract({
address: CORRECT_CONTRACT,
abi: ODDYSSEY_ABI,
functionName: 'getCurrentCycle'
});
console.log(' Current contract cycle:', currentCycle.toString());
// Check balance
const balance = await publicClient.getBalance({ address: account.address });
console.log(' Account balance:', (Number(balance) / 1e18).toFixed(4), 'ETH');
if (balance === 0n) {
console.error('❌ Account has no balance for gas fees');
return;
}
if (currentCycle > 0n) {
console.log('⚠️ Contract already has cycles. This might overwrite existing data.');
console.log(' Are you sure you want to proceed? (This script will continue in 5 seconds)');
await new Promise(resolve => setTimeout(resolve, 5000));
}
// Prepare matches for contract
console.log('\n2️⃣ Preparing database matches for contract...');
console.log(' Database matches:', databaseMatches.length);
console.log(' Sample match IDs:', databaseMatches.slice(0, 3).map(m => m.id.toString()).join(', '));
// Estimate gas
try {
console.log('\n3️⃣ Estimating gas for startDailyCycle...');
const gasEstimate = await publicClient.estimateContractGas({
account,
address: CORRECT_CONTRACT,
abi: ODDYSSEY_ABI,
functionName: 'startDailyCycle',
args: [databaseMatches]
});
console.log(' Estimated gas:', gasEstimate.toString());
// Execute the transaction
console.log('\n4️⃣ Executing startDailyCycle with database matches...');
const hash = await walletClient.writeContract({
address: CORRECT_CONTRACT,
abi: ODDYSSEY_ABI,
functionName: 'startDailyCycle',
args: [databaseMatches],
gas: gasEstimate * 120n / 100n // Add 20% buffer
});
console.log('🚀 Transaction sent:', hash);
console.log('⏳ Waiting for confirmation...');
const receipt = await publicClient.waitForTransactionReceipt({ hash });
console.log('✅ Transaction confirmed in block:', receipt.blockNumber);
// Verify the cycle was created
console.log('\n5️⃣ Verifying synchronization...');
const newCycle = await publicClient.readContract({
address: CORRECT_CONTRACT,
abi: ODDYSSEY_ABI,
functionName: 'getCurrentCycle'
});
console.log(' New contract cycle:', newCycle.toString());
if (newCycle > 0n) {
console.log('🎉 SUCCESS! Contract synchronized with database matches');
console.log('✅ Frontend should now show team names and odds');
console.log('\n📋 NEXT STEPS:');
console.log('1. Wait a few minutes for backend to restart with new contract address');
console.log('2. Check frontend - it should now show matches with team names and odds');
console.log('3. Verify backend can fetch matches from the correct contract');
} else {
console.log('⚠️ Contract cycle still 0. Check if transaction reverted.');
}
} catch (gasError) {
console.error('❌ Transaction execution failed:', gasError.message);
if (gasError.message.includes('execution reverted')) {
console.log('\n🔍 Transaction would revert. Possible reasons:');
console.log(' - Not authorized to call startDailyCycle');
console.log(' - Contract conditions not met');
console.log(' - Invalid match data format');
console.log(' - Contract paused or has other restrictions');
// Check if we're the owner
console.log('\n🔐 Checking authorization...');
console.log(' Calling account:', account.address);
console.log(' You may not be authorized to call startDailyCycle on this contract.');
}
}
} catch (error) {
console.error('💥 Error synchronizing:', error);
}
}
// Run the sync
console.log('⚠️ This script will sync database matches to the contract');
console.log('⚠️ Make sure the PRIVATE_KEY environment variable is set to an authorized account');
console.log('⚠️ Starting in 3 seconds...');
setTimeout(() => {
syncDatabaseToContract().catch(console.error);
}, 3000);