|
| 1 | +import { PublicKey } from '@solana/web3.js' |
| 2 | +import { |
| 3 | + SolanaAgentKit, |
| 4 | + executeAction, |
| 5 | + type Action, |
| 6 | + type Plugin, |
| 7 | +} from 'solana-agent-kit' |
| 8 | +import { |
| 9 | + SOL_MINT, |
| 10 | + TOKEN_PROGRAM_ID, |
| 11 | + createReadOnlyWallet, |
| 12 | + createReadonlySolanaPlugin, |
| 13 | + createSolanaAgentTools, |
| 14 | + type ReadonlySolanaConnection, |
| 15 | +} from '@pay/solana-agent-tools' |
| 16 | + |
| 17 | +const DEVNET_RPC = 'https://api.devnet.solana.com' |
| 18 | +const DEMO_WALLET = process.env.WALLET ?? '11111111111111111111111111111111' |
| 19 | +const PYTH_SOL_USD = 'ef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d' |
| 20 | +const useMock = process.argv.includes('--mock') |
| 21 | + |
| 22 | +function json(body: unknown): Response { |
| 23 | + return new Response(JSON.stringify(body), { |
| 24 | + status: 200, |
| 25 | + headers: { 'content-type': 'application/json' }, |
| 26 | + }) |
| 27 | +} |
| 28 | + |
| 29 | +function mockConnection(): ReadonlySolanaConnection { |
| 30 | + return { |
| 31 | + async getBalance(pubkey) { |
| 32 | + if (pubkey.toBase58() !== DEMO_WALLET) throw new Error(`unexpected balance lookup ${pubkey.toBase58()}`) |
| 33 | + return 1_230_000_000 |
| 34 | + }, |
| 35 | + async getParsedTokenAccountsByOwner(owner, filter) { |
| 36 | + if (filter.programId.toBase58() !== TOKEN_PROGRAM_ID.toBase58()) { |
| 37 | + throw new Error(`unexpected token program ${filter.programId.toBase58()}`) |
| 38 | + } |
| 39 | + return { |
| 40 | + value: [{ |
| 41 | + pubkey: new PublicKey(SOL_MINT), |
| 42 | + account: { |
| 43 | + data: { |
| 44 | + parsed: { |
| 45 | + info: { |
| 46 | + mint: SOL_MINT, |
| 47 | + owner: owner.toBase58(), |
| 48 | + tokenAmount: { |
| 49 | + amount: '42000000', |
| 50 | + decimals: 9, |
| 51 | + uiAmount: 0.042, |
| 52 | + uiAmountString: '0.042', |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }], |
| 59 | + } |
| 60 | + }, |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +const mockFetch: typeof fetch = async (input) => { |
| 65 | + const href = input instanceof Request ? input.url : String(input) |
| 66 | + if (href.includes('price/v3')) { |
| 67 | + return json({ [SOL_MINT]: { usdPrice: 123.45, decimals: 9, blockId: 42 } }) |
| 68 | + } |
| 69 | + if (href.includes('hermes.pyth.network') || href.includes('/updates/price/latest')) { |
| 70 | + return json({ |
| 71 | + parsed: [{ |
| 72 | + id: PYTH_SOL_USD, |
| 73 | + price: { price: '12345000000', conf: '1000000', expo: -8, publish_time: 1710000000 }, |
| 74 | + }], |
| 75 | + }) |
| 76 | + } |
| 77 | + return new Response(`unexpected mock fetch ${href}`, { status: 404 }) |
| 78 | +} |
| 79 | + |
| 80 | +function getAction(agent: SolanaAgentKit, name: string): Action { |
| 81 | + const action = agent.actions.find((candidate) => candidate.name === name) |
| 82 | + if (!action) throw new Error(`missing action ${name}`) |
| 83 | + return action |
| 84 | +} |
| 85 | + |
| 86 | +function summarizeTokenBalances(result: Record<string, unknown>): Record<string, unknown> { |
| 87 | + const balances = Array.isArray(result.balances) ? result.balances : [] |
| 88 | + return { |
| 89 | + ...result, |
| 90 | + balanceCount: balances.length, |
| 91 | + balances: balances.slice(0, 5), |
| 92 | + truncated: balances.length > 5, |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +const tools = createSolanaAgentTools(useMock |
| 97 | + ? { connection: mockConnection(), fetch: mockFetch } |
| 98 | + : { rpcUrl: process.env.SOLANA_RPC_URL ?? DEVNET_RPC }) |
| 99 | + |
| 100 | +const agent = new SolanaAgentKit( |
| 101 | + createReadOnlyWallet(DEMO_WALLET), |
| 102 | + process.env.SOLANA_RPC_URL ?? DEVNET_RPC, |
| 103 | + { signOnly: true }, |
| 104 | +).use(createReadonlySolanaPlugin(tools) as unknown as Plugin) |
| 105 | + |
| 106 | +const balance = await executeAction( |
| 107 | + getAction(agent, 'solana.read_wallet_balance'), |
| 108 | + agent, |
| 109 | + { address: DEMO_WALLET }, |
| 110 | +) |
| 111 | +const tokenBalances = await executeAction( |
| 112 | + getAction(agent, 'solana.read_token_balances'), |
| 113 | + agent, |
| 114 | + { owner: DEMO_WALLET }, |
| 115 | +) |
| 116 | +const jupiterPrice = await executeAction( |
| 117 | + getAction(agent, 'solana.fetch_token_price'), |
| 118 | + agent, |
| 119 | + { id: 'SOL' }, |
| 120 | +) |
| 121 | +const pythPrice = await executeAction( |
| 122 | + getAction(agent, 'solana.fetch_pyth_price'), |
| 123 | + agent, |
| 124 | + { priceFeedId: PYTH_SOL_USD }, |
| 125 | +) |
| 126 | +const transferIntent = await executeAction( |
| 127 | + getAction(agent, 'solana.simulate_transfer_intent'), |
| 128 | + agent, |
| 129 | + { |
| 130 | + service: 'sak.readonly-demo', |
| 131 | + buyer: DEMO_WALLET, |
| 132 | + recipient: DEMO_WALLET, |
| 133 | + amountSol: 0.0001, |
| 134 | + awardedPriceSol: 0.0001, |
| 135 | + policy: { |
| 136 | + maxSolPerRound: 0.001, |
| 137 | + maxSolPerSession: 0.01, |
| 138 | + allowedServices: ['sak.readonly-demo'], |
| 139 | + expectedPayout: DEMO_WALLET, |
| 140 | + }, |
| 141 | + }, |
| 142 | +) |
| 143 | + |
| 144 | +const decision = { |
| 145 | + mode: useMock ? 'mock-smoke' : 'live-read-devnet', |
| 146 | + plugin: 'pay-readonly-solana-tools', |
| 147 | + agentActions: agent.actions.map((action) => action.name), |
| 148 | + observations: { |
| 149 | + balance, |
| 150 | + tokenBalances: summarizeTokenBalances(tokenBalances), |
| 151 | + jupiterPrice, |
| 152 | + pythPrice, |
| 153 | + }, |
| 154 | + decision: { |
| 155 | + summary: 'Agent has enough read-only context to reason, but not enough authority to move funds.', |
| 156 | + nextStep: 'Escalate any real payment to the repo payment rail flow: policy, approval, ledger receipt, UI surface.', |
| 157 | + transferIntent, |
| 158 | + }, |
| 159 | +} |
| 160 | + |
| 161 | +if ((transferIntent as { executable?: unknown }).executable !== false) { |
| 162 | + throw new Error('read-only transfer intent unexpectedly became executable') |
| 163 | +} |
| 164 | + |
| 165 | +console.log(JSON.stringify(decision, null, 2)) |
0 commit comments