Skip to content

Commit

Permalink
Added getTokenLists, getTokenListTokens and fetchExchangeRates endpoi…
Browse files Browse the repository at this point in the history
…nts (#45)

* Added getTokenLists, getTokenListTokens and fetchExchangeRates endpoints
* Changes as per comments
* Changes as per comments
* Changed console log to console error
  • Loading branch information
kaushalrajbacancy authored Oct 23, 2023
1 parent 05a1b05 commit 3d2136d
Show file tree
Hide file tree
Showing 17 changed files with 343 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# Changelog

## [1.3.4] - 2023-10-20
### New
- Added getTokenLists and getTokenListTokens to fetch token details
- Added fetchExchangeRates to fetch exchange rates of tokens

## [1.3.3] - 2023-10-18
### New
- Added Scroll testnet and Mainnet network support
Expand Down
32 changes: 32 additions & 0 deletions examples/17-token-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { PrimeSdk } from '../src';
import * as dotenv from 'dotenv';

dotenv.config();

async function main(): Promise<void> {
// initializating sdk...
const primeSdk = new PrimeSdk({ privateKey: process.env.WALLET_PRIVATE_KEY }, {
chainId: Number(process.env.CHAIN_ID),
projectKey: '', // project key
});

const tokenLists = await primeSdk.getTokenLists();

console.log('\x1b[33m%s\x1b[0m', `TokenLists:`, tokenLists);

const { name } = tokenLists[0];

let tokenListTokens = await primeSdk.getTokenListTokens();

console.log('\x1b[33m%s\x1b[0m', `Default token list tokens length:`, tokenListTokens.length);

tokenListTokens = await primeSdk.getTokenListTokens({
name,
});

console.log('\x1b[33m%s\x1b[0m', `${name} token list tokens length:`, tokenListTokens.length);
}

main()
.catch(console.error)
.finally(() => process.exit());
31 changes: 31 additions & 0 deletions examples/18-exchange-rates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { PrimeSdk, RateData } from '../src';
import * as dotenv from 'dotenv';

dotenv.config();

async function main(): Promise<void> {
// initializating sdk...
const primeSdk = new PrimeSdk({ privateKey: process.env.WALLET_PRIVATE_KEY }, {
chainId: Number(process.env.CHAIN_ID),
projectKey: '', // project key
});

const ETH_AAVE_ADDR = '0x7Fc66500c84A76Ad7e9c93437bFc5Ac33E2DDaE9';
const ETH_MATIC_ADDR = '0x7D1AfA7B718fb893dB30A3aBc0Cfc608AaCfeBB0';
const ETH_USDC_ADDR = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48';
const TOKEN_LIST = [ETH_AAVE_ADDR, ETH_MATIC_ADDR, ETH_USDC_ADDR];
const ETH_CHAIN_ID = 1;

const requestPayload = {
tokens: TOKEN_LIST,
chainId: ETH_CHAIN_ID,
};

const rates: RateData = await primeSdk.fetchExchangeRates(requestPayload);

console.log('\x1b[33m%s\x1b[0m', `EtherspotWallet Rates:`, rates);
}

main()
.catch(console.error)
.finally(() => process.exit());
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@etherspot/prime-sdk",
"version": "1.3.3",
"version": "1.3.4",
"description": "Etherspot Prime (Account Abstraction) SDK",
"keywords": [
"ether",
Expand Down Expand Up @@ -35,6 +35,8 @@
"13-paymaster": "./node_modules/.bin/ts-node ./examples/13-paymaster",
"14-zeroDev-address": "./node_modules/.bin/ts-node ./examples/14-zeroDev-address",
"15-simpleAccount-address": "./node_modules/.bin/ts-node ./examples/15-simpleAccount-address",
"17-token-list": "./node_modules/.bin/ts-node ./examples/17-token-list",
"18-exchange-rates": "./node_modules/.bin/ts-node ./examples/18-exchange-rates",
"format": "prettier --write \"{src,test,examples}/**/*.ts\"",
"lint": "eslint \"{src,test}/**/*.ts\"",
"lint-fix": "npm run lint -- --fix",
Expand Down
5 changes: 5 additions & 0 deletions src/sdk/data/classes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ export * from './transaction-data';
export * from './advance-routes-lifi';
export * from './step-transactions-lifi';
export * from './exchange-bridging-quote';
export * from './rate-info';
export * from './rate-data';
export * from './token-list-token';
export * from './token-list';
export * from './token-lists';
11 changes: 11 additions & 0 deletions src/sdk/data/classes/rate-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Type } from 'class-transformer';
import { RateInfo } from './rate-info';

export class RateData {
errored: boolean;

error: string;

@Type(() => RateInfo)
items: Array<RateInfo>;
}
7 changes: 7 additions & 0 deletions src/sdk/data/classes/rate-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class RateInfo {
address: string;
eth: number;
eur: number;
gbp: number;
usd: number;
}
8 changes: 8 additions & 0 deletions src/sdk/data/classes/token-list-token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export class TokenListToken {
address: string;
chainId: number;
name: string;
symbol: string;
decimals: number;
logoURI: string;
}
19 changes: 19 additions & 0 deletions src/sdk/data/classes/token-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Type } from 'class-transformer';
import { TokenListToken } from './token-list-token';

export class TokenList {
name: string;

endpoint: string;

isDefault: boolean;

@Type(() => TokenListToken)
tokens: TokenListToken[];

@Type(() => Date)
createdAt: Date;

@Type(() => Date)
updatedAt: Date;
}
7 changes: 7 additions & 0 deletions src/sdk/data/classes/token-lists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Type } from 'class-transformer';
import { TokenList } from './token-list';

export class TokenLists {
@Type(() => TokenList)
items: TokenList[];
}
3 changes: 3 additions & 0 deletions src/sdk/data/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@ export enum LiFiBridge {
polygon = 'polygon',
stargate = 'stargate',
}

export const TOKEN_LIST_MIN_NAME_LENGTH = 3;
export const TOKEN_LIST_MAX_NAME_LENGTH = 32;
115 changes: 113 additions & 2 deletions src/sdk/data/data.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { gql } from '@apollo/client/core';
import { HeaderNames, ObjectSubject, Service } from '../common';
import { Route } from '@lifi/sdk';
import { AccountBalances, AdvanceRoutesLiFi, BridgingQuotes, ExchangeOffer, ExchangeOffers, NftList, StepTransaction, StepTransactions, Transaction } from './classes';
import { AccountBalances, AdvanceRoutesLiFi, BridgingQuotes, ExchangeOffer, ExchangeOffers, NftList, RateData, StepTransaction, StepTransactions, TokenList, TokenListToken, TokenLists, Transaction } from './classes';
import { BigNumber } from 'ethers';
import { CrossChainServiceProvider, LiFiBridge } from './constants';

Expand Down Expand Up @@ -466,5 +466,116 @@ export class DataService extends Service {

return result ? result : null;
}
}

async getTokenLists(): Promise<TokenList[]> {
const { apiService } = this.services;

try {
const { result } = await apiService.query<{
result: TokenLists;
}>(
gql`
query($chainId: Int) {
result: tokenLists(chainId: $chainId) {
items {
name
endpoint
isDefault
createdAt
updatedAt
}
}
}
`,
{
models: {
result: TokenLists,
},
},
);

return result ? result.items : [];
} catch (error) {
console.error(error);
return [];
}
}

async getTokenListTokens(name: string = null): Promise<TokenListToken[]> {
const { apiService } = this.services;

try {
const { result } = await apiService.query<{
result: TokenList;
}>(
gql`
query($chainId: Int, $name: String) {
result: tokenList(chainId: $chainId, name: $name) {
tokens {
address
name
symbol
decimals
logoURI
chainId
}
}
}
`,
{
variables: {
name,
},
models: {
result: TokenList,
},
fetchPolicy: 'cache-first',
},
);

return result ? result.tokens : [];
} catch (error) {
console.error(error);
return [];
}
}

async fetchExchangeRates(tokens: string[], ChainId: number): Promise<RateData> {
const { apiService } = this.services;
try {
const { result } = await apiService.query<{
result: RateData;
}>(
gql`
query($tokens: [String!]!, $ChainId: Int!) {
result: fetchExchangeRates(tokens: $tokens, chainId: $ChainId) {
errored
error
items {
address
eth
eur
gbp
usd
}
}
}
`,
{
variables: {
tokens,
ChainId,
},
models: {
result: RateData,
},
},
);

return result ?? null;
} catch (error) {
console.error(error);
return null;
}
}
}
14 changes: 14 additions & 0 deletions src/sdk/dto/fetch-exchange-rates.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { IsAddress } from './validators';
import { ArrayNotEmpty, IsInt, IsPositive } from 'class-validator';

export class FetchExchangeRatesDto {
@IsAddress({
each: true,
})
@ArrayNotEmpty()
tokens: Array<string>;

@IsInt()
@IsPositive()
chainId: number;
}
10 changes: 10 additions & 0 deletions src/sdk/dto/get-token-list.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { IsString, MinLength, MaxLength, IsOptional } from 'class-validator';
import { TOKEN_LIST_MAX_NAME_LENGTH, TOKEN_LIST_MIN_NAME_LENGTH } from '../data';

export class GetTokenListDto {
@IsOptional()
@IsString()
@MinLength(TOKEN_LIST_MIN_NAME_LENGTH)
@MaxLength(TOKEN_LIST_MAX_NAME_LENGTH)
name?: string = null;
}
2 changes: 2 additions & 0 deletions src/sdk/dto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ export * from './get-exchange-offers.dto';
export * from './advance-routes-lifi.dto';
export * from './get-step-transactions-lifi.dto';
export * from './get-exchange-cross-chain-quote.dto';
export * from './fetch-exchange-rates.dto';
export * from './get-token-list.dto';
Loading

0 comments on commit 3d2136d

Please sign in to comment.