Skip to content

Commit

Permalink
Add getExchangeOffers endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalrajbacancy committed Apr 1, 2024
1 parent b51581c commit 9263e1d
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Changelog

## [1.6.5] - 2024-04-01
### New
- Added `getExchangeOffers` endpoint into DataUtils

## [1.6.4] - 2024-03-20
### New
- Added `getTransactions` endpoint into DataUtils
Expand Down
22 changes: 20 additions & 2 deletions examples/09-exchange.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
import { BigNumber, constants } from 'ethers';
import { DataUtils } from '../src';
import * as dotenv from 'dotenv';

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

async function main(): Promise<void> {
// initializating Data service...
const dataService = new DataUtils();
const exchangeSupportedAssets = await dataService.getExchangeSupportedAssets({ page: 1, limit: 100, account: '', chainId: Number(process.env.CHAIN_ID) });
const dataService = new DataUtils(dataApiKey);
const exchangeSupportedAssets = await dataService.getExchangeSupportedAssets({ page: 1, limit: 100, account: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045', chainId: Number(process.env.CHAIN_ID) });
console.log('\x1b[33m%s\x1b[0m', `Found exchange supported assets:`, exchangeSupportedAssets.items.length);

const fromChainId = 1;
const fromAddress = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045';
const fromTokenAddress = constants.AddressZero;
const toTokenAddress = '0xe3818504c1b32bf1557b16c238b2e01fd3149c17';
const fromAmount = '1000000000000000000';

const offers = await dataService.getExchangeOffers({
fromAddress,
fromChainId,
fromTokenAddress,
toTokenAddress,
fromAmount: BigNumber.from(fromAmount),
});

console.log('\x1b[33m%s\x1b[0m', `Exchange offers:`, offers);
}

main()
Expand Down
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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@etherspot/prime-sdk",
"version": "1.6.4",
"version": "1.6.5",
"description": "Etherspot Prime (Account Abstraction) SDK",
"keywords": [
"ether",
Expand Down
1 change: 1 addition & 0 deletions src/sdk/api/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const API_ENDPOINTS = {
GET_TRANSACTIONS: 'transactions',
GET_ADVANCE_ROUTES_LIFI: 'exchange/getAdvanceRoutesLiFi',
GET_STEP_TRANSACTIONS: 'exchange/getStepTransactions',
GET_EXCHANGE_OFFERS: 'exchange/offers',
GET_EXCHANGE_SUPPORTED_ASSETS: 'assets/exchangeSupportedAssets',
GET_TOKEN_LISTS: 'assets/tokenLists',
GET_TOKEN_LIST_TOKENS: 'assets/tokenListTokens',
Expand Down
10 changes: 5 additions & 5 deletions src/sdk/data/classes/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { BigNumber } from 'ethers';
import { TransactionStatuses } from '../constants';

export class Transactions {
transactions: UserOpsTransaction[];
transactions: UserOpTransaction[];
pageInfo?: {
currentPage: number;
limit: number;
};
}

class UserOpsTransaction {
export class UserOpTransaction {
chainId: number;
sender: string;
target?: string | null;
Expand Down Expand Up @@ -41,7 +41,7 @@ class UserOpsTransaction {
nftTransfers?: NFTTransfersEntity[];
}

class Erc20TransfersEntity {
export class Erc20TransfersEntity {
from: string;
to: string;
value: number;
Expand All @@ -50,7 +50,7 @@ class Erc20TransfersEntity {
decimal: number;
}

class NativeTransfersEntity {
export class NativeTransfersEntity {
from: string;
to: string;
value: string;
Expand All @@ -60,7 +60,7 @@ class NativeTransfersEntity {
data: string;
}

class NFTTransfersEntity {
export class NFTTransfersEntity {
from: string;
to: string;
value: number;
Expand Down
34 changes: 33 additions & 1 deletion src/sdk/data/data.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BigNumber } from 'ethers';
import { Route } from '@lifi/sdk';
import { ObjectSubject } from '../common';
import { AccountBalances, AdvanceRoutesLiFi, NftList, PaginatedTokens, RateData, StepTransactions, TokenList, TokenListToken, Transaction, Transactions } from './classes';
import { AccountBalances, AdvanceRoutesLiFi, ExchangeOffer, NftList, PaginatedTokens, RateData, StepTransactions, TokenList, TokenListToken, Transaction, Transactions } from './classes';
import { RestApiService } from '../api';
import { API_ENDPOINTS, MethodTypes } from '../api/constants';

Expand Down Expand Up @@ -171,6 +171,38 @@ export class DataModule {
}
}

async getExchangeOffers(
fromTokenAddress: string,
toTokenAddress: string,
fromAmount: BigNumber,
fromChainId: number,
fromAddress: string,
toAddress?: string,
showZeroUsd?: boolean
): Promise<ExchangeOffer[]> {
const account = fromAddress;

try {
const queryParams = {
'api-key': this.currentApi,
account,
fromTokenAddress,
toTokenAddress,
fromAmount: fromAmount.toString(),
chainId: fromChainId,
fromAddress,
toAddress,
showZeroUsd,
};

const result = await this.apiService.makeRequest(API_ENDPOINTS.GET_EXCHANGE_OFFERS, MethodTypes.GET, queryParams);

return result ? result.items : null;
} catch (error) {
throw new Error(error.message || 'Failed to get exchange offers');
}
}

async getTokenLists(chainId: number): Promise<TokenList[]> {
try {
const queryParams = {
Expand Down
29 changes: 27 additions & 2 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, NftList, PaginatedTokens, RateData, StepTransactions, TokenList, TokenListToken, Transaction, Transactions } from "./data";
import { FetchExchangeRatesDto, GetAccountBalancesDto, GetAdvanceRoutesLiFiDto, GetExchangeSupportedAssetsDto, GetNftListDto, GetStepTransactionsLiFiDto, GetTokenListDto, GetTokenListsDto, GetTransactionDto, GetTransactionsDto, validateDto } from "./dto";
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 { BigNumber } from "ethers";

export class DataUtils {
Expand Down Expand Up @@ -137,6 +137,31 @@ export class DataUtils {
return this.dataModule.getExchangeSupportedAssets(page, limit, chainId, account);
}

/**
* gets exchange offers
* @param dto
* @return Promise<ExchangeOffer[]>
*/
async getExchangeOffers(dto: GetExchangeOffersDto): Promise<ExchangeOffer[]> {
const { fromTokenAddress, toTokenAddress, fromAmount, fromChainId, showZeroUsd, fromAddress } = await validateDto(dto, GetExchangeOffersDto, {
addressKeys: ['fromTokenAddress', 'toTokenAddress', 'fromAddress'],
});

let { toAddress } = dto;

if (!toAddress) toAddress = fromAddress;

return this.dataModule.getExchangeOffers(
fromTokenAddress,
toTokenAddress,
BigNumber.from(fromAmount),
fromChainId,
fromAddress,
toAddress,
showZeroUsd,
);
}

/**
* gets token lists
* @param dto
Expand Down
1 change: 0 additions & 1 deletion src/sdk/dto/get-exchange-offers.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@ export class GetExchangeOffersDto {
@IsOptional()
@IsBoolean()
showZeroUsd?: boolean;

}

0 comments on commit 9263e1d

Please sign in to comment.