Skip to content
Open
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
54 changes: 7 additions & 47 deletions apps/mcp/src/lib/3rd-parties/payment-strategies/cdp-strategy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { getCDPNetworks, type UnifiedNetwork } from "../cdp/wallet/networks.js";
import { createSignerFromViemAccount } from "mcpay/utils";
import { getCDPAccount } from "../cdp/wallet/index.js";
import { txOperations, Wallet } from "../../db/actions.js";
Expand Down Expand Up @@ -27,12 +26,12 @@ export class CDPSigningStrategy implements PaymentSigningStrategy {
const activeWallets = cdpWallets.filter(w => w.isActive);
const compatibleWallets = activeWallets.filter(wallet => {
const walletNetwork = (wallet.walletMetadata as unknown as CDPWalletMetadata)?.cdpNetwork;
return walletNetwork === network || this.isNetworkCompatible(walletNetwork, network);
return walletNetwork === network;
});

console.log(`[CDP Strategy] Found ${compatibleWallets.length} compatible wallets for user ${context.user!.id}`);

const canSign = compatibleWallets.length > 0 || activeWallets.length > 0;
const canSign = compatibleWallets.length > 0;
console.log(`[CDP Strategy] Can sign: ${canSign} (compatible: ${compatibleWallets.length}, active: ${activeWallets.length})`);
return canSign;
} catch (error) {
Expand Down Expand Up @@ -65,22 +64,19 @@ export class CDPSigningStrategy implements PaymentSigningStrategy {
const activeWallets = cdpWallets.filter(w => w.isActive);
const compatibleWallets = activeWallets.filter(wallet => {
const walletNetwork = (wallet.walletMetadata as unknown as CDPWalletMetadata)?.cdpNetwork;
return walletNetwork === network || this.isNetworkCompatible(walletNetwork, network);
return walletNetwork === network;
});

// If none are compatible by family, fall back to any active CDP wallet
const candidateWallets = compatibleWallets.length > 0 ? compatibleWallets : activeWallets;

if (candidateWallets.length === 0) {
if (compatibleWallets.length === 0) {
return {
success: false,
error: `No active CDP wallets available for user`
error: `No active CDP wallet found on network "${network}". Please create or activate a wallet on this network.`
};
}

// Prefer smart accounts (gas-sponsored) over regular accounts
const smartWallets = candidateWallets.filter(w => (w.walletMetadata as unknown as CDPWalletMetadata)?.isSmartAccount);
const regularWallets = candidateWallets.filter(w => !(w.walletMetadata as unknown as CDPWalletMetadata)?.isSmartAccount);
const smartWallets = compatibleWallets.filter(w => (w.walletMetadata as unknown as CDPWalletMetadata)?.isSmartAccount);
const regularWallets = compatibleWallets.filter(w => !(w.walletMetadata as unknown as CDPWalletMetadata)?.isSmartAccount);

const walletsToTry = [...smartWallets, ...regularWallets];

Expand Down Expand Up @@ -120,42 +116,6 @@ export class CDPSigningStrategy implements PaymentSigningStrategy {
}
}

private isNetworkCompatible(walletNetwork: string | undefined, targetNetwork: UnifiedNetwork): boolean {
if (!walletNetwork) return false;

// Normalize to unified network names if possible
const cdpNetworks = getCDPNetworks();
const normalizedWallet = cdpNetworks.includes(walletNetwork as CDPNetwork)
? (walletNetwork as UnifiedNetwork)
: (walletNetwork as unknown as UnifiedNetwork);

// Direct match
if (normalizedWallet === targetNetwork) return true;

// Family pairing: treat testnets as compatible with their mainnet family
const family = (n: UnifiedNetwork | string) => {
switch (n) {
case 'polygon':
case 'polygon-amoy':
return 'polygon';
case 'base':
case 'base-sepolia':
return 'base';
case 'ethereum':
case 'ethereum-sepolia':
return 'ethereum';
case 'arbitrum':
return 'arbitrum';
case 'sei-testnet':
return 'sei';
default:
return String(n);
}
};

return family(normalizedWallet) === family(targetNetwork);
}

private async signWithCDPWallet(
wallet: Wallet,
paymentRequirement: PaymentRequirements,
Expand Down