A comprehensive TypeScript SDK for interacting with the different contracts and accounts accross multiple networks.
yarn add @nchamo/sdk
npm install @nchamo/sdk
import { buildSDK } from "@nchamo/sdk";
const sdk = buildSDK(config);
const accountBalances = await sdk.balanceService.getBalancesForTokens({
account: "0x000000000000000000000000000000000000dead",
tokens: {
[Chains.ETHEREUM.chainId]: [
"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // USDC
"0x6b175474e89094c44da98b954eedeac495271d0f", // DAI
],
[Chains.OPTIMISM.chainId]: [
"0x7f5c764cbc14f9669b88837ca1490cca17c31607", // USDC
"0xda10009cbd5d07dd0cecc66161fc93d7c9000da1", // DAI
],
},
config: {
timeout: "30s",
},
});
const accountAllowances = await sdk.allowanceService.getAllowances({
chainId: Chains.ETHEREUM.chainId,
token: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // USDC
owner: "0x000000000000000000000000000000000000dead",
spenders: ["0x6666666600000000000000000000000000009999"],
});
const allQuotes = await sdk.quoteService.getAllQuotesWithTxs({
request: {
chainId: Chains.ETHEREUM.chainId,
sellToken: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
buyToken: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
order: {
type: "sell",
sellAmount: utils.parseUnits("1000", 6), // 1000 USDC
},
slippagePercentage: 1,
takerAddress: signer.address,
gasSpeed: {
speed: "instant",
},
},
config: {
sort: {
by: "most-swapped-accounting-for-gas",
},
},
});
The Web3 SDK provides efficient tools to manage token balances, retrieve trade quotes from DEX aggregators, and check token holdings across multiple chains. It's designed to be modular, with each functionality organized into specialized services that handle specific aspects of blockchain interaction.
The SDK is divided into the following services:
- Allowances Service: Manage token approvals and permissions across different chains
- Balances Service: Query token balances across multiple chains and tokens
- Quotes Service: Get optimized swap quotes from various DEX aggregators
- Gas Service: Optimize transaction costs and estimate gas prices
- Prices Service: Retrieve token price information across multiple chains
- Metadata Service: Access token metadata and information
Each service provides a focused set of functionality while maintaining a consistent interface and error handling approach. This modular design allows developers to use only the services they need while ensuring a cohesive experience across the entire SDK.
The Allowances Service provides functionality to check and manage token allowances across different chains.
- Objective: Enable efficient management of token approvals across multiple chains and protocols
- Potential Use Cases:
- Batch checking multiple token approvals in a single call
- Optimizing gas costs by checking approvals before transactions
- Managing permissions for DeFi protocols and dApps
- Cross-chain allowance monitoring and management
Returns an array of chain IDs that are supported by the service.
const chains = sdk.allowanceService.supportedChains();
Gets the allowance for a specific token and spender on a given chain.
const allowance = await sdk.allowanceService.getAllowanceInChain({
chainId: Chains.ETHEREUM.chainId,
token: "0x...", // Token address
owner: "0x...", // Owner address
spender: "0x...", // Spender address
config: { timeout: TimeString },
});
Gets multiple allowances in a single call for a specific chain.
const allowances = await sdk.allowanceService.getAllowancesInChain({
chainId: Chains.ETHEREUM.chainId,
allowances: [
{ token: "0x...", owner: "0x...", spender: "0x..." },
{ token: "0x...", owner: "0x...", spender: "0x..." },
],
config: { timeout: TimeString },
});
Gets allowances across multiple chains in a single call.
const allowances = await sdk.allowanceService.getAllowances({
allowances: [
{
chainId: Chains.ETHEREUM.chainId,
token: "0x...",
owner: "0x...",
spender: "0x...",
},
{
chainId: Chains.OPTIMISM.chainId,
token: "0x...",
owner: "0x...",
spender: "0x...",
},
],
config: { timeout: TimeString },
});
The Balances Service allows querying token balances across multiple chains and tokens.
- Objective: Provide a unified interface for retrieving token balances across multiple chains
- Potential Use Cases:
- Portfolio tracking across multiple chains
- Balance monitoring for DeFi positions
- Multi-chain wallet integration
- Automated balance checks for trading strategies
Returns an array of chain IDs that are supported by the service.
const chains = sdk.balanceService.supportedChains();
Gets balances for a specific account in a single chain.
const balances = await sdk.balanceService.getBalancesForAccountInChain({
chainId: Chains.ETHEREUM.chainId,
account: "0x...",
tokens: ["0x...", "0x..."],
config: { timeout: "30s" },
});
Gets balances for a specific account across multiple chains.
const balances = await sdk.balanceService.getBalancesForAccount({
account: "0x...",
tokens: [
{ chainId: Chains.ETHEREUM.chainId, token: "0x..." },
{ chainId: Chains.OPTIMISM.chainId, token: "0x..." },
],
config: { timeout: "30s" },
});
Gets balances for multiple accounts in a single chain.
const balances = await sdk.balanceService.getBalancesInChain({
chainId: Chains.ETHEREUM.chainId,
tokens: [
{ account: "0x...", token: "0x..." },
{ account: "0x...", token: "0x..." },
],
config: { timeout: "30s" },
});
Gets balances for multiple accounts across multiple chains.
const balances = await sdk.balanceService.getBalances({
tokens: [
{ chainId: Chains.ETHEREUM.chainId, account: "0x...", token: "0x..." },
{ chainId: Chains.OPTIMISM.chainId, account: "0x...", token: "0x..." },
],
config: { timeout: "30s" },
});
The Quotes Service provides comprehensive functionality for getting trade quotes from various DEX aggregators.
- Objective: Aggregate and optimize trade quotes from multiple DEX sources
- Potential Use Cases:
- Finding the best trade routes across multiple DEXs
- Gas-optimized trading strategies
- Cross-chain arbitrage opportunities
- Automated trading systems
- Price impact analysis
Returns metadata about all supported quote sources.
const sources = sdk.quoteService.supportedSources();
Returns an array of chain IDs that are supported by the service.
const chains = sdk.quoteService.supportedChains();
Returns metadata about quote sources supported in a specific chain.
const sources = sdk.quoteService.supportedSourcesInChain({
chainId: Chains.ETHEREUM.chainId,
});
Returns supported gas speeds for each chain.
const gasSpeeds = sdk.quoteService.supportedGasSpeeds();
Gets estimated quotes from all sources without transaction details.
const quotes = sdk.quoteService.estimateQuotes({
request: {
chainId: Chains.ETHEREUM.chainId,
sellToken: "0x...",
buyToken: "0x...",
order: { type: "sell", sellAmount: BigInt("1000000") },
slippagePercentage: 1,
},
config: { timeout: TimeString },
});
Gets estimated quotes from all sources and returns them in a sorted array.
const quotes = await sdk.quoteService.estimateAllQuotes({
request: {
chainId: Chains.ETHEREUM.chainId,
sellToken: "0x...",
buyToken: "0x...",
order: { type: "sell", sellAmount: BigInt("1000000") },
slippagePercentage: 1,
},
config: {
ignoredFailed: boolean,
sort: { by: "most-swapped-accounting-for-gas", using: "gas-price" },
timeout: TimeString,
},
});
Gets quotes from all sources with transaction details.
const quotes = sdk.quoteService.getQuotes({
request: {
chainId: Chains.ETHEREUM.chainId,
sellToken: "0x...",
buyToken: "0x...",
order: { type: "sell", sellAmount: BigInt("1000000") },
slippagePercentage: 1,
takerAddress: "0x...",
},
config: { timeout: TimeString },
});
Gets quotes from all sources and returns them in a sorted array.
const quotes = await sdk.quoteService.getAllQuotes({
request: {
chainId: Chains.ETHEREUM.chainId,
sellToken: "0x...",
buyToken: "0x...",
order: { type: "sell", sellAmount: BigInt("1000000") },
slippagePercentage: 1,
takerAddress: "0x...",
},
config: {
ignoredFailed: boolean,
sort: { by: "most-swapped-accounting-for-gas", using: "gas-price" },
timeout: TimeString,
},
});
Gets the best quote according to specified criteria.
const bestQuote = await sdk.quoteService.getBestQuote({
request: {
chainId: Chains.ETHEREUM.chainId,
sellToken: "0x...",
buyToken: "0x...",
order: { type: "sell", sellAmount: BigInt("1000000") },
slippagePercentage: 1,
takerAddress: "0x...",
},
config: {
choose: { by: "most-swapped-accounting-for-gas", using: "gas-price" },
timeout: TimeString,
},
});
Gets quotes with built transactions from all sources.
const quotesWithTxs = await sdk.quoteService.getAllQuotesWithTxs({
request: {
chainId: Chains.ETHEREUM.chainId,
sellToken: "0x...",
buyToken: "0x...",
order: { type: "sell", sellAmount: BigInt("1000000") },
slippagePercentage: 1,
takerAddress: "0x...",
},
config: {
ignoredFailed: boolean,
sort: { by: "most-swapped-accounting-for-gas", using: "gas-price" },
timeout: TimeString,
},
});
Builds transactions for given quotes.
const txs = sdk.quoteService.buildTxs({
quotes: quotes,
sourceConfig: SourceConfig,
config: { timeout: TimeString },
});
Builds transactions for all quotes and returns them in a sorted array.
const allTxs = await sdk.quoteService.buildAllTxs({
quotes: quotes,
sourceConfig: SourceConfig,
config: {
timeout: TimeString,
ignoredFailed: boolean,
},
});
The Gas Service provides gas price estimation and optimization across different chains.
- Objective: Optimize transaction costs across different chains and networks
- Potential Use Cases:
- Gas price monitoring and optimization
- Transaction cost estimation
- Gas-aware trading strategies
- Multi-chain gas price comparison
- Automated gas price optimization
Returns an array of chain IDs that are supported by the service.
const chains = sdk.gasService.supportedChains();
Returns supported gas speeds for each chain.
const speeds = sdk.gasService.supportedSpeeds();
Estimates gas usage for a transaction.
const gasEstimation = await sdk.gasService.estimateGas({
chainId: Chains.ETHEREUM.chainId,
tx: {
from: "0x...",
to: "0x...",
data: "0x...",
},
config: { timeout: TimeString },
});
Gets gas prices for different speeds on a chain.
const gasPrices = await sdk.gasService.getGasPrice({
chainId: Chains.ETHEREUM.chainId,
config: {
timeout: TimeString,
fields: {
standard: "required" | "best effort" | "can ignore",
fast: "required" | "best effort" | "can ignore",
instant: "required" | "best effort" | "can ignore",
},
},
});
Calculates gas cost for a transaction.
const gasCost = await sdk.gasService.calculateGasCost({
chainId: Chains.ETHEREUM.chainId,
gasEstimation: BigInt("21000"),
tx: {
from: "0x...",
to: "0x...",
data: "0x...",
},
config: {
timeout: TimeString,
fields: {
standard: "required" | "best effort" | "can ignore",
fast: "required" | "best effort" | "can ignore",
instant: "required" | "best effort" | "can ignore",
},
},
});
Gets a quick gas calculator for a specific chain.
const calculator = await sdk.gasService.getQuickGasCalculator({
chainId: Chains.ETHEREUM.chainId,
config: {
timeout: TimeString,
fields: {
standard: "required" | "best effort" | "can ignore",
fast: "required" | "best effort" | "can ignore",
instant: "required" | "best effort" | "can ignore",
},
},
});
// Use the calculator
const gasPrices = calculator.getGasPrice();
const gasCost = calculator.calculateGasCost({
gasEstimation: BigInt("21000"),
tx: {
from: "0x...",
to: "0x...",
data: "0x...",
},
});
The Prices Service provides token price information across multiple chains.
- Objective: Provide a unified interface for retrieving token prices across multiple chains
- Potential Use Cases:
- Price feeds for DeFi applications
- Token value calculations
- Historical price analysis
- Price chart generation
- Multi-chain price aggregation
Returns an array of chain IDs that are supported by the service.
const chains = sdk.pricesService.supportedChains();
Returns the supported price queries for each chain.
const queries = sdk.pricesService.supportedQueries();
Gets current prices for tokens in a specific chain.
const prices = await sdk.pricesService.getCurrentPricesInChain({
chainId: Chains.ETHEREUM.chainId,
tokens: ["0x...", "0x..."],
config: { timeout: "30s" },
});
Gets current prices for tokens across multiple chains.
const prices = await sdk.pricesService.getCurrentPrices({
tokens: [
{ chainId: Chains.ETHEREUM.chainId, token: "0x..." },
{ chainId: Chains.OPTIMISM.chainId, token: "0x..." },
],
config: { timeout: "30s" },
});
Gets historical prices for tokens in a specific chain at a given timestamp.
const prices = await sdk.pricesService.getHistoricalPricesInChain({
chainId: Chains.ETHEREUM.chainId,
tokens: ["0x...", "0x..."],
timestamp: 1234567890,
searchWidth: "1h",
config: { timeout: "30s" },
});
Gets historical prices for tokens across multiple chains at a given timestamp.
const prices = await sdk.pricesService.getHistoricalPrices({
tokens: [
{ chainId: Chains.ETHEREUM.chainId, token: "0x..." },
{ chainId: Chains.OPTIMISM.chainId, token: "0x..." },
],
timestamp: 1234567890,
searchWidth: "1h",
config: { timeout: "30s" },
});
Gets historical prices for multiple tokens at different timestamps.
const prices = await sdk.pricesService.getBulkHistoricalPrices({
tokens: [
{ chainId: Chains.ETHEREUM.chainId, token: "0x...", timestamp: 1234567890 },
{ chainId: Chains.OPTIMISM.chainId, token: "0x...", timestamp: 1234567890 },
],
searchWidth: "1h",
config: { timeout: "30s" },
});
Gets price chart data for tokens over a specified time period.
const chart = await sdk.pricesService.getChart({
tokens: [
{ chainId: Chains.ETHEREUM.chainId, token: "0x..." },
{ chainId: Chains.OPTIMISM.chainId, token: "0x..." },
],
span: 100,
period: "1d",
bound: { from: 1234567890 },
searchWidth: "1h",
});
The Metadata Service provides token metadata information across multiple chains.
- Objective: Provide a unified interface for retrieving token metadata across multiple chains
- Potential Use Cases:
- Token information display in UIs
- Token validation and verification
- Multi-chain token management
- Token data aggregation
Returns an array of chain IDs that are supported by the service.
const chains = sdk.metadataService.supportedChains();
Returns the supported metadata properties for each chain.
const properties = sdk.metadataService.supportedProperties();
Gets metadata for tokens in a specific chain.
const metadata = await sdk.metadataService.getMetadataInChain({
chainId: Chains.ETHEREUM.chainId,
tokens: ["0x...", "0x..."],
config: {
fields: { symbol: "required", decimals: "required" },
timeout: "30s",
},
});
Gets metadata for tokens across multiple chains.
const metadata = await sdk.metadataService.getMetadata({
tokens: [
{ chainId: Chains.ETHEREUM.chainId, token: "0x..." },
{ chainId: Chains.OPTIMISM.chainId, token: "0x..." },
],
config: {
fields: { symbol: "required", decimals: "required" },
timeout: "30s",
},
});
The SDK provides comprehensive error handling for all services:
try {
const quotes = await sdk.quoteService.getAllQuotes({...});
} catch (error) {
if (error instanceof FailedToGenerateAnyQuotesError) {
// Handle quote generation failure
}
}
Each service can be configured with custom timeouts and other parameters:
const quotes = await sdk.quoteService.getAllQuotes({
request: {...},
config: {
timeout: "30s",
ignoredFailed: true,
sort: {
by: "most-swapped-accounting-for-gas",
using: "gas-price"
}
}
});
All services support operations across multiple chains:
const balances = await sdk.balanceService.getBalancesForTokens({
account: "0x...",
tokens: {
[Chains.ETHEREUM.chainId]: ["0x..."],
[Chains.OPTIMISM.chainId]: ["0x..."],
[Chains.ARBITRUM.chainId]: ["0x..."],
},
});
yarn install
Contributions are welcome! Please feel free to submit a Pull Request.