8
8
bytesToStr ,
9
9
bytesToU256 ,
10
10
bytesToU64 ,
11
+ toMAS ,
11
12
u256ToBytes ,
12
13
} from '@massalabs/massa-web3' ;
13
14
import { ToastContent , toast } from '@massalabs/react-ui-kit' ;
@@ -49,6 +50,11 @@ export interface DnsUserEntryListResult {
49
50
tokenId : bigint ;
50
51
}
51
52
53
+ export interface DnsGetAllocCostResponse {
54
+ price : bigint | null ;
55
+ error ?: string ;
56
+ }
57
+
52
58
type callSmartContractOptions = {
53
59
coins ?: bigint ;
54
60
fee ?: bigint ;
@@ -63,8 +69,11 @@ export function useWriteMNS(client?: Client) {
63
69
const [ isSuccess , setIsSuccess ] = useState ( false ) ;
64
70
const [ isError , setIsError ] = useState ( false ) ;
65
71
const [ opId , setOpId ] = useState < string | undefined > ( undefined ) ;
72
+ const [ list , setList ] = useState < DnsUserEntryListResult [ ] > ( [ ] ) ;
73
+ const [ listSpinning , setListSpinning ] = useState ( false ) ;
66
74
67
- async function getAllocCost ( params : DnsAllocParams ) : Promise < bigint > {
75
+ async function getAllocCost ( params : DnsAllocParams ) : Promise < DnsGetAllocCostResponse > {
76
+ let price = 0n
68
77
try {
69
78
let args = new Args ( ) ;
70
79
args . addString ( params . domain ) ;
@@ -75,12 +84,63 @@ export function useWriteMNS(client?: Client) {
75
84
parameter : args . serialize ( ) ,
76
85
} ) ;
77
86
if ( ! response ) {
78
- return 0n ;
87
+ return {
88
+ price : null ,
89
+ error : 'Failed to get alloc cost' ,
90
+ } ;
79
91
}
80
- return bytesToU64 ( response . returnValue ) ;
92
+ price = bytesToU64 ( response . returnValue ) ;
93
+ } catch ( error ) {
94
+ 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
+ } ;
98
+ }
99
+ 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
+ }
113
+ return {
114
+ price : null ,
115
+ error : `Domain already claimed by ${ bytesToStr ( result . returnValue ) } ` ,
116
+ }
81
117
} catch ( error ) {
82
- return 0n ;
83
118
}
119
+ try {
120
+ let resultBalance = await client ?. publicApi ( ) . getAddresses ( [ params . targetAddress ] ) ;
121
+ if ( ! resultBalance ) {
122
+ return {
123
+ price : null ,
124
+ 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 {
130
+ 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.`
132
+ }
133
+ }
134
+ } catch ( error ) {
135
+ console . log ( error )
136
+ 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.' ,
139
+ } ;
140
+ }
141
+ return {
142
+ price : price ,
143
+ } ;
84
144
}
85
145
86
146
function callSmartContract (
@@ -158,6 +218,7 @@ export function useWriteMNS(client?: Client) {
158
218
setIsSuccess ( true ) ;
159
219
setIsPending ( false ) ;
160
220
toast . dismiss ( toastId ) ;
221
+ getUserEntryList ( { address : client . wallet ( ) . getBaseAccount ( ) ?. address ( ) ! } )
161
222
toast . success ( ( t ) => (
162
223
< ToastContent t = { t } >
163
224
< OperationToast
@@ -230,13 +291,15 @@ export function useWriteMNS(client?: Client) {
230
291
async function getUserEntryList (
231
292
params : DnsUserEntryListParams ,
232
293
) : Promise < DnsUserEntryListResult [ ] > {
294
+ setListSpinning ( true ) ;
233
295
let resultBalance = await client ?. smartContracts ( ) . readSmartContract ( {
234
296
targetAddress : SC_ADDRESS ,
235
297
targetFunction : 'balanceOf' ,
236
298
parameter : new Args ( ) . addString ( params . address ) . serialize ( ) ,
237
299
} ) ;
238
300
if ( ! resultBalance ) {
239
301
toast . error ( 'Failed to get user entry list' , { duration : 5000 } ) ;
302
+ setListSpinning ( false ) ;
240
303
return [ ] ;
241
304
}
242
305
let balance = bytesToU256 ( resultBalance . returnValue ) ;
@@ -255,13 +318,15 @@ export function useWriteMNS(client?: Client) {
255
318
let results = await client ?. publicApi ( ) . getDatastoreEntries ( listAsked ) ;
256
319
if ( ! results ) {
257
320
toast . error ( 'Failed to get user entry list' , { duration : 5000 } ) ;
321
+ setListSpinning ( false ) ;
258
322
return [ ] ;
259
323
}
260
324
for ( let j = 0 ; j < results . length ; j ++ ) {
261
325
let entry = results [ j ] . candidate_value ;
262
326
if ( ! entry || entry . length == 0 ) {
263
327
continue ;
264
328
}
329
+ console . log ( entry , addressBytes ) ;
265
330
if ( compareUint8Array ( entry , addressBytes ) ) {
266
331
let tokenId = i + BigInt ( j ) ;
267
332
let resultDomain = await client ?. smartContracts ( ) . readSmartContract ( {
@@ -271,6 +336,7 @@ export function useWriteMNS(client?: Client) {
271
336
} ) ;
272
337
if ( ! resultDomain ) {
273
338
toast . error ( 'Failed to get user entry list' , { duration : 5000 } ) ;
339
+ setListSpinning ( false ) ;
274
340
return [ ] ;
275
341
}
276
342
const domain = bytesToStr ( resultDomain . returnValue ) ;
@@ -281,6 +347,7 @@ export function useWriteMNS(client?: Client) {
281
347
} ) ;
282
348
if ( ! targetAddress ) {
283
349
toast . error ( 'Failed to get user entry list' , { duration : 5000 } ) ;
350
+ setListSpinning ( false ) ;
284
351
return [ ] ;
285
352
}
286
353
list . push ( {
@@ -296,8 +363,12 @@ export function useWriteMNS(client?: Client) {
296
363
// Rate limiting
297
364
await sleep ( 1000 ) ;
298
365
}
366
+ setList ( list ) ;
367
+ setListSpinning ( false ) ;
299
368
return list ;
300
369
}
370
+ console . log ( 'listSpinning in hook' , listSpinning ) ;
371
+
301
372
const sleep = ( ms : number ) => new Promise ( ( r ) => setTimeout ( r , ms ) ) ;
302
373
303
374
function compareUint8Array ( a : Uint8Array , b : Uint8Array ) {
@@ -348,6 +419,8 @@ export function useWriteMNS(client?: Client) {
348
419
isPending,
349
420
isSuccess,
350
421
isError,
422
+ list,
423
+ listSpinning,
351
424
dnsAlloc,
352
425
getAllocCost,
353
426
getUserEntryList,
0 commit comments