From d31be22a6b3a9b2d67418576b92d6aa795795b7d Mon Sep 17 00:00:00 2001 From: SerCry <92796990+SerCry@users.noreply.github.com> Date: Wed, 25 Oct 2023 16:44:55 +0300 Subject: [PATCH] Create Simple_BlockExplorer.ts --- Simple_BlockExplorer.ts | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Simple_BlockExplorer.ts diff --git a/Simple_BlockExplorer.ts b/Simple_BlockExplorer.ts new file mode 100644 index 0000000..134a218 --- /dev/null +++ b/Simple_BlockExplorer.ts @@ -0,0 +1,44 @@ +// blockchain-explorer.ts + +// https://github.com/SerCry/Artela_Block_Explorer_ExampleV1/tree/main + + +import { storage, env } from "near-sdk-as"; + +// Define a simple struct for storing block information +@nearBindgen +class Block { + constructor(public hash: string, public timestamp: u64) {} +} + +// Store blocks in a map +const blocks = new Map(); + +// Add a new block +export function addBlock(hash: string, timestamp: u64): void { + const height = storage.getPrimitive("blockHeight", u64.Zero); + blocks.set(height, new Block(hash, timestamp)); + storage.set("blockHeight", height + u64.One); +} + +// Get block information by block height +export function getBlockByHeight(height: u64): Block | null { + if (height >= storage.getPrimitive("blockHeight", u64.Zero)) { + return null; + } + return blocks.get(height); +} + +// Get the latest block +export function getLatestBlock(): Block | null { + const height = storage.getPrimitive("blockHeight", u64.Zero); + if (height === u64.Zero) { + return null; + } + return blocks.get(height - u64.One); +} + +// Export a function to get the total number of blocks +export function getTotalBlockCount(): u64 { + return storage.getPrimitive("blockHeight", u64.Zero); +}