forked from w3f/polkadot-wiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuction-Schedule.jsx
169 lines (154 loc) · 5.77 KB
/
Auction-Schedule.jsx
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
import React, { useEffect, useState } from 'react';
import { ApolloClient, ApolloLink, HttpLink, InMemoryCache } from '@apollo/client/core';
import { AUCTIONS, supportedNetworks } from './utilities/auctionVariables';
let ChainState = {
BlockNumber: undefined,
}
let Options = [];
// Component for displaying auction data
function AuctionSchedule({ network }) {
const [auctions, setAuctions] = useState("Loading Auctions...");
useEffect(async () => {
// Set http link for indexer, as well as the explorer based on network
const networkInfo = setHttpLinkAndExplorer(network);
// The indexer utilizes a GraphQL Api
const client = new ApolloClient({
cache: new InMemoryCache(),
link: ApolloLink.from([networkInfo.httpLink]),
});
const res = await client.query({
query: AUCTIONS
}).catch(e => handleAndRenderError(e, setAuctions));
if (res !== undefined) {
let height = res.data.squidStatus.height;
let squidAuctions = res.data.auctions;
// If the network has no auctions to report
if (squidAuctions.length == 0) {
setAuctions(<div>No auctions found on this network.</div>)
} else {
ChainState.BlockNumber = height;
// Populate options from latest to oldest auction
await LoadOptions(squidAuctions);
// Get the initial index for the latest auction
let id = squidAuctions.length - 1;
// Render component with the auctions, starting from the latest
Render(networkInfo.explorer, squidAuctions, setAuctions, id);
}
}
}, []);
// Render
if (auctions !== undefined) {
return auctions;
}
else {
return (<div>Loading auction data...</div>)
}
}
// This is meant to handle the case where there is an error fetching data from the indexer API
// The error is logged in the console.
function handleAndRenderError(e, setAuctions) {
console.log("There was a problem fetching from with your query: ", e);
setAuctions(
<div>There was a problem with the query used to fetch auction data.
If this issue persists, please submit an issue at the
<a href="https://github.com/w3f/polkadot-wiki/" target="_blank"> Polkadot Wiki repository on Github</a>
</div>)
}
// Sets initial network info for the component based on the network supplied
function setHttpLinkAndExplorer(network) {
switch (network) {
case supportedNetworks.POLKADOT:
return {
httpLink: new HttpLink({
uri: "https://squid.subsquid.io/polkadot-wiki-squid/v/v1/graphql",
}),
explorer: "https://polkadot.subscan.io/block/"
};
case supportedNetworks.KUSAMA:
return {
httpLink: new HttpLink({
uri: "https://squid.subsquid.io/kusama-guide-squid/v/v1/graphql",
}),
explorer: "https://kusama.subscan.io/block/"
};
}
}
// Loads drop-down selections
async function LoadOptions(auctions) {
auctions.map((a) => parseInt(a.id)).reverse().forEach((id) => {
const option = <option value={id} key={id}>{`Auction #${id}`}</option>
Options.push(option);
})
}
// Re-renders component based on the selected information - used for the <select> element
function switchAuctions(chain, auctions, setAuctions, e) {
Render(chain, auctions, setAuctions, parseInt(e.target.value) - 1)
}
// Update JSX
function Render(explorerUrl, auctions, setAuctions, index) {
// Current block information
let currentBlockNumber = ChainState.BlockNumber;
const onboardStartDate = new Date(parseInt(auctions[index].onboardStartBlock.timestamp)).toDateString();
const onboardEndDate = new Date(parseInt(auctions[index].onboardEndBlock.timestamp)).toDateString();
const biddingStartsDate = new Date(parseInt(auctions[index].biddingStartBlock.timestamp)).toDateString();
const biddingEndsDate = new Date(parseInt(auctions[index].biddingEndsBlock.timestamp)).toDateString();
// On-boarding range
let onboarding = <div>
{`${onboardStartDate} - `}
<a href={`${explorerUrl}${auctions[index].onboardStartBlock.height}`}>
Block #{auctions[index].onboardStartBlock.height}
</a>
{` to `}
{`${onboardEndDate} - `}
<a href={`${explorerUrl}${auctions[index].onboardEndBlock.height}`}>
Block #{auctions[index].onboardEndBlock.height}
</a>
</div>
const auctionNumber = parseInt(index) + 1;
let auctionsUrl = explorerUrl.startsWith("https://polkadot") ? "https://polkadot.subscan.io/auction/" : "https://kusama.subscan.io/auction/"
const content = <div>
<div><a target="_blank" href={`${auctionsUrl}${auctionNumber}`}>Auction #{auctionNumber} is {auctions[index].status}</a></div>
<br />
<select
id="AuctionSelector"
onChange={(e) => switchAuctions(explorerUrl, auctions, setAuctions, e)}
style={{ border: '2px solid #e6007a', height: '40px' }}
>
{Options.map((option) => (option))}
</select>
<hr />
<b>Auction Starts:</b>
<br />
{`${new Date(parseInt(auctions[index].startBlock.timestamp)).toDateString()} - `}
<a href={`${explorerUrl}${auctions[index].startBlock.height}`}>
Block #{auctions[index].startBlock.height}
</a>
<hr />
<b>Ending Period Starts:</b>
<br />
{`${biddingStartsDate} - `}
<a href={`${explorerUrl}${auctions[index].biddingStartBlock.height}`}>
Block #{auctions[index].biddingStartBlock.height}
</a>
<hr />
<b>Auction Ends:</b>
<br />
{`${biddingEndsDate} - `}
<a href={`${explorerUrl}${auctions[index].biddingEndsBlock.height}`}>
Block #{auctions[index].biddingEndsBlock.height}
</a>
<hr />
<b>Lease Period:</b>
<br />
{onboarding}
<hr />
<p style={{ color: "#6c757d" }}>
The dates and block numbers listed above can change based on network block production and the potential for skipped blocks.
Dates for finalized blocks are pulled from on-chain, while future blocks are estimated using 6 second average block times.
The current block is <a href={`${explorerUrl}${currentBlockNumber}`}> Block #{currentBlockNumber}</a>.
</p>
</div>
setAuctions(content);
return auctions;
}
export default AuctionSchedule;