UNPKG

@galliun/sofi-sdk

Version:

SDK for interacting with the Galliun SocialFi protocol

135 lines (120 loc) 3.83 kB
/* eslint-disable @typescript-eslint/no-explicit-any */ import type { SuiObjectResponse, SuiTransactionBlockResponse } from '@mysten/sui/client'; import type { AnyProtocolTx } from '../SoFiTxTypes.js'; import type { ObjChangeKind, txResToData } from '../util.js'; import { getArgVal, getCommonTxProps, normalizeCoinType } from '../util.js'; type TxData = ReturnType<typeof txResToData>; /** * ProfileTxParser handles parsing of transaction responses from the profile module. * It extracts relevant information and converts it into strongly typed transaction objects. * * @example * ```typescript * const parser = new ProfileTxParser("0x123..."); * const tx = parser.parseProfileModuleTx(moveCall, txInputs, txData, resp); * if (tx?.kind === 'create_profile') { * console.log(`New profile created: ${tx.profileId}`); * } * ``` */ export class ProfileTxParser { constructor(protected readonly packageId: string) {} /** * Parses transactions from the profile module * @param tx Transaction data * @param txInputs Transaction inputs * @param txData Full transaction data * @param resp Original transaction response * @returns Parsed profile transaction or null */ public parseProfileModuleTx( tx: any, txInputs: any[], txData: TxData, resp: SuiTransactionBlockResponse, ): AnyProtocolTx | null { switch (tx.MoveCall.function) { case 'create_profile': { if (txInputs.length !== 3) return null; const profileObjChange = this.extractProfileObjCreated(resp); if (!profileObjChange) return null; return { kind: 'create_profile', inputs: { owner: getArgVal(txInputs[0]), username: getArgVal(txInputs[1]), bio: getArgVal(txInputs[2]), display_name: getArgVal(txInputs[1]), profile_picture: '', background_picture: '', description: '', }, ...getCommonTxProps(resp, txData), }; } case 'update_profile': { if (txInputs.length !== 2) return null; return { kind: 'update_profile', inputs: { profile_id: getArgVal(txInputs[0]), bio: getArgVal(txInputs[1]), display_name: '', profile_picture: '', background_picture: '', description: '', }, ...getCommonTxProps(resp, txData), }; } case 'follow_profile': { if (txInputs.length !== 2) return null; return { kind: 'follow_profile', inputs: { profile_id: getArgVal(txInputs[0]), target_profile_id: getArgVal(txInputs[1]), }, ...getCommonTxProps(resp, txData), }; } case 'unfollow_profile': { if (txInputs.length !== 2) return null; return { kind: 'unfollow_profile', inputs: { profile_id: getArgVal(txInputs[0]), target_profile_id: getArgVal(txInputs[1]), }, ...getCommonTxProps(resp, txData), }; } } return null; } // === Object Extractors === /** * Extracts the created Profile object from transaction response * @param resp Transaction response * @returns Created profile object change or undefined */ public extractProfileObjCreated( resp: SuiTransactionBlockResponse, ): ObjChangeKind<'created'> | undefined { return resp.objectChanges?.find( (o) => o.type === 'created' && o.objectType.startsWith(`${this.packageId}::profile::Profile`), ) as ObjChangeKind<'created'> | undefined; } /** * Extracts the mutated Profile object from transaction response * @param resp Transaction response * @returns Mutated profile object change or undefined */ public extractProfileObjMutated( resp: SuiTransactionBlockResponse, ): ObjChangeKind<'mutated'> | undefined { return resp.objectChanges?.find( (o) => o.type === 'mutated' && o.objectType.startsWith(`${this.packageId}::profile::Profile`), ) as ObjChangeKind<'mutated'> | undefined; } }