-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontract.js
More file actions
152 lines (137 loc) · 4 KB
/
contract.js
File metadata and controls
152 lines (137 loc) · 4 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
import {
encodeFunctionData,
parseAbi,
parseEther,
decodeEventLog,
} from 'https://esm.sh/viem@2';
import {
getPublicClient,
getWalletClient,
getAddress,
getChain,
} from './wallet.js';
// ========== CONFIG ==========
// Deploy ettikten sonra contract adresini buraya yaz
export const CONTRACT_ADDRESS = '0x87ea2144fbb25759a23d489e5655e73bd7899d0a';
// Tip almak istersen kendi adresini yaz
const DEV_ADDRESS = '0xBFbD06913A61235B47393AB4e668dC3E1b2a03aA';
// ============================
export const CONTRACT_ABI = parseAbi([
'function submitScore(uint256 captured) external',
'function highScores(address) external view returns (uint256)',
'function gamesPlayed(address) external view returns (uint256)',
'function totalCaptures(address) external view returns (uint256)',
'function getPlayerStats(address player) external view returns (uint256 highScore, uint256 games, uint256 captures)',
'event ScoreSubmitted(address indexed player, uint256 captured, uint256 timestamp)',
'event NewHighScore(address indexed player, uint256 captured)',
]);
function isConfigured() {
return CONTRACT_ADDRESS !== '0x0000000000000000000000000000000000000000';
}
// --- Single Transaction: Submit Score ---
export async function submitScore(captured) {
if (!isConfigured()) throw new Error('Contract not configured');
const wc = getWalletClient();
const addr = getAddress();
if (!wc || !addr) throw new Error('Wallet not connected');
const hash = await wc.sendTransaction({
to: CONTRACT_ADDRESS,
data: encodeFunctionData({
abi: CONTRACT_ABI,
functionName: 'submitScore',
args: [BigInt(captured)],
}),
account: addr,
});
return hash;
}
// --- Batch Transaction (EIP-5792): Submit Score + Tip Dev ---
export async function submitScoreAndTip(captured, tipEth = '0.0001') {
if (!isConfigured()) throw new Error('Contract not configured');
const wc = getWalletClient();
const addr = getAddress();
const chain = getChain();
if (!wc || !addr) throw new Error('Wallet not connected');
const scoreData = encodeFunctionData({
abi: CONTRACT_ABI,
functionName: 'submitScore',
args: [BigInt(captured)],
});
try {
const result = await wc.request({
method: 'wallet_sendCalls',
params: [{
version: '1.0',
chainId: `0x${chain.id.toString(16)}`,
from: addr,
calls: [
{
to: CONTRACT_ADDRESS,
data: scoreData,
},
{
to: DEV_ADDRESS,
value: `0x${parseEther(tipEth).toString(16)}`,
},
],
}],
});
return { type: 'batch', result };
} catch (e) {
console.warn('Batch tx failed, falling back to single tx:', e.message);
const hash = await submitScore(captured);
return { type: 'single', result: hash };
}
}
// --- Read: Player Stats ---
export async function getPlayerStats(playerAddress) {
if (!isConfigured()) return null;
const pc = getPublicClient();
if (!pc) return null;
try {
const data = await pc.readContract({
address: CONTRACT_ADDRESS,
abi: CONTRACT_ABI,
functionName: 'getPlayerStats',
args: [playerAddress],
});
return {
highScore: Number(data[0]),
games: Number(data[1]),
captures: Number(data[2]),
};
} catch (e) {
console.warn('getPlayerStats failed:', e.message);
return null;
}
}
// --- Read: Single High Score ---
export async function getHighScore(playerAddress) {
if (!isConfigured()) return 0;
const pc = getPublicClient();
if (!pc) return 0;
try {
const score = await pc.readContract({
address: CONTRACT_ADDRESS,
abi: CONTRACT_ABI,
functionName: 'highScores',
args: [playerAddress],
});
return Number(score);
} catch {
return 0;
}
}
// --- Decode ScoreSubmitted event log ---
export function decodeScoreEvent(log) {
try {
return decodeEventLog({
abi: CONTRACT_ABI,
data: log.data,
topics: log.topics,
});
} catch {
return null;
}
}
export { isConfigured };