-
Notifications
You must be signed in to change notification settings - Fork 33
/
register.tsx
206 lines (190 loc) · 5.96 KB
/
register.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
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
/* eslint-disable @next/next/no-img-element */
import {
FormControl,
FormHelperText,
InputLabel,
ListItemIcon,
ListItemText,
MenuItem,
Select,
TextField,
} from "@mui/material";
import { FunctionComponent, useEffect, useState } from "react";
import Button from "../UI/button";
import styles from "../../styles/home.module.css";
import { usePricingContract } from "../../hooks/contracts";
import {
useStarknet,
useStarknetCall,
// useStarknetExecute,
} from "@starknet-react/core";
import { stringToFelt } from "../../utils/felt";
import { Call } from "starknet";
import { useStarknetExecute } from '@starknet-react/core'
import { useEncoded } from "../../hooks/naming";
type RegisterProps = {
domain: string;
isAvailable?: boolean;
};
type Identity = {
token_id: number;
image_uri: string;
};
type starknetExecute = {
calls?: Call | Call[];
metadata?: any;
};
const Register: FunctionComponent<RegisterProps> = ({
domain,
isAvailable,
}) => {
const maxYearsToRegister = 25;
const [ownerAddress, setOwnerAddress] = useState<string>(""); // mettre la get caller address par défaut
const [duration, setDuration] = useState<number>(20);
const [tokenId, setTokenId] = useState<number>(0);
const [ownedIdentities, setOwnedIdentities] = useState<any>([]);
const [price, setPrice] = useState<number | any>("0x10000000000000000000000000000000000000000000000000000000000000");
const { contract } = usePricingContract();
const { data: priceData, error: priceError } = useStarknetCall({
contract: contract,
method: "compute_buy_price",
args: [stringToFelt(domain), duration * 365],
});
const { account } = useStarknet();
const encodedDomain = useEncoded(domain);
const { data, error, execute } = useStarknetExecute({
calls: [
{
contractAddress: "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
entrypoint: "approve",
calldata: ["0x051a28475a0169c67d656fb968343d5769c198700a398e8315b8e54c3fb4284b", price, 0]
},
/*
{
contractAddress: "0x051a28475a0169c67d656fb968343d5769c198700a398e8315b8e54c3fb4284b",
entrypoint: "buy",
calldata: [tokenId, 0, encodedDomain, duration * 365, account]
}
*/
]
})
console.log("err", error, account)
// const { data, loading, error, reset, execute } = useStarknetExecute({
// calls,
// metadata,
// });
useEffect(() => {
if (priceError || !priceData) {
setPrice(0);
} else {
setPrice(priceData?.["price"].low.words[0] / (10 ^ 18));
}
}, [priceData, priceError]);
useEffect(() => {
if (account) {
fetch(
`https://api-testnet.aspect.co/api/v0/assets?contract_address=0x0798e884450c19e072d6620fefdbeb7387d0453d3fd51d95f5ace1f17633d88b&owner_address=${account}&sort_by=minted_at&order_by=desc`
)
.then((response) => response.json())
.then((data) => setOwnedIdentities(data.assets));
}
}, [account]);
function changeAddress(e: any): void {
setOwnerAddress(e.target.value);
}
function changeDuration(e: any): void {
setDuration(e.target.value);
}
function changeTokenId(e: any): void {
setTokenId(e.target.value);
}
if (isAvailable)
return (
<div className="sm:w-full w-2/3">
<div className="flex">
<div className="mr-1 z-[0] w-1/2">
<TextField
fullWidth
id="outlined-basic"
label="Owner address"
placeholder="Owner address"
variant="outlined"
onChange={changeAddress}
color="secondary"
required
/>
</div>
<div className="mr-1 z-[0] w-1/2">
<TextField
fullWidth
className="ml-1 z-[0]"
id="outlined-basic"
label="years"
type="number"
placeholder="years"
variant="outlined"
onChange={changeDuration}
InputProps={{
inputProps: { min: 0, max: maxYearsToRegister },
}}
defaultValue={duration}
color="secondary"
required
/>
</div>
</div>
{ownedIdentities.length ? (
<FormControl fullWidth className="mt-3">
<InputLabel>Starknet.id</InputLabel>
<Select
value={tokenId}
defaultValue={ownedIdentities[0]?.token_id}
label="Starknet.id"
onChange={changeTokenId}
sx={{
"& .MuiSelect-select": {
display: "flex",
justifyContent: "center",
alignItems: "center",
},
}}
>
{ownedIdentities.map((identity: Identity, index: number) => (
<MenuItem key={index} value={identity.token_id}>
<ListItemIcon>
<img
width={"25px"}
src={identity.image_uri}
alt="starknet.id avatar"
/>
</ListItemIcon>
<ListItemText primary={identity.token_id} />
</MenuItem>
))}
</Select>
<FormHelperText>
Choose the starknet identity you want to link with your domain
</FormHelperText>
</FormControl>
) : null}
<div className={styles.cardCenter}>
<p>Price Approximation : 0 ETH</p>
</div>
<div className="text-beige mt-5">
<Button
onClick={() => {
console.log("hello")
execute()
}}
disabled={
!Boolean(account) || !duration || !ownerAddress || !tokenId
}
>
Register
</Button>
</div>
</div>
);
return <p>This domain is not available you can’t register it</p>;
};
export default Register;