mutable.filter.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { Filter, FilterResult } from './pool-filters';
  2. import { Connection } from '@solana/web3.js';
  3. import { LiquidityPoolKeysV4 } from '@raydium-io/raydium-sdk';
  4. import { getPdaMetadataKey } from '@raydium-io/raydium-sdk';
  5. import { MetadataAccountData, MetadataAccountDataArgs } from '@metaplex-foundation/mpl-token-metadata';
  6. import { Serializer } from '@metaplex-foundation/umi/serializers';
  7. import { logger } from '../helpers';
  8. export class MutableFilter implements Filter {
  9. constructor(private readonly connection: Connection, private readonly metadataSerializer: Serializer<MetadataAccountDataArgs, MetadataAccountData>, private readonly checkMutable: boolean, private readonly checkSocials: boolean) {}
  10. async execute(poolKeys: LiquidityPoolKeysV4): Promise<FilterResult> {
  11. const errorMessage = [ this.checkMutable ? 'mutable' : undefined, this.checkSocials ? 'socials' : undefined ].filter((e) => e !== undefined);
  12. try {
  13. const metadataPDA = getPdaMetadataKey(poolKeys.baseMint);
  14. const metadataAccount = await this.connection.getAccountInfo(metadataPDA.publicKey);
  15. if (!metadataAccount?.data) {
  16. return { ok: false, message: 'Mutable -> Failed to fetch account data' };
  17. }
  18. const deserialize = this.metadataSerializer.deserialize(metadataAccount.data);
  19. const mutable = this.checkMutable ? deserialize[0].isMutable: false;
  20. const hasSocials = this.checkSocials ? (Object.values(await this.getSocials(deserialize[0])).some((value: any) => value !== null && value.length > 0)) === true: true;
  21. const message = [ !mutable ? undefined : 'metadata can be changed', hasSocials ? undefined : 'has no socials' ].filter((e) => e !== undefined);
  22. const ok = !mutable && hasSocials;
  23. return { ok: ok, message: ok ? undefined : `MutableSocials -> Token ${message.join(' and ')}` };
  24. } catch (e) {
  25. logger.error({ mint: poolKeys.baseMint, error: e }, `MutableSocials -> Failed to check ${errorMessage.join(' and ')}`);
  26. return { ok: false, message: `MutableSocials -> Failed to check ${errorMessage.join(' and ')}` };
  27. }
  28. logger.error({ mint: poolKeys.baseMint }, `MutableSocials -> Failed to check ${errorMessage.join(' and ')}`);
  29. return { ok: false, message: `MutableSocials -> Failed to check ${errorMessage.join(' and ')}` };
  30. }
  31. async getSocials(metadata: MetadataAccountData): Promise<Object> {
  32. const response = await fetch(metadata.uri);
  33. const data = await response.json();
  34. return data?.extensions;
  35. }
  36. }