Skip to content

Instantly share code, notes, and snippets.

@sisou
Created December 25, 2021 23:24
Show Gist options
  • Save sisou/0adc9a16ec4bbea5ee8f1f05ba6aa515 to your computer and use it in GitHub Desktop.
Save sisou/0adc9a16ec4bbea5ee8f1f05ba6aa515 to your computer and use it in GitHub Desktop.
NodeJS script to run a Nimiq (full) node to listen to all transactions getting added to the mempool.
const Nimiq = require('@nimiq/core');
Nimiq.load().then(async function() {
// Configure for Testnet. For Mainnet, use main().
Nimiq.GenesisConfig.test();
// Instantiate a config builder.
const configBuilder = Nimiq.Client.Configuration.builder();
// Require a mempool
configBuilder.feature(Nimiq.Client.Feature.MEMPOOL);
/**
* The above creates a so-called "light" client. However, light clients can currently not
* establish consensus, because the accounts-tree is too big.
* So you will need to run a full node to listen to _all_ transactions:
*/
configBuilder.feature(Nimiq.Client.Feature.LOCAL_HISTORY);
// Create a client based on your configuration,
// it will automatically connect to the network.
const client = configBuilder.instantiateClient();
client.mempool.addTransactionAddedListener(async (hash) => {
const tx = await client.getTransaction(hash);
// Do something with `tx`
// The object's type is this:
// https://github.com/nimiq/core-js/blob/master/dist/types.d.ts#L281
const sender = tx.sender.toUserFriendlyAddress();
const recipient = tx.recipient.toUserFriendlyAddress();
const valueInNim = Nimiq.Policy.lunasToCoins(tx.value);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment