Set a Transmission protocol

Prerequisites

Before getting started, make sure you have the following ready:

  1. Create a Ternoa account with Alphanet CAPS

  2. Install and set up your editor of choice (we will use Visual Studio Code [VSC] in this tutorial)

  3. Install NodeJS v.14+ & NPM

How to set an AtBlockWithReset (Date with countdown reset) Protocol on-chain?

This function creates a AtBlockWithReset Protocol on the Ternoa chain. It returns an object promise containing the ProtocolSetEvent provided by the Ternoa blockchain.

Use your own account by updating the //TernoaTestAccount with your account seed when retrieving the keyring from the example below. Replace the variables according to your needs. Each protocol requires its own specific set of parameters but once you will have understood this AtBlockWithReset example, you will know of to set them all.

import {
    initializeApi,
    getKeyringFromSeed,
    createNft,
    getLastBlock,
    setTransmissionProtocol,
    WaitUntil,
} from "ternoa-js";

//First import the corresponding protocol helpers.
//Helpers expect primitive values (strings, numbers, boolean (..)) and return the corresponding formatted object in the format expected by the chain.
import {
    formatAtBlockWithResetProtocol,
    formatProtocolCancellation,
} from "ternoa-js/protocols/utils";

const createContract = async () => {
    try {
        await initializeApi();
        const keyring = await getKeyringFromSeed("//TernoaTestAccount");
        const nftId = // update with the NFT id to be transferred with the AtBlockWithReset protocol.
        const protocolRecipient = // update with the address of the recipient.
        const protocolExecutionDate = // update with the date you want the protocol to be executed.

        // As the blockchain is expecting block numbers rather than dates, an approximative dateToBlock converter could be used like below: consider approximately one new block every 6 seconds.
        const lastBlockId = await getLastBlock()
        const duration = protocolExecutionDate.getTime() - new Date().getTime();
        const numberOfBlocks = duration / 6 / 1000
        const transmissionBlockId = Math.ceil(lastBlockId + numberOfBlocks)

        // Here you create some constants with each helper and value you want.
        // First set the transmission protocol values: protocol kind and the block number (here the date converted to block)
        const protocol = formatAtBlockWithResetProtocol(
            "atBlockWithReset",
            transmissionBlockId
        );
        // Second set the cancellation option of your protocol: It can be anytime, none, or untilBlock. if untilBlock is set, you need to add the corresponding block id.
        const cancellation = formatProtocolCancellation("anytime");

        // Provide each const one by one as parameters in our function below:
        const {nftId, recipient, protocol } = await setTransmissionProtocol(
            nftId,
            protocolRecipient,
            protocol,
            cancellation,
            keyring,
            WaitUntil.BlockInclusion
        );

        console.log(`Protocol ${protocol} created for NFT: ${nftId}.`);
    } catch (e) {
        console.error(e);
    }
};

The expected params

The expected parameters for creating an AtBlockWithReset protocol are objects under a specific format. The best practice and the easiest way to create the params is to use the formatters we provide:

  • formatAtBlockProtocol()

  • formatAtBlockWithResetProtocol()

  • formatOnConsentProtocol()

  • formatOnConsentAtBlockProtocol()

  • formatProtocolCancellation()

Response

The response provided from the blockchain event includes all the information below according to the parameters provided when creating a date with the countdown protocol.

Support

If you face any trouble, feel free to reach out to our community engineers in our Discord.

Last updated