-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
230 lines (210 loc) · 12.2 KB
/
index.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
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
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>BeefLedger Deal Room</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" href="styles.css" />
<script src="https://cdn.jsdelivr.net/npm/magic-sdk/dist/magic.js" type="text/javascript"></script>
<script src="https://cdn.ethers.io/lib/ethers-5.0.umd.min.js" type="text/javascript"></script>
<!--script src="https://cdn.ethers.io/scripts/ethers-v5.min.js" type="text/javascript"></script-->
<!--script src="https://cdn.tutorialjinni.com/bignumber.js/9.0.0/bignumber.min.js" type="text/javascript"></script-->
<script src="./dist/bundle.js" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
/* Configure Ethereum provider */
const magic = new Magic("pk_test_6ABE2E341F154BF7", {
network: {
rpcUrl: 'http://127.0.0.1:8545'
//chainId: 1011 // Your own node's chainId
}
});
const provider = new ethers.providers.Web3Provider(magic.rpcProvider);
/* Smart contract values */
const contractABI =
'[{"constant":false,"inputs":[{"name":"newMessage","type":"string"}],"name":"update","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"message","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"initMessage","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]';
const contractByteCode =
"0x608060405234801561001057600080fd5b5060405161047f38038061047f8339818101604052602081101561003357600080fd5b81019080805164010000000081111561004b57600080fd5b8281019050602081018481111561006157600080fd5b815185600182028301116401000000008211171561007e57600080fd5b5050929190505050806000908051906020019061009c9291906100a3565b5050610148565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100e457805160ff1916838001178555610112565b82800160010185558215610112579182015b828111156101115782518255916020019190600101906100f6565b5b50905061011f9190610123565b5090565b61014591905b80821115610141576000816000905550600101610129565b5090565b90565b610328806101576000396000f3fe608060405234801561001057600080fd5b5060043610610053576000357c0100000000000000000000000000000000000000000000000000000000900480633d7403a314610058578063e21f37ce14610113575b600080fd5b6101116004803603602081101561006e57600080fd5b810190808035906020019064010000000081111561008b57600080fd5b82018360208201111561009d57600080fd5b803590602001918460018302840111640100000000831117156100bf57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610196565b005b61011b6101b0565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561015b578082015181840152602081019050610140565b50505050905090810190601f1680156101885780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b80600090805190602001906101ac92919061024e565b5050565b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102465780601f1061021b57610100808354040283529160200191610246565b820191906000526020600020905b81548152906001019060200180831161022957829003601f168201915b505050505081565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061028f57805160ff19168380011785556102bd565b828001600101855582156102bd579182015b828111156102bc5782518255916020019190600101906102a1565b5b5090506102ca91906102ce565b5090565b6102f091905b808211156102ec5760008160009055506001016102d4565b5090565b9056fea265627a7a7230582003ae1ef5a63bf058bfd2b31398bdee39d3cbfbb7fbf84235f4bc2ec352ee810f64736f6c634300050a0032";
let contractAddress = "0x8b211dfebf490a648f6de859dfbed61fa22f35e0";
const render = async () => {
const isLoggedIn = await magic.user.isLoggedIn();
/* Show login form if user is not logged in */
let authHtml = `
<div class="container">
<h3>Please sign up or login<\/h3>
<form onsubmit="handleLogin(event)">
<input type="email" name="email" required="required" placeholder="Enter your email" />
<button type="submit">Send<\/button>
<\/form>
<\/div>
`;
let userHtml = "";
let txnHtml = "";
let signHtml = "";
var dealRoomController;
const target = document.querySelector("#app");
if (isLoggedIn) {
/* Get user metadata including email */
const userMetadata = await magic.user.getMetadata();
const signer = provider.getSigner();
const network = await provider.getNetwork();
const userAddress = await signer.getAddress();
const userBalance = ethers.utils.formatEther(
await provider.getBalance(userAddress)
);
authHtml = `
<div class="container">
<h1>Current user: ${userMetadata.email}<\/h1>
<button onclick="handleLogout()">Logout<\/button>
<\/div>
`;
userHtml = `
<div class="container">
<h1>Ethereum address<\/h1>
<div class="info">
<a href="https://rinkeby.etherscan.io/address/${userAddress}" target="_blank">${userAddress}<\/a>
<\/div>
<h1>Network<\/h1>
<div class="info">${network.name}<\/div>
<h1>Balance<\/h1>
<div class="info">${userBalance} ETH<\/div>
<\/div>
`;
dealRoomHtml = `
<div class="container">
<h1>Make a Deal Room<\/h1>
<div class="info">
Make a new deal room for you, a seller, and an arbitrator
<\/div>
<input type="text" id="sellerAddress" class="full-width" required="required" placeholder="Seller address" value="0x4667D332c39c115C4E4e1d50aaB5E6D09A5134a4" />
<button id="btn-deploy" onclick="handleMakeDealRoom()">Make<\/button>
<\/div>
<div class="container">
<h1>Make a Deal<\/h1>
<div class="info">
Make a deal in your deal room
<\/div>
<input type="text" id="erc20" class="full-width" required="required" placeholder="Address of ERC-20 contract" value="0xCDf0C6cf21EF3cB70176a35cf0a84acF7588d264" />
<input type="text" id="erc721" class="full-width" required="required" placeholder="Address of ERC-721 contract" value="0x53A7b7aa1a1dD7A6031D4Fa5bE6764d6Bb368609" />
<input type="text" id="price" class="full-width" required="required" placeholder="Price" value="100" />
<input type="text" id="items" class="full-width" required="required" placeholder="Items" value="111,222,333,444" />
<button id="btn-deploy" onclick="handleMakeDeal()">Make<\/button>
<\/div>
`;
}
target.innerHTML =
authHtml + userHtml + txnHtml + signHtml + dealRoomHtml;
};
const handleLogin = async e => {
e.preventDefault();
const email = new FormData(e.target).get("email");
if (email) {
/* One-liner login 🤯 */
await magic.auth.loginWithMagicLink({ email });
render();
}
};
const handleMakeDealRoom = async () => {
//evt.preventDefault();
const sellerAddress = document.getElementById("sellerAddress").value;
const signer = provider.getSigner();
const params = {
buyer: await signer.getAddress(),
seller: sellerAddress,
arbitrator: arbitratorAddress,
}
dealRoomController = new DealService.DealService.DealRoomController(DEALROOM_HUB, params, signer);
dealRoomContract = await dealRoomController.getDealRoomContract()
}
const handleMakeDeal = async () => {
const erc20 = document.getElementById("erc20").value;
const erc721 = document.getElementById("erc721").value;
const price = ethers.BigNumber.from(document.getElementById("price").value);
const assetItems = (document.getElementById("items").value).split(",").map(n=>ethers.BigNumber.from(n));
const deal = {
id: ethers.BigNumber.from(1000),
erc20,
erc721,
price,
assetItems,
}
await dealRoomController.makeDeal(deal);
}
const handleSendTxn = async e => {
e.preventDefault();
const destination = new FormData(e.target).get("destination");
const amount = new FormData(e.target).get("amount");
if (destination && amount) {
const btnSendTxn = document.getElementById("btn-send-txn");
btnSendTxn.disabled = true;
btnSendTxn.innerText = "Sending...";
const signer = provider.getSigner();
const tx = await signer.sendTransaction({
to: destination,
value: ethers.utils.parseEther(amount)
});
const receipt = await tx.wait();
render();
}
};
const handleSignMsg = async e => {
e.preventDefault();
const message = new FormData(e.target).get("message");
if (message) {
const signer = provider.getSigner();
const userAddress = await signer.getAddress();
const signedMessage = await signer.signMessage(message);
alert(`Signed Message: ${signedMessage}`);
const signerAddress = ethers.utils.verifyMessage(
message,
signedMessage
);
}
};
const handleUpdateMsg = async e => {
e.preventDefault();
const newMessage = new FormData(e.target).get("new-message");
if (newMessage) {
const btnUpdateMsg = document.getElementById("btn-update-msg");
btnUpdateMsg.disabled = true;
btnUpdateMsg.innerText = "Updating...";
const signer = provider.getSigner();
let contract = new ethers.Contract(
contractAddress,
contractABI,
signer
);
const tx = await contract.update(newMessage);
const receipt = await tx.wait();
render();
}
};
const handleDeploy = async () => {
const btnDeploy = document.getElementById("btn-deploy");
btnDeploy.disabled = true;
btnDeploy.innerText = "Deploying...";
const signer = provider.getSigner();
const factory = new ethers.ContractFactory(
contractABI,
contractByteCode,
signer
);
const contract = await factory.deploy("Hello World!");
const receipt = await contract.deployed();
contractAddress = contract.address;
render();
};
const handleLogout = async () => {
await magic.user.logout();
render();
};
//]]>
</script>
</head>
<body onload="setTimeout(render, 2000)">
<div id="app">
<div class="container">
Loading...
</div>
</div>
</body>
</html>