Skip to content

Commit

Permalink
Integrated Swing for advanceRoutes endpoint (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalrajbacancy authored Aug 16, 2023
1 parent 789364e commit 46ac840
Show file tree
Hide file tree
Showing 12 changed files with 360 additions and 5 deletions.
3 changes: 2 additions & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
40 changes: 40 additions & 0 deletions examples/src/32-advance-routes.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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)
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",
"version": "1.43.8",
"version": "1.43.9",
"description": "Etherspot SDK",
"keywords": [
"ether",
Expand Down
51 changes: 51 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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!]!
}
Expand Down
44 changes: 44 additions & 0 deletions src/sdk/dto/advance-routes.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 2 additions & 1 deletion src/sdk/dto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
export * from './get-account-24hour-net-curve.dto';
export * from './advance-routes.dto';
56 changes: 56 additions & 0 deletions src/sdk/exchange/classes/advance-route.ts
Original file line number Diff line number Diff line change
@@ -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[];
}
5 changes: 5 additions & 0 deletions src/sdk/exchange/classes/advance-routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { AdvanceRoute } from './advance-route';

export class AdvanceRoutes {
items: AdvanceRoute[];
}
2 changes: 2 additions & 0 deletions src/sdk/exchange/classes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
115 changes: 115 additions & 0 deletions src/sdk/exchange/exchange.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
StepTransactions,
AdvanceRoutesLiFi,
LiFiStatus,
AdvanceRoutes,
} from './classes';

import { PaginatedTokens } from '../assets';
Expand Down Expand Up @@ -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<AdvanceRoutes> {
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<StepTransactions> {
const { apiService, accountService } = this.services;

Expand Down
Loading

0 comments on commit 46ac840

Please sign in to comment.