Skip to content

Commit

Permalink
[web3js] refactor around anchor stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
ochaloup committed Mar 18, 2024
1 parent b479ded commit 3f867b5
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 55 deletions.
4 changes: 2 additions & 2 deletions packages/lib/anchor-common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './error'
export * from './wallet'
export * from './validator'
export * from './extendedProvider'
export * from './validator'
export * from './wallet'
57 changes: 14 additions & 43 deletions packages/lib/anchor-common/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
9 changes: 6 additions & 3 deletions packages/lib/cli-common/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,7 +35,7 @@ export abstract class Context {
logger,
skipPreflight,
confirmationFinality = 'finalized',
computeUnitPrice = 0,
computeUnitPrice = undefined,
simulate,
printOnly,
commandName,
Expand All @@ -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
}
Expand Down
40 changes: 40 additions & 0 deletions packages/lib/web3js-common/src/epoch.ts
Original file line number Diff line number Diff line change
@@ -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)
}
14 changes: 8 additions & 6 deletions packages/lib/web3js-common/src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
2 changes: 1 addition & 1 deletion packages/lib/web3js-common/src/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Expand Down

0 comments on commit 3f867b5

Please sign in to comment.