diff --git a/examples/package.json b/examples/package.json index 8dd4d44..6d9442b 100644 --- a/examples/package.json +++ b/examples/package.json @@ -33,7 +33,8 @@ "28-number-of-transactions" : "../node_modules/.bin/ts-node ./src/28-number-of-transactions.ts", "29-trading-history" : "../node_modules/.bin/ts-node ./src/29-trading-history.ts", "30-market-details" : "../node_modules/.bin/ts-node ./src/30-market-details.ts", - "31-net-curve-balances" : "../node_modules/.bin/ts-node ./src/31-net-curve-balances.ts" + "31-net-curve-balances" : "../node_modules/.bin/ts-node ./src/31-net-curve-balances.ts", + "32-advance-routes" : "../node_modules/.bin/ts-node ./src/32-advance-routes.ts" }, "dependencies": { "dotenv": "16.0.1" diff --git a/examples/src/32-advance-routes.ts b/examples/src/32-advance-routes.ts new file mode 100644 index 0000000..ecefe38 --- /dev/null +++ b/examples/src/32-advance-routes.ts @@ -0,0 +1,40 @@ +import { ethers, utils, Wallet } from 'ethers'; +import { EnvNames, NetworkNames, Sdk, NETWORK_NAME_TO_CHAIN_ID } from '../../src'; +import { logger } from './common'; +import * as dotenv from 'dotenv'; +dotenv.config(); + +async function main(): Promise { + const wallet = Wallet.createRandom(); + + logger.log('sender wallet', wallet.address); + + const sdk = new Sdk(wallet, { + env: EnvNames.LocalNets, + networkName: NetworkNames.LocalA, + }); + + const { state } = sdk; + + logger.log('key account', state.account); + + const fromChainId: number = NETWORK_NAME_TO_CHAIN_ID[NetworkNames.Mainnet]; + const toChainId: number = NETWORK_NAME_TO_CHAIN_ID[NetworkNames.Bsc]; + + const fromAmount = utils.parseUnits('1', 18); + + const quoteRequestPayload = { + fromChainId: fromChainId, + toChainId: toChainId, + fromTokenAddress: ethers.constants.AddressZero, + toTokenAddress: ethers.constants.AddressZero, + fromAmount: fromAmount, + }; + + const quotes = await sdk.advanceRoutes(quoteRequestPayload); + + logger.log('Advance Routes: ', quotes.items); +} + +main() + .catch(logger.error) \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 1cdbd65..ad0ff8c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "etherspot", - "version": "1.43.8", + "version": "1.43.9", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "etherspot", - "version": "1.43.8", + "version": "1.43.9", "license": "MIT", "dependencies": { "@apollo/client": "3.4.0", diff --git a/package.json b/package.json index 5da9bc4..598f1cf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "etherspot", - "version": "1.43.8", + "version": "1.43.9", "description": "Etherspot SDK", "keywords": [ "ether", diff --git a/schema.graphql b/schema.graphql index c486f83..b53035c 100644 --- a/schema.graphql +++ b/schema.graphql @@ -733,6 +733,19 @@ type Query { numberOfTransactions(chainId: Int, tokenAddress: String!, provider: String): NumberOfTransactions! tradingHistory(chainId: Int, tokenAddress: String!, provider: String, page: Int): TradingHistories! marketDetails(chainId: Int, tokenAddress: String!, provider: String, timePeriod: String!): MarketDetails! + advanceRoutes( + serviceProvider: String + account: String! + fromTokenAddress: String! + toTokenAddress: String! + fromAmount: BigNumber! + fromChainId: Int! + toChainId: Int! + toAddress: String + allowSwitchChain: Boolean + fromAddress: String! + showZeroUsd: Boolean + ): AdvanceRoutes! resolveName(chainId: Int, name: String!): NameResolutionsNodes nativeCurrencies: NativeCurrencies! nftList(account: String!, chainId: Int): NftList! @@ -939,6 +952,44 @@ type MarketDetails { priceChangePercentage1y?: number! } +type FeesToken { + address: string! + symbol: string! + decimals: number + chain: string! +} + +type FeesCost { + type: string! + token: FeesToken! + amount: BigNumber! + amountUSD: string +} + +type AdvanceRouteToken { + address: string! + symbol: string! + decimals: number + name: string! +} + +type AdvanceRoute { + provider: string! + fromToken: AdvanceRouteToken! + toToken: AdvanceRouteToken! + duration: number! + gasUSD: string! + tool: string! + amount: BigNumber! + amountUSD: string + feeCosts: [FeesCost!]! + gasCosts: [FeesCost!]! +} + +type AdvanceRoutes { + items: [AdvanceRoute!]! +} + type TokenLists { items: [TokenListPublic!]! } diff --git a/src/sdk/dto/advance-routes.dto.ts b/src/sdk/dto/advance-routes.dto.ts new file mode 100644 index 0000000..5589bde --- /dev/null +++ b/src/sdk/dto/advance-routes.dto.ts @@ -0,0 +1,44 @@ +import { BigNumber } from 'ethers'; +import { Type } from 'class-transformer'; +import { IsBoolean, IsInt, IsOptional, IsPositive } from 'class-validator'; +import { IsAddress, IsBigNumberish } from './validators'; + +export class GetAdvanceRoutesDto { + @IsOptional() + serviceProvider?: string; + + @IsAddress() + fromTokenAddress: string; + + @IsAddress() + toTokenAddress: string; + + @IsPositive() + @IsInt() + @Type(() => Number) + fromChainId: number | null; + + @IsPositive() + @IsInt() + @Type(() => Number) + toChainId: number; + + @IsBigNumberish() + fromAmount: BigNumber; + + @IsOptional() + @IsAddress() + toAddress?: string; + + @IsOptional() + @IsAddress() + fromAddress?: string; + + @IsOptional() + @IsBoolean() + allowSwitchChain?: boolean; + + @IsOptional() + @IsBoolean() + showZeroUsd?: boolean; +} \ No newline at end of file diff --git a/src/sdk/dto/index.ts b/src/sdk/dto/index.ts index 5c4bb05..42513f7 100644 --- a/src/sdk/dto/index.ts +++ b/src/sdk/dto/index.ts @@ -82,4 +82,5 @@ export * from './get-token-details.dto'; export * from './get-historical-token-price.dto'; export * from './get-pools-activity.dto'; export * from './get-trading-history.dto'; -export * from './get-account-24hour-net-curve.dto'; \ No newline at end of file +export * from './get-account-24hour-net-curve.dto'; +export * from './advance-routes.dto'; \ No newline at end of file diff --git a/src/sdk/exchange/classes/advance-route.ts b/src/sdk/exchange/classes/advance-route.ts new file mode 100644 index 0000000..798cdd9 --- /dev/null +++ b/src/sdk/exchange/classes/advance-route.ts @@ -0,0 +1,56 @@ +import { BigNumber } from 'ethers'; +import { TransformBigNumber } from '../../common/transformers/transform-big-number'; + +export class AdvanceRouteToken { + address: string; + + symbol: string; + + decimals?: number; + + name: string; +} + +export class FeesToken { + address: string; + + symbol: string; + + decimals?: number; + + chain: string; +} + +export class FeesCost { + type: string; + + token: FeesToken; + + @TransformBigNumber() + amount: BigNumber; + + amountUSD?: string; +} + +export class AdvanceRoute { + provider: string; + + fromToken: AdvanceRouteToken; + + toToken: AdvanceRouteToken; + + duration: number; + + gasUSD: string; + + tool: string; + + @TransformBigNumber() + amount: BigNumber; + + amountUSD: string; + + feeCosts?: FeesCost[]; + + gasCosts?: FeesCost[]; +} diff --git a/src/sdk/exchange/classes/advance-routes.ts b/src/sdk/exchange/classes/advance-routes.ts new file mode 100644 index 0000000..696880b --- /dev/null +++ b/src/sdk/exchange/classes/advance-routes.ts @@ -0,0 +1,5 @@ +import { AdvanceRoute } from './advance-route'; + +export class AdvanceRoutes { + items: AdvanceRoute[]; +} diff --git a/src/sdk/exchange/classes/index.ts b/src/sdk/exchange/classes/index.ts index 8f2a21e..03be324 100644 --- a/src/sdk/exchange/classes/index.ts +++ b/src/sdk/exchange/classes/index.ts @@ -12,3 +12,5 @@ export * from './exchange-router-address'; export * from './step-transactions-lifi'; export * from './advance-routes-lifi'; export * from './lifi-status'; +export * from './advance-route'; +export * from './advance-routes'; diff --git a/src/sdk/exchange/exchange.service.ts b/src/sdk/exchange/exchange.service.ts index abfc579..f96ab58 100644 --- a/src/sdk/exchange/exchange.service.ts +++ b/src/sdk/exchange/exchange.service.ts @@ -18,6 +18,7 @@ import { StepTransactions, AdvanceRoutesLiFi, LiFiStatus, + AdvanceRoutes, } from './classes'; import { PaginatedTokens } from '../assets'; @@ -261,6 +262,120 @@ export class ExchangeService extends Service { return data; } + async getAdvanceRoutes( + fromTokenAddress: string, + toTokenAddress: string, + fromChainId: number, + toChainId: number, + fromAmount: BigNumber, + toAddress?: string, + allowSwitchChain?: boolean, + fromAddress?: string, + showZeroUsd?: boolean, + serviceProvider?: string, + ): Promise { + const { apiService, accountService } = this.services; + + const account = accountService.accountAddress; + + const { result } = await apiService.query<{ + result: AdvanceRoutes; + }>( + gql` + query( + $serviceProvider: String + $account: String! + $fromTokenAddress: String! + $toTokenAddress: String! + $fromAmount: BigNumber! + $fromChainId: Int + $toChainId: Int + $toAddress: String + $allowSwitchChain: Boolean + $fromAddress: String + $showZeroUsd: Boolean + ) { + result: advanceRoutes( + serviceProvider: $serviceProvider + account: $account + fromTokenAddress: $fromTokenAddress + toTokenAddress: $toTokenAddress + fromAmount: $fromAmount + fromChainId: $fromChainId + toChainId: $toChainId + toAddress: $toAddress + allowSwitchChain: $allowSwitchChain + fromAddress: $fromAddress + showZeroUsd: $showZeroUsd + ) { + items { + provider + tool + duration + amount + amountUSD + gasUSD + fromToken { + address + name + decimals + symbol + } + toToken { + address + name + decimals + symbol + } + feeCosts { + type + amount + amountUSD + token { + address + symbol + decimals + chain + } + } + gasCosts { + type + amount + amountUSD + token { + address + symbol + decimals + chain + } + } + } + } + } + `, + { + variables: { + serviceProvider, + account, + fromTokenAddress, + toTokenAddress, + fromChainId, + toChainId, + fromAmount, + toAddress, + allowSwitchChain, + fromAddress, + showZeroUsd, + }, + models: { + result: AdvanceRoutes, + }, + }, + ); + + return result; + } + async getStepTransaction(selectedRoute: Route): Promise { const { apiService, accountService } = this.services; diff --git a/src/sdk/sdk.ts b/src/sdk/sdk.ts index b30f67a..f140657 100644 --- a/src/sdk/sdk.ts +++ b/src/sdk/sdk.ts @@ -118,6 +118,7 @@ import { GetPoolsActivityDto, GetTradingHistoryDto, GetAccount24HourNetCurveDto, + GetAdvanceRoutesDto, } from './dto'; import { ENSNode, ENSNodeStates, ENSRootNode, ENSService, parseENSName } from './ens'; import { Env, EnvNames } from './env'; @@ -132,6 +133,7 @@ import { AdvanceRoutesLiFi, StepTransactions, LiFiStatus, + AdvanceRoutes, } from './exchange'; import { FaucetService } from './faucet'; @@ -1493,6 +1495,44 @@ export class Sdk { return data; } + /** + * gets advance routes of multi chain + * @param dto + * @return Promise + */ + async advanceRoutes(dto: GetAdvanceRoutesDto): Promise { + const { + serviceProvider, + fromChainId, + toChainId, + fromTokenAddress, + toTokenAddress, + fromAmount, + toAddress, + allowSwitchChain, + fromAddress, + showZeroUsd, + } = await validateDto(dto, GetAdvanceRoutesDto, { + addressKeys: ['fromTokenAddress', 'toTokenAddress'], + }); + + let { chainId } = this.services.networkService; + chainId = fromChainId ? fromChainId : chainId; + + return this.services.exchangeService.getAdvanceRoutes( + fromTokenAddress, + toTokenAddress, + chainId, + toChainId, + BigNumber.from(fromAmount), + toAddress, + allowSwitchChain, + fromAddress, + showZeroUsd, + serviceProvider, + ); + } + async getStepTransaction(dto: GetStepTransactionsLiFiDto): Promise { return this.services.exchangeService.getStepTransaction(dto.route); }