-
Notifications
You must be signed in to change notification settings - Fork 3
/
buy.html
218 lines (201 loc) · 11 KB
/
buy.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Su Squares, buy a square</title>
<link rel="canonical" href="https://tenthousandsu.com/buy" />
<link rel="stylesheet" href="assets/main.css">
<style>
#square-number {
margin-top: 0.5em;
margin-bottom: 0.5em;
}
</style>
</head>
<body>
<header>
<a id="logo" href="/"><img src="assets/logo-su-squares.svg" style="width:348px;height:57px;object-fit: cover;" alt="Su Squares logo"></a>
<button id="connect-wallet" class="btn">Connect wallet</button>
</header>
<article class="lead">
<h1>Buy Su Squares <span id="square-number-with-number-sign"></span></h1>
<p><em>If you have a question, please mail <a href="mailto:[email protected]?subject=Sales%20question&body=HiSu,%20Please%20help!%20I%20have%20a%20question%20about%20Su%20Squares.">[email protected]</a>.</em></p>
<div id="pick-a-square">
<h2>Which Square would you like?</h2>
<form>
<p><strong>Square number</strong> (between 1 and 10,000)</p>
<input id="square-number" type=number min=1 max=10000 required style="width:7em">
</form>
<button id="select-square" type="button" class="btn btn-lg">Select Square</button>
</div>
<div id="buy-selected-square" style="display:none">
<p>Please connect your wallet using the button above and then click buy below.</p>
<button id="buy" type="button" class="btn btn-lg">Buy Square</button>
</div>
<p>Your purchase is subject to the terms, and your understanding of, our <a href="/white-paper">white paper</a>.</p>
<div id="receipts">
</div>
</article>
<footer>
<small>
No cookies. No analytics. To the extent possible under law, Su Entriken has waived all copyright and related or neighboring rights to the TenThousandSu.com website. This work is published from the United States.
</small>
</footer>
<script type="module">
// Elements and state //////////////////////////////////////////////////////////////////////////////////////////////
const squareNumberWithNumberSignSpan = document.getElementById("square-number-with-number-sign");
const squareNumberInput = document.getElementById("square-number");
const connectWalletButton = document.getElementById("connect-wallet");
const buyButton = document.getElementById("buy");
const pickASquareDiv = document.getElementById("pick-a-square");
const buySelectedSquareDiv = document.getElementById("buy-selected-square");
const receiptsDiv = document.getElementById("receipts");
let squareNumber;
let squarePersonalizations;
// Load personalizations ///////////////////////////////////////////////////////////////////////////////////////////
fetch("build/squarePersonalizations.json")
.then(response => response.json())
.then(data => { squarePersonalizations = data; })
.then(data => {
// Setup datalist for square number input
const datalist = document.createElement("datalist");
datalist.id = "square-numbers";
for (let i = 1; i <= 10000; i++) {
if (squarePersonalizations && squarePersonalizations[i-1]) {
continue;
}
const option = document.createElement("option");
option.value = i;
datalist.appendChild(option);
}
squareNumberInput.setAttribute("list", "square-numbers");
squareNumberInput.parentNode.appendChild(datalist);
console.log(datalist);
})
// Validate Square number //////////////////////////////////////////////////////////////////////////////////////////
squareNumberInput.addEventListener("input", ()=>{
squareNumber = null;
const value = parseInt(squareNumberInput.value);
if (isNaN(value) || value < 1 || value > 10000) {
return alert("Invalid Square number, please enter a number between 1 and 10000.");
}
squareNumber = value;
});
// Handle select Square button /////////////////////////////////////////////////////////////////////////////////////
document.getElementById("select-square").addEventListener("click", ()=>{
if (!squareNumber) {
return alert("Please enter a Square number.");
}
// If the personalizations are loaded, check that the Square is available
if (squarePersonalizations) {
if (squarePersonalizations[squareNumber-1]) {
return alert("Sorry, Square " + squareNumber + " was already purchased.");
}
}
pickASquareDiv.style.display = "none";
buySelectedSquareDiv.style.display = "block";
squareNumberWithNumberSignSpan.innerText = "#" + squareNumber;
});
// Handle ?square= query parameter /////////////////////////////////////////////////////////////////////////////////
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.has("square")) {
squareNumberInput.value = urlParams.get("square");
squareNumberInput.dispatchEvent(new Event("input"));
//document.getElementById("select-square").click(); let the customer click it, by then the personalizations will be loaded
}
// Web3Modal setup /////////////////////////////////////////////////////////////////////////////////////////////////
// warning: Web3Modal does not use SRI, error reported at https://github.com/WalletConnect/web3modal/issues/1305
// documentation from https://docs.walletconnect.com/2.0/web3modal/html/wagmi/installation
// hack from https://github.com/WalletConnect/web3modal-examples/issues/46
window.process = { env: { NODE_ENV: "development" } };
import {
EthereumClient,
w3mConnectors,
w3mProvider,
WagmiCore, // same as from @wagmi/core
WagmiCoreChains, // same as from @wagmi/core/chains
} from 'https://unpkg.com/@web3modal/[email protected]'
import { Web3Modal } from 'https://unpkg.com/@web3modal/[email protected]'
const { configureChains, createConfig, writeContract, waitForTransaction } = WagmiCore;
const { mainnet } = WagmiCoreChains;
const walletConnectProjectId = '2aca272d18deb10ff748260da5f78bfd';
const chains = [mainnet];
const { publicClient } = configureChains(chains, [w3mProvider({ projectId: walletConnectProjectId })]);
const wagmiConfig = createConfig({
autoConnect: true,
connectors: w3mConnectors({ projectId: walletConnectProjectId, chains }),
publicClient
});
const ethereumClient = new EthereumClient(wagmiConfig, chains);
// todo: remove WalletConnect as the huge default option
const web3modal = new Web3Modal({ projectId: walletConnectProjectId }, ethereumClient);
// Handle connect wallet button and its animation //////////////////////////////////////////////////////////////////
ethereumClient.watchAccount((account) => {
connectWalletButton.innerText = account.isConnected
? "Connected: " + account.address.slice(0, 6) + "\u2026" + account.address.slice(-4)
: "Connect wallet";
});
document.getElementById('connect-wallet').addEventListener('click', () => {
web3modal.openModal()
});
// Handle buy button ///////////////////////////////////////////////////////////////////////////////////////////////
const mainContractABI = [
{"constant":false,"inputs":[{"name":"_tokenId","type":"uint256"},{"name":"_newOwner","type":"address"}],"name":"grantToken","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},
{"anonymous":false,"inputs":[{"indexed":false,"name":"_nftId","type":"uint256"}],"name":"Personalized","type":"event"},
{"constant":false,"inputs":[{"name":"_squareId","type":"uint256"},{"name":"_rgbData","type":"bytes"},{"name":"_title","type":"string"},{"name":"_href","type":"string"}],"name":"personalizeSquare","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},
{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":true,"name":"_to","type":"address"},{"indexed":true,"name":"_tokenId","type":"uint256"}],"name":"Transfer","type":"event"},
{"constant":false,"inputs":[{"name":"_nftId","type":"uint256"}],"name":"purchase","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},
{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},
{"constant":true,"inputs":[{"name":"_tokenId","type":"uint256"}],"name":"ownerOf","outputs":[{"name":"_owner","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},
{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_index","type":"uint256"}],"name":"tokenOfOwnerByIndex","outputs":[{"name":"_tokenId","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},
];
buyButton.addEventListener("click", async ()=>{
if (!squareNumber) {
return alert("Please enter a Square number.");
}
const doSendTransaction = async ()=>{
receiptsDiv.innerHTML = "<p>Sending transaction, waiting for confirmation…</p>";
let result;
try {
result = await writeContract({
address: "0xE9e3F9cfc1A64DFca53614a0182CFAD56c10624F",
abi: mainContractABI,
functionName: "purchase",
args: [ squareNumber ],
value: "500000000000000000", // 0.5 Eth
});
const transaction = await waitForTransaction({ hash: result.hash });
receiptsDiv.innerHTML = `<h3>Transaction confirmed</h3><p>Transaction ID: <a target="_blank" href="https://etherscan.io/tx/${transaction.transactionHash}">${transaction.transactionHash}</a>. Next, go to <a href="https://tenthousandsu.com/personalize?square=${squareNumber}">personalize</a> to personalize your Square.</p>`;
} catch (error) {
console.log(error);
return alert(error.message);
}
console.log('hash', result.hash);
//const transaction = await ethereumClient.provider.waitForTransaction(result.hash);
console.log('transaction', transaction);
receiptsDiv.innerHTML = "<h2>Transaction confirmed</h2><p>Transaction ID: <a href='https://etherscan.io/tx/" + transaction.hash + "'>" + transaction.hash + "</a></p>";
};
// If the wallet is not connected, connect it
if (!ethereumClient.getAccount().isConnected) {
// Arm on connecting wallet, if connected then fire the transaction
const accountConnectionUnsubscriber = ethereumClient.watchAccount((account) => {
if (account.isConnected) {
doSendTransaction();
}
});
// Disarm if modal is closed
const modalUnsubscriber = web3modal.subscribeModal((modalState) => {
if (!modalState.open) {
accountConnectionUnsubscriber();
modalUnsubscriber();
}
});
await web3modal.openModal();
} else {
// The wallet is connected, fire the transaction
doSendTransaction();
}
});
</script>
</body>
</html>