diff --git a/packages/lib/anchor-common/src/index.ts b/packages/lib/anchor-common/src/index.ts index 6848d4a..be82721 100644 --- a/packages/lib/anchor-common/src/index.ts +++ b/packages/lib/anchor-common/src/index.ts @@ -1,4 +1,4 @@ export * from './error' -export * from './wallet' -export * from './validator' export * from './extendedProvider' +export * from './validator' +export * from './wallet' diff --git a/packages/lib/anchor-common/src/validator.ts b/packages/lib/anchor-common/src/validator.ts index 005e471..403deaf 100644 --- a/packages/lib/anchor-common/src/validator.ts +++ b/packages/lib/anchor-common/src/validator.ts @@ -2,7 +2,8 @@ import { Connection, Keypair, PublicKey } from '@solana/web3.js' import { readFile } from 'fs/promises' import fs from 'fs' import { sleep } from '@marinade.finance/ts-common' -import { getStakeAccount } from '@marinade.finance/web3js-common/src/stakeAccount' +import { getStakeAccount } from '@marinade.finance/web3js-common' +import { waitForEpoch } from '@marinade.finance/web3js-common' export async function getAnchorValidatorInfo( connection: Connection, @@ -39,23 +40,24 @@ export async function getAnchorValidatorInfo( const voteAccounts = await connection.getVoteAccounts() // expecting run on localhost and only one voting vote account is available // i.e., one validator solana-test-validator is voting and the validator identity is the same - if (voteAccounts.current.length !== 1) { + const anchorValidatorVoteAccounts = voteAccounts.current.filter( + v => v.nodePubkey === validatorIdentity.publicKey.toBase58() + ) + if (anchorValidatorVoteAccounts.length <= 0) { throw new Error( - 'Expected one vote account of solana-test-validator. Cannot continue in global local test setup.' + - ` Number of vote accounts found: ${voteAccounts.current.length}` + 'Expected solana-test-validator to be voting. Cannot continue in global local test setup. ' + + `No one with "nodePubkey" of validator ${validatorIdentity.publicKey.toBase58()}. ` + + `Number of all vote accounts found: ${voteAccounts.current.length}` ) } - const votePubkey = new PublicKey(voteAccounts.current[0].votePubkey) - if ( - voteAccounts.current[0].nodePubkey !== - validatorIdentity.publicKey.toBase58() - ) { + if (anchorValidatorVoteAccounts.length > 1) { throw new Error( - `Expected validator identity ${validatorIdentity.publicKey.toBase58()} to be the same as the vote account node pubkey ${ - voteAccounts.current[0].nodePubkey - }` + 'Expected one vote account of solana-test-validator. Cannot continue in global local test setup.' + + `More vote accounts of "nodePubkey" of validator ${validatorIdentity.publicKey.toBase58()}. ` + + ` Number of solana-test-validator vote accounts found: ${anchorValidatorVoteAccounts.length}` ) } + const votePubkey = new PublicKey(anchorValidatorVoteAccounts[0].votePubkey) return { votePubkey, @@ -139,34 +141,3 @@ export async function waitForStakeAccountActivation({ } } } - -export async function waitForEpoch( - connection: Connection, - targetEpoch: number, - timeoutSeconds: number -) { - const startTime = Date.now() - let currentEpoch = (await connection.getEpochInfo()).epoch - if (currentEpoch < targetEpoch) { - console.debug( - `Waiting for the epoch ${targetEpoch}, current epoch is ${currentEpoch}` - ) - } - while (currentEpoch < targetEpoch) { - if (Date.now() - startTime > timeoutSeconds * 1000) { - throw new Error( - `Timeout ${timeoutSeconds} elapsed when waiting for epoch ${targetEpoch} (current epoch: ${currentEpoch})` - ) - } - await sleep(1000) - currentEpoch = (await connection.getEpochInfo()).epoch - } -} - -export async function waitForNextEpoch( - connection: Connection, - timeoutSeconds: number -) { - const currentEpoch = (await connection.getEpochInfo()).epoch - await waitForEpoch(connection, currentEpoch + 1, timeoutSeconds) -} diff --git a/packages/lib/cli-common/src/context.ts b/packages/lib/cli-common/src/context.ts index 1baa0e9..9351961 100644 --- a/packages/lib/cli-common/src/context.ts +++ b/packages/lib/cli-common/src/context.ts @@ -25,7 +25,7 @@ export abstract class Context { readonly logger: Logger readonly skipPreflight: boolean readonly confirmationFinality: Finality - readonly computeUnitPrice: number + readonly computeUnitPrice: number | undefined readonly simulate: boolean readonly printOnly: boolean readonly commandName: string @@ -35,7 +35,7 @@ export abstract class Context { logger, skipPreflight, confirmationFinality = 'finalized', - computeUnitPrice = 0, + computeUnitPrice = undefined, simulate, printOnly, commandName, @@ -54,7 +54,10 @@ export abstract class Context { this.logger = logger this.skipPreflight = skipPreflight this.confirmationFinality = confirmationFinality - this.computeUnitPrice = computeUnitPrice + this.computeUnitPrice = + computeUnitPrice === undefined || computeUnitPrice < 0 + ? undefined + : computeUnitPrice this.simulate = simulate this.printOnly = printOnly } diff --git a/packages/lib/web3js-common/src/epoch.ts b/packages/lib/web3js-common/src/epoch.ts new file mode 100644 index 0000000..2f46b28 --- /dev/null +++ b/packages/lib/web3js-common/src/epoch.ts @@ -0,0 +1,40 @@ +import { sleep } from '@marinade.finance/ts-common' +import { Provider, instanceOfProvider } from './provider' +import { Connection } from '@solana/web3.js' + +export async function waitForEpoch( + connection: Connection | Provider, + targetEpoch: number, + timeoutSeconds: number +) { + connection = instanceOfProvider(connection) + ? connection.connection + : connection + const startTime = Date.now() + let currentEpoch = (await connection.getEpochInfo()).epoch + if (currentEpoch < targetEpoch) { + console.debug( + `Waiting for the epoch ${targetEpoch}, current epoch is ${currentEpoch}` + ) + } + while (currentEpoch < targetEpoch) { + if (Date.now() - startTime > timeoutSeconds * 1000) { + throw new Error( + `Timeout ${timeoutSeconds} elapsed when waiting for epoch ${targetEpoch} (current epoch: ${currentEpoch})` + ) + } + await sleep(1000) + currentEpoch = (await connection.getEpochInfo()).epoch + } +} + +export async function waitForNextEpoch( + connection: Connection | Provider, + timeoutSeconds: number +) { + connection = instanceOfProvider(connection) + ? connection.connection + : connection + const currentEpoch = (await connection.getEpochInfo()).epoch + await waitForEpoch(connection, currentEpoch + 1, timeoutSeconds) +} diff --git a/packages/lib/web3js-common/src/index.ts b/packages/lib/web3js-common/src/index.ts index 614e80c..cb13b38 100644 --- a/packages/lib/web3js-common/src/index.ts +++ b/packages/lib/web3js-common/src/index.ts @@ -1,12 +1,14 @@ +export * from './account' export * from './borsh' +export * from './epoch' export * from './error' +export * from './execution' export * from './fileKeypair' -export * from './tx' -export * from './txToBase64' -export * from './wallet' +export * from './math' export * from './provider' -export * from './execution' -export * from './account' -export * from './voteAccount' export * from './stakeAccount' export * from './tokenMetadata' +export * from './tx' +export * from './txToBase64' +export * from './voteAccount' +export * from './wallet' diff --git a/packages/lib/web3js-common/src/tx.ts b/packages/lib/web3js-common/src/tx.ts index 42db829..46133ee 100644 --- a/packages/lib/web3js-common/src/tx.ts +++ b/packages/lib/web3js-common/src/tx.ts @@ -323,7 +323,7 @@ async function addComputeBudgetIxes({ if (computeUnitLimit !== undefined && computeUnitLimit >= 0) { transaction.add(setComputeUnitLimitIx(computeUnitLimit)) } - if (computeUnitPrice !== undefined && computeUnitPrice >= 0) { + if (computeUnitPrice !== undefined && computeUnitPrice > 0) { transaction.add(setComputeUnitPriceIx(computeUnitPrice)) } }