You can force-produce blocks using the produceBlocks
helper to achieve an arbitrary block height. This is especially useful when you want to do some testing regarding transaction maturity.
const provider = new Provider('http://127.0.0.1:4000/graphql');
const block = await provider.getBlock('latest');
if (!block) {
throw new Error('No latest block');
}
const { height: latestBlockNumberBeforeProduce } = block;
const amountOfBlocksToProduce = 3;
const latestBlockNumber = await provider.produceBlocks(amountOfBlocksToProduce);
expect(latestBlockNumber.toHex()).toEqual(
latestBlockNumberBeforeProduce.add(amountOfBlocksToProduce).toHex()
);
You can also produce blocks with custom timestamps using the produceBlocks
helper by specifying the second optional parameter.
const lastBlockTimestamp = latestBlock.time;
const latestBlockNumber = await provider.produceBlocks(3, {
blockTimeInterval: '1000',
startTime: lastBlockTimestamp,
});
Note that the startTime
should be in the tai64
format. You can use the fromUnixToTai64
helper to convert unix timestamps to tai64
.