-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.html
84 lines (74 loc) · 3.29 KB
/
deploy.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Deployer</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1 id="title">Deploy something</h1>
<form>
<label for="artifact">Artifact</label>
<textarea autocomplete="false" name="artifact" id="artifact" rows="25" style="resize: vertical;" placeholder="{ "abi": [{}], "bytecode": "0x" }"></textarea>
<label for="args">Arguments</label>
<input autocomplete="false" name="args" id="args" type="text" placeholder="[]">
<button type="submit" disabled>Deploy</button>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ethers/5.2.0/ethers.umd.min.js" type="application/javascript"></script>
<script>
function getFormData(form) {
const data = new FormData(form);
const artifact = data.get("artifact");
const args = data.get("args");
return { artifact, args };
}
function updateTitle(input) {
try {
const { contractName = "something" } = JSON.parse(input);
document.getElementById("title").innerText = `Deploy ${contractName}`;
} catch (_) {
document.getElementById("title").innerText = "Deploy something";
}
}
const form = document.querySelector("form");
const button = form.querySelector("button");
const artifactInput = document.getElementById("artifact");
const argsInput = document.getElementById("args");
try {
const base64 = decodeURIComponent(window.location.hash.substr(1));
const { artifact, args } = JSON.parse(atob(base64));
artifactInput.value = artifact;
argsInput.value = args;
updateTitle(artifact);
} catch (_) {}
artifactInput.addEventListener("input", (event) => updateTitle(event.currentTarget.value));
form.addEventListener("change", event => {
const data = getFormData(event.currentTarget);
const base64 = btoa(JSON.stringify(data));
window.location.hash = `#${encodeURIComponent(base64)}`;
});
const disableForm = event => {
event.preventDefault();
};
form.addEventListener("submit", disableForm);
const provider = new ethers.providers.Web3Provider(window.ethereum);
provider.send("eth_requestAccounts", []).then(() => {
const signer = provider.getSigner();
button.disabled = false;
form.removeEventListener(form, disableForm);
form.addEventListener("submit", event => {
event.preventDefault();
const { artifact, args } = getFormData(event.currentTarget);
const { abi, bytecode } = JSON.parse(artifact);
const factory = new ethers.ContractFactory(abi, bytecode, signer);
button.disabled = true;
factory.deploy(...JSON.parse(args)).then(contract => {
console.log(contract);
});
});
});
</script>
</body>
</html>