const { ClobClient } = require('@polymarket/clob-client'); const { Wallet, JsonRpcProvider, TypedDataEncoder } = require('ethers'); const dotenv = require('dotenv'); dotenv.config(); // Patch the Wallet to add _signTypedData method if it doesn't exist function patchWallet(wallet) { if (!wallet._signTypedData) { wallet._signTypedData = async function(domain, types, value) { // Use the signTypedData method from ethers v6 return await this.signTypedData(domain, types, value); }; } return wallet; } async function generateApiKeys() { try { const privateKey = process.env.POLYMARKET_PRIVATE_KEY; const rpcUrl = process.env.NETWORK_RPC_URL; if (!privateKey) { console.error('Error: POLYMARKET_PRIVATE_KEY not found in .env'); process.exit(1); } console.log('\nšŸ” Generating new Polymarket API credentials...\n'); // Create provider and signer (ethers v6 way) const provider = new JsonRpcProvider(rpcUrl); let signer = new Wallet(privateKey, provider); signer = patchWallet(signer); const signerAddress = signer.address; console.log(`Signer Address: ${signerAddress}`); // Create a temporary client without credentials to derive new ones const tempClient = new ClobClient( 'https://clob.polymarket.com', 137, // Polygon mainnet signer, undefined, // No credentials yet 1 // SignatureType.EOA = 1 ); console.log('Calling createOrDeriveApiKey()...\n'); const credentials = await tempClient.createOrDeriveApiKey(); console.log('āœ“ API Credentials Generated Successfully!\n'); console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'); console.log('Add these to your .env file:'); console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n'); console.log(`POLYMARKET_API_KEY=${credentials.key}`); console.log(`POLYMARKET_API_SECRET=${credentials.secret}`); console.log(`POLYMARKET_API_PASSPHRASE=${credentials.passphrase}`); console.log('\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n'); process.exit(0); } catch (error) { console.error('Error generating API keys:', error.message); console.error('\nFull error:', error); process.exit(1); } } generateApiKeys();