Skip to content

Commit

Permalink
Add Connext endpoints (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalrajbacancy authored Apr 12, 2024
1 parent 405cd0c commit 7be0768
Show file tree
Hide file tree
Showing 16 changed files with 303 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.7.1] - 2024-04-12
### New
- Added `getSupportedAssets` to get supported tokens
- Added `getQuotes` to get transaction quotes
- Added `getTransactionStatus` to get the transaction status
- The default provider is `Connext`

## [1.7.0] - 2024-04-10
### New
Expand Down
46 changes: 46 additions & 0 deletions examples/25-get-quotes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { utils } from 'ethers';
import { DataUtils } from '../src';
import * as dotenv from 'dotenv';
import { BridgingProvider } from '../src/sdk/data';

dotenv.config();
const dataApiKey = '';

async function main(): Promise<void> {
// initializating Data service...
const dataService = new DataUtils(dataApiKey);

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.getSupportedAssets({
chainId: 1,
provider: BridgingProvider.Connext,
});
console.log('\x1b[33m%s\x1b[0m', `Connext supported assets per chain:`, supportedAssets.length);

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

const transactionStatus = await dataService.getTransactionStatus({
fromChainId: 100,
toChainId: 56,
transactionHash: '0xfc46adedf462d3fd6cdbe0214ed11c06cba20c385b9875aa4d51c60afbd9725d',
provider: BridgingProvider.Connext,
});
console.log('\x1b[33m%s\x1b[0m', `Connext transaction status:`, transactionStatus);
}

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.

3 changes: 2 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.7.0",
"version": "1.7.1",
"description": "Etherspot Prime (Account Abstraction) SDK",
"keywords": [
"ether",
Expand Down Expand Up @@ -42,6 +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-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
3 changes: 3 additions & 0 deletions src/sdk/api/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export const API_ENDPOINTS = {
GET_ADVANCE_ROUTES_LIFI: 'exchange/getAdvanceRoutesLiFi',
GET_STEP_TRANSACTIONS: 'exchange/getStepTransactions',
GET_EXCHANGE_OFFERS: 'exchange/offers',
GET_CONNEXT_SUPPORTED_ASSETS: 'exchange/connext/supportedAssets',
GET_CONNEXT_QUOTE_TRANSACTIONS: 'exchange/connext/quoteTransactions',
GET_CONNEXT_TRANSACTION_STATUS: 'exchange/connext/transactionStatus',
GET_EXCHANGE_SUPPORTED_ASSETS: 'assets/exchangeSupportedAssets',
GET_TOKEN_LISTS: 'assets/tokenLists',
GET_TOKEN_LIST_TOKENS: 'assets/tokenListTokens',
Expand Down
3 changes: 3 additions & 0 deletions src/sdk/data/classes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ export * from './token-lists';
export * from './token-list-token';
export * from './paginated-tokens';
export * from './transactions';
export * from './token';
export * from './quote';
export * from './transaction-status';
10 changes: 10 additions & 0 deletions src/sdk/data/classes/quote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { BigNumberish, BytesLike } from "ethers";

export class Quote {
to?: string;
data?: BytesLike;
value?: BigNumberish;
gasLimit?: string;
gasPrice?: string;
chainId?: number;
}
8 changes: 8 additions & 0 deletions src/sdk/data/classes/token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export class Token {
symbol: string;
address: string;
decimals: number;
chainId: number;
name: string;
icon: string;
}
6 changes: 6 additions & 0 deletions src/sdk/data/classes/transaction-status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class TransactionStatus {
status: string;
transactionHash?: string;
connextscanUrl: string;
transferId?: string;
}
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 BridgingProvider {
Connext = 'Connext',
}

export enum LiFiBridge {
across = 'across',
arbitrum = 'arbitrum',
Expand Down
95 changes: 94 additions & 1 deletion 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, ExchangeOffer, NftList, PaginatedTokens, RateData, StepTransactions, TokenList, TokenListToken, Transaction, Transactions } from './classes';
import { AccountBalances, AdvanceRoutesLiFi, Token, Quote, TransactionStatus, ExchangeOffer, NftList, PaginatedTokens, RateData, StepTransactions, TokenList, TokenListToken, Transaction, Transactions } from './classes';
import { RestApiService } from '../api';
import { API_ENDPOINTS, MethodTypes } from '../api/constants';
import { BridgingProvider } from './constants';

export class DataModule {
readonly apiKey$ = new ObjectSubject<string>('');
Expand Down Expand Up @@ -249,4 +250,96 @@ export class DataModule {
throw new Error(error.message || 'Failed to fetch exchange rates');
}
}

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

switch (provider) {
case BridgingProvider.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 supported assets');
}
}

async getQuotes(
fromAddress: string,
toAddress: string,
fromChainId: number,
toChainId: number,
fromToken: string,
fromAmount: BigNumber,
slippage: number,
provider?: BridgingProvider
): Promise<Quote[]> {
try {
const queryParams = {
'api-key': this.currentApi,
fromAddress,
toAddress,
fromChainId,
toChainId,
fromToken,
fromAmount: fromAmount.toString(),
slippage
};
let apiUrl: string;

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

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

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

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

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

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 transaction status');
}
}
}
59 changes: 56 additions & 3 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, DataModule, ExchangeOffer, NftList, PaginatedTokens, RateData, StepTransactions, TokenList, TokenListToken, Transaction, Transactions } from "./data";
import { FetchExchangeRatesDto, GetAccountBalancesDto, GetAdvanceRoutesLiFiDto, GetExchangeOffersDto, GetExchangeSupportedAssetsDto, GetNftListDto, GetStepTransactionsLiFiDto, GetTokenListDto, GetTokenListsDto, GetTransactionDto, GetTransactionsDto, validateDto } from "./dto";
import { AccountBalances, AdvanceRoutesLiFi, Token, Quote, 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 @@ -224,4 +224,57 @@ export class DataUtils {

return data;
}
}

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

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

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

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

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

return this.dataModule.getTransactionStatus(fromChainId, toChainId, transactionHash, provider);
}
}
29 changes: 29 additions & 0 deletions src/sdk/dto/get-quotes.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { BigNumberish } from 'ethers';
import { IsAddress, IsBigNumberish } from './validators';
import { IsOptional } from 'class-validator';
import { BridgingProvider } from '../data';

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

@IsAddress()
toAddress: string;

fromChainId: number;

toChainId: number;

@IsAddress()
fromToken: string;

@IsBigNumberish({
positive: true,
})
fromAmount: BigNumberish;

slippage: number;

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

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

@IsOptional()
provider?: BridgingProvider;
}
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 { BridgingProvider } from '../data';

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

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

@IsHex32()
transactionHash: string;

@IsOptional()
provider?: BridgingProvider;
}
3 changes: 3 additions & 0 deletions src/sdk/dto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +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-supported-assets.dto';
export * from './get-quotes.dto';
export * from './get-transaction-status.dto';

0 comments on commit 7be0768

Please sign in to comment.