|
@@ -7,37 +7,60 @@ import { Serializer } from '@metaplex-foundation/umi/serializers';
|
|
|
import { logger } from '../helpers';
|
|
|
|
|
|
export class MutableFilter implements Filter {
|
|
|
- constructor(private readonly connection: Connection, private readonly metadataSerializer: Serializer<MetadataAccountDataArgs, MetadataAccountData>, private readonly checkMutable: boolean, private readonly checkSocials: boolean) {}
|
|
|
+ private readonly errorMessage: string[] = [];
|
|
|
+
|
|
|
+ constructor(
|
|
|
+ private readonly connection: Connection,
|
|
|
+ private readonly metadataSerializer: Serializer<MetadataAccountDataArgs, MetadataAccountData>,
|
|
|
+ private readonly checkMutable: boolean,
|
|
|
+ private readonly checkSocials: boolean,
|
|
|
+ ) {
|
|
|
+ if (this.checkMutable) {
|
|
|
+ this.errorMessage.push('mutable');
|
|
|
+ }
|
|
|
+
|
|
|
+ if (this.checkSocials) {
|
|
|
+ this.errorMessage.push('socials');
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
async execute(poolKeys: LiquidityPoolKeysV4): Promise<FilterResult> {
|
|
|
- const errorMessage = [ this.checkMutable ? 'mutable' : undefined, this.checkSocials ? 'socials' : undefined ].filter((e) => e !== undefined);
|
|
|
try {
|
|
|
const metadataPDA = getPdaMetadataKey(poolKeys.baseMint);
|
|
|
- const metadataAccount = await this.connection.getAccountInfo(metadataPDA.publicKey);
|
|
|
+ const metadataAccount = await this.connection.getAccountInfo(metadataPDA.publicKey, this.connection.commitment);
|
|
|
+
|
|
|
if (!metadataAccount?.data) {
|
|
|
return { ok: false, message: 'Mutable -> Failed to fetch account data' };
|
|
|
}
|
|
|
+
|
|
|
const deserialize = this.metadataSerializer.deserialize(metadataAccount.data);
|
|
|
- const mutable = this.checkMutable ? deserialize[0].isMutable: false;
|
|
|
+ const mutable = !this.checkMutable || deserialize[0].isMutable;
|
|
|
+ const hasSocials = !this.checkSocials || (await this.hasSocials(deserialize[0]));
|
|
|
+ const ok = !mutable && hasSocials;
|
|
|
+ const message: string[] = [];
|
|
|
|
|
|
- const hasSocials = this.checkSocials ? (Object.values(await this.getSocials(deserialize[0])).some((value: any) => value !== null && value.length > 0)) === true: true;
|
|
|
+ if (mutable) {
|
|
|
+ message.push('metadata can be changed');
|
|
|
+ }
|
|
|
|
|
|
- const message = [ !mutable ? undefined : 'metadata can be changed', hasSocials ? undefined : 'has no socials' ].filter((e) => e !== undefined);
|
|
|
- const ok = !mutable && hasSocials;
|
|
|
+ if (!hasSocials) {
|
|
|
+ message.push('has no socials');
|
|
|
+ }
|
|
|
|
|
|
return { ok: ok, message: ok ? undefined : `MutableSocials -> Token ${message.join(' and ')}` };
|
|
|
} catch (e) {
|
|
|
- logger.error({ mint: poolKeys.baseMint, error: e }, `MutableSocials -> Failed to check ${errorMessage.join(' and ')}`);
|
|
|
- return { ok: false, message: `MutableSocials -> Failed to check ${errorMessage.join(' and ')}` };
|
|
|
+ logger.error({ mint: poolKeys.baseMint }, `MutableSocials -> Failed to check ${this.errorMessage.join(' and ')}`);
|
|
|
}
|
|
|
|
|
|
- logger.error({ mint: poolKeys.baseMint }, `MutableSocials -> Failed to check ${errorMessage.join(' and ')}`);
|
|
|
- return { ok: false, message: `MutableSocials -> Failed to check ${errorMessage.join(' and ')}` };
|
|
|
+ return {
|
|
|
+ ok: false,
|
|
|
+ message: `MutableSocials -> Failed to check ${this.errorMessage.join(' and ')}`,
|
|
|
+ };
|
|
|
}
|
|
|
|
|
|
- async getSocials(metadata: MetadataAccountData): Promise<Object> {
|
|
|
+ private async hasSocials(metadata: MetadataAccountData) {
|
|
|
const response = await fetch(metadata.uri);
|
|
|
const data = await response.json();
|
|
|
- return data?.extensions;
|
|
|
+ return Object.values(data?.extensions ?? {}).some((value: any) => value !== null && value.length > 0);
|
|
|
}
|
|
|
}
|