Skip to content

Commit

Permalink
changes as per the comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalrajbacancy committed Apr 12, 2024
1 parent e3f4539 commit ec66543
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 65 deletions.
19 changes: 12 additions & 7 deletions examples/25-connext.ts → examples/25-get-quotes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { utils } from 'ethers';
import { DataUtils } from '../src';
import * as dotenv from 'dotenv';
import { QuotesProvider } from '../src/sdk/data';

dotenv.config();
const dataApiKey = '';
Expand All @@ -9,29 +10,33 @@ async function main(): Promise<void> {
// initializating Data service...
const dataService = new DataUtils(dataApiKey);

const allSupportedAssets = await dataService.getConnextSupportedAssets({});
console.log('\x1b[33m%s\x1b[0m', `Connext all supported assets:`, allSupportedAssets.length);
const allSupportedAssets = await dataService.getSupportedAssets({});
// the default provider is Connext
console.log('\x1b[33m%s\x1b[0m', `All supported assets:`, allSupportedAssets.length);

const supportedAssets = await dataService.getConnextSupportedAssets({
const supportedAssets = await dataService.getSupportedAssets({
chainId: 1,
provider: QuotesProvider.Connext,
});
console.log('\x1b[33m%s\x1b[0m', `Connext supported assets per chain:`, supportedAssets.length);

const quotes = await dataService.getConnextQuotes({
const quotes = await dataService.getQuotes({
fromAddress: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
toAddress: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
fromChainId: 1,
toChainId: 10,
fromToken: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
fromAmount: utils.parseUnits('1', 18),
slippage: 0.1
slippage: 0.1,
provider: QuotesProvider.Connext,
});
console.log('\x1b[33m%s\x1b[0m', `Connext quote transactions:`, quotes);

const transactionStatus = await dataService.getConnextTransactionStatus({
const transactionStatus = await dataService.getTransactionStatus({
fromChainId: 100,
toChainId: 56,
transactionHash: '0xfc46adedf462d3fd6cdbe0214ed11c06cba20c385b9875aa4d51c60afbd9725d'
transactionHash: '0xfc46adedf462d3fd6cdbe0214ed11c06cba20c385b9875aa4d51c60afbd9725d',
provider: QuotesProvider.Connext,
});
console.log('\x1b[33m%s\x1b[0m', `Connext transaction status:`, transactionStatus);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"20-callGasLimit": "./node_modules/.bin/ts-node ./examples/20-callGasLimit",
"21-get-multiple-accounts": "./node_modules/.bin/ts-node ./examples/21-get-multiple-accounts",
"22-concurrent-userops": "./node_modules/.bin/ts-node ./examples/22-concurrent-userops",
"25-connext": "./node_modules/.bin/ts-node ./examples/25-connext",
"25-get-quotes": "./node_modules/.bin/ts-node ./examples/25-get-quotes",
"format": "prettier --write \"{src,test,examples}/**/*.ts\"",
"lint": "eslint \"{src,test,examples}/**/*.ts\"",
"lint-fix": "npm run lint -- --fix",
Expand Down
6 changes: 3 additions & 3 deletions src/sdk/data/classes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ export * from './token-lists';
export * from './token-list-token';
export * from './paginated-tokens';
export * from './transactions';
export * from './connext-token';
export * from './connext-transaction';
export * from './connext-transaction-status';
export * from './token';
export * from './quote-transaction';
export * from './transaction-status';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BigNumberish, BytesLike } from "ethers";

export class ConnextTransaction {
export class QuoteTransaction {
to?: string;
data?: BytesLike;
value?: BigNumberish;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export class ConnextToken {
export class Token {
symbol: string;
address: string;
decimals: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export class ConnextTransactionStatus {
export class TransactionStatus {
status: string;
transactionHash?: string;
connextscanUrl: string;
Expand Down
4 changes: 4 additions & 0 deletions src/sdk/data/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export enum CrossChainServiceProvider {
Etherspot = 'Connext',
}

export enum QuotesProvider {
Connext = 'Connext',
}

export enum LiFiBridge {
across = 'across',
arbitrum = 'arbitrum',
Expand Down
56 changes: 44 additions & 12 deletions src/sdk/data/data.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { BigNumber } from 'ethers';
import { Route } from '@lifi/sdk';
import { ObjectSubject } from '../common';
import { AccountBalances, AdvanceRoutesLiFi, ConnextToken, ConnextTransaction, ConnextTransactionStatus, ExchangeOffer, NftList, PaginatedTokens, RateData, StepTransactions, TokenList, TokenListToken, Transaction, Transactions } from './classes';
import { AccountBalances, AdvanceRoutesLiFi, Token, QuoteTransaction, TransactionStatus, ExchangeOffer, NftList, PaginatedTokens, RateData, StepTransactions, TokenList, TokenListToken, Transaction, Transactions } from './classes';
import { RestApiService } from '../api';
import { API_ENDPOINTS, MethodTypes } from '../api/constants';
import { QuotesProvider } from './constants';

export class DataModule {
readonly apiKey$ = new ObjectSubject<string>('');
Expand Down Expand Up @@ -250,30 +251,41 @@ export class DataModule {
}
}

async getConnextSupportedAssets(chainId?: number): Promise<ConnextToken[]> {
async getSupportedAssets(chainId?: number, provider?: QuotesProvider): Promise<Token[]> {
try {
const queryParams = {
'api-key': this.currentApi,
chainId,
};
let apiUrl: string;

const result = await this.apiService.makeRequest(API_ENDPOINTS.GET_CONNEXT_SUPPORTED_ASSETS, MethodTypes.GET, queryParams);
switch (provider) {
case QuotesProvider.Connext:
apiUrl = API_ENDPOINTS.GET_CONNEXT_SUPPORTED_ASSETS;
break;
default:
apiUrl = API_ENDPOINTS.GET_CONNEXT_SUPPORTED_ASSETS;
break;
}

const result: { tokens: Token[] } = await this.apiService.makeRequest(apiUrl, MethodTypes.GET, queryParams);

return result ? result.tokens : [];
} catch (error) {
throw new Error(error.message || 'Failed to get connext supported assets');
throw new Error(error.message || 'Failed to get supported assets');
}
}

async getConnextQuotes(
async getQuotes(
fromAddress: string,
toAddress: string,
fromChainId: number,
toChainId: number,
fromToken: string,
fromAmount: BigNumber,
slippage: number
): Promise<ConnextTransaction[]> {
slippage: number,
provider?: QuotesProvider
): Promise<QuoteTransaction[]> {
try {
const queryParams = {
'api-key': this.currentApi,
Expand All @@ -285,29 +297,49 @@ export class DataModule {
fromAmount: fromAmount.toString(),
slippage
};
let apiUrl: string;

switch (provider) {
case QuotesProvider.Connext:
apiUrl = API_ENDPOINTS.GET_CONNEXT_QUOTE_TRANSACTIONS;
break;
default:
apiUrl = API_ENDPOINTS.GET_CONNEXT_QUOTE_TRANSACTIONS;
break;
}

const result = await this.apiService.makeRequest(API_ENDPOINTS.GET_CONNEXT_QUOTE_TRANSACTIONS, MethodTypes.GET, queryParams);
const result: { transactions: QuoteTransaction[] } = await this.apiService.makeRequest(apiUrl, MethodTypes.GET, queryParams);

return result ? result.transactions : [];
} catch (error) {
throw new Error(error.message || 'Failed to get connext quotes transactions');
throw new Error(error.message || 'Failed to get quotes transactions');
}
}

async getConnextTransactionStatus(fromChainId: number, toChainId: number, transactionHash: string): Promise<ConnextTransactionStatus> {
async getTransactionStatus(fromChainId: number, toChainId: number, transactionHash: string, provider?: QuotesProvider): Promise<TransactionStatus> {
try {
const queryParams = {
'api-key': this.currentApi,
fromChainId,
toChainId,
transactionHash,
};
let apiUrl: string;

switch (provider) {
case QuotesProvider.Connext:
apiUrl = API_ENDPOINTS.GET_CONNEXT_TRANSACTION_STATUS;
break;
default:
apiUrl = API_ENDPOINTS.GET_CONNEXT_TRANSACTION_STATUS;
break;
}

const result = await this.apiService.makeRequest(API_ENDPOINTS.GET_CONNEXT_TRANSACTION_STATUS, MethodTypes.GET, queryParams);
const result: TransactionStatus = await this.apiService.makeRequest(apiUrl, MethodTypes.GET, queryParams);

return result ? result : null;
} catch (error) {
throw new Error(error.message || 'Failed to get connext transaction status');
throw new Error(error.message || 'Failed to get transaction status');
}
}
}
40 changes: 21 additions & 19 deletions src/sdk/dataUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "reflect-metadata";
import { AccountBalances, AdvanceRoutesLiFi, ConnextToken, ConnextTransaction, ConnextTransactionStatus, DataModule, ExchangeOffer, NftList, PaginatedTokens, RateData, StepTransactions, TokenList, TokenListToken, Transaction, Transactions } from "./data";
import { FetchExchangeRatesDto, GetAccountBalancesDto, GetAdvanceRoutesLiFiDto, GetConnextSupportedAssetsDto, GetConnextTransactionStatusDto, GetExchangeOffersDto, GetExchangeSupportedAssetsDto, GetNftListDto, GetStepTransactionsLiFiDto, GetTokenListDto, GetTokenListsDto, GetTransactionDto, GetTransactionsDto, getConnextQuotesDto, validateDto } from "./dto";
import { AccountBalances, AdvanceRoutesLiFi, Token, QuoteTransaction, TransactionStatus, DataModule, ExchangeOffer, NftList, PaginatedTokens, RateData, StepTransactions, TokenList, TokenListToken, Transaction, Transactions } from "./data";
import { FetchExchangeRatesDto, GetAccountBalancesDto, GetAdvanceRoutesLiFiDto, GetSupportedAssetsDto, GetTransactionStatusDto, GetExchangeOffersDto, GetExchangeSupportedAssetsDto, GetNftListDto, GetStepTransactionsLiFiDto, GetTokenListDto, GetTokenListsDto, GetTransactionDto, GetTransactionsDto, GetQuotesDto, validateDto } from "./dto";
import { BigNumber } from "ethers";

export class DataUtils {
Expand Down Expand Up @@ -226,53 +226,55 @@ export class DataUtils {
}

/**
* gets connext supported tokens
* gets supported tokens
* @param dto
* @return Promise<ConnextToken[]>
* @return Promise<Token[]>
*/
async getConnextSupportedAssets(dto: GetConnextSupportedAssetsDto): Promise<ConnextToken[]> {
const { chainId } = await validateDto(dto, GetConnextSupportedAssetsDto);
async getSupportedAssets(dto: GetSupportedAssetsDto): Promise<Token[]> {
const { chainId, provider } = await validateDto(dto, GetSupportedAssetsDto);

return this.dataModule.getConnextSupportedAssets(chainId);
return this.dataModule.getSupportedAssets(chainId, provider);
}

/**
* gets quote transactions from connext
* gets quote transactions
* @param dto
* @return Promise<ConnextTransaction[]>
* @return Promise<QuoteTransaction[]>
*/
async getConnextQuotes(dto: getConnextQuotesDto): Promise<ConnextTransaction[]> {
async getQuotes(dto: GetQuotesDto): Promise<QuoteTransaction[]> {
const {
fromAddress,
toAddress,
fromChainId,
toChainId,
fromToken,
fromAmount,
slippage
} = await validateDto(dto, getConnextQuotesDto, {
slippage,
provider
} = await validateDto(dto, GetQuotesDto, {
addressKeys: ['fromAddress', 'toAddress', 'fromToken'],
});

return this.dataModule.getConnextQuotes(
return this.dataModule.getQuotes(
fromAddress,
toAddress,
fromChainId,
toChainId,
fromToken,
BigNumber.from(fromAmount),
slippage
slippage,
provider
);
}

/**
* gets connext transaction status
* gets transaction status
* @param dto
* @return Promise<ConnextTransactionStatus>
* @return Promise<TransactionStatus>
*/
async getConnextTransactionStatus(dto: GetConnextTransactionStatusDto): Promise<ConnextTransactionStatus> {
const { fromChainId, toChainId, transactionHash } = await validateDto(dto, GetConnextTransactionStatusDto);
async getTransactionStatus(dto: GetTransactionStatusDto): Promise<TransactionStatus> {
const { fromChainId, toChainId, transactionHash, provider } = await validateDto(dto, GetTransactionStatusDto);

return this.dataModule.getConnextTransactionStatus(fromChainId, toChainId, transactionHash);
return this.dataModule.getTransactionStatus(fromChainId, toChainId, transactionHash, provider);
}
}
15 changes: 0 additions & 15 deletions src/sdk/dto/get-connext-transaction-status.dto.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { BigNumberish } from 'ethers';
import { IsAddress, IsBigNumberish } from './validators';
import { IsOptional } from 'class-validator';
import { QuotesProvider } from '../data';

export class getConnextQuotesDto {
export class GetQuotesDto {
@IsAddress()
fromAddress: string;

Expand All @@ -21,4 +23,7 @@ export class getConnextQuotesDto {
fromAmount: BigNumberish;

slippage: number;

@IsOptional()
provider?: QuotesProvider;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { IsInt, IsOptional, IsPositive } from 'class-validator';
import { QuotesProvider } from '../data';

export class GetConnextSupportedAssetsDto {
export class GetSupportedAssetsDto {
@IsOptional()
@IsPositive()
@IsInt()
chainId?: number = null;

@IsOptional()
provider?: QuotesProvider;
}
19 changes: 19 additions & 0 deletions src/sdk/dto/get-transaction-status.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IsInt, IsOptional, IsPositive } from 'class-validator';
import { IsHex32 } from './validators';
import { QuotesProvider } from '../data';

export class GetTransactionStatusDto {
@IsPositive()
@IsInt()
fromChainId: number;

@IsPositive()
@IsInt()
toChainId: number;

@IsHex32()
transactionHash: string;

@IsOptional()
provider?: QuotesProvider;
}
6 changes: 3 additions & 3 deletions src/sdk/dto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export * from './pagination.dto';
export * from './get-exchange-supported-assets.dto';
export * from './get-token-lists.dto';
export * from './get-transactions.dto';
export * from './get-connext-supported-assets.dto';
export * from './get-connext-quotes.dto';
export * from './get-connext-transaction-status.dto';
export * from './get-supported-assets.dto';
export * from './get-quotes.dto';
export * from './get-transaction-status.dto';

0 comments on commit ec66543

Please sign in to comment.