@@ -7,37 +7,60 @@ import { Serializer } from '@metaplex-foundation/umi/serializers';
7
7
import { logger } from '../helpers' ;
8
8
9
9
export class MutableFilter implements Filter {
10
- constructor ( private readonly connection : Connection , private readonly metadataSerializer : Serializer < MetadataAccountDataArgs , MetadataAccountData > , private readonly checkMutable : boolean , private readonly checkSocials : boolean ) { }
10
+ private readonly errorMessage : string [ ] = [ ] ;
11
+
12
+ constructor (
13
+ private readonly connection : Connection ,
14
+ private readonly metadataSerializer : Serializer < MetadataAccountDataArgs , MetadataAccountData > ,
15
+ private readonly checkMutable : boolean ,
16
+ private readonly checkSocials : boolean ,
17
+ ) {
18
+ if ( this . checkMutable ) {
19
+ this . errorMessage . push ( 'mutable' ) ;
20
+ }
21
+
22
+ if ( this . checkSocials ) {
23
+ this . errorMessage . push ( 'socials' ) ;
24
+ }
25
+ }
11
26
12
27
async execute ( poolKeys : LiquidityPoolKeysV4 ) : Promise < FilterResult > {
13
- const errorMessage = [ this . checkMutable ? 'mutable' : undefined , this . checkSocials ? 'socials' : undefined ] . filter ( ( e ) => e !== undefined ) ;
14
28
try {
15
29
const metadataPDA = getPdaMetadataKey ( poolKeys . baseMint ) ;
16
- const metadataAccount = await this . connection . getAccountInfo ( metadataPDA . publicKey ) ;
30
+ const metadataAccount = await this . connection . getAccountInfo ( metadataPDA . publicKey , this . connection . commitment ) ;
31
+
17
32
if ( ! metadataAccount ?. data ) {
18
33
return { ok : false , message : 'Mutable -> Failed to fetch account data' } ;
19
34
}
35
+
20
36
const deserialize = this . metadataSerializer . deserialize ( metadataAccount . data ) ;
21
- const mutable = this . checkMutable ? deserialize [ 0 ] . isMutable : false ;
37
+ const mutable = ! this . checkMutable || deserialize [ 0 ] . isMutable ;
38
+ const hasSocials = ! this . checkSocials || ( await this . hasSocials ( deserialize [ 0 ] ) ) ;
39
+ const ok = ! mutable && hasSocials ;
40
+ const message : string [ ] = [ ] ;
22
41
23
- const hasSocials = this . checkSocials ? ( Object . values ( await this . getSocials ( deserialize [ 0 ] ) ) . some ( ( value : any ) => value !== null && value . length > 0 ) ) === true : true ;
42
+ if ( mutable ) {
43
+ message . push ( 'metadata can be changed' ) ;
44
+ }
24
45
25
- const message = [ ! mutable ? undefined : 'metadata can be changed' , hasSocials ? undefined : 'has no socials' ] . filter ( ( e ) => e !== undefined ) ;
26
- const ok = ! mutable && hasSocials ;
46
+ if ( ! hasSocials ) {
47
+ message . push ( 'has no socials' ) ;
48
+ }
27
49
28
50
return { ok : ok , message : ok ? undefined : `MutableSocials -> Token ${ message . join ( ' and ' ) } ` } ;
29
51
} catch ( e ) {
30
- logger . error ( { mint : poolKeys . baseMint , error : e } , `MutableSocials -> Failed to check ${ errorMessage . join ( ' and ' ) } ` ) ;
31
- return { ok : false , message : `MutableSocials -> Failed to check ${ errorMessage . join ( ' and ' ) } ` } ;
52
+ logger . error ( { mint : poolKeys . baseMint } , `MutableSocials -> Failed to check ${ this . errorMessage . join ( ' and ' ) } ` ) ;
32
53
}
33
54
34
- logger . error ( { mint : poolKeys . baseMint } , `MutableSocials -> Failed to check ${ errorMessage . join ( ' and ' ) } ` ) ;
35
- return { ok : false , message : `MutableSocials -> Failed to check ${ errorMessage . join ( ' and ' ) } ` } ;
55
+ return {
56
+ ok : false ,
57
+ message : `MutableSocials -> Failed to check ${ this . errorMessage . join ( ' and ' ) } ` ,
58
+ } ;
36
59
}
37
60
38
- async getSocials ( metadata : MetadataAccountData ) : Promise < Object > {
61
+ private async hasSocials ( metadata : MetadataAccountData ) {
39
62
const response = await fetch ( metadata . uri ) ;
40
63
const data = await response . json ( ) ;
41
- return data ?. extensions ;
64
+ return Object . values ( data ?. extensions ?? { } ) . some ( ( value : any ) => value !== null && value . length > 0 ) ;
42
65
}
43
66
}
0 commit comments