-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
94 lines (77 loc) · 3.07 KB
/
app.js
File metadata and controls
94 lines (77 loc) · 3.07 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
const contractAddress = "0x0a26278EDF60c74ddcfce3fCFc9Bb113C09C6894";
let provider, signer, contract;
// Fungsi koneksi wallet (Metamask atau OKX)
document.getElementById("connectBtn").onclick = async () => {
try {
if (window.okxwallet) {
provider = new ethers.providers.Web3Provider(window.okxwallet);
} else if (window.ethereum) {
provider = new ethers.providers.Web3Provider(window.ethereum);
} else {
document.getElementById("status").innerText = "Tidak ada wallet yang terdeteksi.";
return;
}
await provider.send("eth_requestAccounts", []);
signer = provider.getSigner();
const response = await fetch('./abi.json');
const abi = await response.json();
contract = new ethers.Contract(contractAddress, abi, signer);
const address = await signer.getAddress();
document.getElementById("walletAddress").innerText = `Wallet terhubung: ${address}`;
document.getElementById("status").innerText = "Wallet berhasil terhubung.";
} catch (err) {
document.getElementById("status").innerText = "Gagal koneksi wallet: " + err.message;
}
};
// Fungsi mainkan flip
async function playFlip() {
if (!contract) {
document.getElementById("status").innerText = "Hubungkan wallet terlebih dahulu.";
return;
}
const choice = document.getElementById("choice").value;
const bet = document.getElementById("betAmount").value;
if (!bet || bet <= 0) {
document.getElementById("status").innerText = "Masukkan jumlah taruhan yang valid.";
return;
}
const value = ethers.utils.parseEther(bet);
try {
const tx = await contract.playFlip(choice, { value });
document.getElementById("status").innerText = "Transaksi dikirim: " + tx.hash;
const receipt = await tx.wait();
// Cari event FlipResult di receipt
const flipEvent = receipt.events.find(e => e.event === "FlipResult");
if (flipEvent) {
const won = flipEvent.args.won;
const outcome = won ? "Menang 🎉" : "Kalah 😢";
document.getElementById("flipLog").innerText = `Hasil: ${outcome}`;
} else {
document.getElementById("flipLog").innerText = "Tidak bisa membaca hasil flip.";
}
document.getElementById("status").innerText = "Flip selesai!";
} catch (err) {
document.getElementById("status").innerText = "Error saat flip: " + err.message;
}
}
// Fungsi redeem FLIP
async function redeemFLIP() {
if (!contract) {
document.getElementById("status").innerText = "Hubungkan wallet terlebih dahulu.";
return;
}
const amount = document.getElementById("redeemAmount").value;
if (!amount || amount <= 0) {
document.getElementById("status").innerText = "Masukkan jumlah FLIP yang valid.";
return;
}
const tokens = ethers.utils.parseUnits(amount, 18);
try {
const tx = await contract.redeemFLIP(tokens);
document.getElementById("status").innerText = "Convert dikirim: " + tx.hash;
await tx.wait();
document.getElementById("status").innerText = "FLIP berhasil ditukar!";
} catch (err) {
document.getElementById("status").innerText = "Error saat convert: " + err.message;
}
}