-
Notifications
You must be signed in to change notification settings - Fork 0
/
DomainInput.tsx
110 lines (105 loc) · 5.08 KB
/
DomainInput.tsx
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
import { useState, KeyboardEvent, ChangeEvent } from 'react';
import { ethers } from 'ethers';
import { staticUtils } from '@snickerdoodlelabs/erc7529';
import { ERC7529ContractProxy } from '@snickerdoodlelabs/contracts-sdk';
import { ChainId, DomainName, EVMContractAddress } from '@snickerdoodlelabs/objects';
import toast, { Toaster } from "react-hot-toast";
import "reflect-metadata";
import { verifedContract } from './objects'
import ResultsList from './ResultsList';
import './App.css';
export default function DomainInput() {
const [label, setLabel] = useState<string>("Enter a Domain Name:");
const [placeholder, setPlaceholder] = useState<string>("Enter a Domain Name:")
const [value, setValue] = useState<string>("");
const [fieldName, setFieldName] = useState<string>('field');
const [chain, setChain] = useState<ChainId>(ChainId(43113));
const [results, setResult] = useState<verifedContract[]>([]);
function changeValue(event: ChangeEvent) {
const target = event.target;
if (target) setValue((target as HTMLButtonElement).value);
}
async function handleKeyPress(event: KeyboardEvent) {
if (event.code === 'Enter') {
const target: EventTarget = event.target;
if (target) {
// grab the domain that was entered intot the text input field
const domainName: DomainName = DomainName((target as HTMLButtonElement).value);
try {
if (window.ethereum != null) {
const currentChain = ChainId(
Number(
await window.ethereum.request(
{
"method": "eth_chainId",
"params": []
}
)
)
);
console.log("current chain", currentChain)
setChain(currentChain);
// first check if domain has TXT pointer to some contracts
const addresses = await staticUtils.getContractsFromDomain(domainName, currentChain);
console.log("addresses:", addresses);
// if so, verify each contract
if (addresses.isOk()) {
if (addresses.value.length > 0) {
let resultsBuf: verifedContract[] = [];
const provider = new ethers.providers.Web3Provider(window.ethereum);
await provider.getNetwork();
for (const address of addresses.value) {
const myContract = new ERC7529ContractProxy(provider, EVMContractAddress(address));
if (window.ethereum != null) {
const isVerified = await staticUtils.verifyContractForDomain(myContract, domainName, currentChain);
if (isVerified.isOk()) {
resultsBuf.push({ address: address, verified: true })
} else {
resultsBuf.push({ address: address, verified: false })
}
}
}
console.log("resultBuf:", resultsBuf);
setLabel("Found Something! Try another domain:");
setResult(resultsBuf);
} else {
setLabel("Try a different domain:");
setResult([]);
}
}
} else {
toast.error('You will need Metamask installed for this app to verify contracts.')
setLabel("Please install a wallet & try again:");
setResult([]);
}
} catch {
setLabel("Try a different domain:");
} finally {
setValue("");
}
}
}
}
return (
<>
<Toaster />
<div className={fieldName}>
<input
id='1'
type='text'
name='domainInput'
value={value}
placeholder={placeholder}
onFocus={() => { setFieldName('field active'); setPlaceholder(''); }}
onBlur={() => { setFieldName('field'); setPlaceholder(value); }}
onChange={changeValue}
onKeyDown={handleKeyPress}
/>
<label htmlFor='1'>
{label}
</label>
</div><br></br>
<ResultsList results={results} chain={chain} />
</>
);
}