@galliun/sofi-sdk
Version:
SDK for interacting with the Galliun SocialFi protocol
115 lines (102 loc) • 3.22 kB
text/typescript
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { 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>;
/**
* TipTxParser handles parsing of transaction responses from the tip module.
* It extracts relevant information and converts it into strongly typed transaction objects.
*
* @example
* ```typescript
* const parser = new TipTxParser("0x123...");
* const tx = parser.parseTipModuleTx(moveCall, txInputs, txData, resp);
* if (tx?.kind === 'create_tip') {
* console.log(`New tip created: ${tx.tipId}`);
* }
* ```
*/
export class TipTxParser {
constructor(protected readonly packageId: string) {}
/**
* Parses transactions from the tip module
* @param tx Transaction data
* @param txInputs Transaction inputs
* @param txData Full transaction data
* @param resp Original transaction response
* @returns Parsed tip transaction or null
*/
public parseTipModuleTx(
tx: any,
txInputs: any[],
txData: TxData,
resp: SuiTransactionBlockResponse,
): AnyProtocolTx | null {
switch (tx.MoveCall.function) {
case 'create_tip': {
if (txInputs.length !== 5) return null;
const tipObjChange = this.extractTipObjCreated(resp);
if (!tipObjChange) return null;
return {
kind: 'create_tip',
inputs: {
profile_id: getArgVal(txInputs[0]),
recipient: getArgVal(txInputs[1]),
sender: getArgVal(txInputs[2]),
coin_type: tx.MoveCall.type_arguments[0],
amount: getArgVal(txInputs[3]),
description: getArgVal(txInputs[4]),
},
...getCommonTxProps(resp, txData),
};
}
case 'claim_tip': {
if (txInputs.length !== 1) return null;
return {
kind: 'claim_tip',
inputs: {
tip_id: getArgVal(txInputs[0]),
},
...getCommonTxProps(resp, txData),
};
}
case 'cancel_tip': {
if (txInputs.length !== 1) return null;
return {
kind: 'cancel_tip',
inputs: {
tip_id: getArgVal(txInputs[0]),
},
...getCommonTxProps(resp, txData),
};
}
}
return null;
}
// === Object Extractors ===
/**
* Extracts the created Tip object from transaction response
* @param resp Transaction response
* @returns Created tip object change or undefined
*/
public extractTipObjCreated(
resp: SuiTransactionBlockResponse,
): ObjChangeKind<'created'> | undefined {
return resp.objectChanges?.find(
(o) => o.type === 'created' && o.objectType.startsWith(`${this.packageId}::tip::Tip`),
) as ObjChangeKind<'created'> | undefined;
}
/**
* Extracts the mutated Tip object from transaction response
* @param resp Transaction response
* @returns Mutated tip object change or undefined
*/
public extractTipObjMutated(
resp: SuiTransactionBlockResponse,
): ObjChangeKind<'mutated'> | undefined {
return resp.objectChanges?.find(
(o) => o.type === 'mutated' && o.objectType.startsWith(`${this.packageId}::tip::Tip`),
) as ObjChangeKind<'mutated'> | undefined;
}
}