A lightweight "Dapp Survival Kit" to help applications deliver fully functional experiences without sacrificing decentralization, security or privacy
npm install dskit-eth
!DSKit
_A lightweight "Dapp Survival Kit" to help applications deliver fully functional experiences without sacrificing decentralization, security or privacy_
---
cli
npm i dskit-eth
`
$3
`js
import { DSKit } from 'dskit-eth'
const dskit = new DSKit({ rpcUrl: 'https://your-privacy-respecting-rpc/' })
`
$3
`js
import { DSKit } from "dskit-eth"
import { createPublicClient } from "viem"
const dskit = new DSKit({
viemPublicClient: createPublicClient({ ... })
})
`
_More Provider Support Coming Soon..._
---
Modules
$3
Swap or get quotes from one token to another without any external APIs. Current market support includes LPs on Uniswap V3, Uniswap V2, Velodrome, Aerodrome and Ramses.
#### Example:
`js
// Get a quote and swap tx data for USDC -> WETH on Optimism
const swap = await dskit.swap.route({
tokenIn: { address: '0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85', decimals: 6, amount: 10n ** 8n }, // USDC
tokenOut: { address: '0x4200000000000000000000000000000000000006', decimals: 18 }, // WETH
executionOptions: {
recipient: '0xbE4FeAE32210f682A41e1C41e3eaF4f8204cD29E'; // Recipient of the WETH
slippage: 0.001; // 0.1% slippage
}
})
const quoteAmountWeth = swap.quote
const swapRequest = swap.request
`
---
$3
Get token prices for your dapp using live onchain market data.
#### Example:
`js
// Price of WETH in USDC on Optimism
const priceOfWethInUsdc = await dskit.price.ofToken({
token: {
address: '0x4200000000000000000000000000000000000006',
decimals: 18
},
tokenDenominator: {
address: '0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85',
decimals: 6
}
})
`
---
$3
Zap native ETH or tokens into other tokens and bundle actions together.
#### Example:
`js
// Zap from native ETH on Optimism into the PoolTogether Prize USDC vault.
// This performs 3 steps in one tx:
// 1. wraps ETH to WETH
// 2. swaps WETH to USDC
// 3. deposits USDC to przUSDC as an additional action
import { dolphinAddress, zapRouter } from 'dskit-eth'
import { parseAbi } from 'viem'
const nativeEth = {
address: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', // use this for native ETH
decimals: 18,
amount: 10n ** 12n
}
const usdc = {
address: '0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85',
decimals: 6
}
const zapTx = await dskit.zap.tx({
tokenIn: nativeEth,
swapTo: usdc,
action: {
address: '0x03D3CE84279cB6F54f5e6074ff0F8319d830dafe',
abi: parseAbi(['function deposit(uint256,address) returns (uint256)']),
functionName: 'deposit',
args: [0n, zapRouter[10]],
injectedAmountIndex: 4 // replaces the first arg with the output from the swap
},
tokenOut: { address: '0x03D3CE84279cB6F54f5e6074ff0F8319d830dafe', minAmount: 1n },
userAddress: '0xbE4FeAE32210f682A41e1C41e3eaF4f8204cD29E'
})
const config = zapTx.config
const gasEstimate = zapTx.gasEstimate
const zapRequest = zapTx.request
const route = zapTx.route
`
---
$3
Query event logs without worrying about rate limits, block range limits, setting up callbacks, etc.
#### Example:
`js
const transferEvents = await dskit.event.query(
{
address: '0x0cEC1A9154Ff802e7934Fc916Ed7Ca50bDE6844e',
event: {
type: 'event',
name: 'Transfer',
inputs: [
{ indexed: true, name: 'from', type: 'address' },
{ indexed: true, name: 'to', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' }
]
},
args: { to: '0xbE4FeAE32210f682A41e1C41e3eaF4f8204cD29E' },
fromBlock: 16_078_500n,
toBlock: 16_235_800n
},
{
maxPageSizeInBlocks: 100_000n,
paginationDelayInMs: 500,
callback: (log) => {
// handle new event log found (indexing, caching, etc.)
}
}
)
`
---
$3
Provides block tooling for fetching a block within (or close to) a given
timestamp range.
#### Examples
`js
const block = await dskit.block.nearTimestamp({
targetTimestamp: Math.floor(Date.now() / 1000) - 86_400_000, // yesterday
targetRangeSeconds: 60
})
const blockNumber = block.number
``