set mint with npubxcash

This commit is contained in:
lemonknowsall committed 2025-10-13 12:33:02 -07:00
1 parent 5dc19e857f
commit 346ba70710
4 files changed
+173 -614

No files matched your search

+73
View File
@@ -0,0 +1,73 @@
import { useCallback, useState } from 'react';
import { useNIP98 } from './useNIP98';
const NPUBCASH_API_BASE = 'https://npub.cash/api/v2';
export interface NpubCashApiError {
error: true;
message: string;
}
/**
* Hook for interacting with npub.cash API
* Currently supports setting the user's preferred mint
*/
export function useNpubCash() {
const { createAuthHeader, isLoggedIn, pubkey } = useNIP98();
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
/**
* Set the user's preferred mint on npub.cash
*/
const setMint = useCallback(async (mintUrl: string): Promise<boolean> => {
if (!isLoggedIn) {
console.error('❌ User must be logged in to set mint');
return false;
}
try {
setIsLoading(true);
setError(null);
const url = `${NPUBCASH_API_BASE}/user/mint`;
const body = { mint_url: mintUrl };
const authHeader = await createAuthHeader({ url, method: 'PATCH', body });
console.log(`🔄 Setting preferred mint on npub.cash to: ${mintUrl}`);
const response = await fetch(url, {
method: 'PATCH',
headers: {
'Authorization': authHeader,
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
});
if (!response.ok) {
const errorData = await response.json() as NpubCashApiError;
throw new Error(errorData.message || 'Failed to set mint');
}
console.log(`✅ Successfully set preferred mint on npub.cash to: ${mintUrl}`);
return true;
} catch (err) {
const errorMessage = err instanceof Error ? err.message : 'Unknown error';
console.error('❌ Failed to set npub.cash mint:', errorMessage);
setError(errorMessage);
return false;
} finally {
setIsLoading(false);
}
}, [isLoggedIn, createAuthHeader]);
return {
setMint,
isLoading,
error,
isLoggedIn,
pubkey,
};
}