Skip to content

Commit e368d2c

Browse files
committed
fix auto refresh on claim
1 parent a2be651 commit e368d2c

File tree

4 files changed

+136
-88
lines changed

4 files changed

+136
-88
lines changed

frontend/src/components/MNSClaim.tsx

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
import { Button, toast } from '@massalabs/react-ui-kit';
22
import { InputWithRightText } from './InputWithRightText';
33
import { useState } from 'react';
4-
import { DnsGetAllocCostResponse, useWriteMNS } from '../utils/write-mns-sc';
4+
import { DnsAllocParams, DnsGetAllocCostResponse } from '../utils/write-mns-sc';
55
import { useAccountStore } from '../lib/connectMassaWallets/store';
66
import { toMAS } from '@massalabs/massa-web3';
77

8-
export function MNSClaim() {
8+
interface MNSClaimProps {
9+
dnsAlloc: (data: DnsAllocParams) => void;
10+
getAllocCost: (data: DnsAllocParams) => Promise<DnsGetAllocCostResponse>;
11+
}
12+
13+
export function MNSClaim(props: MNSClaimProps) {
14+
const { dnsAlloc, getAllocCost } = props;
915
const [domain, setDomain] = useState<string>('');
10-
const { connectedAccount, massaClient } = useAccountStore();
11-
const { dnsAlloc, getAllocCost } = useWriteMNS(massaClient);
12-
const [allocCost, setAllocCost] = useState<DnsGetAllocCostResponse>({price: 0n});
16+
const { connectedAccount } = useAccountStore();
17+
const [allocCost, setAllocCost] = useState<DnsGetAllocCostResponse>({
18+
price: 0n,
19+
});
1320

1421
function claim() {
1522
if (!connectedAccount) {
1623
toast.error('Please connect your wallet');
1724
return;
1825
}
1926
if (!allocCost.price) {
20-
toast.error('Invalid price');
21-
return;
27+
toast.error('Invalid price');
28+
return;
2229
}
2330
dnsAlloc({
2431
domain,
@@ -33,15 +40,15 @@ export function MNSClaim() {
3340
return;
3441
}
3542
if (domain == '') {
36-
setAllocCost({price: 0n});
37-
return;
43+
setAllocCost({ price: 0n });
44+
return;
3845
}
3946
setDomain(domain);
4047
getAllocCost({
4148
domain,
4249
targetAddress: connectedAccount?.address() ?? '',
4350
}).then((cost) => {
44-
setAllocCost(cost);
51+
setAllocCost(cost);
4552
});
4653
}
4754
return (
@@ -56,19 +63,20 @@ export function MNSClaim() {
5663
onDomainChange(e.target.value);
5764
}}
5865
/>
59-
{
60-
allocCost.price !== null ? (
61-
<p className="mb-4 font-light text-neutral">
62-
Price {toMAS(allocCost.price).toFixed(4)} MAS
63-
</p>
64-
) : (
65-
<p className="mb-4 font-light text-s-error">
66-
{allocCost.error}
67-
</p>
68-
)
69-
}
66+
{allocCost.price !== null ? (
67+
<p className="mb-4 font-light text-neutral">
68+
Price {toMAS(allocCost.price).toFixed(4)} MAS
69+
</p>
70+
) : (
71+
<p className="mb-4 font-light text-s-error">{allocCost.error}</p>
72+
)}
7073
</div>
71-
<Button disabled={allocCost.price !== null ? false : true} onClick={() => claim()}>Claim</Button>
74+
<Button
75+
disabled={allocCost.price !== null ? false : true}
76+
onClick={() => claim()}
77+
>
78+
Claim
79+
</Button>
7280
</div>
7381
</div>
7482
);

frontend/src/components/MNSList.tsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,29 @@ import {
1313
Spinner,
1414
Tooltip,
1515
} from '@massalabs/react-ui-kit';
16-
import { useAccountStore } from '../lib/connectMassaWallets/store';
17-
import { useEffect, useState } from 'react';
18-
import { useWriteMNS } from '../utils/write-mns-sc';
16+
import { useState } from 'react';
17+
import {
18+
DnsChangeTargetParams,
19+
DnsDeleteParams,
20+
DnsUserEntryListResult,
21+
} from '../utils/write-mns-sc';
22+
23+
interface MNSListProps {
24+
list: DnsUserEntryListResult[];
25+
listSpinning: boolean;
26+
deleteDnsEntry: (params: DnsDeleteParams) => void;
27+
changeTargetAddressDnsEntry: (params: DnsChangeTargetParams) => void;
28+
}
1929

20-
export function MNSList() {
21-
const { connectedAccount, massaClient } = useAccountStore();
22-
const { getUserEntryList, deleteDnsEntry, changeTargetAddressDnsEntry, list, listSpinning } =
23-
useWriteMNS(massaClient);
30+
export function MNSList(props: MNSListProps) {
31+
const { list, listSpinning, deleteDnsEntry, changeTargetAddressDnsEntry } =
32+
props;
2433

25-
console.log('listSpinning in list', listSpinning);
2634
const [changeTargetModalId, setChangeTargetModalId] = useState<string | null>(
2735
null,
2836
);
2937
const [newTargetAddress, setNewTargetAddress] = useState<string>('');
30-
31-
useEffect(() => {
32-
if (!connectedAccount || !massaClient || listSpinning) return;
33-
getUserEntryList({address: connectedAccount.address() })
34-
}, [connectedAccount, massaClient]);
38+
3539
return (
3640
<div>
3741
<Accordion customClass="border-none" title="Owned MNS">

frontend/src/components/MNSManagement.tsx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { useEffect } from 'react';
12
import { useAccountStore } from '../lib/connectMassaWallets/store';
3+
import { useWriteMNS } from '../utils/write-mns-sc';
24
import { MNSClaim } from './MNSClaim';
35
import { MNSList } from './MNSList';
46

@@ -8,10 +10,23 @@ interface MNSManagementProps {
810

911
export function MNSManagement(props: MNSManagementProps) {
1012
const { customClass } = props;
11-
const { connectedAccount, currentProvider } = useAccountStore();
13+
const { massaClient, connectedAccount, currentProvider } = useAccountStore();
14+
const {
15+
list,
16+
listSpinning,
17+
getUserEntryList,
18+
dnsAlloc,
19+
getAllocCost,
20+
changeTargetAddressDnsEntry,
21+
deleteDnsEntry,
22+
} = useWriteMNS(massaClient);
1223

1324
const connected = !!connectedAccount && !!currentProvider;
1425

26+
useEffect(() => {
27+
if (!connectedAccount || !massaClient || listSpinning) return;
28+
getUserEntryList({ address: connectedAccount.address() });
29+
}, [connectedAccount, massaClient]);
1530
return (
1631
<div className={customClass}>
1732
{!connected ? (
@@ -22,8 +37,13 @@ export function MNSManagement(props: MNSManagementProps) {
2237
</div>
2338
) : (
2439
<div className="flex flex-col divide-y">
25-
<MNSClaim />
26-
<MNSList />
40+
<MNSClaim dnsAlloc={dnsAlloc} getAllocCost={getAllocCost} />
41+
<MNSList
42+
deleteDnsEntry={deleteDnsEntry}
43+
changeTargetAddressDnsEntry={changeTargetAddressDnsEntry}
44+
list={list}
45+
listSpinning={listSpinning}
46+
/>
2747
</div>
2848
)}
2949
</div>

frontend/src/utils/write-mns-sc.tsx

Lines changed: 66 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,21 @@ interface ToasterMessage {
2525
timeout?: string;
2626
}
2727

28-
interface DnsAllocParams {
28+
export interface DnsAllocParams {
2929
domain: string;
3030
targetAddress: string;
3131
coins?: bigint;
3232
}
3333

34-
interface DnsDeleteParams {
34+
export interface DnsDeleteParams {
3535
tokenId: bigint;
3636
}
3737

3838
interface DnsUserEntryListParams {
3939
address: string;
4040
}
4141

42-
interface DnsChangeTargetParams {
42+
export interface DnsChangeTargetParams {
4343
domain: string;
4444
targetAddress: string;
4545
}
@@ -51,8 +51,8 @@ export interface DnsUserEntryListResult {
5151
}
5252

5353
export interface DnsGetAllocCostResponse {
54-
price: bigint | null;
55-
error?: string;
54+
price: bigint | null;
55+
error?: string;
5656
}
5757

5858
type callSmartContractOptions = {
@@ -72,8 +72,10 @@ export function useWriteMNS(client?: Client) {
7272
const [list, setList] = useState<DnsUserEntryListResult[]>([]);
7373
const [listSpinning, setListSpinning] = useState(false);
7474

75-
async function getAllocCost(params: DnsAllocParams): Promise<DnsGetAllocCostResponse> {
76-
let price = 0n
75+
async function getAllocCost(
76+
params: DnsAllocParams,
77+
): Promise<DnsGetAllocCostResponse> {
78+
let price = 0n;
7779
try {
7880
let args = new Args();
7981
args.addString(params.domain);
@@ -85,62 +87,73 @@ export function useWriteMNS(client?: Client) {
8587
});
8688
if (!response) {
8789
return {
88-
price: null,
89-
error: 'Failed to get alloc cost',
90+
price: null,
91+
error: 'Failed to get alloc cost',
9092
};
9193
}
92-
price = bytesToU64(response.returnValue);
94+
price = bytesToU64(response.returnValue);
9395
} catch (error) {
9496
return {
95-
price: null,
96-
error: 'Invalid domain name. Name can only be 2-100 characters long and can contains only lowercase letters, numbers and hyphens.',
97+
price: null,
98+
error:
99+
'Name can only be 2-100 characters long and can contains only lowercase letters, numbers and hyphens.',
97100
};
98101
}
99102
try {
100-
let args = new Args();
101-
args.addString(params.domain);
102-
let result = await client?.smartContracts().readSmartContract({
103-
targetAddress: SC_ADDRESS,
104-
targetFunction: 'dnsResolve',
105-
parameter: args.serialize(),
106-
});
107-
if (!result) {
108-
return {
109-
price: null,
110-
error: 'Failed to get alloc cost',
111-
};
112-
}
103+
let args = new Args();
104+
args.addString(params.domain);
105+
let result = await client?.smartContracts().readSmartContract({
106+
targetAddress: SC_ADDRESS,
107+
targetFunction: 'dnsResolve',
108+
parameter: args.serialize(),
109+
});
110+
if (!result) {
113111
return {
114-
price: null,
115-
error: `Domain already claimed by ${bytesToStr(result.returnValue)}`,
116-
}
112+
price: null,
113+
error: 'Failed to get alloc cost',
114+
};
115+
}
116+
return {
117+
price: null,
118+
error: `Domain already claimed by ${bytesToStr(result.returnValue)}`,
119+
};
117120
} catch (error) {
118-
}
119-
try {
120-
let resultBalance = await client?.publicApi().getAddresses([params.targetAddress]);
121-
if (!resultBalance) {
122-
return {
121+
try {
122+
let resultBalance = await client
123+
?.publicApi()
124+
.getAddresses([params.targetAddress]);
125+
if (!resultBalance) {
126+
return {
123127
price: null,
124128
error: 'Failed to get alloc cost',
125-
};
126-
}
127-
let balance = BigInt((parseFloat(resultBalance[0].candidate_balance) * 1_000_000_000).toFixed(0));
128-
if (balance < price) {
129-
return {
129+
};
130+
}
131+
let balance = BigInt(
132+
(
133+
parseFloat(resultBalance[0].candidate_balance) * 1_000_000_000
134+
).toFixed(0),
135+
);
136+
if (balance < price) {
137+
return {
130138
price: null,
131-
error: `The price of the domain is ${toMAS(price).toFixed(4)} MAS. Your balance is ${toMAS(balance).toFixed(4)} MAS. Please top up your account.`
139+
error: `The price of the domain is ${toMAS(price).toFixed(
140+
4,
141+
)} MAS. Your balance is ${toMAS(balance).toFixed(
142+
4,
143+
)} MAS. Please top up your account.`,
144+
};
132145
}
133-
}
134-
} catch (error) {
135-
console.log(error)
146+
} catch (error) {
136147
return {
137-
price: null,
138-
error: 'Your account does not exist in the Massa network. Please transfer 0.1 MAS to your account to create it on chain.',
148+
price: null,
149+
error:
150+
'Your account does not exist in the Massa network. Transfer 0.1 MAS to your account to create it onchain.',
139151
};
140-
}
141-
return {
152+
}
153+
return {
142154
price: price,
143-
};
155+
};
156+
}
144157
}
145158

146159
function callSmartContract(
@@ -218,7 +231,12 @@ export function useWriteMNS(client?: Client) {
218231
setIsSuccess(true);
219232
setIsPending(false);
220233
toast.dismiss(toastId);
221-
getUserEntryList({address: client.wallet().getBaseAccount()?.address()!})
234+
const baseAccount = client.wallet().getBaseAccount();
235+
if (client && baseAccount) {
236+
getUserEntryList({
237+
address: baseAccount.address()!,
238+
});
239+
}
222240
toast.success((t) => (
223241
<ToastContent t={t}>
224242
<OperationToast
@@ -326,7 +344,6 @@ export function useWriteMNS(client?: Client) {
326344
if (!entry || entry.length == 0) {
327345
continue;
328346
}
329-
console.log(entry, addressBytes);
330347
if (compareUint8Array(entry, addressBytes)) {
331348
let tokenId = i + BigInt(j);
332349
let resultDomain = await client?.smartContracts().readSmartContract({
@@ -367,7 +384,6 @@ export function useWriteMNS(client?: Client) {
367384
setListSpinning(false);
368385
return list;
369386
}
370-
console.log('listSpinning in hook', listSpinning);
371387

372388
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
373389

0 commit comments

Comments
 (0)