zerion-sdk
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -1,29 +0,17 @@ | ||
import { ChainData, PortfolioData, PositionData } from "./types"; | ||
export declare class ZerionAPI { | ||
private apiKey; | ||
env: string | undefined; | ||
import { iZerionAPI, iZerionUI } from "./types/interface"; | ||
import { ChainData, FungibleTokenData, PortfolioData, PositionData, UserBalanceOptions, UserDashboardResponse } from "./types"; | ||
import { ZerionService } from "./services/zerion"; | ||
export declare class ZerionAPI implements iZerionAPI { | ||
service: ZerionService; | ||
readonly ui: iZerionUI; | ||
constructor(apiKey: string, testnet: boolean); | ||
/** | ||
* Fetches all supported blockchain chains from Zerion. | ||
* @returns A promise resolving to an array of chains supported by Zerion. | ||
*/ | ||
getChains(): Promise<ChainData[]>; | ||
/** | ||
* Fetches the portfolio of a specific wallet, partitioned by network. | ||
* @param walletAddress The wallet address whose portfolio will be fetched. | ||
* @param apiKey The Zerion API key for authorization. | ||
* @param currency Optional currency code to get portfolio values (e.g., 'usd'). | ||
* @returns A promise resolving to the portfolio data for the wallet. | ||
*/ | ||
getPortfolio(walletAddress: string, currency?: string): Promise<PortfolioData>; | ||
/** | ||
* Fetches the fungible token positions of a specific wallet. | ||
* @param walletAddress The wallet address whose fungible positions will be fetched. | ||
* @param currency Optional currency code to get token values (e.g., 'usd'). | ||
* @param filterPositions Optional filter to include only simple or specific positions. | ||
* @param filterTrash Optional filter to exclude positions marked as trash. | ||
* @param sort Optional sorting parameter (e.g., 'value'). | ||
* @returns A promise resolving to the wallet's fungible token positions. | ||
*/ | ||
getFungiblePositions(walletAddress: string, currency?: string, filterPositions?: string, filterTrash?: string, sort?: string): Promise<PositionData[]>; | ||
fungibles(id: string): Promise<FungibleTokenData>; | ||
} | ||
export declare class ZerionUI implements iZerionUI { | ||
private client; | ||
constructor(client: ZerionAPI); | ||
getUserBalances(walletAddress: string, options?: UserBalanceOptions): Promise<UserDashboardResponse>; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ZerionAPI = void 0; | ||
const BASE_URL = "https://api.zerion.io/v1"; | ||
// Utility function to make API requests with error handling | ||
async function fetchFromZerion(endpoint, apiKey, env) { | ||
const headers = { | ||
accept: "application/json", | ||
authorization: `Basic ${apiKey}`, | ||
// Optionally add the "X-Env" header for testnet or other environments | ||
...(env ? { "X-Env": env } : {}), | ||
}; | ||
const response = await fetch(`${BASE_URL}${endpoint}`, { headers }); | ||
if (!response.ok) { | ||
throw new Error(`Failed to fetch ${endpoint}: ${response.statusText}`); | ||
} | ||
const data = await response.json(); | ||
return data; | ||
} | ||
exports.ZerionUI = exports.ZerionAPI = void 0; | ||
const ui_1 = require("./transform/ui"); | ||
const zerion_1 = require("./services/zerion"); | ||
const config_1 = require("./config"); | ||
class ZerionAPI { | ||
constructor(apiKey, testnet) { | ||
this.apiKey = apiKey; | ||
this.env = testnet ? "testnet" : undefined; | ||
this.service = new zerion_1.ZerionService(apiKey, testnet ? "testnet" : undefined); | ||
this.ui = new ZerionUI(this); | ||
} | ||
/** | ||
* Fetches all supported blockchain chains from Zerion. | ||
* @returns A promise resolving to an array of chains supported by Zerion. | ||
*/ | ||
async getChains() { | ||
const { data } = await fetchFromZerion("/chains/", this.apiKey, this.env); | ||
const { data } = await this.service.fetchFromZerion("/chains/"); | ||
return data; | ||
} | ||
/** | ||
* Fetches the portfolio of a specific wallet, partitioned by network. | ||
* @param walletAddress The wallet address whose portfolio will be fetched. | ||
* @param apiKey The Zerion API key for authorization. | ||
* @param currency Optional currency code to get portfolio values (e.g., 'usd'). | ||
* @returns A promise resolving to the portfolio data for the wallet. | ||
*/ | ||
async getPortfolio(walletAddress, currency = "usd") { | ||
const { data } = await fetchFromZerion(`/wallets/${walletAddress}/portfolio?currency=${currency}`, this.apiKey, this.env); | ||
const { data } = await this.service.fetchFromZerion(`/wallets/${walletAddress}/portfolio?currency=${currency}`); | ||
return data; | ||
} | ||
/** | ||
* Fetches the fungible token positions of a specific wallet. | ||
* @param walletAddress The wallet address whose fungible positions will be fetched. | ||
* @param currency Optional currency code to get token values (e.g., 'usd'). | ||
* @param filterPositions Optional filter to include only simple or specific positions. | ||
* @param filterTrash Optional filter to exclude positions marked as trash. | ||
* @param sort Optional sorting parameter (e.g., 'value'). | ||
* @returns A promise resolving to the wallet's fungible token positions. | ||
*/ | ||
async getFungiblePositions(walletAddress, currency = "usd", filterPositions = "only_simple", filterTrash = "only_non_trash", sort = "value") { | ||
const { data } = await fetchFromZerion(`/wallets/${walletAddress}/positions/?filter[positions]=${filterPositions}¤cy=${currency}&filter[trash]=${filterTrash}&sort=${sort}`, this.apiKey, this.env); | ||
async getFungiblePositions(walletAddress, currency = config_1.ZERION_CONFIG.DEFAULT_CURRENCY, filterPositions = config_1.ZERION_CONFIG.DEFAULT_FILTER, filterTrash = config_1.ZERION_CONFIG.DEFAULT_TRASH_FILTER, sort = config_1.ZERION_CONFIG.DEFAULT_SORT) { | ||
const { data } = await this.service.fetchFromZerion(`/wallets/${walletAddress}/positions/?filter[positions]=${filterPositions}¤cy=${currency}&filter[trash]=${filterTrash}&sort=${sort}`); | ||
return data; | ||
} | ||
async fungibles(id) { | ||
const { data } = await this.service.fetchFromZerion(`/fungibles/${id}`); | ||
return data; | ||
} | ||
} | ||
exports.ZerionAPI = ZerionAPI; | ||
class ZerionUI { | ||
constructor(client) { | ||
this.client = client; | ||
} | ||
async getUserBalances(walletAddress, options) { | ||
const [chains, positions] = await Promise.all([ | ||
this.client.getChains(), | ||
this.client.getFungiblePositions(walletAddress), | ||
]); | ||
// If showZeroNative, fetch native token info for relevant chains | ||
let nativeTokens = {}; | ||
if (options?.showZeroNative) { | ||
const supportedChains = options?.supportedChains; | ||
const relevantChains = supportedChains | ||
? chains.filter((chain) => supportedChains.includes(parseInt(chain.attributes.external_id, 16))) | ||
: chains; | ||
const nativeTokenResponses = await Promise.all(relevantChains.map(async (chain) => { | ||
const nativeTokenId = chain.relationships.native_fungible.data.id; | ||
const tokenData = await this.client.fungibles(nativeTokenId); | ||
return { chainId: chain.id, tokenData }; | ||
})); | ||
nativeTokens = Object.fromEntries(nativeTokenResponses.map(({ chainId, tokenData }) => [ | ||
chainId, | ||
tokenData, | ||
])); | ||
} | ||
return (0, ui_1.transformPositionDataToUserDashboardResponse)(positions, chains, { | ||
...options, | ||
nativeTokens, // Pass native token data to transform | ||
}); | ||
} | ||
} | ||
exports.ZerionUI = ZerionUI; |
export * from "./types"; | ||
export * from "./client"; | ||
export * from "./transform/ui"; |
@@ -19,1 +19,2 @@ "use strict"; | ||
__exportStar(require("./client"), exports); | ||
__exportStar(require("./transform/ui"), exports); |
@@ -26,4 +26,6 @@ import { Links } from "./common"; | ||
export interface FungibleRelationship { | ||
type: string; | ||
id: string; | ||
data: { | ||
type: string; | ||
id: string; | ||
}; | ||
links: Links; | ||
@@ -30,0 +32,0 @@ } |
export interface Links { | ||
self: string; | ||
self?: string; | ||
related?: string; | ||
} |
import { Links } from "./common"; | ||
import { FungibleImplementation } from "./fungibles"; | ||
export interface FungiblePositionsResponse { | ||
@@ -20,2 +21,3 @@ links: Links; | ||
price: number; | ||
changes: null | unknown; | ||
fungible_info: FungibleInfo; | ||
@@ -45,7 +47,2 @@ flags: PositionFlags; | ||
} | ||
export interface FungibleImplementation { | ||
chain_id: string; | ||
address: string | null; | ||
decimals: number; | ||
} | ||
export interface PositionFlags { | ||
@@ -52,0 +49,0 @@ displayable: boolean; |
@@ -5,1 +5,4 @@ export * from "./chains"; | ||
export * from "./portfolio"; | ||
export * from "./ui"; | ||
export * from "./interface"; | ||
export * from "./fungibles"; |
@@ -21,1 +21,4 @@ "use strict"; | ||
__exportStar(require("./portfolio"), exports); | ||
__exportStar(require("./ui"), exports); | ||
__exportStar(require("./interface"), exports); | ||
__exportStar(require("./fungibles"), exports); |
@@ -1,29 +0,17 @@ | ||
import { ChainData, PortfolioData, PositionData } from "./types"; | ||
export declare class ZerionAPI { | ||
private apiKey; | ||
env: string | undefined; | ||
import { iZerionAPI, iZerionUI } from "./types/interface"; | ||
import { ChainData, FungibleTokenData, PortfolioData, PositionData, UserBalanceOptions, UserDashboardResponse } from "./types"; | ||
import { ZerionService } from "./services/zerion"; | ||
export declare class ZerionAPI implements iZerionAPI { | ||
service: ZerionService; | ||
readonly ui: iZerionUI; | ||
constructor(apiKey: string, testnet: boolean); | ||
/** | ||
* Fetches all supported blockchain chains from Zerion. | ||
* @returns A promise resolving to an array of chains supported by Zerion. | ||
*/ | ||
getChains(): Promise<ChainData[]>; | ||
/** | ||
* Fetches the portfolio of a specific wallet, partitioned by network. | ||
* @param walletAddress The wallet address whose portfolio will be fetched. | ||
* @param apiKey The Zerion API key for authorization. | ||
* @param currency Optional currency code to get portfolio values (e.g., 'usd'). | ||
* @returns A promise resolving to the portfolio data for the wallet. | ||
*/ | ||
getPortfolio(walletAddress: string, currency?: string): Promise<PortfolioData>; | ||
/** | ||
* Fetches the fungible token positions of a specific wallet. | ||
* @param walletAddress The wallet address whose fungible positions will be fetched. | ||
* @param currency Optional currency code to get token values (e.g., 'usd'). | ||
* @param filterPositions Optional filter to include only simple or specific positions. | ||
* @param filterTrash Optional filter to exclude positions marked as trash. | ||
* @param sort Optional sorting parameter (e.g., 'value'). | ||
* @returns A promise resolving to the wallet's fungible token positions. | ||
*/ | ||
getFungiblePositions(walletAddress: string, currency?: string, filterPositions?: string, filterTrash?: string, sort?: string): Promise<PositionData[]>; | ||
fungibles(id: string): Promise<FungibleTokenData>; | ||
} | ||
export declare class ZerionUI implements iZerionUI { | ||
private client; | ||
constructor(client: ZerionAPI); | ||
getUserBalances(walletAddress: string, options?: UserBalanceOptions): Promise<UserDashboardResponse>; | ||
} |
@@ -1,56 +0,60 @@ | ||
const BASE_URL = "https://api.zerion.io/v1"; | ||
// Utility function to make API requests with error handling | ||
async function fetchFromZerion(endpoint, apiKey, env) { | ||
const headers = { | ||
accept: "application/json", | ||
authorization: `Basic ${apiKey}`, | ||
// Optionally add the "X-Env" header for testnet or other environments | ||
...(env ? { "X-Env": env } : {}), | ||
}; | ||
const response = await fetch(`${BASE_URL}${endpoint}`, { headers }); | ||
if (!response.ok) { | ||
throw new Error(`Failed to fetch ${endpoint}: ${response.statusText}`); | ||
} | ||
const data = await response.json(); | ||
return data; | ||
} | ||
import { transformPositionDataToUserDashboardResponse } from "./transform/ui"; | ||
import { ZerionService } from "./services/zerion"; | ||
import { ZERION_CONFIG } from "./config"; | ||
export class ZerionAPI { | ||
apiKey; | ||
env; | ||
service; | ||
ui; | ||
constructor(apiKey, testnet) { | ||
this.apiKey = apiKey; | ||
this.env = testnet ? "testnet" : undefined; | ||
this.service = new ZerionService(apiKey, testnet ? "testnet" : undefined); | ||
this.ui = new ZerionUI(this); | ||
} | ||
/** | ||
* Fetches all supported blockchain chains from Zerion. | ||
* @returns A promise resolving to an array of chains supported by Zerion. | ||
*/ | ||
async getChains() { | ||
const { data } = await fetchFromZerion("/chains/", this.apiKey, this.env); | ||
const { data } = await this.service.fetchFromZerion("/chains/"); | ||
return data; | ||
} | ||
/** | ||
* Fetches the portfolio of a specific wallet, partitioned by network. | ||
* @param walletAddress The wallet address whose portfolio will be fetched. | ||
* @param apiKey The Zerion API key for authorization. | ||
* @param currency Optional currency code to get portfolio values (e.g., 'usd'). | ||
* @returns A promise resolving to the portfolio data for the wallet. | ||
*/ | ||
async getPortfolio(walletAddress, currency = "usd") { | ||
const { data } = await fetchFromZerion(`/wallets/${walletAddress}/portfolio?currency=${currency}`, this.apiKey, this.env); | ||
const { data } = await this.service.fetchFromZerion(`/wallets/${walletAddress}/portfolio?currency=${currency}`); | ||
return data; | ||
} | ||
/** | ||
* Fetches the fungible token positions of a specific wallet. | ||
* @param walletAddress The wallet address whose fungible positions will be fetched. | ||
* @param currency Optional currency code to get token values (e.g., 'usd'). | ||
* @param filterPositions Optional filter to include only simple or specific positions. | ||
* @param filterTrash Optional filter to exclude positions marked as trash. | ||
* @param sort Optional sorting parameter (e.g., 'value'). | ||
* @returns A promise resolving to the wallet's fungible token positions. | ||
*/ | ||
async getFungiblePositions(walletAddress, currency = "usd", filterPositions = "only_simple", filterTrash = "only_non_trash", sort = "value") { | ||
const { data } = await fetchFromZerion(`/wallets/${walletAddress}/positions/?filter[positions]=${filterPositions}¤cy=${currency}&filter[trash]=${filterTrash}&sort=${sort}`, this.apiKey, this.env); | ||
async getFungiblePositions(walletAddress, currency = ZERION_CONFIG.DEFAULT_CURRENCY, filterPositions = ZERION_CONFIG.DEFAULT_FILTER, filterTrash = ZERION_CONFIG.DEFAULT_TRASH_FILTER, sort = ZERION_CONFIG.DEFAULT_SORT) { | ||
const { data } = await this.service.fetchFromZerion(`/wallets/${walletAddress}/positions/?filter[positions]=${filterPositions}¤cy=${currency}&filter[trash]=${filterTrash}&sort=${sort}`); | ||
return data; | ||
} | ||
async fungibles(id) { | ||
const { data } = await this.service.fetchFromZerion(`/fungibles/${id}`); | ||
return data; | ||
} | ||
} | ||
export class ZerionUI { | ||
client; | ||
constructor(client) { | ||
this.client = client; | ||
} | ||
async getUserBalances(walletAddress, options) { | ||
const [chains, positions] = await Promise.all([ | ||
this.client.getChains(), | ||
this.client.getFungiblePositions(walletAddress), | ||
]); | ||
// If showZeroNative, fetch native token info for relevant chains | ||
let nativeTokens = {}; | ||
if (options?.showZeroNative) { | ||
const supportedChains = options?.supportedChains; | ||
const relevantChains = supportedChains | ||
? chains.filter((chain) => supportedChains.includes(parseInt(chain.attributes.external_id, 16))) | ||
: chains; | ||
const nativeTokenResponses = await Promise.all(relevantChains.map(async (chain) => { | ||
const nativeTokenId = chain.relationships.native_fungible.data.id; | ||
const tokenData = await this.client.fungibles(nativeTokenId); | ||
return { chainId: chain.id, tokenData }; | ||
})); | ||
nativeTokens = Object.fromEntries(nativeTokenResponses.map(({ chainId, tokenData }) => [ | ||
chainId, | ||
tokenData, | ||
])); | ||
} | ||
return transformPositionDataToUserDashboardResponse(positions, chains, { | ||
...options, | ||
nativeTokens, // Pass native token data to transform | ||
}); | ||
} | ||
} |
export * from "./types"; | ||
export * from "./client"; | ||
export * from "./transform/ui"; |
export * from "./types"; | ||
export * from "./client"; | ||
export * from "./transform/ui"; |
@@ -26,4 +26,6 @@ import { Links } from "./common"; | ||
export interface FungibleRelationship { | ||
type: string; | ||
id: string; | ||
data: { | ||
type: string; | ||
id: string; | ||
}; | ||
links: Links; | ||
@@ -30,0 +32,0 @@ } |
export interface Links { | ||
self: string; | ||
self?: string; | ||
related?: string; | ||
} |
import { Links } from "./common"; | ||
import { FungibleImplementation } from "./fungibles"; | ||
export interface FungiblePositionsResponse { | ||
@@ -20,2 +21,3 @@ links: Links; | ||
price: number; | ||
changes: null | unknown; | ||
fungible_info: FungibleInfo; | ||
@@ -45,7 +47,2 @@ flags: PositionFlags; | ||
} | ||
export interface FungibleImplementation { | ||
chain_id: string; | ||
address: string | null; | ||
decimals: number; | ||
} | ||
export interface PositionFlags { | ||
@@ -52,0 +49,0 @@ displayable: boolean; |
@@ -5,1 +5,4 @@ export * from "./chains"; | ||
export * from "./portfolio"; | ||
export * from "./ui"; | ||
export * from "./interface"; | ||
export * from "./fungibles"; |
@@ -5,1 +5,4 @@ export * from "./chains"; | ||
export * from "./portfolio"; | ||
export * from "./ui"; | ||
export * from "./interface"; | ||
export * from "./fungibles"; |
{ | ||
"name": "zerion-sdk", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "A Typed Interface for ZerionAPI", | ||
@@ -5,0 +5,0 @@ "author": "bh2smith", |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
42078
55
1153