Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@xchainjs/xchain-util

Package Overview
Dependencies
Maintainers
9
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@xchainjs/xchain-util - npm Package Compare versions

Comparing version 0.6.1 to 0.7.0

11

CHANGELOG.md

@@ -0,1 +1,12 @@

# v.0.7.0 (2022-04-13)
### Add
- Helper `eqAsset`
- Estimate fees using any native Terra asset
### Breaking change
- Remove `AssetLuna` (will be supported by `xchain-terra`)
# v.0.6.1 (2022-04-04)

@@ -2,0 +13,0 @@

13

lib/asset.d.ts

@@ -136,8 +136,2 @@ import BigNumber from 'bignumber.js';

export declare const AssetDOGE: Asset;
/**
* Luna asset
* Based on definition in Thorchain
* @see TBD
*/
export declare const AssetLUNA: Asset;
export declare const RUNE_TICKER = "RUNE";

@@ -282,1 +276,8 @@ /**

}) => string;
/**
* Checks equality of two `Assets`
* @param {Asset} a Asset one
* @param {Asset} b Asset two
* @return {boolean} Result of equality check
*/
export declare const eqAsset: (a: Asset, b: Asset) => boolean;

@@ -346,8 +346,2 @@ import BigNumber from 'bignumber.js';

const AssetDOGE = { chain: Chain.Doge, symbol: 'DOGE', ticker: 'DOGE', synth: false };
/**
* Luna asset
* Based on definition in Thorchain
* @see TBD
*/
const AssetLUNA = { chain: Chain.Terra, symbol: 'LUNA', ticker: 'LUNA', synth: false };
const RUNE_TICKER = 'RUNE';

@@ -561,4 +555,11 @@ /**

const formatBaseAsAssetAmount = ({ amount, decimal, trimZeros = false, }) => formatAssetAmount({ amount: baseToAsset(amount), decimal, trimZeros });
/**
* Checks equality of two `Assets`
* @param {Asset} a Asset one
* @param {Asset} b Asset two
* @return {boolean} Result of equality check
*/
const eqAsset = (a, b) => a.chain === b.chain && a.symbol === b.symbol && a.ticker === b.ticker && a.synth === b.synth;
export { AssetBCH, AssetBNB, AssetBTC, AssetCurrencySymbol, AssetDOGE, AssetETH, AssetLTC, AssetLUNA, AssetRune67C, AssetRuneB1A, AssetRuneERC20, AssetRuneERC20Testnet, AssetRuneNative, BCHChain, BNBChain, BTCChain, Chain, CosmosChain, DOGEChain, Denomination, ETHChain, LTCChain, PolkadotChain, RUNE_TICKER, THORChain, TerraChain, assetAmount, assetFromString, assetToBase, assetToString, baseAmount, baseToAsset, bn, bnOrZero, chainToString, currencySymbolByAsset, delay, fixedBN, formatAssetAmount, formatAssetAmountCurrency, formatBN, formatBNCurrency, formatBaseAmount, formatBaseAsAssetAmount, isAssetAmount, isBaseAmount, isBigNumberValue, isChain, isSynthAsset, isValidAsset, isValidBN, trimZeros, validBNOrZero };
export { AssetBCH, AssetBNB, AssetBTC, AssetCurrencySymbol, AssetDOGE, AssetETH, AssetLTC, AssetRune67C, AssetRuneB1A, AssetRuneERC20, AssetRuneERC20Testnet, AssetRuneNative, BCHChain, BNBChain, BTCChain, Chain, CosmosChain, DOGEChain, Denomination, ETHChain, LTCChain, PolkadotChain, RUNE_TICKER, THORChain, TerraChain, assetAmount, assetFromString, assetToBase, assetToString, baseAmount, baseToAsset, bn, bnOrZero, chainToString, currencySymbolByAsset, delay, eqAsset, fixedBN, formatAssetAmount, formatAssetAmountCurrency, formatBN, formatBNCurrency, formatBaseAmount, formatBaseAsAssetAmount, isAssetAmount, isBaseAmount, isBigNumberValue, isChain, isSynthAsset, isValidAsset, isValidBN, trimZeros, validBNOrZero };
//# sourceMappingURL=index.esm.js.map

@@ -352,8 +352,2 @@ 'use strict';

const AssetDOGE = { chain: exports.Chain.Doge, symbol: 'DOGE', ticker: 'DOGE', synth: false };
/**
* Luna asset
* Based on definition in Thorchain
* @see TBD
*/
const AssetLUNA = { chain: exports.Chain.Terra, symbol: 'LUNA', ticker: 'LUNA', synth: false };
const RUNE_TICKER = 'RUNE';

@@ -563,2 +557,9 @@ /**

const formatBaseAsAssetAmount = ({ amount, decimal, trimZeros = false, }) => formatAssetAmount({ amount: baseToAsset(amount), decimal, trimZeros });
/**
* Checks equality of two `Assets`
* @param {Asset} a Asset one
* @param {Asset} b Asset two
* @return {boolean} Result of equality check
*/
const eqAsset = (a, b) => a.chain === b.chain && a.symbol === b.symbol && a.ticker === b.ticker && a.synth === b.synth;

@@ -571,3 +572,2 @@ exports.AssetBCH = AssetBCH;

exports.AssetLTC = AssetLTC;
exports.AssetLUNA = AssetLUNA;
exports.AssetRune67C = AssetRune67C;

@@ -600,2 +600,3 @@ exports.AssetRuneB1A = AssetRuneB1A;

exports.delay = delay;
exports.eqAsset = eqAsset;
exports.fixedBN = fixedBN;

@@ -602,0 +603,0 @@ exports.formatAssetAmount = formatAssetAmount;

{
"name": "@xchainjs/xchain-util",
"version": "0.6.1",
"version": "0.7.0",
"description": "Helper utilities for XChain clients",

@@ -5,0 +5,0 @@ "keywords": [

@@ -24,149 +24,7 @@ # `@xchainjs/xchain-util`

## Basic example usage for Asset Helpers
## Documentation
Imports
```ts
import {
assetAmount,
baseAmount,
isAssetAmount,
isBaseAmount,
baseToAsset,
assetToBase,
formatAssetAmount,
formatBaseAsAssetAmount,
formatAssetAmountCurrency,
} from '@xchainjs/xchain-util'
```
### [`xchain util`](http://docs.xchainjs.org/xchain-util/)
[`How xchain-util works`](http://docs.xchainjs.org/xchain-util/how-it-works.html)\
[`How to use xchain-util`](http://docs.xchainjs.org/xchain-util/how-to-use.html)
## Convert between Asset and base vice versa
Use the helper to convert between base amounts and Assets.
The Decimal represents the denomination of the asset. i.e BTC_DECIMAL = 8.
These are normally a constant exported by the client.
```ts
const assetConversions = async () => {
let amountToTransfer = 0.01
// convert to type baseAmount
let amount = assetToBase(assetAmount(amountToTransfer, BTC_DECIMAL))
console.log(`Amount has been converted to type base amount: ${JSON.stringify(amount)}`)
// Convert back to asset amount
let asset = (baseToAsset(amount).amount())
console.log(`Converted back to ${asset}`)
}
```
## Format helpers for assets
```ts
// Imports
import {
assetToBase,
assetAmount,
formatAssetAmount,
formatBaseAmount,
formatBNCurrency,
AssetBTC,
AssetCurrencySymbol,
formatAssetAmountCurrency,
currencySymbolByAsset,
AssetRuneNative,
AssetETH,
AssetLTC } from "@xchainjs/xchain-util"
// usage Example
const formatHelpers = async () => {
let amountToTransfer = 0.01
let Amount = assetToBase(assetAmount(amountToTransfer, DECIMAL))
// Formats an `AssetAmount` into `string` based on decimal places
let formatAsset = formatAssetAmount({ amount: assetAmount(amountToTransfer, BTC_DECIMAL), decimal: BTC_DECIMAL})
console.log(formatAsset)
// Formats a `BaseAmount` value into a `string`
let formatBase = formatBaseAmount(Amount)
console.log(formatBase)
// Formats a big number value by prefixing it with `$`
let formatBN = formatBNCurrency(Amount.amount())
console.log(formatBN)
// Formats an asset amount using its currency symbol
let formatAssetCurrency = formatAssetAmountCurrency({ amount: assetAmount(amountToTransfer, BTC_DECIMAL), asset: AssetBTC, decimal: BTC_DECIMAL})
console.log(formatAssetCurrency)
// Returns Asset symbol from ticker.
let assetA = currencySymbolByAsset(AssetRuneNative)
let assetB = currencySymbolByAsset(AssetBTC)
let assetC = currencySymbolByAsset(AssetETH)
let assetD = currencySymbolByAsset(AssetLTC)
console.log(assetA, assetB, assetC, assetD)
// Respective outputs
/*0.01000000
1,000,000
$1,000,000.00
⚡ 1,000,000
ᚱ ₿ Ξ LTC */
}
```
## BigNumber helper functions
```ts
// Imports
import {
bnOrZero,
validBNOrZero,
fixedBN} from "@xchainjs/xchain-util"
const bigNumberhelpers = async () => {
let amountToTransfer = 0.01
let Amount = assetToBase(assetAmount(amountToTransfer, DECIMAL))
// Helper to create a big number from string or number If it fails to create a big number
console.log(bnOrZero(amountToTransfer))
// Helper to validate a possible BigNumber If the given value is invalid or undefined
console.log(validBNOrZero(Amount.amount()))
// Helper to get a fixed BigNumber
console.log(fixedBN(Amount.amount()))
}
```
## Chain helper
```ts
//Imports
import { isChain } from from "@xchainjs/xchain-util"
const chain = async () => {
// Type guard to check whether string is based on type Chain
let thor = "THOR"
console.log(isChain(thor)) // Returns true
}
```
## Async helper
```ts
//Imports
import delay from '@xchainjs/xchain-util'
// Helper to delay anything within an async function
const anyAsyncFunc = async () => {
// do something
console.log('before delay')
// wait for 200ms
await delay(200)
// and do other things
console.log('after delay')
}
```

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc