From a10090305bf281dcf6d3ab2694aafe68945c3d69 Mon Sep 17 00:00:00 2001 From: Conner Gallagher Date: Wed, 5 Jul 2023 11:44:34 -0600 Subject: [PATCH] final checks --- javascript/solana.js/package.json | 1 + .../solana.js/scripts/close-functions.ts | 80 +++++++++++++++++++ rust/switchboard-solana/Cargo.lock | 2 +- rust/switchboard-solana/Cargo.toml | 2 +- .../attestation_program/accounts/function.rs | 8 ++ .../accounts/function_request.rs | 8 ++ 6 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 javascript/solana.js/scripts/close-functions.ts diff --git a/javascript/solana.js/package.json b/javascript/solana.js/package.json index 7ca512d17..95281c992 100644 --- a/javascript/solana.js/package.json +++ b/javascript/solana.js/package.json @@ -143,6 +143,7 @@ "./package.json": "./package.json" }, "scripts": { + "close-functions": "tsx ./scripts/close-functions.ts", "keypair:create": "shx find ~/.config/solana/id.json || solana-keygen new -s --no-bip39-passphrase --outfile ~/.config/solana/id.json", "localnet:down": "kill -9 $(pgrep command solana-test-validator) || exit 0", "localnet": "tsx ./scripts/localnet.ts", diff --git a/javascript/solana.js/scripts/close-functions.ts b/javascript/solana.js/scripts/close-functions.ts new file mode 100644 index 000000000..d7ee32488 --- /dev/null +++ b/javascript/solana.js/scripts/close-functions.ts @@ -0,0 +1,80 @@ +#!/usr/bin/env tsx + +// import { SwitchboardAttestationProgram } from "../../../../switchboard-core/switchboard_v2/target/types/switchboard_attestation_program"; +import * as sbv2 from "../src"; +import { AggregatorAccount, CrankAccount, QueueAccount } from "../src"; + +import { + Aggregator, + CHECK_ICON, + FAILED_ICON, + IAggregatorDefinition, + jsonReplacers, + PLUS_ICON, + setupOutputDir, +} from "./utils.js"; + +import * as anchor from "@coral-xyz/anchor"; +import { Connection, TransactionInstruction } from "@solana/web3.js"; + +const VERBOSE = process.env.VERBOSE || false; + +const fnDiscriminator = Buffer.from([76, 139, 47, 44, 240, 182, 148, 200]); + +const enclaveDiscriminator = Buffer.from([90, 162, 39, 88, 77, 157, 156, 165]); + +type FunctionAccountWithState = { + account: sbv2.FunctionAccount; + // state: sbv2.attestationTypes.FunctionAccountData; +}; + +async function main() { + const program = await sbv2.SwitchboardProgram.load( + "devnet", + new Connection( + "https://switchbo-switchbo-6225.devnet.rpcpool.com/f6fb9f02-0777-498b-b8f5-67cbb1fc0d14" + ), + sbv2.loadKeypair("~/.config/solana/id.json") + ); + + const attestationProgramAccounts = + await program.connection.getProgramAccounts(sbv2.SB_ATTESTATION_PID); + + const functionAccounts: FunctionAccountWithState[] = []; + const ixns: TransactionInstruction[] = []; + + for (const { pubkey, account } of attestationProgramAccounts) { + const discriminator = account.data.slice(0, 8); + if (Buffer.compare(discriminator, enclaveDiscriminator) === 0) { + functionAccounts.push({ + account: new sbv2.FunctionAccount(program, pubkey), + // state: sbv2.attestationTypes.FunctionAccountData.decode(account.data), + }); + + ixns.push( + sbv2.attestationTypes.functionOverrideClose(program, { + function: pubkey, + solDest: program.walletPubkey, + systemProgram: anchor.web3.SystemProgram.programId, + }) + ); + } + } + + for (const account of functionAccounts) { + console.log(account.account.publicKey.toBase58()); + } + + console.log(`Found ${functionAccounts.length} enclaves`); + + const txns = sbv2.TransactionObject.packIxns(program.walletPubkey, ixns); + + console.log(`Sending ${txns.length} transactions`); + + const signatures = await program.signAndSendAll(txns); + console.log(signatures); +} + +main().catch((error) => { + console.error(error); +}); diff --git a/rust/switchboard-solana/Cargo.lock b/rust/switchboard-solana/Cargo.lock index 825573df4..edc3aa3f0 100644 --- a/rust/switchboard-solana/Cargo.lock +++ b/rust/switchboard-solana/Cargo.lock @@ -4219,7 +4219,7 @@ dependencies = [ [[package]] name = "switchboard-solana" -version = "0.8.1" +version = "0.8.2" dependencies = [ "anchor-client", "anchor-lang", diff --git a/rust/switchboard-solana/Cargo.toml b/rust/switchboard-solana/Cargo.toml index 0129889ce..24c2d23ac 100644 --- a/rust/switchboard-solana/Cargo.toml +++ b/rust/switchboard-solana/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "switchboard-solana" -version = "0.8.1" +version = "0.8.2" edition = "2021" description = "A Rust library to interact with Switchboard accounts." readme = "README.md" diff --git a/rust/switchboard-solana/src/attestation_program/accounts/function.rs b/rust/switchboard-solana/src/attestation_program/accounts/function.rs index deaf6a30a..4646e330f 100644 --- a/rust/switchboard-solana/src/attestation_program/accounts/function.rs +++ b/rust/switchboard-solana/src/attestation_program/accounts/function.rs @@ -355,6 +355,13 @@ impl FunctionAccountData { } cfg_client! { + pub fn get_discriminator_filter() -> solana_client::rpc_filter::RpcFilterType { + solana_client::rpc_filter::RpcFilterType::Memcmp(solana_client::rpc_filter::Memcmp::new( + 0, + solana_client::rpc_filter::MemcmpEncodedBytes::Bytes(FunctionAccountData::discriminator().to_vec()), + )) + } + pub fn get_is_triggered_filter() -> solana_client::rpc_filter::RpcFilterType { solana_client::rpc_filter::RpcFilterType::Memcmp(solana_client::rpc_filter::Memcmp::new( 9, @@ -385,6 +392,7 @@ impl FunctionAccountData { pub fn get_is_ready_filters(queue_pubkey: &Pubkey) -> Vec { vec![ + FunctionAccountData::get_discriminator_filter(), FunctionAccountData::get_is_triggered_filter(), FunctionAccountData::get_is_scheduled_filter(), FunctionAccountData::get_is_active_filter(), diff --git a/rust/switchboard-solana/src/attestation_program/accounts/function_request.rs b/rust/switchboard-solana/src/attestation_program/accounts/function_request.rs index da4a23267..de90064c4 100644 --- a/rust/switchboard-solana/src/attestation_program/accounts/function_request.rs +++ b/rust/switchboard-solana/src/attestation_program/accounts/function_request.rs @@ -211,6 +211,13 @@ impl FunctionRequestAccountData { } cfg_client! { + pub fn get_discriminator_filter() -> solana_client::rpc_filter::RpcFilterType { + solana_client::rpc_filter::RpcFilterType::Memcmp(solana_client::rpc_filter::Memcmp::new( + 0, + solana_client::rpc_filter::MemcmpEncodedBytes::Bytes(FunctionRequestAccountData::discriminator().to_vec()), + )) + } + pub fn get_is_triggered_filter() -> solana_client::rpc_filter::RpcFilterType { solana_client::rpc_filter::RpcFilterType::Memcmp(solana_client::rpc_filter::Memcmp::new( 8, @@ -234,6 +241,7 @@ impl FunctionRequestAccountData { pub fn get_is_ready_filters(queue_pubkey: &Pubkey) -> Vec { vec![ + FunctionRequestAccountData::get_discriminator_filter(), FunctionRequestAccountData::get_is_triggered_filter(), FunctionRequestAccountData::get_is_active_filter(), FunctionRequestAccountData::get_queue_filter(queue_pubkey),