Skip to content

Extensibility

Don Kackman edited this page Aug 18, 2022 · 5 revisions

Since the repl uses the nodejs REPL it is easy to define your own functions and modules. Scripts loaded with these mechanisms have access to the full repl environment so can use the various globally defined objects and default imports.

.editor

This enters a edit mode where you can define functions that are subsequently available to be called.

.load

The node repl has the ability to load an arbitrary js file into the environment with .load. This will execute the js in immediate mode and any defined functions will be available to be subsequently called.

For instance if you have this script defined in a file:

async function mintSimpleNFT(walletId, targetAddress, name, filepath, type) {
    let dataFileInfo = {
        name: name,
        type: type,
        filepath: filepath
    };
    let mintingInfo = {
        wallet_id: walletId,
        target_address: targetAddress,
    };
    let collectionMetaData = metadataFactory.createCollectionMetadata(`${name}-collection`);
    let nftMetadata = metadataFactory.createNftMetadata(name, collectionMetaData);
    await minter.createNftFromFile(dataFileInfo, mintingInfo, nftMetadata);
}

you can do this:

🌿 .load c:\\path\\to\\mintSimpleNFT.js
// it will echo the top part of the file
undefined
🌿 await mintSimpleNFT(2, 'someaddres', 'somename', 'some file path', 'image/png')

Custom Modules

experimental

When a connection to the chia daemon is made, the repl will dynamically import any modules located in options.scriptFolder. Each module has to exist in its own sub-directory and there must be an .mjs file with the same name. For instance, in the mintHelper folder there will be mintHelper.mjs. Further, that mjs module has to have a default export that is a class which can be instantiated. The loaded module will have its own dependency tree, so those most also be installed alongside the mjs file.

The class constructor will be passed a pointer to the repl context object so it has access to the same state as the repl, including the chia daemon. Once loaded, the module is available by name in the repl context.

The examples folder demonstrates an user defined module that will make an NFT collection from the contents of a folder.

let collectionMetaData = metadataFactory.createCollectionMetadata('my-folder-collection', [
    ['Twitter', '@dkackman'],
    ['webstite', 'https://github.com/dkackman/chia-repl']
]);

await mintHelper.mintCollectionFromFolder(2,
    'txch1xvggspky3kh2tdwd252j04d6mjytsryexkpmgf4s22utu4qjvmfq5dfkzy',
    collectionMetaData,
    'C:\\tmp\\nft',
    '.jpg',
    10,
    'txch1xvggspky3kh2tdwd252j04d6mjytsryexkpmgf4s22utu4qjvmfq5dfkzy',
    150);

This is helpful for tailoring the functionality of the chia-repl to your specific needs.

Clone this wiki locally