diff --git a/javascript/solana.js/src/accounts/aggregatorAccount.ts b/javascript/solana.js/src/accounts/aggregatorAccount.ts index 58eb310..72c13e7 100644 --- a/javascript/solana.js/src/accounts/aggregatorAccount.ts +++ b/javascript/solana.js/src/accounts/aggregatorAccount.ts @@ -267,13 +267,7 @@ export class AggregatorAccount extends Account { } const jobs = await this.loadJobs(aggregator); - const jobAuthorities = jobs.map(j => j.state.authority); - // const jobAuthorities = Array.from( - // jobs.reduce((set, job) => { - // set.add(job.state.authority); - // return set; - // }, new Set()) - // ); + const jobAuthorities = jobs.map(job => job.state.authority); const [oldLeaseAccount] = LeaseAccount.fromSeed( this.program, diff --git a/javascript/solana.js/src/accounts/queueAccount.ts b/javascript/solana.js/src/accounts/queueAccount.ts index 5e21b1b..6212ca0 100644 --- a/javascript/solana.js/src/accounts/queueAccount.ts +++ b/javascript/solana.js/src/accounts/queueAccount.ts @@ -172,7 +172,7 @@ export class QueueAccount extends Account { payer, [ SystemProgram.createAccount({ - fromPubkey: program.wallet.publicKey, + fromPubkey: payer, newAccountPubkey: dataBuffer.publicKey, space: queueDataSize, lamports: await program.connection.getMinimumBalanceForRentExemption( diff --git a/javascript/solana.js/src/accounts/vrfAccount.ts b/javascript/solana.js/src/accounts/vrfAccount.ts index 46d751d..15dace4 100644 --- a/javascript/solana.js/src/accounts/vrfAccount.ts +++ b/javascript/solana.js/src/accounts/vrfAccount.ts @@ -94,7 +94,7 @@ export class VrfAccount extends Account { const ixns = [ spl.createAssociatedTokenAccountInstruction( - program.wallet.payer.publicKey, + payer, escrow, params.vrfKeypair.publicKey, program.mint.address @@ -103,11 +103,10 @@ export class VrfAccount extends Account { escrow, params.vrfKeypair.publicKey, spl.AuthorityType.AccountOwner, - program.programState.publicKey, - [program.wallet.payer, params.vrfKeypair] + program.programState.publicKey ), SystemProgram.createAccount({ - fromPubkey: program.wallet.payer.publicKey, + fromPubkey: payer, newAccountPubkey: params.vrfKeypair.publicKey, space: size, lamports: await program.connection.getMinimumBalanceForRentExemption( diff --git a/javascript/solana.js/src/program.ts b/javascript/solana.js/src/program.ts index 9c807c6..746b3ee 100644 --- a/javascript/solana.js/src/program.ts +++ b/javascript/solana.js/src/program.ts @@ -15,6 +15,10 @@ import { SwitchboardEvents } from './switchboardEvents'; import { fromCode as fromSwitchboardCode } from './generated/errors/custom'; import { fromCode as fromAnchorCode } from './generated/errors/anchor'; +const DEVNET_GENESIS_HASH = 'EtWTRABZaYq6iMfeYKouRu166VU2xqa1wcaWoxPkrZBG'; + +const MAINNET_GENESIS_HASH = '5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9d'; + /** * Switchboard Devnet Program ID */ @@ -177,6 +181,68 @@ export class SwitchboardProgram { return new SwitchboardProgram(program, cluster, mint); }; + /** + * Create and initialize a {@linkcode SwitchboardProgram} connection object. + * + * @param provider - the anchor provider containing the rpc and wallet connection. + * + * @return the {@linkcode SwitchboardProgram} used to create and interact with Switchboard accounts. + * + * Basic usage example: + * + * ```ts + * import * as anchor from "@project-serum/anchor"; + * import { Connection } from "@solana/web3.js"; + * import { AnchorWallet, SwitchboardProgram, TransactionObject } from '@switchboard-xyz/solana.js'; + * + * const connection = new Connection("https://api.mainnet-beta.solana.com"); + * const provider = new anchor.AnchorProvider( + connection, + new AnchorWallet(payerKeypair ?? SwitchboardProgram._readOnlyKeypair), + { commitment: 'confirmed' } + ); + * const program = await SwitchboardProgram.fromProvider(provider); + * + * const txn = new TransactionObject(program.walletPubkey, [], []); + * const txnSignature = await program.signAndSend(txn); + * ``` + */ + static fromProvider = async ( + provider: anchor.AnchorProvider + ): Promise => { + const payer = (provider.wallet as AnchorWallet).payer; + + // try mainnet program ID + const mainnetAccountInfo = await provider.connection.getAccountInfo( + SBV2_MAINNET_PID + ); + if (mainnetAccountInfo && mainnetAccountInfo.executable) { + return await SwitchboardProgram.load( + 'mainnet-beta', + provider.connection, + payer, + SBV2_MAINNET_PID + ); + } + + // try devnet program ID + const devnetAccountInfo = await provider.connection.getAccountInfo( + SBV2_DEVNET_PID + ); + if (devnetAccountInfo && devnetAccountInfo.executable) { + return await SwitchboardProgram.load( + 'devnet', + provider.connection, + payer, + SBV2_DEVNET_PID + ); + } + + throw new Error( + `Failed to find the Switchboard program using the mainnet or devnet program ID` + ); + }; + /** * The Switchboard Program ID for the currently connected cluster. */