import fs from 'node:fs';
import { createWalletClient, createPublicClient, http, erc20Abi, formatUnits, encodeFunctionData } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';
import { polygon } from 'viem/chains';
let PK = '';
for (const l of fs.readFileSync('/home/app/polymarket-weather-bot/.env', 'utf8').split('\n')) { const t = l.trim(); if (t.startsWith('PRIVATE_KEY=')) PK = t.slice(12).trim(); }
if (!PK.startsWith('0x')) PK = '0x' + PK;
const account = privateKeyToAccount(PK);
const RPC = 'https://polygon.drpc.org';
const pub = createPublicClient({ chain: polygon, transport: http(RPC) });
const wal = createWalletClient({ account, chain: polygon, transport: http(RPC) });
const PUSD = '0xC011a7E12a19f7B1f670d46F03B03f3342E82DFB';
const SAFE = '0x3643914646900cA7A5df15B8f5d1Cc5E32728c1a';
const DW = '0x50A8061e9448EB1e5d5e7aF07BE4E4F63C6F24Ff';
const SAFE_ABI = [
{ name: 'nonce', type: 'function', stateMutability: 'view', inputs: [], outputs: [{ type: 'uint256' }] },
{ name: 'getThreshold', type: 'function', stateMutability: 'view', inputs: [], outputs: [{ type: 'uint256' }] },
{ name: 'isOwner', type: 'function', stateMutability: 'view', inputs: [{ type: 'address' }], outputs: [{ type: 'bool' }] },
{ name: 'getTransactionHash', type: 'function', stateMutability: 'view', inputs: [{ type: 'address' }, { type: 'uint256' }, { type: 'bytes' }, { type: 'uint8' }, { type: 'uint256' }, { type: 'uint256' }, { type: 'uint256' }, { type: 'address' }, { type: 'address' }, { type: 'uint256' }], outputs: [{ type: 'bytes32' }] },
{ name: 'execTransaction', type: 'function', stateMutability: 'payable', inputs: [{ type: 'address' }, { type: 'uint256' }, { type: 'bytes' }, { type: 'uint8' }, { type: 'uint256' }, { type: 'uint256' }, { type: 'uint256' }, { type: 'address' }, { type: 'address' }, { type: 'bytes' }], outputs: [{ type: 'bool' }] },
];
console.log('signer:', account.address, '| isOwner:', await pub.readContract({ address: SAFE, abi: SAFE_ABI, functionName: 'isOwner', args: [account.address] }), '| threshold:', await pub.readContract({ address: SAFE, abi: SAFE_ABI, functionName: 'getThreshold' }));
const bal = await pub.readContract({ address: PUSD, abi: erc20Abi, functionName: 'balanceOf', args: [SAFE] });
console.log('weather Safe pUSD:', formatUnits(bal, 6));
if (bal === 0n) { console.log('nothing'); process.exit(0); }
const data = encodeFunctionData({ abi: erc20Abi, functionName: 'transfer', args: [DW, bal] });
const nonce = await pub.readContract({ address: SAFE, abi: SAFE_ABI, functionName: 'nonce' });
const Z = '0x0000000000000000000000000000000000000000';
const args = [PUSD, 0n, data, 0, 0n, 0n, 0n, Z, Z];
const safeTxHash = await pub.readContract({ address: SAFE, abi: SAFE_ABI, functionName: 'getTransactionHash', args: [...args, nonce] });
const sig = await account.sign({ hash: safeTxHash });
const hash = await wal.writeContract({ address: SAFE, abi: SAFE_ABI, functionName: 'execTransaction', args: [...args, sig] });
console.log('tx:', hash, 'waiting');
const rc = await pub.waitForTransactionReceipt({ hash, timeout: 120000 });
console.log('status:', rc.status);
console.log('DW pUSD now:', formatUnits(await pub.readContract({ address: PUSD, abi: erc20Abi, functionName: 'balanceOf', args: [DW] }), 6));