@galliun/sofi-sdk
Version:
SDK for interacting with the Galliun SocialFi protocol
157 lines (135 loc) • 4.82 kB
text/typescript
import { getFullnodeUrl, SuiClient } from '@mysten/sui/client';
import { ConfigManager } from './Config/ConfigManager.js';
import { GoalManager } from './Goal/GoalManager.js';
import { ProfileManager } from './Profile/ProfileManager.js';
import { TipManager } from './Tip/TipManager.js';
import { SoFiTxParser } from './SoFiTxParser.js';
import type { NetworkName } from './config.js';
import { SOFI_IDS } from './config.js';
import type { SignTransaction } from './util.js';
/**
* Main client for interacting with the Galliun SoFi Protocol.
* Provides access to all protocol features through specialized managers.
*
* @example
* ```typescript
* const client = new SoFiClient({
* network: 'mainnet',
* packageId: '0x...',
* configId: '0x...',
* signTransaction: async (tx) => {
* // Your transaction signing logic
* return signedTx;
* }
* });
* ```
*
* @category Client
*/
export class SoFiClient {
/** The Sui network this client is connected to */
public readonly network: NetworkName;
/** The protocol package ID on the network */
public readonly packageId: string;
/** The protocol configuration object ID */
public readonly configId: string;
/** Optional admin capability ID for admin operations */
public readonly adminCapId?: string;
/** The registry object ID */
public readonly profileRegistryId?: string;
/** Default sender address for transactions */
public readonly sender?: string;
/** Sui client instance for blockchain interaction */
public readonly suiClient: SuiClient;
/** Manager for goal-related operations */
public readonly goalManager: GoalManager;
/** Manager for protocol configuration */
public readonly configManager: ConfigManager;
/** Manager for profile operations */
public readonly profileManager: ProfileManager;
/** Manager for tip operations */
public readonly tipManager: TipManager;
private readonly txParser: SoFiTxParser;
/**
* Creates a new SoFiClient instance.
*
* @param args - Client initialization parameters
* @param args.network - The Sui network to connect to ('mainnet' or 'testnet')
* @param args.packageId - The protocol package ID
* @param args.configId - The protocol configuration object ID
* @param args.signTransaction - Function to sign transactions
* @param args.adminCapId - Optional admin capability ID
* @param args.profileRegistryId - Optional profile registry ID
* @param args.sender - Optional default sender address
* @param args.suiClient - Optional custom Sui client instance
*/
constructor(args: {
network: NetworkName;
packageId?: string;
configId?: string;
adminCapId?: string;
profileRegistryId?: string;
sender?: string;
signTransaction: SignTransaction;
suiClient?: SuiClient;
}) {
const { network, packageId, configId, adminCapId, profileRegistryId, sender } = args;
this.network = network;
this.packageId = packageId || SOFI_IDS[network].packageId;
this.configId = configId || SOFI_IDS[network].configId;
this.adminCapId = adminCapId || SOFI_IDS[network].adminCapId;
this.profileRegistryId = profileRegistryId || SOFI_IDS[network].profileRegistryId;
this.sender = sender;
this.suiClient = args.suiClient ?? new SuiClient({ url: getFullnodeUrl(args.network) });
this.txParser = new SoFiTxParser(this.packageId);
// Initialize managers
const baseManagerArgs = {
network: this.network,
packageId: this.packageId,
configId: this.configId,
profileRegistryId: this.profileRegistryId,
sender: this.sender,
suiClient: this.suiClient,
signTransaction: args.signTransaction,
};
this.goalManager = new GoalManager({
...baseManagerArgs,
txParser: this.txParser.goalTxParser,
});
this.configManager = new ConfigManager({
...baseManagerArgs,
txParser: this.txParser,
adminCapId: this.adminCapId ?? '',
});
this.profileManager = new ProfileManager({
...baseManagerArgs,
txParser: this.txParser.profileTxParser,
});
this.tipManager = new TipManager({
...baseManagerArgs,
txParser: this.txParser.tipTxParser,
});
}
/**
* Validates if an amount is valid for a given coin type.
* Checks if the amount is divisible by the coin's tick size.
*
* @param coinType - The type of coin to validate (e.g., "0x2::sui::SUI")
* @param amount - The amount to validate
* @returns True if the amount is valid for the coin type
*
* @example
* ```typescript
* const isValid = await client.isValidAmount("0x2::sui::SUI", 1000000n);
* ```
*/
public async isValidAmount(coinType: string, amount: bigint): Promise<boolean> {
try {
const tickSize = await this.configManager.getCoinTickSize(coinType);
return amount % tickSize === 0n;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (error) {
return false;
}
}
}