1.1.0 • Published 7 months ago

@vols/sdk-core v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

@vols/sdk-core

A TypeScript library providing core functionality for blockchain and DeFi applications, with a focus on token handling, currency conversions, and price calculations.

npm version License: MIT

Installation

# Using npm
npm install @vols/sdk-core

# Using yarn
yarn add @vols/sdk-core

Features

  • Token Management: Easily create and manage ERC20 tokens with support for various chain IDs
  • Currency Handling: Work with native currencies (like ETH) and ERC20 tokens
  • Price Calculations: Utilities for price impact calculations and conversions
  • Fraction Utilities: Precise fraction handling for financial calculations
  • Address Validation: Tools for validating and parsing Ethereum addresses
  • Multi-chain Support: Built-in support for multiple blockchain networks

Usage Examples

Working with Tokens

import { Token, ChainId } from '@vols/sdk-core'

// Create a token instance
const myToken = new Token(
  1, // Ethereum Mainnet
  '0x1234567890123456789012345678901234567890', // Token address
  18, // Decimals
  'TKN', // Symbol
  'My Token' // Name
)

// Check if two tokens are the same
const isSameToken = myToken.equals(otherToken)

Working with Currency Amounts

import { Token, CurrencyAmount } from '@vols/sdk-core'

// Create a token
const myToken = new Token(1, '0x...', 18, 'TKN', 'My Token')

// Create a currency amount
const amount = CurrencyAmount.fromRawAmount(myToken, '1000000000000000000') // 1 TKN

// Perform operations
const doubled = amount.multiply('2')
const halved = amount.divide('2')

Price Calculations

import { Price, Token, CurrencyAmount } from '@vols/sdk-core'

// Create tokens
const tokenA = new Token(1, '0x...', 18, 'TKNA')
const tokenB = new Token(1, '0x...', 6, 'TKNB')

// Create amounts
const amountA = CurrencyAmount.fromRawAmount(tokenA, '1000000000000000000') // 1 TKNA
const amountB = CurrencyAmount.fromRawAmount(tokenB, '2000000') // 2 TKNB

// Create a price
const price = new Price(
  tokenA,
  tokenB,
  amountA.quotient,
  amountB.quotient
)

// Price is 2 TKNB per 1 TKNA
console.log(`Price: ${price.toSignificant(6)} ${tokenB.symbol} per ${tokenA.symbol}`)

Address Validation

import { validateAndParseAddress } from '@vols/sdk-core'

try {
  const parsedAddress = validateAndParseAddress('0x1234...')
  console.log('Valid address:', parsedAddress)
} catch (error) {
  console.error('Invalid address')
}

Core Components

Entities

  • Token: Represents an ERC20 token with address, decimals, symbol, and name
  • Currency: Base class for all currency types
  • NativeCurrency: Represents native blockchain currencies (like ETH)
  • CurrencyAmount: Represents an amount of a specific currency
  • Fraction: Provides precise fraction calculations
  • Price: Represents the price of one currency in terms of another
  • Percent: Utility for working with percentages

Utilities

  • validateAndParseAddress: Validates and checksums Ethereum addresses
  • computePriceImpact: Calculates price impact for trades
  • sqrt: Square root implementation optimized for DeFi calculations
  • sortedInsert: Utility for sorted insertions in arrays
  • computeZksyncCreate2Address: Utility for zkSync address computation

Development

# Install dependencies
yarn

# Start development mode
yarn start

# Build the package
yarn build

# Run tests
yarn test

# Lint the code
yarn lint

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.