npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@stacks/ui-utils

v7.5.0

Published

UI utilities for Stacks UI based projects.

Downloads

213

Readme

Stacks UI utilities

Table of contents

Accounts

hasAssetBalance

This is a helper to determine if a principal has any asset balances -- fungible or non-fungible.

Takes a balances response object.

const data = {
  stx: {
    balance: '1000000',
    total_sent: '0',
    total_received: '1000000',
  },
  fungible_tokens: {},
  non_fungible_tokens: {},
};

if (hasAssetBalance(data)) {
  // do something
}

Colors

generateHash

This takes a string and generates a number has to use with %.

const hash = generateHash('some string');
// get a number between 0-360
const hue = hash % 360;

hashValue

This function generates a hash from a string, and then gets a value from an array. An example usage would be to have an array of colors you'd like to pick from.

const colors = ['red', 'blue', 'green'];
const string = 'Hiro Protaganist';
const colorFromString = hashValue<('red' | 'blue' | 'green')[]>(string, colors);
// will be one of the colors

stringToHslColor

This takes a string, saturation value, and lightness value to output a color in HSL.

const string = 'Hiro Protaganist';
const color = stringToHslColor(string, 60, 55);
// an hsl color

moduloRange

This is a helper function to select a number from a range.

const restrictedHue = moduloRange(hash, [40, 60], true);
// will return a number between 40 and 60, including 60 as an option

Errors

getTxErrorMessage

Will return a human readable error message for the status of a transaction.

// tx.tx_status === "abort_by_post_condition"
const error = getTxErrorMessage(tx);
// This transaction would have succeeded, but was rolled back by a supplied post-condition.

// tx.tx_status === "abort_by_response"
const error = getTxErrorMessage(tx);
// This transaction did not succeed because the transaction was aborted during its execution.

Strings

capitalize

This will convert the first letter of a string to upper case.

const string = 'hello stx';

console.log(capitalize(string));
// -> Hello stx

with0x

This will prefix a string with 0x, unless it contains a ..

const hex = `33cc9a437e704e790958f7bb66492f5ad3a863ab3bcbef47138069725549a353`;
const contract = 'ST12EY99GS4YKP0CP2CFW6SEPWQ2CGVRWK5GHKDRV.market';
const id = 'muneeb.id';

const query = with0x(contract);
// ST12EY99GS4YKP0CP2CFW6SEPWQ2CGVRWK5GHKDRV.market

const query = with0x(id);
// muneeb.id

const query = with0x(hex);
// 0x33cc9a437e704e790958f7bb66492f5ad3a863ab3bcbef47138069725549a353

truncateHex

This will truncate a hex, keeping the 0x out of the offset amount.

const hex = `0x33cc9a437e704e790958f7bb66492f5ad3a863ab3bcbef47138069725549a353`;

const shortened = truncateHex(hex, 4);
// 0x33cc...a353

truncateMiddle

This will truncate any string in the middle, given an offset amount.

const hex = `0x33cc9a437e704e790958f7bb66492f5ad3a863ab3bcbef47138069725549a353`;

const shortened = truncateHex(hex, 4);
// 0x33cc...a353

const contract = 'ST12EY99GS4YKP0CP2CFW6SEPWQ2CGVRWK5GHKDRV.market';

const shortenedContract = truncateHex(contract, 4);
// ST12...KDRV.market

withSTX

This will wrap any value with a suffix STX.

const value = '1,000.00';

console.log(withSTX(value));
// 1,000.00 STX

addSepBetweenStrings

This will combine an array of strings with a divider character.

const string = addSepBetweenStrings(['Pending', 'Contract deploy', '5 min ago'], '|');

// Pending | Contract deploy | 5 min ago

Transactions

getContractName

This will parse a string and return the contract name.

const contract =
  'ST12EY99GS4YKP0CP2CFW6SEPWQ2CGVRWK5GHKDRV.market' ||
  'ST12EY99GS4YKP0CP2CFW6SEPWQ2CGVRWK5GHKDRV.market::asset-name';

const name = getContractName(contract);

// market

getAssetName

This will parse a fully qualified asset name string and pull out the name of the asset.

const contract = 'ST12EY99GS4YKP0CP2CFW6SEPWQ2CGVRWK5GHKDRV.market::asset-name';

const asset = getAssetName(contract);

// asset-name

getAssetStringParts

This will parse a fully qualified asset name string and return the various parts: address, contractName, assetName.

const contract = 'ST12EY99GS4YKP0CP2CFW6SEPWQ2CGVRWK5GHKDRV.market::asset-name';

const { address, contractName, assetName } = getAssetStringParts(contract);

// ST12EY99GS4YKP0CP2CFW6SEPWQ2CGVRWK5GHKDRV
// market
// asset-name

hasMemo

Helper function to determine if a transaction has a memo.

if (hasMemo(tx)) {
  // do something
}

getRecipientAddress

Returns the recipient address from a token transfer transaction.

const recipient = getRecipientAddress(tx);
// ST12EY99GS4YKP0CP2CFW6SEPWQ2CGVRWK5GHKDRV

getFunctionName

Returns the function name from a contract call transaction.

const functionName = getFunctionName(tx); // a contract-call tx
// set-value

isPendingTx

Check if transaction is pending.

if (isPendingTx(tx)) {
  // do something
}

getTxTitle

Returns a pretty title for a transaction.

switch (transaction.tx_type) {
  case 'smart_contract':
  // returns contract-name -> key-value-contract
  case 'contract_call':
  // returns function-name -> set-value
  case 'token_transfer':
  // returns human readable stx amount -> 1,000.00 STX
  case 'coinbase':
  // returns -> Block #256 coinbase;
  default:
  // returns truncated tx_id -> 0x33cc...a353
}

getTxTypeName

Returns a pretty name for transaction type.

const txTypeNamesMap = {
  [TransactionType.SMART_CONTRACT]: 'Contract deploy',
  [TransactionType.CONTRACT_CALL]: 'Function call',
  [TransactionType.TOKEN_TRANSFER]: 'Token transfer',
  [TransactionType.COINBASE]: 'Coinbase',
  [TransactionType.POISON_MICROBLOCK]: 'Poison-microblock',
} as const;

getMemoString

Returns the memo as a human readable string.

const memo = getMemoString(tx);
// example: Faucet

Units

microStxToStx

Converts uSTX to STX.

toHumanReadableStx

Converts uSTX to STX with suffix and number formatting.

stxToMicroStx

Converts STX to uSTX.

Validation

validateHex

Checks validity of a hash string via regex.

validateStacksAddress

Checks validity of a Stacks address.

validateContractId

Checks validity of a contract_id ([principal].[contract_name]).