From e0882bfdcbfc66c7633c08fb5dc8c8d73a215dff Mon Sep 17 00:00:00 2001 From: Jackson Jessup Date: Tue, 29 Nov 2022 10:06:00 -0500 Subject: [PATCH] Add SwitchboardError to Solana.js exports --- javascript/solana.js/package-lock.json | 4 +- .../src/accounts/aggregatorAccount.ts | 2 + .../solana.js/src/accounts/leaseAccount.ts | 14 -- javascript/solana.js/src/index.ts | 2 + javascript/solana.js/src/program.ts | 129 +----------------- javascript/solana.js/src/switchboardError.ts | 54 ++++++++ javascript/solana.js/src/switchboardEvents.ts | 120 ++++++++++++++++ 7 files changed, 185 insertions(+), 140 deletions(-) create mode 100644 javascript/solana.js/src/switchboardError.ts create mode 100644 javascript/solana.js/src/switchboardEvents.ts diff --git a/javascript/solana.js/package-lock.json b/javascript/solana.js/package-lock.json index 92ad162..b113463 100644 --- a/javascript/solana.js/package-lock.json +++ b/javascript/solana.js/package-lock.json @@ -1,12 +1,12 @@ { "name": "@switchboard-xyz/solana.js", - "version": "2.0.2", + "version": "2.0.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@switchboard-xyz/solana.js", - "version": "2.0.2", + "version": "2.0.4", "license": "MIT", "dependencies": { "@project-serum/anchor": "^0.25.0", diff --git a/javascript/solana.js/src/accounts/aggregatorAccount.ts b/javascript/solana.js/src/accounts/aggregatorAccount.ts index 2673bc0..3d61c9e 100644 --- a/javascript/solana.js/src/accounts/aggregatorAccount.ts +++ b/javascript/solana.js/src/accounts/aggregatorAccount.ts @@ -183,6 +183,8 @@ export interface AggregatorSaveResultParams { tokenMint: PublicKey; /** * List of parsed oracles. + * + * TODO: Add better typing to this field type */ oracles: Array; } diff --git a/javascript/solana.js/src/accounts/leaseAccount.ts b/javascript/solana.js/src/accounts/leaseAccount.ts index a1bde30..6d9b35a 100644 --- a/javascript/solana.js/src/accounts/leaseAccount.ts +++ b/javascript/solana.js/src/accounts/leaseAccount.ts @@ -312,10 +312,6 @@ export class LeaseAccount extends Account { if (!queueAccountInfo) { throw new errors.AccountNotFoundError(lease.queue); } - const queue: types.OracleQueueAccountData = coder.decode( - QueueAccount.accountName, - queueAccountInfo.data - ); const jobWallets: Array = []; const walletBumps: Array = []; @@ -436,8 +432,6 @@ export class LeaseAccount extends Account { } const lease = await this.loadData(); - const coder = this.program.coder; - const accountInfos = await this.program.connection.getMultipleAccountsInfo([ lease.aggregator, lease.queue, @@ -452,20 +446,12 @@ export class LeaseAccount extends Account { if (!aggregatorAccountInfo) { throw new errors.AccountNotFoundError(lease.aggregator); } - const aggregator: types.AggregatorAccountData = coder.decode( - AggregatorAccount.accountName, - aggregatorAccountInfo.data - ); // decode queue const queueAccountInfo = accountInfos.shift(); if (!queueAccountInfo) { throw new errors.AccountNotFoundError(lease.queue); } - const queue: types.OracleQueueAccountData = coder.decode( - QueueAccount.accountName, - queueAccountInfo.data - ); const [_, leaseBump] = LeaseAccount.fromSeed( this.program, diff --git a/javascript/solana.js/src/index.ts b/javascript/solana.js/src/index.ts index 8077ab3..e4b33bf 100644 --- a/javascript/solana.js/src/index.ts +++ b/javascript/solana.js/src/index.ts @@ -3,5 +3,7 @@ export * from './clock'; export * as types from './generated'; export * from './mint'; export * from './program'; +export * from './switchboardError'; +export * from './switchboardEvents'; export * from './transaction'; export * from './utils'; diff --git a/javascript/solana.js/src/program.ts b/javascript/solana.js/src/program.ts index 0a6d682..ca3f0ee 100644 --- a/javascript/solana.js/src/program.ts +++ b/javascript/solana.js/src/program.ts @@ -1,7 +1,6 @@ import * as anchor from '@project-serum/anchor'; import * as errors from './errors'; import * as sbv2 from './accounts'; -import * as types from './generated'; import { Cluster, Connection, @@ -12,124 +11,8 @@ import { } from '@solana/web3.js'; import { Mint } from './mint'; import { TransactionObject } from './transaction'; +import { SwitchboardEvents } from './switchboardEvents'; -export type SwitchboardEvents = { - AggregatorInitEvent: { - feedPubkey: PublicKey; - }; - VrfRequestRandomnessEvent: { - vrfPubkey: PublicKey; - oraclePubkeys: PublicKey[]; - loadAmount: anchor.BN; - existingAmount: anchor.BN; - }; - VrfRequestEvent: { - vrfPubkey: PublicKey; - oraclePubkeys: PublicKey[]; - }; - VrfProveEvent: { - vrfPubkey: PublicKey; - oraclePubkey: PublicKey; - authorityPubkey: PublicKey; - }; - VrfVerifyEvent: { - vrfPubkey: PublicKey; - oraclePubkey: PublicKey; - authorityPubkey: PublicKey; - amount: anchor.BN; - }; - VrfCallbackPerformedEvent: { - vrfPubkey: PublicKey; - oraclePubkey: PublicKey; - amount: anchor.BN; - }; - AggregatorOpenRoundEvent: { - feedPubkey: PublicKey; - oraclePubkeys: PublicKey[]; - jobPubkeys: PublicKey[]; - remainingFunds: anchor.BN; - queueAuthority: PublicKey; - }; - AggregatorValueUpdateEvent: { - feedPubkey: PublicKey; - value: types.SwitchboardDecimal; - slot: anchor.BN; - timestamp: anchor.BN; - oraclePubkeys: PublicKey[]; - oracleValues: types.SwitchboardDecimal[]; - }; - OracleRewardEvent: { - feedPubkey: PublicKey; - leasePubkey: PublicKey; - oraclePubkey: PublicKey; - walletPubkey: PublicKey; - amount: anchor.BN; - roundSlot: anchor.BN; - timestamp: anchor.BN; - }; - OracleWithdrawEvent: { - oraclePubkey: PublicKey; - walletPubkey: PublicKey; - destinationWallet: PublicKey; - previousAmount: anchor.BN; - newAmount: anchor.BN; - timestamp: anchor.BN; - }; - LeaseWithdrawEvent: { - leasePubkey: PublicKey; - walletPubkey: PublicKey; - previousAmount: anchor.BN; - newAmount: anchor.BN; - timestamp: anchor.BN; - }; - OracleSlashEvent: { - feedPubkey: PublicKey; - leasePubkey: PublicKey; - oraclePubkey: PublicKey; - walletPubkey: PublicKey; - amount: anchor.BN; - roundSlot: anchor.BN; - timestamp: anchor.BN; - }; - LeaseFundEvent: { - leasePubkey: PublicKey; - funder: PublicKey; - amount: anchor.BN; - timestamp: anchor.BN; - }; - ProbationBrokenEvent: { - feedPubkey: PublicKey; - queuePubkey: PublicKey; - timestamp: anchor.BN; - }; - FeedPermissionRevokedEvent: { - feedPubkey: PublicKey; - timestamp: anchor.BN; - }; - GarbageCollectFailureEvent: { - queuePubkey: PublicKey; - }; - OracleBootedEvent: { - queuePubkey: PublicKey; - oraclePubkey: PublicKey; - }; - AggregatorCrankEvictionEvent: {}; - CrankLeaseInsufficientFundsEvent: { - feedPubkey: PublicKey; - leasePubkey: PublicKey; - }; - CrankPopExpectedFailureEvent: { - feedPubkey: PublicKey; - leasePubkey: PublicKey; - }; - BufferRelayerOpenRoundEvent: { - relayerPubkey: PublicKey; - jobPubkey: PublicKey; - oraclePubkeys: PublicKey[]; - remainingFunds: anchor.BN; - queue: PublicKey; - }; -}; /** * Switchboard Devnet Program ID */ @@ -142,6 +25,10 @@ export const SBV2_DEVNET_PID = new PublicKey( export const SBV2_MAINNET_PID = new PublicKey( 'SW1TCH7qEPTdLsDHRgPuMQjbQxKdH2aBStViMFnt64f' ); +/** + * A generated keypair that is assigned as the _payerKeypair_ when in read-only mode. + */ +export const READ_ONLY_KEYPAIR = Keypair.generate(); /** * Returns the Switchboard Program ID for the specified Cluster. */ @@ -167,12 +54,6 @@ const isBrowser = process.env.ANCHOR_BROWSER || (typeof window !== 'undefined' && !window.process?.hasOwnProperty('type')); // eslint-disable-line no-prototype-builtins -// export const READ_ONLY_KEYPAIR = Keypair.fromSecretKey( -// new Uint8Array([...(Array(32).fill(255) as number[])]) -// ); -export const READ_ONLY_KEYPAIR = Keypair.generate(); -export const READ_ONLY_PUBKEY = READ_ONLY_KEYPAIR.publicKey; - /** * Wrapper class for the Switchboard anchor Program. */ diff --git a/javascript/solana.js/src/switchboardError.ts b/javascript/solana.js/src/switchboardError.ts new file mode 100644 index 0000000..0c18eae --- /dev/null +++ b/javascript/solana.js/src/switchboardError.ts @@ -0,0 +1,54 @@ +import { SwitchboardProgram } from './program'; + +/** + * Switchboard wrapper for anchor program errors. + */ +export class SwitchboardError { + /** + * Converts a numerical error code to a SwitchboardError based on the program + * IDL. + * @param program the Switchboard program object containing the program IDL. + * @param code Error code to convert to a SwitchboardError object. + * @return SwitchboardError + */ + static fromCode(program: SwitchboardProgram, code: number): SwitchboardError { + for (const e of program.idl.errors ?? []) { + if (code === e.code) { + return new SwitchboardError(program, e.name, e.code, e.msg); + } + } + throw new Error(`Could not find SwitchboardError for error code ${code}`); + } + + /** + * The program containing the Switchboard IDL specifying error codes. + */ + readonly program: SwitchboardProgram; + + /** + * Stringified name of the error type. + */ + readonly name: string; + + /** + * Numerical SwitchboardError representation. + */ + readonly code: number; + + /** + * Message describing this error in detail. + */ + readonly msg?: string; + + private constructor( + program: SwitchboardProgram, + name: string, + code: number, + msg?: string + ) { + this.program = program; + this.name = name; + this.code = code; + this.msg = msg; + } +} diff --git a/javascript/solana.js/src/switchboardEvents.ts b/javascript/solana.js/src/switchboardEvents.ts new file mode 100644 index 0000000..a00e24f --- /dev/null +++ b/javascript/solana.js/src/switchboardEvents.ts @@ -0,0 +1,120 @@ +import * as anchor from '@project-serum/anchor'; +import * as types from './generated'; + +export type SwitchboardEvents = { + AggregatorInitEvent: { + feedPubkey: anchor.web3.PublicKey; + }; + VrfRequestRandomnessEvent: { + vrfPubkey: anchor.web3.PublicKey; + oraclePubkeys: anchor.web3.PublicKey[]; + loadAmount: anchor.BN; + existingAmount: anchor.BN; + }; + VrfRequestEvent: { + vrfPubkey: anchor.web3.PublicKey; + oraclePubkeys: anchor.web3.PublicKey[]; + }; + VrfProveEvent: { + vrfPubkey: anchor.web3.PublicKey; + oraclePubkey: anchor.web3.PublicKey; + authorityPubkey: anchor.web3.PublicKey; + }; + VrfVerifyEvent: { + vrfPubkey: anchor.web3.PublicKey; + oraclePubkey: anchor.web3.PublicKey; + authorityPubkey: anchor.web3.PublicKey; + amount: anchor.BN; + }; + VrfCallbackPerformedEvent: { + vrfPubkey: anchor.web3.PublicKey; + oraclePubkey: anchor.web3.PublicKey; + amount: anchor.BN; + }; + AggregatorOpenRoundEvent: { + feedPubkey: anchor.web3.PublicKey; + oraclePubkeys: anchor.web3.PublicKey[]; + jobPubkeys: anchor.web3.PublicKey[]; + remainingFunds: anchor.BN; + queueAuthority: anchor.web3.PublicKey; + }; + AggregatorValueUpdateEvent: { + feedPubkey: anchor.web3.PublicKey; + value: types.SwitchboardDecimal; + slot: anchor.BN; + timestamp: anchor.BN; + oraclePubkeys: anchor.web3.PublicKey[]; + oracleValues: types.SwitchboardDecimal[]; + }; + OracleRewardEvent: { + feedPubkey: anchor.web3.PublicKey; + leasePubkey: anchor.web3.PublicKey; + oraclePubkey: anchor.web3.PublicKey; + walletPubkey: anchor.web3.PublicKey; + amount: anchor.BN; + roundSlot: anchor.BN; + timestamp: anchor.BN; + }; + OracleWithdrawEvent: { + oraclePubkey: anchor.web3.PublicKey; + walletPubkey: anchor.web3.PublicKey; + destinationWallet: anchor.web3.PublicKey; + previousAmount: anchor.BN; + newAmount: anchor.BN; + timestamp: anchor.BN; + }; + LeaseWithdrawEvent: { + leasePubkey: anchor.web3.PublicKey; + walletPubkey: anchor.web3.PublicKey; + previousAmount: anchor.BN; + newAmount: anchor.BN; + timestamp: anchor.BN; + }; + OracleSlashEvent: { + feedPubkey: anchor.web3.PublicKey; + leasePubkey: anchor.web3.PublicKey; + oraclePubkey: anchor.web3.PublicKey; + walletPubkey: anchor.web3.PublicKey; + amount: anchor.BN; + roundSlot: anchor.BN; + timestamp: anchor.BN; + }; + LeaseFundEvent: { + leasePubkey: anchor.web3.PublicKey; + funder: anchor.web3.PublicKey; + amount: anchor.BN; + timestamp: anchor.BN; + }; + ProbationBrokenEvent: { + feedPubkey: anchor.web3.PublicKey; + queuePubkey: anchor.web3.PublicKey; + timestamp: anchor.BN; + }; + FeedPermissionRevokedEvent: { + feedPubkey: anchor.web3.PublicKey; + timestamp: anchor.BN; + }; + GarbageCollectFailureEvent: { + queuePubkey: anchor.web3.PublicKey; + }; + OracleBootedEvent: { + queuePubkey: anchor.web3.PublicKey; + oraclePubkey: anchor.web3.PublicKey; + }; + AggregatorCrankEvictionEvent: {}; + CrankLeaseInsufficientFundsEvent: { + feedPubkey: anchor.web3.PublicKey; + leasePubkey: anchor.web3.PublicKey; + }; + CrankPopExpectedFailureEvent: { + feedPubkey: anchor.web3.PublicKey; + leasePubkey: anchor.web3.PublicKey; + }; + BufferRelayerOpenRoundEvent: { + relayerPubkey: anchor.web3.PublicKey; + jobPubkey: anchor.web3.PublicKey; + oraclePubkeys: anchor.web3.PublicKey[]; + remainingFunds: anchor.BN; + queue: anchor.web3.PublicKey; + }; +};