async function main() {
  // Substrate node we are connected to and listening to remarks
  // const provider = new WsProvider("wss://dev-node.substrate.dev:9944");
  const provider = new WsProvider(WS_PROVIDER);

  const api = await ApiPromise.create({ provider });

  // Get general information about the node we are connected to
  const [chain, nodeName, nodeVersion] = await Promise.all([
    api.rpc.system.chain(),
    api.rpc.system.name(),
    api.rpc.system.version()
  ]);
  console.log(
    `You are connected to chain ${chain} using ${nodeName} v${nodeVersion}`
  );

  // Bitmap Image
  let bitmap;

  // Try to open the file, else create a new bitmap
  try {
    bitmap = bitmapManipulation.BMPBitmap.fromFile("./public/image.bmp");
    console.log("file found");
  } catch {
    console.log("file NOT found");
    bitmap = new bitmapManipulation.BMPBitmap(IMAGE_SIZE, IMAGE_SIZE);
    bitmap.clear(bitmap.palette.indexOf(0x000000));
    bitmap.save("./public/image.bmp");
    convertToJPG();
  }
  // Subscribe to new blocks being produced, not necessarily finalized ones.
  const unsubscribe = await api.rpc.chain.subscribeNewHead(async header => {
    await api.rpc.chain.getBlock(header.hash, async block => {
      console.log("Block is: ", block.block.header.number.toNumber());
      // Extrinsics in the block
      let extrinsics = await block.block.extrinsics;
      // Variable to check if we need to update the image
      let update = false;

      // Check each extrinsic in the block
      for (extrinsic of extrinsics) {
        // This specific call index [0,1] represents `system.remark`
        if (extrinsic.callIndex[0] == 0 && extrinsic.callIndex[1] == 1) {
          // Get the `byte` data from a `remark`
          let pixel = parseBuffer(extrinsic.args[0]);
          console.log("Pixel: ", pixel);
          if (pixel) {
            await updateImage(bitmap, pixel);
            update = true;
          }
        }
      }

      // Check if we need to update the image
      if (update) {
        bitmap.save("./public/image.bmp");
        convertToJPG();
      }
    });
  });
}
// Main function which needs to run at start
Main function which needs to run at start
Main function which needs to run at start
async function main() {
  const provider = new WsProvider(WS_PROVIDER);
  const api = await ApiPromise.create({ provider });
  const [chain, nodeName, nodeVersion] = await Promise.all([
    api.rpc.system.chain(),
    api.rpc.system.name(),
    api.rpc.system.version()
  ]);
  console.log(
    `You are connected to chain ${chain} using ${nodeName} v${nodeVersion}`
  );
  let bitmap;
  try {
    bitmap = bitmapManipulation.BMPBitmap.fromFile("./public/image.bmp");
    console.log("file found");
  } catch {
    console.log("file NOT found");
    bitmap = new bitmapManipulation.BMPBitmap(IMAGE_SIZE, IMAGE_SIZE);
    bitmap.clear(bitmap.palette.indexOf(0x000000));
    bitmap.save("./public/image.bmp");
    convertToJPG();
  }
  const unsubscribe = await api.rpc.chain.subscribeNewHead(async header => {
    await api.rpc.chain.getBlock(header.hash, async block => {
      console.log("Block is: ", block.block.header.number.toNumber());
      let extrinsics = await block.block.extrinsics;
      let update = false;
      for (extrinsic of extrinsics) {
        if (extrinsic.callIndex[0] == 0 && extrinsic.callIndex[1] == 1) {
          let pixel = parseBuffer(extrinsic.args[0]);
          console.log("Pixel: ", pixel);
          if (pixel) {
            await updateImage(bitmap, pixel);
            update = true;
          }
        }
      }
      if (update) {
        bitmap.save("./public/image.bmp");
        convertToJPG();
      }
    });
  });
}
async function main ( ) { const provider = new WsProvider ( WS_PROVIDER ) ; const api = await ApiPromise . create ( { provider } ) ; const [ chain , nodeName , nodeVersion ] = await Promise . all ( [ api . rpc . system . chain ( ) , api . rpc . system . name ( ) , api . rpc . system . version ( ) ] ) ; console . log ( ` ${ chain } ${ nodeName } ${ nodeVersion } ` ) ; let bitmap ; try { bitmap = bitmapManipulation . BMPBitmap . fromFile ( "./public/image.bmp" ) ; console . log ( "file found" ) ; } catch { console . log ( "file NOT found" ) ; bitmap = new bitmapManipulation . BMPBitmap ( IMAGE_SIZE , IMAGE_SIZE ) ; bitmap . clear ( bitmap . palette . indexOf ( 0x000000 ) ) ; bitmap . save ( "./public/image.bmp" ) ; convertToJPG ( ) ; } const unsubscribe = await api . rpc . chain . subscribeNewHead ( async header => { await api . rpc . chain . getBlock ( header . hash , async block => { console . log ( "Block is: " , block . block . header . number . toNumber ( ) ) ; let extrinsics = await block . block . extrinsics ; let update = false ; for ( extrinsic of extrinsics ) { if ( extrinsic . callIndex [ 0 ] == 0 && extrinsic . callIndex [ 1 ] == 1 ) { let pixel = parseBuffer ( extrinsic . args [ 0 ] ) ; console . log ( "Pixel: " , pixel ) ; if ( pixel ) { await updateImage ( bitmap , pixel ) ; update = true ; } } } if ( update ) { bitmap . save ( "./public/image.bmp" ) ; convertToJPG ( ) ; } } ) ; } ) ; }
Main function which needs to run at start
Main function which needs to run at start
// Substrate node we are connected to and listening to remarks // const provider = new WsProvider("wss://dev-node.substrate.dev:9944"); // Get general information about the node we are connected to // Bitmap Image // Try to open the file, else create a new bitmap // Subscribe to new blocks being produced, not necessarily finalized ones. // Extrinsics in the block // Variable to check if we need to update the image // Check each extrinsic in the block // This specific call index [0,1] represents `system.remark` // Get the `byte` data from a `remark` // Check if we need to update the image