|
| 1 | +import * as anchor from "@coral-xyz/anchor"; |
| 2 | +import * as spl from "@solana/spl-token"; |
| 3 | +import type { AccountInfo, AccountMeta } from "@solana/web3.js"; |
| 4 | +import { |
| 5 | + AddressLookupTableProgram, |
| 6 | + Connection, |
| 7 | + Keypair, |
| 8 | + MessageV0, |
| 9 | + PublicKey, |
| 10 | + sendAndConfirmTransaction, |
| 11 | + SystemProgram, |
| 12 | + Transaction, |
| 13 | + TransactionInstruction, |
| 14 | + TransactionMessage, |
| 15 | + VersionedTransaction, |
| 16 | +} from "@solana/web3.js"; |
| 17 | +import { Big, BigUtils, bs58 } from "@switchboard-xyz/common"; |
| 18 | +import { OracleJob } from "@switchboard-xyz/common"; |
| 19 | +import * as sb from "@switchboard-xyz/solana.js"; |
| 20 | +import { toBufferLE } from "bigint-buffer"; |
| 21 | +import * as crypto from "crypto"; |
| 22 | +import * as fs from "fs"; |
| 23 | +const assert = require("assert"); |
| 24 | + |
| 25 | +const walletFile = "your wallet file json here"; |
| 26 | +// example "/Users/mgild/switchboard_environments_v2/devnet/upgrade_authority/test.json" |
| 27 | +const payerFile = "your payer file json here" |
| 28 | +let PID = new PublicKey("sbattyXrzedoNATfc4L31wC9Mhxsi1BmFhTiN8gDshx"); |
| 29 | +// PID = new PublicKey("CR1hCrkKveeWrYYs5kk7rasRM2AH1vZy8s8fn42NBwkq"); |
| 30 | +const RPC_URL = "https://api.devnet.solana.com"; |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +async function fetchLatestSlotHash( |
| 35 | + connection: Connection |
| 36 | +): Promise<[bigint, string]> { |
| 37 | + const slotHashesSysvarKey = new PublicKey( |
| 38 | + "SysvarS1otHashes111111111111111111111111111" |
| 39 | + ); |
| 40 | + const accountInfo = await connection.getAccountInfo(slotHashesSysvarKey, { |
| 41 | + commitment: "confirmed", |
| 42 | + dataSlice: { length: 40, offset: 8 }, |
| 43 | + }); |
| 44 | + let buffer = accountInfo!.data; |
| 45 | + const slotNumber = buffer.readBigUInt64LE(); |
| 46 | + buffer = buffer.slice(8); |
| 47 | + return [slotNumber, bs58.encode(buffer)]; |
| 48 | +} |
| 49 | + |
| 50 | +async function initWalletFromFile(filePath: string): Promise<anchor.Wallet> { |
| 51 | + // Read the file |
| 52 | + const secretKeyString: string = fs.readFileSync(filePath, { |
| 53 | + encoding: "utf8", |
| 54 | + }); |
| 55 | + const secretKey: Uint8Array = Uint8Array.from(JSON.parse(secretKeyString)); |
| 56 | + |
| 57 | + // Create a keypair from the secret key |
| 58 | + const keypair: Keypair = Keypair.fromSecretKey(secretKey); |
| 59 | + |
| 60 | + // Create a wallet |
| 61 | + const wallet: anchor.Wallet = new anchor.Wallet(keypair); |
| 62 | + |
| 63 | + return wallet; |
| 64 | +} |
| 65 | + |
| 66 | +async function initKeypairFromFile(filePath: string): Promise<Keypair> { |
| 67 | + // Read the file |
| 68 | + const secretKeyString: string = fs.readFileSync(filePath, { |
| 69 | + encoding: "utf8", |
| 70 | + }); |
| 71 | + const secretKey: Uint8Array = Uint8Array.from(JSON.parse(secretKeyString)); |
| 72 | + |
| 73 | + // Create a keypair from the secret key |
| 74 | + const keypair: Keypair = Keypair.fromSecretKey(secretKey); |
| 75 | + |
| 76 | + return keypair; |
| 77 | +} |
| 78 | + |
| 79 | +async function keypairFromJson(secretKeyString: string): Promise<Keypair> { |
| 80 | + const secretKey: Uint8Array = Uint8Array.from(JSON.parse(secretKeyString)); |
| 81 | + |
| 82 | + // Create a keypair from the secret key |
| 83 | + return Keypair.fromSecretKey(secretKey); |
| 84 | +} |
| 85 | + |
| 86 | +export function logEnvVariables( |
| 87 | + env: Array<[string, string | anchor.web3.PublicKey]>, |
| 88 | + pre = "Make sure to add the following to your .env file:" |
| 89 | +) { |
| 90 | + console.log( |
| 91 | + `\n${pre}\n\t${env |
| 92 | + .map(([key, value]) => `${key.toUpperCase()}=${value}`) |
| 93 | + .join("\n\t")}\n` |
| 94 | + ); |
| 95 | +} |
| 96 | + |
| 97 | +(async () => { |
| 98 | + const ORACLE_IP = "127.0.0.1"; |
| 99 | + |
| 100 | + let PID = new PublicKey("sbattyXrzedoNATfc4L31wC9Mhxsi1BmFhTiN8gDshx"); |
| 101 | + PID = sb.SB_ON_DEMAND_PID; |
| 102 | + const connection = new Connection( |
| 103 | + RPC_URL, |
| 104 | + "confirmed" |
| 105 | + ); |
| 106 | + |
| 107 | + const wallet = await initWalletFromFile( |
| 108 | + walletFile |
| 109 | + ); |
| 110 | + const devnetPayer = await initKeypairFromFile( |
| 111 | + payerFile |
| 112 | + ); |
| 113 | + const provider = new anchor.AnchorProvider(connection, wallet, {}); |
| 114 | + const idl = await anchor.Program.fetchIdl(PID, provider); |
| 115 | + const program = new anchor.Program(idl!, PID, provider); |
| 116 | + const switchboardProgram = sb.SwitchboardProgram.from( |
| 117 | + connection, |
| 118 | + devnetPayer, |
| 119 | + sb.SB_V2_PID, |
| 120 | + PID |
| 121 | + ); |
| 122 | + |
| 123 | + const [slotNumber, slotHash] = await fetchLatestSlotHash(connection); |
| 124 | + const bootstrappedQueue = (await sb.AttestationQueueAccount.bootstrapNewQueue( |
| 125 | + switchboardProgram |
| 126 | + )) as any; |
| 127 | + console.log(bootstrappedQueue); |
| 128 | + |
| 129 | + const attestationQueueAccount = bootstrappedQueue.attestationQueue.account; |
| 130 | + const verifierOracleAccount = bootstrappedQueue.verifier.account; |
| 131 | + const quoteKeypair2 = Keypair.generate(); |
| 132 | + |
| 133 | + const [verifier2, signature] = await attestationQueueAccount.createVerifier({ |
| 134 | + createPermissions: true, |
| 135 | + keypair: quoteKeypair2, |
| 136 | + enable: true, |
| 137 | + queueAuthorityPubkey: devnetPayer.publicKey, |
| 138 | + authority: devnetPayer.publicKey, |
| 139 | + queueAccount: attestationQueueAccount.publicKey, |
| 140 | + registryKey: new Uint8Array(64).fill(0), |
| 141 | + }); |
| 142 | + console.log(verifier2.publicKey); |
| 143 | + |
| 144 | + logEnvVariables([ |
| 145 | + ["SWITCHBOARD_ATTESTATION_QUEUE_KEY", attestationQueueAccount.publicKey], |
| 146 | + ["SWITCHBOARD_VERIFIER_ORACLE_KEY", verifierOracleAccount.publicKey], |
| 147 | + ["SWITCHBOARD_VERIFIER_ORACLE_KEY2", verifier2.publicKey.toString()], |
| 148 | + ]); |
| 149 | + |
| 150 | + const y = bootstrappedQueue.signatures.map((s: any, i: any): any => { |
| 151 | + return { name: `bootstrap_queue #${i + 1}`, tx: s }; |
| 152 | + }); |
| 153 | + console.log(y); |
| 154 | + return; |
| 155 | +})(); |
0 commit comments