From a8eb9893df8587053ba218a316a8b4f10ec87cda Mon Sep 17 00:00:00 2001 From: Conner Gallagher Date: Thu, 1 Dec 2022 10:49:39 -0700 Subject: [PATCH] solana.js: create methods return new account as first param --- .../src/accounts/aggregatorAccount.ts | 14 +- .../src/accounts/aggregatorHistoryBuffer.ts | 18 +- .../src/accounts/bufferRelayAccount.ts | 10 +- .../solana.js/src/accounts/crankAccount.ts | 10 +- .../solana.js/src/accounts/crankDataBuffer.ts | 4 +- .../solana.js/src/accounts/jobAccount.ts | 10 +- .../solana.js/src/accounts/leaseAccount.ts | 10 +- .../solana.js/src/accounts/oracleAccount.ts | 10 +- .../src/accounts/permissionAccount.ts | 37 +-- .../src/accounts/programStateAccount.ts | 3 +- .../solana.js/src/accounts/queueAccount.ts | 244 +++++++++--------- .../solana.js/src/accounts/queueDataBuffer.ts | 14 +- .../solana.js/src/accounts/vrfAccount.ts | 16 +- javascript/solana.js/test/aggregator.spec.ts | 114 ++++---- javascript/solana.js/test/job.spec.ts | 2 +- javascript/solana.js/test/mint.spec.ts | 28 +- javascript/solana.js/test/open-round.spec.ts | 92 +++++++ javascript/solana.js/test/queue.spec.ts | 90 +++---- 18 files changed, 400 insertions(+), 326 deletions(-) create mode 100644 javascript/solana.js/test/open-round.spec.ts diff --git a/javascript/solana.js/src/accounts/aggregatorAccount.ts b/javascript/solana.js/src/accounts/aggregatorAccount.ts index 8be7078..7c354d9 100644 --- a/javascript/solana.js/src/accounts/aggregatorAccount.ts +++ b/javascript/solana.js/src/accounts/aggregatorAccount.ts @@ -125,7 +125,7 @@ export class AggregatorAccount extends Account { * * ```ts * import {AggregatorAccount} from '@switchboard-xyz/solana.js'; - * const [aggregatorInit, aggregatorAccount] = await AggregatorAccount.createInstruction(program, payer, { + * const [aggregatorAccount, aggregatorInit ] = await AggregatorAccount.createInstruction(program, payer, { * queueAccount, * queueAuthority, * batchSize: 5, @@ -140,7 +140,7 @@ export class AggregatorAccount extends Account { program: SwitchboardProgram, payer: PublicKey, params: AggregatorInitParams - ): Promise<[TransactionObject, AggregatorAccount]> { + ): Promise<[AggregatorAccount, TransactionObject]> { const keypair = params.keypair ?? Keypair.generate(); program.verifyNewKeypair(keypair); @@ -194,7 +194,7 @@ export class AggregatorAccount extends Account { const aggregatorInit = new TransactionObject(payer, ixns, signers); const aggregatorAccount = new AggregatorAccount(program, keypair.publicKey); - return [aggregatorInit, aggregatorAccount]; + return [aggregatorAccount, aggregatorInit]; } /** @@ -207,7 +207,7 @@ export class AggregatorAccount extends Account { * * ```ts * import {AggregatorAccount} from '@switchboard-xyz/solana.js'; - * const [txnSignature, aggregatorAccount] = await AggregatorAccount.create(program, { + * const [aggregatorAccount, txnSignature] = await AggregatorAccount.create(program, { * queueAccount, * queueAuthority, * batchSize: 5, @@ -221,14 +221,14 @@ export class AggregatorAccount extends Account { public static async create( program: SwitchboardProgram, params: AggregatorInitParams - ): Promise<[TransactionSignature, AggregatorAccount]> { - const [transaction, account] = await AggregatorAccount.createInstruction( + ): Promise<[AggregatorAccount, TransactionSignature]> { + const [account, transaction] = await AggregatorAccount.createInstruction( program, program.walletPubkey, params ); const txnSignature = await program.signAndSend(transaction); - return [txnSignature, account]; + return [account, txnSignature]; } public getAccounts(params: { diff --git a/javascript/solana.js/src/accounts/aggregatorHistoryBuffer.ts b/javascript/solana.js/src/accounts/aggregatorHistoryBuffer.ts index 50e643c..2dcaeb0 100644 --- a/javascript/solana.js/src/accounts/aggregatorHistoryBuffer.ts +++ b/javascript/solana.js/src/accounts/aggregatorHistoryBuffer.ts @@ -147,10 +147,10 @@ export class AggregatorHistoryBuffer extends Account< * Basic usage example: * * ```ts - * import {AggregatorAccount,AggregatorHistoryBuffer} from '@switchboard-xyz/solana.js'; + * import { AggregatorAccount,AggregatorHistoryBuffer } from '@switchboard-xyz/solana.js'; * const aggregatorAccount = new AggregatorAccount(program, aggregatorKey); * const aggregator = await aggregatorAccount.loadData(); - * const [addHistoryTxn, historyBuffer] = await AggregatorHistoryBuffer.createInstructions(program, payer, { + * const [historyBuffer, addHistoryTxn] = await AggregatorHistoryBuffer.createInstructions(program, payer, { * aggregatorAccount, * maxSamples: 10000, * }); @@ -162,7 +162,7 @@ export class AggregatorHistoryBuffer extends Account< program: SwitchboardProgram, payer: PublicKey, params: AggregatorHistoryInit - ): Promise<[TransactionObject, AggregatorHistoryBuffer]> { + ): Promise<[AggregatorHistoryBuffer, TransactionObject]> { const buffer = params.keypair ?? Keypair.generate(); program.verifyNewKeypair(buffer); @@ -199,8 +199,8 @@ export class AggregatorHistoryBuffer extends Account< ); return [ - new TransactionObject(payer, ixns, signers), new AggregatorHistoryBuffer(program, buffer.publicKey), + new TransactionObject(payer, ixns, signers), ]; } @@ -214,10 +214,10 @@ export class AggregatorHistoryBuffer extends Account< * Basic usage example: * * ```ts - * import {AggregatorAccount,AggregatorHistoryBuffer} from '@switchboard-xyz/solana.js'; + * import { AggregatorAccount,AggregatorHistoryBuffer } from '@switchboard-xyz/solana.js'; * const aggregatorAccount = new AggregatorAccount(program, aggregatorKey); * const aggregator = await aggregatorAccount.loadData(); - * const [addHistorySignature, historyBuffer] = await AggregatorHistoryBuffer.create(program, { + * const [historyBuffer, addHistorySignature] = await AggregatorHistoryBuffer.create(program, { * aggregatorAccount, * maxSamples: 10000, * }); @@ -227,14 +227,14 @@ export class AggregatorHistoryBuffer extends Account< public static async create( program: SwitchboardProgram, params: AggregatorHistoryInit - ): Promise<[TransactionSignature, AggregatorHistoryBuffer]> { - const [transaction, account] = + ): Promise<[AggregatorHistoryBuffer, TransactionSignature]> { + const [account, transaction] = await AggregatorHistoryBuffer.createInstructions( program, program.walletPubkey, params ); const txnSignature = await program.signAndSend(transaction); - return [txnSignature, account]; + return [account, txnSignature]; } } diff --git a/javascript/solana.js/src/accounts/bufferRelayAccount.ts b/javascript/solana.js/src/accounts/bufferRelayAccount.ts index 011313e..11005bd 100644 --- a/javascript/solana.js/src/accounts/bufferRelayAccount.ts +++ b/javascript/solana.js/src/accounts/bufferRelayAccount.ts @@ -92,7 +92,7 @@ export class BufferRelayerAccount extends Account { + ): Promise<[BufferRelayerAccount, TransactionObject]> { const keypair = params.keypair ?? Keypair.generate(); program.verifyNewKeypair(keypair); @@ -143,8 +143,8 @@ export class BufferRelayerAccount extends Account { - const [bufferInit, bufferAccount] = + ): Promise<[BufferRelayerAccount, TransactionSignature]> { + const [bufferAccount, bufferInit] = await BufferRelayerAccount.createInstructions( program, program.walletPubkey, params ); const txnSignature = await program.signAndSend(bufferInit); - return [txnSignature, bufferAccount]; + return [bufferAccount, txnSignature]; } public async openRoundInstructions( diff --git a/javascript/solana.js/src/accounts/crankAccount.ts b/javascript/solana.js/src/accounts/crankAccount.ts index a441ad4..6f2f580 100644 --- a/javascript/solana.js/src/accounts/crankAccount.ts +++ b/javascript/solana.js/src/accounts/crankAccount.ts @@ -72,7 +72,7 @@ export class CrankAccount extends Account { program: SwitchboardProgram, payer: PublicKey, params: CrankInitParams - ): Promise<[TransactionObject, CrankAccount]> { + ): Promise<[CrankAccount, TransactionObject]> { const crankAccount = params.keypair ?? Keypair.generate(); program.verifyNewKeypair(crankAccount); @@ -115,20 +115,20 @@ export class CrankAccount extends Account { [crankAccount, buffer] ); - return [crankInit, new CrankAccount(program, crankAccount.publicKey)]; + return [new CrankAccount(program, crankAccount.publicKey), crankInit]; } public static async create( program: SwitchboardProgram, params: CrankInitParams - ): Promise<[TransactionSignature, CrankAccount]> { - const [crankInit, crankAccount] = await CrankAccount.createInstructions( + ): Promise<[CrankAccount, TransactionSignature]> { + const [crankAccount, crankInit] = await CrankAccount.createInstructions( program, program.walletPubkey, params ); const txnSignature = await program.signAndSend(crankInit); - return [txnSignature, crankAccount]; + return [crankAccount, txnSignature]; } public async popInstruction( diff --git a/javascript/solana.js/src/accounts/crankDataBuffer.ts b/javascript/solana.js/src/accounts/crankDataBuffer.ts index ad6b195..e86ed7e 100644 --- a/javascript/solana.js/src/accounts/crankDataBuffer.ts +++ b/javascript/solana.js/src/accounts/crankDataBuffer.ts @@ -84,7 +84,9 @@ export class CrankDataBuffer extends Account> { } /** - * Return an aggregator's assigned history buffer or undefined if it doesn't exist. + * Return a crank's dataBuffer + * + * @throws {string} if dataBuffer is equal to default publicKey */ static fromCrank( program: SwitchboardProgram, diff --git a/javascript/solana.js/src/accounts/jobAccount.ts b/javascript/solana.js/src/accounts/jobAccount.ts index 90da247..b0fdb97 100644 --- a/javascript/solana.js/src/accounts/jobAccount.ts +++ b/javascript/solana.js/src/accounts/jobAccount.ts @@ -51,7 +51,7 @@ export class JobAccount extends Account { program: SwitchboardProgram, payer: PublicKey, params: JobInitParams - ): [Array, JobAccount] { + ): [JobAccount, Array] { if (params.data.byteLength > 6400) { throw new Error('Switchboard jobs need to be less than 6400 bytes'); } @@ -138,20 +138,20 @@ export class JobAccount extends Account { } } - return [txns, new JobAccount(program, jobKeypair.publicKey)]; + return [new JobAccount(program, jobKeypair.publicKey), txns]; } public static async create( program: SwitchboardProgram, params: JobInitParams - ): Promise<[Array, JobAccount]> { - const [transactions, account] = JobAccount.createInstructions( + ): Promise<[JobAccount, Array]> { + const [account, transactions] = JobAccount.createInstructions( program, program.walletPubkey, params ); const txSignature = await program.signAndSendAll(transactions); - return [txSignature, account]; + return [account, txSignature]; } decode(data: Buffer): types.JobAccountData { diff --git a/javascript/solana.js/src/accounts/leaseAccount.ts b/javascript/solana.js/src/accounts/leaseAccount.ts index e8b7631..8924090 100644 --- a/javascript/solana.js/src/accounts/leaseAccount.ts +++ b/javascript/solana.js/src/accounts/leaseAccount.ts @@ -102,7 +102,7 @@ export class LeaseAccount extends Account { withdrawAuthority?: PublicKey; jobAuthorities: Array; } - ): Promise<[TransactionObject, LeaseAccount]> { + ): Promise<[LeaseAccount, TransactionObject]> { const ixns: TransactionInstruction[] = []; const signers: Keypair[] = []; @@ -183,7 +183,7 @@ export class LeaseAccount extends Account { ) ); - return [new TransactionObject(payer, ixns, signers), leaseAccount]; + return [leaseAccount, new TransactionObject(payer, ixns, signers)]; } public static async create( @@ -198,8 +198,8 @@ export class LeaseAccount extends Account { withdrawAuthority?: PublicKey; jobAuthorities: Array; } - ): Promise<[TransactionSignature, LeaseAccount]> { - const [transaction, leaseAccount] = await LeaseAccount.createInstructions( + ): Promise<[LeaseAccount, TransactionSignature]> { + const [leaseAccount, transaction] = await LeaseAccount.createInstructions( program, program.walletPubkey, { @@ -211,7 +211,7 @@ export class LeaseAccount extends Account { const signature = await program.signAndSend(transaction); - return [signature, leaseAccount]; + return [leaseAccount, signature]; } public async getBalance(): Promise { diff --git a/javascript/solana.js/src/accounts/oracleAccount.ts b/javascript/solana.js/src/accounts/oracleAccount.ts index b169299..82e859f 100644 --- a/javascript/solana.js/src/accounts/oracleAccount.ts +++ b/javascript/solana.js/src/accounts/oracleAccount.ts @@ -48,7 +48,7 @@ export class OracleAccount extends Account { params: { queueAccount: QueueAccount; } & OracleInitParams - ): Promise<[TransactionObject, OracleAccount]> { + ): Promise<[OracleAccount, TransactionObject]> { const tokenWallet = Keypair.generate(); // console.log(`tokenWallet`, tokenWallet.publicKey.toBase58()); @@ -108,12 +108,12 @@ export class OracleAccount extends Account { ]; return [ + new OracleAccount(program, oracleAccount.publicKey), new TransactionObject( payer, ixns, params.authority ? [tokenWallet, params.authority] : [tokenWallet] ), - new OracleAccount(program, oracleAccount.publicKey), ]; } @@ -122,8 +122,8 @@ export class OracleAccount extends Account { params: { queueAccount: QueueAccount; } & OracleInitParams - ): Promise<[TransactionSignature, OracleAccount]> { - const [txnObject, oracleAccount] = await OracleAccount.createInstructions( + ): Promise<[OracleAccount, TransactionSignature]> { + const [oracleAccount, txnObject] = await OracleAccount.createInstructions( program, program.walletPubkey, params @@ -131,7 +131,7 @@ export class OracleAccount extends Account { const txnSignature = await program.signAndSend(txnObject); - return [txnSignature, oracleAccount]; + return [oracleAccount, txnSignature]; } /** diff --git a/javascript/solana.js/src/accounts/permissionAccount.ts b/javascript/solana.js/src/accounts/permissionAccount.ts index 9d20e80..4eb5676 100644 --- a/javascript/solana.js/src/accounts/permissionAccount.ts +++ b/javascript/solana.js/src/accounts/permissionAccount.ts @@ -1,5 +1,10 @@ import * as anchor from '@project-serum/anchor'; -import { Keypair, PublicKey, SystemProgram } from '@solana/web3.js'; +import { + Keypair, + PublicKey, + SystemProgram, + TransactionSignature, +} from '@solana/web3.js'; import * as errors from '../errors'; import * as types from '../generated'; import { SwitchboardProgram } from '../program'; @@ -60,24 +65,11 @@ export class PermissionAccount extends Account { return [new PermissionAccount(program, publicKey), bump]; } - public static async create( - program: SwitchboardProgram, - params: PermissionAccountInitParams - ): Promise<[string, PermissionAccount]> { - const [txnObject, account] = this.createInstruction( - program, - program.walletPubkey, - params - ); - const txSignature = await program.signAndSend(txnObject); - return [txSignature, account]; - } - public static createInstruction( program: SwitchboardProgram, payer: PublicKey, params: PermissionAccountInitParams - ): [TransactionObject, PermissionAccount] { + ): [PermissionAccount, TransactionObject] { const [account] = PermissionAccount.fromSeed( program, params.authority, @@ -96,7 +88,20 @@ export class PermissionAccount extends Account { payer, } ); - return [new TransactionObject(payer, [instruction], []), account]; + return [account, new TransactionObject(payer, [instruction], [])]; + } + + public static async create( + program: SwitchboardProgram, + params: PermissionAccountInitParams + ): Promise<[PermissionAccount, TransactionSignature]> { + const [account, txnObject] = this.createInstruction( + program, + program.walletPubkey, + params + ); + const txSignature = await program.signAndSend(txnObject); + return [account, txSignature]; } /** diff --git a/javascript/solana.js/src/accounts/programStateAccount.ts b/javascript/solana.js/src/accounts/programStateAccount.ts index f33f96d..eddec95 100644 --- a/javascript/solana.js/src/accounts/programStateAccount.ts +++ b/javascript/solana.js/src/accounts/programStateAccount.ts @@ -8,6 +8,7 @@ import { Mint } from '../mint'; import { PublicKey, SystemProgram, + TransactionInstruction, TransactionSignature, } from '@solana/web3.js'; import { TransactionObject } from '../transaction'; @@ -103,7 +104,7 @@ export class ProgramStateAccount extends Account { program: SwitchboardProgram, mint = Mint.native, daoMint = Mint.native - ): [ProgramStateAccount, anchor.web3.TransactionInstruction] { + ): [ProgramStateAccount, TransactionInstruction] { const [programStateAccount, stateBump] = ProgramStateAccount.fromSeed(program); diff --git a/javascript/solana.js/src/accounts/queueAccount.ts b/javascript/solana.js/src/accounts/queueAccount.ts index 92e3151..89f678a 100644 --- a/javascript/solana.js/src/accounts/queueAccount.ts +++ b/javascript/solana.js/src/accounts/queueAccount.ts @@ -113,8 +113,8 @@ export class QueueAccount extends Account { * Basic usage example: * * ```ts - * import {QueueAccount} from '@switchboard-xyz/solana.js'; - * const [queueInitTxn, queueAccount] = await QueueAccount.createInstructions(program, payer, { + * import { QueueAccount } from '@switchboard-xyz/solana.js'; + * const [queueAccount, queueInitTxn] = await QueueAccount.createInstructions(program, payer, { name: 'My Queue', metadata: 'Top Secret', queueSize: 100, @@ -134,7 +134,7 @@ export class QueueAccount extends Account { program: SwitchboardProgram, payer: PublicKey, params: QueueInitParams - ): Promise<[TransactionObject, QueueAccount]> { + ): Promise<[QueueAccount, TransactionObject]> { const queueKeypair = params.keypair ?? Keypair.generate(); program.verifyNewKeypair(queueKeypair); @@ -148,66 +148,64 @@ export class QueueAccount extends Account { const reward = program.mint.toTokenAmountBN(params.reward); const minStake = program.mint.toTokenAmountBN(params.minStake); - return [ - new TransactionObject( - payer, - [ - SystemProgram.createAccount({ - fromPubkey: program.wallet.publicKey, - newAccountPubkey: dataBuffer.publicKey, - space: queueDataSize, - lamports: - await program.connection.getMinimumBalanceForRentExemption( - queueDataSize - ), - programId: program.programId, - }), - types.oracleQueueInit( - program, - { - params: { - name: Array.from( - new Uint8Array(Buffer.from(params.name ?? '').slice(0, 32)) - ), - metadata: [ - ...new Uint8Array( - Buffer.from(params.metadata ?? '').slice(0, 64) - ), - ], - reward: reward, - minStake: minStake, - feedProbationPeriod: params.feedProbationPeriod ?? 0, - oracleTimeout: params.oracleTimeout ?? 180, - slashingEnabled: params.slashingEnabled ?? false, - varianceToleranceMultiplier: SwitchboardDecimal.fromBig( - new Big(params.varianceToleranceMultiplier ?? 2) - ), - consecutiveFeedFailureLimit: new anchor.BN( - params.consecutiveFeedFailureLimit ?? 1000 - ), - consecutiveOracleFailureLimit: new anchor.BN( - params.consecutiveOracleFailureLimit ?? 1000 - ), - queueSize: queueSize, - unpermissionedFeeds: params.unpermissionedFeeds ?? false, - unpermissionedVrf: params.unpermissionedVrf ?? false, - enableBufferRelayers: params.enableBufferRelayers ?? false, - }, - }, - { - oracleQueue: account.publicKey, - authority: params.authority ?? payer, - buffer: dataBuffer.publicKey, - systemProgram: SystemProgram.programId, - payer, - mint: program.mint.address, - } + const txn = new TransactionObject( + payer, + [ + SystemProgram.createAccount({ + fromPubkey: program.wallet.publicKey, + newAccountPubkey: dataBuffer.publicKey, + space: queueDataSize, + lamports: await program.connection.getMinimumBalanceForRentExemption( + queueDataSize ), - ], - [dataBuffer, queueKeypair] - ), - account, - ]; + programId: program.programId, + }), + types.oracleQueueInit( + program, + { + params: { + name: Array.from( + new Uint8Array(Buffer.from(params.name ?? '').slice(0, 32)) + ), + metadata: [ + ...new Uint8Array( + Buffer.from(params.metadata ?? '').slice(0, 64) + ), + ], + reward: reward, + minStake: minStake, + feedProbationPeriod: params.feedProbationPeriod ?? 0, + oracleTimeout: params.oracleTimeout ?? 180, + slashingEnabled: params.slashingEnabled ?? false, + varianceToleranceMultiplier: SwitchboardDecimal.fromBig( + new Big(params.varianceToleranceMultiplier ?? 2) + ), + consecutiveFeedFailureLimit: new anchor.BN( + params.consecutiveFeedFailureLimit ?? 1000 + ), + consecutiveOracleFailureLimit: new anchor.BN( + params.consecutiveOracleFailureLimit ?? 1000 + ), + queueSize: queueSize, + unpermissionedFeeds: params.unpermissionedFeeds ?? false, + unpermissionedVrf: params.unpermissionedVrf ?? false, + enableBufferRelayers: params.enableBufferRelayers ?? false, + }, + }, + { + oracleQueue: account.publicKey, + authority: params.authority ?? payer, + buffer: dataBuffer.publicKey, + systemProgram: SystemProgram.programId, + payer, + mint: program.mint.address, + } + ), + ], + [dataBuffer, queueKeypair] + ); + + return [account, txn]; } /** @@ -222,8 +220,8 @@ export class QueueAccount extends Account { * Basic usage example: * * ```ts - * import {QueueAccount} from '@switchboard-xyz/solana.js'; - * const [txnSignature, queueAccount] = await QueueAccount.create(program, { + * import { QueueAccount } from '@switchboard-xyz/solana.js'; + * const [queueAccount, txnSignature] = await QueueAccount.create(program, { name: 'My Queue', metadata: 'Top Secret', queueSize: 100, @@ -241,14 +239,14 @@ export class QueueAccount extends Account { public static async create( program: SwitchboardProgram, params: QueueInitParams - ): Promise<[string, QueueAccount]> { - const [txnObject, account] = await this.createInstructions( + ): Promise<[QueueAccount, string]> { + const [account, txnObject] = await this.createInstructions( program, program.walletPubkey, params ); const txnSignature = await program.signAndSend(txnObject); - return [txnSignature, account]; + return [account, txnSignature]; } /** @@ -263,9 +261,9 @@ export class QueueAccount extends Account { * Basic usage example: * * ```ts - * import {QueueAccount} from '@switchboard-xyz/solana.js'; + * import { QueueAccount } from '@switchboard-xyz/solana.js'; * const queueAccount = new QueueAccount(program, queuePubkey); - * const [oracleInitTxn, oracleAccount] = await queueAccount.createOracleInstructions(payer, { + * const [oracleAccount, oracleInitTxn] = await queueAccount.createOracleInstructions(payer, { * name: "My Oracle", * metadata: "Oracle #1" * }); @@ -277,16 +275,16 @@ export class QueueAccount extends Account { /** The publicKey of the account that will pay for the new accounts. Will also be used as the account authority if no other authority is provided. */ payer: PublicKey, params: OracleInitParams & Partial> - ): Promise<[TransactionObject, OracleAccount]> { + ): Promise<[OracleAccount, TransactionObject]> { const queue = await this.loadData(); - const [createOracleTxnObject, oracleAccount] = + const [oracleAccount, createOracleTxnObject] = await OracleAccount.createInstructions(this.program, payer, { ...params, queueAccount: this, }); - const [createPermissionTxnObject, permissionAccount] = + const [permissionAccount, createPermissionTxnObject] = PermissionAccount.createInstruction(this.program, payer, { granter: this.publicKey, grantee: oracleAccount.publicKey, @@ -303,8 +301,8 @@ export class QueueAccount extends Account { } return [ - createOracleTxnObject.combine(createPermissionTxnObject), oracleAccount, + createOracleTxnObject.combine(createPermissionTxnObject), ]; } @@ -318,9 +316,9 @@ export class QueueAccount extends Account { * Basic usage example: * * ```ts - * import {QueueAccount} from '@switchboard-xyz/solana.js'; + * import { QueueAccount } from '@switchboard-xyz/solana.js'; * const queueAccount = new QueueAccount(program, queuePubkey); - * const [oracleInitSignature, oracleAccount] = await queueAccount.createOracle({ + * const [oracleAccount, oracleInitSignature] = await queueAccount.createOracle({ * name: "My Oracle", * metadata: "Oracle #1" * }); @@ -329,7 +327,7 @@ export class QueueAccount extends Account { */ public async createOracle( params: OracleInitParams & Partial> - ): Promise<[TransactionSignature, OracleAccount]> { + ): Promise<[OracleAccount, TransactionSignature]> { const signers: Keypair[] = []; const queue = await this.loadData(); @@ -341,14 +339,14 @@ export class QueueAccount extends Account { signers.push(params.queueAuthority); } - const [txn, oracleAccount] = await this.createOracleInstructions( + const [oracleAccount, txn] = await this.createOracleInstructions( this.program.walletPubkey, params ); const signature = await this.program.signAndSend(txn); - return [signature, oracleAccount]; + return [oracleAccount, signature]; } /** @@ -363,9 +361,9 @@ export class QueueAccount extends Account { * Optionally, enable the permissions by setting a queueAuthority keypair along with the enable boolean set to true. * * ```ts - * import {QueueAccount} from '@switchboard-xyz/solana.js'; + * import { QueueAccount } from '@switchboard-xyz/solana.js'; * const queueAccount = new QueueAccount(program, queuePubkey); - * const [aggregatorInitTxnObject, aggregatorAccount] = + * const [aggregatorAccount, aggregatorInitTxnObject] = await queueAccount.createFeedInstructions({ enable: true, // not needed if queue has unpermissionedFeedsEnabled queueAuthority: queueAuthority, // not needed if queue has unpermissionedFeedsEnabled @@ -413,7 +411,7 @@ export class QueueAccount extends Account { // job params jobs?: Array<{ pubkey: PublicKey; weight?: number } | JobInitParams>; } - ): Promise<[TransactionObject[], AggregatorAccount]> { + ): Promise<[AggregatorAccount, TransactionObject[]]> { const queue = await this.loadData(); const pre: TransactionObject[] = []; @@ -437,7 +435,7 @@ export class QueueAccount extends Account { if (params.jobs && Array.isArray(params.jobs)) { for await (const job of params.jobs) { if ('data' in job) { - const [jobInit, jobAccount] = JobAccount.createInstructions( + const [jobAccount, jobInit] = JobAccount.createInstructions( this.program, payer, { @@ -461,7 +459,7 @@ export class QueueAccount extends Account { } } - const [aggregatorInit, aggregatorAccount] = + const [aggregatorAccount, aggregatorInit] = await AggregatorAccount.createInstruction(this.program, payer, { ...params, queueAccount: this, @@ -472,7 +470,7 @@ export class QueueAccount extends Account { txns.push(aggregatorInit); - const [leaseInit] = await LeaseAccount.createInstructions( + const [leaseAccount, leaseInit] = await LeaseAccount.createInstructions( this.program, payer, { @@ -487,7 +485,7 @@ export class QueueAccount extends Account { txns.push(leaseInit); // create permission account - const [permissionInit, permissionAccount] = + const [permissionAccount, permissionInit] = PermissionAccount.createInstruction(this.program, payer, { granter: this.publicKey, authority: queue.authority, @@ -525,7 +523,7 @@ export class QueueAccount extends Account { } if (params.historyLimit && params.historyLimit > 0) { - const [historyBufferInit] = + const [historyBuffer, historyBufferInit] = await AggregatorHistoryBuffer.createInstructions(this.program, payer, { aggregatorAccount, maxSamples: params.historyLimit, @@ -539,7 +537,7 @@ export class QueueAccount extends Account { ...TransactionObject.pack(post), ]); - return [packed, aggregatorAccount]; + return [aggregatorAccount, packed]; } /** @@ -550,9 +548,9 @@ export class QueueAccount extends Account { * Optionally, enable the permissions by setting a queueAuthority keypair along with the enable boolean set to true. * * ```ts - * import {QueueAccount} from '@switchboard-xyz/solana.js'; + * import { QueueAccount } from '@switchboard-xyz/solana.js'; * const queueAccount = new QueueAccount(program, queuePubkey); - * const [aggregatorInitSignatures, aggregatorAccount] = + * const [aggregatorAccount, aggregatorInitSignatures] = await queueAccount.createFeed({ enable: true, // not needed if queue has unpermissionedFeedsEnabled queueAuthority: queueAuthority, // not needed if queue has unpermissionedFeedsEnabled @@ -598,7 +596,7 @@ export class QueueAccount extends Account { // job params jobs?: Array<{ pubkey: PublicKey; weight?: number } | JobInitParams>; } - ): Promise<[Array, AggregatorAccount]> { + ): Promise<[AggregatorAccount, Array]> { const signers: Keypair[] = []; const queue = await this.loadData(); @@ -610,14 +608,14 @@ export class QueueAccount extends Account { signers.push(params.queueAuthority); } - const [txns, aggregatorAccount] = await this.createFeedInstructions( + const [aggregatorAccount, txns] = await this.createFeedInstructions( this.program.walletPubkey, params ); const signatures = await this.program.signAndSendAll(txns); - return [signatures, aggregatorAccount]; + return [aggregatorAccount, signatures]; } /** @@ -632,9 +630,9 @@ export class QueueAccount extends Account { * Basic usage example: * * ```ts - * import {QueueAccount} from '@switchboard-xyz/solana.js'; + * import { QueueAccount } from '@switchboard-xyz/solana.js'; * const queueAccount = new QueueAccount(program, queuePubkey); - * const [crankInitTxn, crankAccount] = await queueAccount.createCrankInstructions(payer, { + * const [crankAccount, crankInitTxn] = await queueAccount.createCrankInstructions(payer, { * name: "My Crank", * metadata: "Crank #1", * maxRows: 1000, @@ -646,7 +644,7 @@ export class QueueAccount extends Account { public async createCrankInstructions( payer: PublicKey, params: Omit - ): Promise<[TransactionObject, CrankAccount]> { + ): Promise<[CrankAccount, TransactionObject]> { return await CrankAccount.createInstructions(this.program, payer, { ...params, queueAccount: this, @@ -663,9 +661,9 @@ export class QueueAccount extends Account { * Basic usage example: * * ```ts - * import {QueueAccount} from '@switchboard-xyz/solana.js'; + * import { QueueAccount } from '@switchboard-xyz/solana.js'; * const queueAccount = new QueueAccount(program, queuePubkey); - * const [crankInitSignature, crankAccount] = await queueAccount.createCrank({ + * const [crankAccount, crankInitSignature] = await queueAccount.createCrank({ * name: "My Crank", * metadata: "Crank #1", * maxRows: 1000, @@ -675,13 +673,13 @@ export class QueueAccount extends Account { */ public async createCrank( params: Omit - ): Promise<[TransactionSignature, CrankAccount]> { - const [txn, crankAccount] = await this.createCrankInstructions( + ): Promise<[CrankAccount, TransactionSignature]> { + const [crankAccount, txn] = await this.createCrankInstructions( this.program.walletPubkey, params ); const txnSignature = await this.program.signAndSend(txn); - return [txnSignature, crankAccount]; + return [crankAccount, txnSignature]; } /** @@ -696,10 +694,10 @@ export class QueueAccount extends Account { * Basic usage example: * * ```ts - * import {QueueAccount} from '@switchboard-xyz/solana.js'; + * import { QueueAccount } from '@switchboard-xyz/solana.js'; * const queueAccount = new QueueAccount(program, queuePubkey); * const vrfKeypair = Keypair.generate(); - * const [vrfInitTxn, vrfAccount] = await queueAccount.createVrfInstructions(payer, { + * const [vrfAccount, vrfInitTxn] = await queueAccount.createVrfInstructions(payer, { * vrfKeypair: vrfKeypair, * callback: { * programId: "", @@ -715,10 +713,10 @@ export class QueueAccount extends Account { payer: PublicKey, params: Omit & Partial> - ): Promise<[TransactionObject, VrfAccount]> { + ): Promise<[VrfAccount, TransactionObject]> { const queue = await this.loadData(); - const [vrfInit, vrfAccount] = await VrfAccount.createInstructions( + const [vrfAccount, vrfInit] = await VrfAccount.createInstructions( this.program, payer, { @@ -730,7 +728,7 @@ export class QueueAccount extends Account { ); // eslint-disable-next-line prefer-const - let [permissionInit, permissionAccount] = + let [permissionAccount, permissionInit] = PermissionAccount.createInstruction(this.program, payer, { granter: this.publicKey, grantee: vrfAccount.publicKey, @@ -748,7 +746,7 @@ export class QueueAccount extends Account { } } - return [vrfInit.combine(permissionInit), vrfAccount]; + return [vrfAccount, vrfInit.combine(permissionInit)]; } /** @@ -761,10 +759,10 @@ export class QueueAccount extends Account { * Basic usage example: * * ```ts - * import {QueueAccount} from '@switchboard-xyz/solana.js'; + * import { QueueAccount } from '@switchboard-xyz/solana.js'; * const queueAccount = new QueueAccount(program, queuePubkey); * const vrfKeypair = Keypair.generate(); - * const [vrfInitSignature, vrfAccount] = await queueAccount.createVrf({ + * const [vrfAccount, vrfInitSignature] = await queueAccount.createVrf({ * vrfKeypair: vrfKeypair, * callback: { * programId: "", @@ -778,13 +776,13 @@ export class QueueAccount extends Account { public async createVrf( params: Omit & Partial> - ): Promise<[TransactionSignature, VrfAccount]> { - const [txn, vrfAccount] = await this.createVrfInstructions( + ): Promise<[VrfAccount, TransactionSignature]> { + const [vrfAccount, txn] = await this.createVrfInstructions( this.program.walletPubkey, params ); const txnSignature = await this.program.signAndSend(txn); - return [txnSignature, vrfAccount]; + return [vrfAccount, txnSignature]; } /** @@ -799,9 +797,9 @@ export class QueueAccount extends Account { * Basic usage example: * * ```ts - * import {QueueAccount} from '@switchboard-xyz/solana.js'; + * import { QueueAccount } from '@switchboard-xyz/solana.js'; * const queueAccount = new QueueAccount(program, queuePubkey); - * const [bufferRelayerInitTxn, bufferRelayerAccount] = await queueAccount.createBufferRelayerInstructions(payer, { + * const [bufferRelayerAccount, bufferRelayerInitTxn] = await queueAccount.createBufferRelayerInstructions(payer, { * name: "My Buffer", * minUpdateDelaySeconds: 30, * job: existingJobPubkey, @@ -817,14 +815,14 @@ export class QueueAccount extends Account { // job params job: JobAccount | PublicKey | Omit; } - ): Promise<[TransactionObject, BufferRelayerAccount]> { + ): Promise<[BufferRelayerAccount, TransactionObject]> { const queue = await this.loadData(); const txns: TransactionObject[] = []; let job: JobAccount; if ('data' in params.job) { - const [jobInit, jobAccount] = JobAccount.createInstructions( + const [jobAccount, jobInit] = JobAccount.createInstructions( this.program, payer, { @@ -850,7 +848,7 @@ export class QueueAccount extends Account { ); } - const [bufferInit, bufferAccount] = + const [bufferAccount, bufferInit] = await BufferRelayerAccount.createInstructions(this.program, payer, { name: params.name, minUpdateDelaySeconds: params.minUpdateDelaySeconds, @@ -863,7 +861,7 @@ export class QueueAccount extends Account { txns.push(bufferInit); // eslint-disable-next-line prefer-const - let [permissionInit, permissionAccount] = + let [permissionAccount, permissionInit] = PermissionAccount.createInstruction(this.program, payer, { granter: this.publicKey, grantee: bufferAccount.publicKey, @@ -890,7 +888,7 @@ export class QueueAccount extends Account { ); } - return [packed[0], bufferAccount]; + return [bufferAccount, packed[0]]; } /** @@ -903,9 +901,9 @@ export class QueueAccount extends Account { * Basic usage example: * * ```ts - * import {QueueAccount} from '@switchboard-xyz/solana.js'; + * import { QueueAccount } from '@switchboard-xyz/solana.js'; * const queueAccount = new QueueAccount(program, queuePubkey); - * const [bufferRelayerInitSignature, bufferRelayerAccount] = await queueAccount.createBufferRelayer({ + * const [bufferRelayerAccount, bufferRelayerInitSignature] = await queueAccount.createBufferRelayer({ * name: "My Buffer", * minUpdateDelaySeconds: 30, * job: existingJobPubkey, @@ -919,14 +917,14 @@ export class QueueAccount extends Account { // job params job: JobAccount | PublicKey | Omit; } - ): Promise<[TransactionSignature, BufferRelayerAccount]> { - const [txn, bufferRelayerAccount] = + ): Promise<[BufferRelayerAccount, TransactionSignature]> { + const [bufferRelayerAccount, txn] = await this.createBufferRelayerInstructions( this.program.walletPubkey, params ); const txnSignature = await this.program.signAndSend(txn); - return [txnSignature, bufferRelayerAccount]; + return [bufferRelayerAccount, txnSignature]; } /** Load the list of oracles that are currently stored in the buffer */ diff --git a/javascript/solana.js/src/accounts/queueDataBuffer.ts b/javascript/solana.js/src/accounts/queueDataBuffer.ts index ab96846..3ad6987 100644 --- a/javascript/solana.js/src/accounts/queueDataBuffer.ts +++ b/javascript/solana.js/src/accounts/queueDataBuffer.ts @@ -80,16 +80,18 @@ export class QueueDataBuffer extends Account> { } /** - * Return an aggregator's assigned history buffer or undefined if it doesn't exist. + * Return a queues dataBuffer + * + * @throws {string} if dataBuffer is equal to default publicKey */ - static fromCrank( + static fromQueue( program: SwitchboardProgram, - crank: types.CrankAccountData + queue: types.OracleQueueAccountData ): QueueDataBuffer { - if (crank.dataBuffer.equals(PublicKey.default)) { - throw new Error(`Failed to find crank data buffer`); + if (queue.dataBuffer.equals(PublicKey.default)) { + throw new Error(`Failed to find queue data buffer`); } - return new QueueDataBuffer(program, crank.dataBuffer); + return new QueueDataBuffer(program, queue.dataBuffer); } } diff --git a/javascript/solana.js/src/accounts/vrfAccount.ts b/javascript/solana.js/src/accounts/vrfAccount.ts index af67deb..c7116de 100644 --- a/javascript/solana.js/src/accounts/vrfAccount.ts +++ b/javascript/solana.js/src/accounts/vrfAccount.ts @@ -72,7 +72,7 @@ export class VrfAccount extends Account { program: SwitchboardProgram, payer: PublicKey, params: VrfInitParams - ): Promise<[TransactionObject, VrfAccount]> { + ): Promise<[VrfAccount, TransactionObject]> { program.verifyNewKeypair(params.vrfKeypair); const vrfAccount = new VrfAccount(program, params.vrfKeypair.publicKey); const size = program.account.vrfAccountData.size; @@ -121,8 +121,10 @@ export class VrfAccount extends Account { ), ]; - const txn = new TransactionObject(payer, ixns, [params.vrfKeypair]); - return [txn, vrfAccount]; + return [ + vrfAccount, + new TransactionObject(payer, ixns, [params.vrfKeypair]), + ]; } /** @@ -133,14 +135,14 @@ export class VrfAccount extends Account { public static async create( program: SwitchboardProgram, params: VrfInitParams - ): Promise<[string, VrfAccount]> { - const [createTxn, vrfAccount] = await VrfAccount.createInstructions( + ): Promise<[VrfAccount, string]> { + const [vrfAccount, vrfInitTxn] = await VrfAccount.createInstructions( program, program.walletPubkey, params ); - const txnSignature = await program.signAndSend(createTxn); - return [txnSignature, vrfAccount]; + const txnSignature = await program.signAndSend(vrfInitTxn); + return [vrfAccount, txnSignature]; } public async requestRandomnessInstruction( diff --git a/javascript/solana.js/test/aggregator.spec.ts b/javascript/solana.js/test/aggregator.spec.ts index 68034a3..a2701dd 100644 --- a/javascript/solana.js/test/aggregator.spec.ts +++ b/javascript/solana.js/test/aggregator.spec.ts @@ -3,8 +3,6 @@ import 'mocha'; import chai, { expect } from 'chai'; import assert from 'assert'; -import * as anchor from '@project-serum/anchor'; -import * as spl from '@solana/spl-token'; import * as sbv2 from '../src'; import { setupTest, TestContext } from './utilts'; import { Keypair } from '@solana/web3.js'; @@ -15,7 +13,6 @@ import { QueueAccount, } from '../src'; import { OracleJob } from '@switchboard-xyz/common'; -import { PermitOracleQueueUsage } from '../src/generated/types/SwitchboardPermission'; describe('Aggregator Tests', () => { let ctx: TestContext; @@ -30,22 +27,19 @@ describe('Aggregator Tests', () => { before(async () => { ctx = await setupTest(); - const [createQueueSignature, oracleQueue] = await sbv2.QueueAccount.create( - ctx.program, - { - name: 'aggregator-queue', - metadata: '', - authority: queueAuthority.publicKey, - queueSize: 1, - reward: 0, - minStake: 0, - oracleTimeout: 86400, - slashingEnabled: false, - unpermissionedFeeds: true, - unpermissionedVrf: true, - enableBufferRelayers: false, - } - ); + const [oracleQueue] = await sbv2.QueueAccount.create(ctx.program, { + name: 'aggregator-queue', + metadata: '', + authority: queueAuthority.publicKey, + queueSize: 1, + reward: 0, + minStake: 0, + oracleTimeout: 86400, + slashingEnabled: false, + unpermissionedFeeds: true, + unpermissionedVrf: true, + enableBufferRelayers: false, + }); queueAccount = oracleQueue; @@ -56,7 +50,7 @@ describe('Aggregator Tests', () => { name: 'oracle-1', }); - const [createJobSigs, jobAccount1] = await JobAccount.create(ctx.program, { + const [jobAccount1] = await JobAccount.create(ctx.program, { data: OracleJob.encodeDelimited( OracleJob.fromObject({ tasks: [ @@ -77,18 +71,17 @@ describe('Aggregator Tests', () => { const aggregatorKeypair = Keypair.generate(); const aggregatorAuthority = Keypair.generate(); - const [createAggregatorSig, aggregatorAccount] = - await AggregatorAccount.create(ctx.program, { - queueAccount, - queueAuthority: queueAuthority.publicKey, - authority: aggregatorAuthority.publicKey, - batchSize: 1, - minRequiredOracleResults: 1, - minRequiredJobResults: 1, - minUpdateDelaySeconds: 60, - keypair: aggregatorKeypair, - }); - const aggregator = await aggregatorAccount.loadData(); + const [aggregatorAccount] = await AggregatorAccount.create(ctx.program, { + queueAccount, + queueAuthority: queueAuthority.publicKey, + authority: aggregatorAuthority.publicKey, + batchSize: 1, + minRequiredOracleResults: 1, + minRequiredJobResults: 1, + minUpdateDelaySeconds: 60, + keypair: aggregatorKeypair, + }); + await aggregatorAccount.loadData(); const oracleJob = OracleJob.fromObject({ tasks: [ @@ -100,7 +93,7 @@ describe('Aggregator Tests', () => { ], }); - const [createJobSigs, jobAccount] = await JobAccount.create( + const [jobAccount] = await JobAccount.create( ctx.program, { @@ -109,7 +102,7 @@ describe('Aggregator Tests', () => { } ); - const addSig = await aggregatorAccount.addJob({ + await aggregatorAccount.addJob({ job: jobAccount, weight: 1, authority: aggregatorAuthority, @@ -123,7 +116,7 @@ describe('Aggregator Tests', () => { throw new Error(`Failed to add job to aggregator`); } - const removeSig = await aggregatorAccount.removeJob({ + await aggregatorAccount.removeJob({ job: jobAccount, jobIdx: jobIdx, authority: aggregatorAuthority, @@ -138,33 +131,32 @@ describe('Aggregator Tests', () => { }); it('Creates and funds aggregator', async () => { - const [createAggregatorSig, aggregatorAccount] = - await queueAccount.createFeed({ - queueAuthority: queueAuthority, - batchSize: 1, - minRequiredOracleResults: 1, - minRequiredJobResults: 1, - minUpdateDelaySeconds: 60, - fundAmount: 2.5, - enable: true, - jobs: [ - { pubkey: jobAccount.publicKey }, - { - weight: 2, - data: OracleJob.encodeDelimited( - OracleJob.fromObject({ - tasks: [ - { - valueTask: { - value: 1, - }, + const [aggregatorAccount] = await queueAccount.createFeed({ + queueAuthority: queueAuthority, + batchSize: 1, + minRequiredOracleResults: 1, + minRequiredJobResults: 1, + minUpdateDelaySeconds: 60, + fundAmount: 2.5, + enable: true, + jobs: [ + { pubkey: jobAccount.publicKey }, + { + weight: 2, + data: OracleJob.encodeDelimited( + OracleJob.fromObject({ + tasks: [ + { + valueTask: { + value: 1, }, - ], - }) - ).finish(), - }, - ], - }); + }, + ], + }) + ).finish(), + }, + ], + }); const aggregator = await aggregatorAccount.loadData(); diff --git a/javascript/solana.js/test/job.spec.ts b/javascript/solana.js/test/job.spec.ts index e6aa62a..a85aa01 100644 --- a/javascript/solana.js/test/job.spec.ts +++ b/javascript/solana.js/test/job.spec.ts @@ -22,7 +22,7 @@ describe('Job Tests', () => { }); const data = OracleJob.encodeDelimited(oracleJob).finish(); - const [jobInit, jobAccount] = JobAccount.createInstructions( + const [jobAccount, jobInit] = JobAccount.createInstructions( ctx.program, ctx.program.walletPubkey, { diff --git a/javascript/solana.js/test/mint.spec.ts b/javascript/solana.js/test/mint.spec.ts index 0282562..16d55ca 100644 --- a/javascript/solana.js/test/mint.spec.ts +++ b/javascript/solana.js/test/mint.spec.ts @@ -23,8 +23,10 @@ describe('Mint Tests', () => { ); await ctx.program.connection.confirmTransaction(airdropTxn); - const [tokenAddress, createTokenAccountSig] = - await ctx.program.mint.createAssocatedUser(ctx.payer.publicKey, user); + const [tokenAddress] = await ctx.program.mint.createAssocatedUser( + ctx.payer.publicKey, + user + ); userTokenAddress = tokenAddress; const userTokenBalance = @@ -42,11 +44,7 @@ describe('Mint Tests', () => { throw new Error(`User token address does not exist`); } - const wrapSignature = await ctx.program.mint.wrap( - ctx.payer.publicKey, - { amount: 0.25 }, - user - ); + await ctx.program.mint.wrap(ctx.payer.publicKey, { amount: 0.25 }, user); const userTokenBalance = (await ctx.program.mint.getBalance(user.publicKey)) ?? 0; @@ -69,11 +67,7 @@ describe('Mint Tests', () => { throw new Error(`Final user token address would be negative`); } - const unwrapSignature = await ctx.program.mint.unwrap( - ctx.payer.publicKey, - 0.1, - user - ); + await ctx.program.mint.unwrap(ctx.payer.publicKey, 0.1, user); const userTokenBalance = await ctx.program.mint.getBalance(user.publicKey); if (userTokenBalance !== expectedFinalBalance) { @@ -88,15 +82,9 @@ describe('Mint Tests', () => { throw new Error(`User token address does not exist`); } - const initialUserTokenBalance = await ctx.program.mint.getBalance( - user.publicKey - ); + await ctx.program.mint.getBalance(user.publicKey); - const unwrapSignature = await ctx.program.mint.unwrap( - ctx.payer.publicKey, - undefined, - user - ); + await ctx.program.mint.unwrap(ctx.payer.publicKey, undefined, user); const userTokenAccount = await ctx.program.connection.getAccountInfo( userTokenAddress diff --git a/javascript/solana.js/test/open-round.spec.ts b/javascript/solana.js/test/open-round.spec.ts new file mode 100644 index 0000000..792a002 --- /dev/null +++ b/javascript/solana.js/test/open-round.spec.ts @@ -0,0 +1,92 @@ +import 'mocha'; + +import * as sbv2 from '../src'; +import { setupTest, TestContext } from './utilts'; +import { Keypair } from '@solana/web3.js'; +import { AggregatorAccount, OracleAccount, QueueAccount, types } from '../src'; +import { OracleJob } from '@switchboard-xyz/common'; + +describe('Open Round Tests', () => { + let ctx: TestContext; + + const queueAuthority = Keypair.generate(); + + let createQueueSignature: string; + let queueAccount: QueueAccount; + let queue: types.OracleQueueAccountData; + + let createOracleSignature1: string; + let oracleAccount1: OracleAccount; + let oracle1: types.OracleAccountData; + + let createOracleSignature2: string; + let oracleAccount2: OracleAccount; + let oracle2: types.OracleAccountData; + + let createAggregatorSignatures: string[]; + let aggregatorAccount: AggregatorAccount; + let aggregator: types.AggregatorAccountData; + + before(async () => { + ctx = await setupTest(); + + [queueAccount, createQueueSignature] = await sbv2.QueueAccount.create( + ctx.program, + { + name: 'q1', + metadata: '', + queueSize: 2, + reward: 0.0025, + minStake: 0, + oracleTimeout: 60, + slashingEnabled: false, + unpermissionedFeeds: false, + unpermissionedVrf: true, + enableBufferRelayers: false, + authority: queueAuthority.publicKey, + } + ); + queue = await queueAccount.loadData(); + + [oracleAccount1, createOracleSignature1] = await queueAccount.createOracle({ + name: 'oracle-1', + metadata: 'oracle-1', + queueAuthority, + }); + oracle1 = await oracleAccount1.loadData(); + + [oracleAccount2, createOracleSignature2] = await queueAccount.createOracle({ + name: 'oracle-2', + metadata: 'oracle-2', + queueAuthority, + }); + oracle2 = await oracleAccount2.loadData(); + + [aggregatorAccount, createAggregatorSignatures] = + await queueAccount.createFeed({ + queueAuthority: queueAuthority, + batchSize: 1, + minRequiredOracleResults: 1, + minRequiredJobResults: 1, + minUpdateDelaySeconds: 60, + fundAmount: 1, + enable: true, + jobs: [ + { + weight: 2, + data: OracleJob.encodeDelimited( + OracleJob.fromObject({ + tasks: [ + { + valueTask: { + value: 1, + }, + }, + ], + }) + ).finish(), + }, + ], + }); + }); +}); diff --git a/javascript/solana.js/test/queue.spec.ts b/javascript/solana.js/test/queue.spec.ts index fadcdec..ae723b7 100644 --- a/javascript/solana.js/test/queue.spec.ts +++ b/javascript/solana.js/test/queue.spec.ts @@ -2,8 +2,6 @@ import 'mocha'; import chai, { expect } from 'chai'; import assert from 'assert'; -import * as anchor from '@project-serum/anchor'; -import * as spl from '@solana/spl-token'; import * as sbv2 from '../src'; import { setupTest, TestContext } from './utilts'; import { Keypair } from '@solana/web3.js'; @@ -16,28 +14,24 @@ describe('Queue Tests', () => { ctx = await setupTest(); }); - let queueAccount: sbv2.QueueAccount; const queueAuthority = Keypair.generate(); + let queueAccount: sbv2.QueueAccount; it('Creates a Queue', async () => { - const [createQueueSignature, oracleQueue] = await sbv2.QueueAccount.create( - ctx.program, - { - name: 'q1', - metadata: '', - queueSize: 2, - reward: 0, - minStake: 0, - oracleTimeout: 60, - slashingEnabled: false, - unpermissionedFeeds: true, - unpermissionedVrf: true, - enableBufferRelayers: false, - authority: queueAuthority.publicKey, - } - ); - queueAccount = oracleQueue; - const queue = await queueAccount.loadData(); + [queueAccount] = await sbv2.QueueAccount.create(ctx.program, { + name: 'q1', + metadata: '', + queueSize: 2, + reward: 0, + minStake: 0, + oracleTimeout: 60, + slashingEnabled: false, + unpermissionedFeeds: true, + unpermissionedVrf: true, + enableBufferRelayers: false, + authority: queueAuthority.publicKey, + }); + await queueAccount.loadData(); }); it('Adds an oracle to a queue', async () => { @@ -46,24 +40,24 @@ describe('Queue Tests', () => { } const oracleAuthority = Keypair.generate(); // Create a new oracle - const [createOracleSignatures, oracleAccount] = - await queueAccount.createOracle({ - name: 'oracle2', - metadata: '', - queueAuthority, - enable: true, - authority: oracleAuthority, - }); + const [oracleAccount] = await queueAccount.createOracle({ + name: 'oracle2', + metadata: '', + queueAuthority, + enable: true, + authority: oracleAuthority, + }); const oracle = await oracleAccount.loadData(); - const [permissionAccount, permissionBump] = PermissionAccount.fromSeed( + const [permissionAccount] = PermissionAccount.fromSeed( ctx.program, queueAuthority.publicKey, queueAccount.publicKey, oracleAccount.publicKey ); + await permissionAccount.loadData(); - const heartbeatSignature = await oracleAccount.heartbeat({ + await oracleAccount.heartbeat({ queueAccount, tokenWallet: oracle.tokenAccount, authority: oracleAuthority, @@ -83,18 +77,17 @@ describe('Queue Tests', () => { const oracleAuthority = Keypair.generate(); // Create a new oracle - const [createOracleSignatures, oracleAccount] = - await queueAccount.createOracle({ - name: 'oracle2', - metadata: '', - queueAuthority, - enable: true, - authority: oracleAuthority, - }); + const [oracleAccount] = await queueAccount.createOracle({ + name: 'oracle2', + metadata: '', + queueAuthority, + enable: true, + authority: oracleAuthority, + }); const oracle = await oracleAccount.loadData(); - const heartbeatSignature = await oracleAccount.heartbeat({ + await oracleAccount.heartbeat({ queueAccount, tokenWallet: oracle.tokenAccount, authority: oracleAuthority, @@ -116,16 +109,15 @@ describe('Queue Tests', () => { const tokenWallet = Keypair.generate(); // Create a new oracle - const [createOracleSignatures, oracleAccount] = - await queueAccount.createOracle({ - name: 'oracle3', - metadata: '', - queueAuthority, - enable: true, - authority: oracleAuthority, - }); + const [oracleAccount] = await queueAccount.createOracle({ + name: 'oracle3', + metadata: '', + queueAuthority, + enable: true, + authority: oracleAuthority, + }); - const oracle = await oracleAccount.loadData(); + await oracleAccount.loadData(); assert.rejects( async () => {