-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathscript.js
137 lines (121 loc) · 3.75 KB
/
script.js
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
// import { ethers } from 'https://cdn.ethers.io/lib/ethers-5.2.umd.min.js';
async function getEclipseBalance(address) {
const response = await fetch("https://mainnetbeta-rpc.eclipse.xyz/", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
jsonrpc: "2.0",
id: 1,
method: "getBalance",
params: [address],
}),
});
const {
result: { value },
} = await response.json();
return value / 1_000_000_000;
}
document.addEventListener("DOMContentLoaded", function () {
if (window.self === window.top) {
document.body.style.transform = "scale(1.6)";
}
document.getElementById("balance").value =
`Your will see your Eclipse balance here.`;
var web3 = new Web3(Web3.givenProvider);
window.valid_address = false;
document
.getElementById("eclipse-wallet")
.addEventListener("input", async function () {
let data = document.getElementById("eclipse-wallet").value;
console.log(data);
if (data.length >= 41) {
let balance = await getEclipseBalance(data)
.then((b) => {
console.log("Balance is ", b);
window.valid_address = true;
document.getElementById("balance").value =
`Your Eclipse balance is: ${b}`;
document.getElementById("bridge-button").disabled = null;
})
.catch((err) => {
window.valid_address = false;
document.getElementById("bridge-button").disabled = true;
});
}
});
async function testi() {
connectBtn = document.getElementById("connect-button");
walletArea = document.getElementById("sender-eth-addr");
// switch to mainnet
await window.ethereum.request({
method: "wallet_switchEthereumChain",
params: [{ chainId: web3.utils.toHex(1) }],
});
await window.ethereum.request({ method: "eth_requestAccounts" });
web3 = new Web3(window.ethereum);
connectBtn.innerText = "Logout";
walletArea.value = (await web3.eth.getAccounts())[0];
}
testi();
async function deposit() {
let chain = await window.ethereum.chainId;
if (chain != 0x1) {
await window.ethereum.request({
method: "wallet_switchEthereumChain",
params: [{ chainId: web3.utils.toHex(1) }],
});
}
eclipseAddr = document.getElementById("eclipse-wallet").value;
amountinWei = ethers.utils.parseEther(
document.getElementById("ether-amount").value,
);
console.log("Eclipse addr: ", eclipseAddr, amountinWei);
var contract = new web3.eth.Contract(
[
{
inputs: [
{
internalType: "bytes32",
name: "hexSolanaAddress",
type: "bytes32",
},
{
internalType: "uint256",
name: "amountWei",
type: "uint256",
},
],
name: "deposit",
outputs: [],
stateMutability: "payable",
type: "function",
},
],
"0x83cb71d80078bf670b3efec6ad9e5e6407cd0fd1",
);
let ecipseAddrParam = ethers.utils.hexlify(
ethers.utils.base58.decode(eclipseAddr),
);
contract.methods
.deposit(ecipseAddrParam, amountinWei)
.send({
from: (await web3.eth.getAccounts())[0],
value: amountinWei,
})
.once("transactionHash", (hash) => {
window.open("https://etherscan.io/tx/" + hash);
console.log("hash", hash);
})
.then(function (result) {
console.log(result);
});
}
document
.getElementById("bridge-button")
.addEventListener("click", async function () {
deposit();
});
window.ethereum.on("accountsChanged", async () => {
this.location.reload();
});
});