x402-goat-facilitator / tree / scripts/deploy-token.mjs ยท raw (this view has old GitHub links rewritten for the mirror; raw is byte-exact)
#!/usr/bin/env node
// Deploy PayGuard Test USD (tUSDC) to GOAT Testnet3 from the facilitator key (must be nonce 0 so the address matches
// src/goat.js), then faucetTo(payer). Never prints keys.
//   FACILITATOR_FILE=... PAYER_ADDRESS=0x... node scripts/deploy-token.mjs
import { readFileSync } from 'node:fs';
import { createWalletClient, http, publicActions, getContractAddress } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';
import { goatTestnet3, GOAT_TOKENS, GOAT_EXPLORER } from '../src/goat.js';

const pk = readFileSync(process.env.FACILITATOR_FILE, 'utf8').match(/^private_key=(0x[0-9a-fA-F]{64})$/m)[1];
const account = privateKeyToAccount(pk);
const c = createWalletClient({ account, chain: goatTestnet3, transport: http() }).extend(publicActions);
const { abi, bytecode } = JSON.parse(readFileSync(new URL('../contracts/PayGuardTestUSD.json', import.meta.url), 'utf8'));
const expected = GOAT_TOKENS.tUSDC.address;
console.log('deployer', account.address, 'balance', await c.getBalance({ address: account.address }));
let code = await c.getCode({ address: expected });
if (!code || code === '0x') {
  const nonce = await c.getTransactionCount({ address: account.address });
  const addr = getContractAddress({ from: account.address, nonce: BigInt(nonce) });
  if (addr.toLowerCase() !== expected.toLowerCase()) throw new Error(`nonce ${nonce} would deploy to ${addr}, not ${expected}; update src/goat.js and PayGuard`);
  const hash = await c.deployContract({ abi, bytecode });
  console.log('deploy tx', hash, `${GOAT_EXPLORER}/tx/${hash}`);
  const r = await c.waitForTransactionReceipt({ hash });
  console.log('deployed', r.contractAddress, r.status);
}
if (process.env.PAYER_ADDRESS) {
  const bal = await c.readContract({ address: expected, abi, functionName: 'balanceOf', args: [process.env.PAYER_ADDRESS] });
  if (bal < 1000n) {
    const hash = await c.writeContract({ address: expected, abi, functionName: 'faucetTo', args: [process.env.PAYER_ADDRESS] });
    console.log('faucetTo tx', hash, `${GOAT_EXPLORER}/tx/${hash}`);
    await c.waitForTransactionReceipt({ hash });
  }
  console.log('payer tUSDC', await c.readContract({ address: expected, abi, functionName: 'balanceOf', args: [process.env.PAYER_ADDRESS] }));
}

agentgate-source: code mirror on Cloudflare Pages. All repos