diff --git a/web/build-xec-apps/src/assets/chronikMethods.js b/web/build-xec-apps/src/assets/chronikMethods.js new file mode 100644 --- /dev/null +++ b/web/build-xec-apps/src/assets/chronikMethods.js @@ -0,0 +1,64 @@ +import { + getBlockHashHelper, + getTokenDetailsHelper, + getTxDetailsHelper, + getTxHistoryHelper, + getUtxosHelper, +} from '../utilities/chronikHelpers'; + +export const chronikMethods = [ + { + key: '1', + name: 'Get Block', + details: "const block = await chronik.block('')", + example: + '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f', + description: 'Accepts a block hash and returns block details', + inputLabel: 'Block Hash', + src: null, + method: getBlockHashHelper, + }, + { + key: '2', + name: 'Get Tx Details', + details: 'render code here', + example: + '0f3c3908a2ddec8dea91d2fe1f77295bbbb158af869bff345d44ae800f0a5498', + description: 'Accepts a txid and returns SLP tx data.', + inputLabel: 'eToken or XEC txid', + src: null, + method: getTxDetailsHelper, + }, + { + key: '3', + name: 'Get Tx History', + details: 'render code here', + example: '95E79F51D4260BC0DC3BA7FB77C7BE92D0FBDD1D', + description: + 'Get first page of tx history of the Genesis pubkey, most recent first.', + inputLabel: 'Tx History', + src: null, + method: getTxHistoryHelper, + }, + { + key: '4', + name: 'Get Utxos', + details: 'render code here', + example: '95E79F51D4260BC0DC3BA7FB77C7BE92D0FBDD1D', + description: 'Get utxos associated with a pubkey', + inputLabel: ' Genesis Pubkey', + src: null, + method: getUtxosHelper, + }, + { + key: '5', + name: 'Get Token Details', + details: "const tokenDetails = await chronik.token('')", + example: + '61302a235634ac9d8e227fd5d456d3cea5fa9d474b1d4d107664e03cbaab7405', + description: 'Accepts an eToken ID and returns the token details', + inputLabel: 'eToken ID', + src: null, + method: getTokenDetailsHelper, + }, +]; diff --git a/web/build-xec-apps/src/utilities/chronikHandlers.js b/web/build-xec-apps/src/utilities/chronikHandlers.js new file mode 100644 --- /dev/null +++ b/web/build-xec-apps/src/utilities/chronikHandlers.js @@ -0,0 +1,107 @@ +const { ChronikClient } = require('chronik-client'); +const util = require('util'); + +// For XEC, eCash chain: +const chronik = new ChronikClient('https://chronik.be.cash/xec'); + +// Get Genesis block (on eCash): +// const genesisHash = '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f' +export async function getBlock(blockhash) { + let blockDetails; + try { + blockDetails = await chronik.block(blockhash); + console.log(blockDetails); + } catch (err) { + console.log(`Error in chronik.block(${blockhash})`); + console.log(err); + } + return blockDetails; +} + +export async function getTokenDetails(eTokenAddress) { + let tokenDetails; + try { + tokenDetails = await chronik.token(eTokenAddress); + console.log( + util.inspect(tokenDetails, { + showHidden: false, + depth: null, + colors: true, + }), + ); + } catch (err) { + console.log(`Error in chronik.tx(${eTokenAddress})`); + console.log(err); + } + return tokenDetails; +} + +// txid +// const txid = '0f3c3908a2ddec8dea91d2fe1f77295bbbb158af869bff345d44ae800f0a5498'; +// // etoken txid +// const etokenTxid = +// '82a3fe0b03ab07a564351443634da1b1ed3960e4771c59b6f8abbf7ef4b3258d'; +// const etokenGenesis = +// 'acba1d7f354c6d4d001eb99d31de174e5cea8a31d692afd6e7eb8474ad541f55'; + +export async function getTxDetails(txid) { + let txDetails; + try { + txDetails = await chronik.tx(txid); + //console.log(txDetails); + console.log( + util.inspect(txDetails, { + showHidden: false, + depth: null, + colors: true, + }), + ); + } catch (err) { + console.log(`Error in chronik.tx(${txid})`); + console.log(err); + } + return txDetails; +} + +// const GENESIS_PK = +// '04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f'; + +// const TripDos_PK = +// '03771805b54969a9bea4e3eb14a82851c67592156ddb5e52d3d53677d14a40fba6'; + +// const TripDos_PK_hash160 = '95E79F51D4260BC0DC3BA7FB77C7BE92D0FBDD1D'; + +export async function getTxHistory(publickey) { + let history; + try { + history = await chronik + .script('p2pkh', publickey) + .history(/*page=*/ 0, /*page_size=*/ 10); + console.log(history); + } catch (err) { + console.log(`Error in chronik.script('p2pkh', ${publickey}).history()`); + console.log(err); + } + return history; +} + +// const GENESIS_PK = +// '04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f'; + +// const TripDos_PK = +// '03771805b54969a9bea4e3eb14a82851c67592156ddb5e52d3d53677d14a40fba6'; + +// const TripDos_PK_hash160 = '95E79F51D4260BC0DC3BA7FB77C7BE92D0FBDD1D'; + +export async function getUtxos(publickey) { + let utxos; + try { + utxos = await chronik.script('p2pkh', publickey).utxos(); + console.log(utxos); + console.log(utxos[0].utxos); + } catch (err) { + console.log(`Error in chronik.utxos(${publickey})`); + console.log(err); + } + return utxos; +} diff --git a/web/build-xec-apps/src/utilities/chronikHelpers.js b/web/build-xec-apps/src/utilities/chronikHelpers.js new file mode 100644 --- /dev/null +++ b/web/build-xec-apps/src/utilities/chronikHelpers.js @@ -0,0 +1,48 @@ +import { + getBlock, + getTokenDetails, + getTxDetails, + getTxHistory, + getUtxos, +} from './chronikHandlers'; + +export const getBlockHashHelper = async input => { + let res; + await getBlock(input).then(blockInfo => { + res = JSON.stringify(blockInfo, null, '\t'); + }); + return res; +}; + +export const getTxDetailsHelper = async input => { + let res; + await getTxDetails(input).then(txDetails => { + res = JSON.stringify(txDetails, null, '\t'); + }); + return res; +}; + +export const getTxHistoryHelper = async input => { + let result; + await getTxHistory(input) + .then(res => JSON.stringify(res, null, '\t')) + .then(res => (result = res)); + + return result; +}; + +export const getUtxosHelper = async input => { + let res; + await getUtxos(input).then(blockInfo => { + res = JSON.stringify(blockInfo, null, '\t'); + }); + return res; +}; + +export const getTokenDetailsHelper = async input => { + let res; + await getTokenDetails(input).then(blockInfo => { + res = JSON.stringify(blockInfo, null, '\t'); + }); + return res; +};