Test and emulate
Emulator allows developers to test and validate their transactions in a controlled environment without interacting with a real blockchain. This is important for debugging, rapid prototyping, and ensuring transaction logic is correct before deploying to a testnet
or mainnet
Initialize emulator
This setup allows you to create a mock blockchain state with predefined addresses and asset distributions
const emulator = new Emulator([
{
address: "addr_test...",
assets: { lovelace: 3000000000n },
},
// You can add multiple addresses with different asset distributions
]);
const lucid = await Lucid(emulator);
Working with time ranges
const tx = await lucid
.newTx()
.validTo(emulator.now())
.complete();
Distribute staking rewards
The emulator allows you to conveniently distribute rewards to all delegated stake addresses.
emulator.distributeRewards(100000000n);
At any point in the emulation you can call the following code to get the current state of the emulated blockchain. It gives you insights into the distribution of funds across addresses and shows you the current block height and slot.
emulator.log();
Example - Simulated transaction
const tx = await lucid
.newTx()
.pay.ToAddress("addr_test...", { lovelace: 1000000n })
.complete();
const signedTx = await tx.sign().complete();
const txHash = await signedTx.submit();
// Simulate the passage of time and block confirmations in the emulator
await emulator.awaitBlock(10);
Protocol Parameters
You can use the emulator with custom protocol parameters to bypass these limits during development.
const lucid = Lucid("<Provider>", "<Network>", {
presetProtocolParameters: {
...PROTOCOL_PARAMETERS_DEFAULT,
minFeeA: 0, // per-byte fee
minFeeB: 0, // constant fee
priceMem: 0,
priceStep: 0,
coinsPerUtxoByte: 0n, // min-ADA requirement
},
});
This configuration disables fees and minimum ADA requirements, allowing you to test and debug your transaction logic without being constrained by network limits. Remember to test with actual protocol parameters before deploying to mainnet.