Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions packages/client/src/PhantomClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ import {
type SignMessageParams,
type SignTransactionParams,
type SignTypedDataParams,
type SpendingLimitConfig,
type SpendContext,
type SpendingState,
type UserConfig,
} from "./types";

Expand Down Expand Up @@ -227,13 +228,39 @@ export class PhantomClient {
}
}

/**
* Gets the current spending limit status for an organization without preparing a transaction
* @param organizationId The organization ID to check spending status for
* @returns The current spending state including spent amount, daily limit, and window start timestamp
*/
async getSpendingStatus(organizationId?: string): Promise<SpendingState> {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to think more about this method signature. The caller should be agnostic to the org ID. We need some other way to get it from the auth session or similar

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need expose the option to fetch from other organizations. The SDK is initiated explicitly for one.

try {
const orgId = organizationId || this.config.organizationId;
if (!orgId) {
throw new Error("organizationId is required to get spending status");
}

const response = await this.axiosInstance.get(`${this.config.apiBaseUrl}/spending-status`, {
params: {
organizationId: orgId,
},
headers: {
"Content-Type": "application/json",
},
});
return response.data;
} catch (error: any) {
throw new Error(`Failed to get spending status: ${error.response?.data?.message || error.message}`);
}
}

/**
* Private method for shared signing logic
*/
private async performTransactionSigning(
params: SignTransactionParams,
includeSubmissionConfig: boolean,
): Promise<{ signedTransaction: string; hash?: string }> {
): Promise<{ signedTransaction: string; hash?: string; spendingContext?: SpendContext }> {
const walletId = params.walletId;
const transactionParam = params.transaction;
const networkIdParam = params.networkId;
Expand Down Expand Up @@ -274,6 +301,7 @@ export class PhantomClient {
// TWO-PHASE SPENDING LIMITS FLOW
// Phase 1: Call wallet service to check spending limits and augment transaction if needed
let augmentedTransaction = encodedTransaction;
let spendingContext: SpendContext | undefined;

// Always check spending limits for Solana transactions
// If we don't receive an account
Expand All @@ -293,6 +321,7 @@ export class PhantomClient {
);

augmentedTransaction = augmentResponse.transaction;
spendingContext = augmentResponse.spendingContext;
} catch (e: any) {
const errorMessage = e?.message || String(e);
throw new Error(
Expand Down Expand Up @@ -342,6 +371,7 @@ export class PhantomClient {
return {
signedTransaction: result.transaction as unknown as string, // Base64 encoded signed transaction
hash,
spendingContext,
};
} catch (error: any) {
const actionType = includeSubmissionConfig ? "sign and send" : "sign";
Expand All @@ -358,6 +388,7 @@ export class PhantomClient {

return {
rawTransaction: result.signedTransaction,
spendingContext: result.spendingContext,
};
}

Expand All @@ -369,6 +400,7 @@ export class PhantomClient {
return {
rawTransaction: result.signedTransaction,
hash: result.hash,
spendingContext: result.spendingContext,
};
}

Expand Down
27 changes: 24 additions & 3 deletions packages/client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ export type Transaction = string;
export interface SignedTransaction {
rawTransaction: string; // base64url encoded signed transaction
hash?: string; // Optional transaction hash if available
spendingContext?: SpendContext; // Optional spending context from /prepare endpoint
}

export interface SignedTransactionResult {
rawTransaction: string; // base64url encoded signed transaction
spendingContext?: SpendContext; // Optional spending context from /prepare endpoint
}

export interface GetWalletsResult {
Expand Down Expand Up @@ -66,7 +68,7 @@ export interface SignTransactionParams {
networkId: NetworkId;
derivationIndex?: number; // Optional account derivation index (defaults to 0)
account?: string; // Optional specific account address to use
walletType: "server-wallet" | "user-wallet";
walletType: "server-wallet" | "user-wallet";
}

export interface SignAndSendTransactionParams {
Expand All @@ -75,7 +77,7 @@ export interface SignAndSendTransactionParams {
networkId: NetworkId;
derivationIndex?: number; // Optional account derivation index (defaults to 0)
account?: string; // Optional specific account address to use
walletType: "server-wallet" | "user-wallet";
walletType: "server-wallet" | "user-wallet";
}

export interface GetWalletWithTagParams {
Expand Down Expand Up @@ -141,8 +143,27 @@ export interface SpendingLimitConfig {
memoryBump: number;
}

export interface SpendContext {
previousSpendCents: number;
newSpendCents: number;
totalSpendCents: number;
windowStartTimestamp?: number;
}

export interface AugmentWithSpendingLimitResponse {
transaction: string; // base64url encoded with Lighthouse instructions
simulationResult?: any;
memoryConfigUsed?: SpendingLimitConfig;
spendingContext?: SpendContext;
}

/**
* Spending state for an organization
*/
export interface SpendingState {
/** Amount in USD cents spent in the current 24-hour window */
currentSpendCents: number;
/** Daily spending limit in USD cents */
dailyLimitCents: number;
/** Unix timestamp (seconds) when the current 24-hour spending window started. None if the memory account doesn't exist yet. */
windowStartTimestamp?: number;
}
Loading