Skip to content

Commit

Permalink
[bankrun] adding warpOffsetSlot
Browse files Browse the repository at this point in the history
  • Loading branch information
ochaloup committed Jul 9, 2024
1 parent ba1fbaf commit f80ff99
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
31 changes: 22 additions & 9 deletions packages/lib/bankrun-utils/src/bankrun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,49 @@ export async function assertNotExist(
}

// https://github.com/solana-labs/solana/blob/v1.17.7/sdk/program/src/epoch_schedule.rs#L29C1-L29C45
export const MINIMUM_SLOTS_PER_EPOCH = 32
export const MINIMUM_SLOTS_PER_EPOCH = BigInt(32)
// https://github.com/solana-labs/solana/blob/v1.17.7/sdk/program/src/epoch_schedule.rs#L167
export function warpToEpoch(provider: BankrunProvider, epoch: number) {
export function warpToEpoch(provider: BankrunProvider, epoch: number | bigint) {
const epochBigInt = BigInt(epoch)
const { slotsPerEpoch, firstNormalEpoch, firstNormalSlot } =
provider.context.genesisConfig.epochSchedule
let warpToSlot: bigint
if (epochBigInt <= firstNormalEpoch) {
warpToSlot = BigInt((2 ** epoch - 1) * MINIMUM_SLOTS_PER_EPOCH)
warpToSlot =
(BigInt(2) ** epochBigInt - BigInt(1)) * MINIMUM_SLOTS_PER_EPOCH
} else {
warpToSlot =
(epochBigInt - firstNormalEpoch) * slotsPerEpoch + firstNormalSlot
}
provider.context.warpToSlot(warpToSlot)
}

export async function warpToNextEpoch(provider: BankrunProvider) {
await warpOffsetEpoch(provider, 1)
export async function currentEpoch(provider: BankrunProvider): Promise<bigint> {
return (await provider.context.banksClient.getClock()).epoch
}

export async function warpOffsetEpoch(
provider: BankrunProvider,
plusEpochs: number
plusEpochs: number | bigint
) {
const nextEpoch = (await currentEpoch(provider)) + plusEpochs
const nextEpoch = (await currentEpoch(provider)) + BigInt(plusEpochs)
warpToEpoch(provider, nextEpoch)
}

export async function currentEpoch(provider: BankrunProvider): Promise<number> {
return Number((await provider.context.banksClient.getClock()).epoch)
export async function warpToNextEpoch(provider: BankrunProvider) {
await warpOffsetEpoch(provider, 1)
}

export async function currentSlot(provider: BankrunProvider): Promise<bigint> {
return (await provider.context.banksClient.getClock()).slot
}

export async function warpOffsetSlot(
provider: BankrunProvider,
plusSlots: number | bigint
) {
const nextSlot = (await currentSlot(provider)) + BigInt(plusSlots)
warpOffsetSlot(provider, nextSlot)
}

export async function warpToNextSlot(provider: BankrunProvider) {
Expand Down
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 @@ -233,7 +233,7 @@ export async function executeTx({
connection,
transaction,
sendOpts,
logger,
logger
)
// Checking if executed
txResponse = await confirmTransaction(
Expand Down

0 comments on commit f80ff99

Please sign in to comment.