Skip to content

Commit

Permalink
add router cross chain provider (#675)
Browse files Browse the repository at this point in the history
  • Loading branch information
IDIDOS authored Aug 8, 2024
2 parents 9ee61e2 + bb20dcf commit dbe2a24
Show file tree
Hide file tree
Showing 16 changed files with 883 additions and 7 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rubic-sdk",
"version": "5.32.10",
"version": "5.33.0",
"description": "Simplify dApp creation",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -79,6 +79,7 @@
"@pancakeswap/smart-router": "4.2.1",
"@pancakeswap/swap-sdk-core": "^1.0.0",
"@pancakeswap/tokens": "0.1.6",
"@solana/spl-token": "^0.4.8",
"@solana/web3.js": "^1.89.1",
"@solflare-wallet/utl-sdk": "^1.4.0",
"@viaprotocol/router-sdk": "^1.0.7",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,6 @@ export const blockchainId: Record<BlockchainName, number> = {
// Non EVN blockchains
[BLOCKCHAIN_NAME.BITCOIN]: 5555,
[BLOCKCHAIN_NAME.FILECOIN]: 314,
[BLOCKCHAIN_NAME.SOLANA]: 7565164
[BLOCKCHAIN_NAME.SOLANA]: 7565164,
[BLOCKCHAIN_NAME.DOGECOIN]: 2000
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import {
ASSOCIATED_TOKEN_PROGRAM_ID,
getAssociatedTokenAddress,
TOKEN_PROGRAM_ID
} from '@solana/spl-token';
import { BlockhashWithExpiryBlockHeight, Connection, PublicKey } from '@solana/web3.js';
import { Client as TokenSdk } from '@solflare-wallet/utl-sdk';
import BigNumber from 'bignumber.js';
Expand Down Expand Up @@ -199,4 +204,23 @@ export class SolanaWeb3Public extends Web3Public {
public async getRecentBlockhash(): Promise<BlockhashWithExpiryBlockHeight> {
return this.connection.getLatestBlockhash();
}

public async getAtaAddress(
walletAddress: string,
tokenAddress: string
): Promise<string | null> {
const tokenKey = new PublicKey(tokenAddress);
const walletKey = new PublicKey(walletAddress);
const ataAddress = await getAssociatedTokenAddress(
tokenKey,
walletKey,
false,
TOKEN_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID
);

const accountInfo = await this.connection.getAccountInfo(ataAddress);

return accountInfo ? ataAddress.toString() : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
type RouterFlowType = 'trustless' | 'mint-burn' | 'circle' | 'gateway' | 'none';

interface RouterAsset {
decimals: number;
symbol: string;
name: string;
chainId: string;
address: string;
resourceID: string;
isMintable: boolean;
isWrappedAsset: boolean;
}

interface RouterSwapTokenInfo {
chainId: string;
asset: RouterAsset;
stableReserveAsset: RouterAsset;
tokenAmount: string;
stableReserveAmount: string;
path: string[];
flags: string[];
priceImpact: string;
tokenPath: string;
dataTx: string[];
}

export interface RouterQuoteResponseConfig {
flowType: RouterFlowType;
isTransfer: boolean;
isWrappedToken: boolean;
allowanceTo: string;
fromTokenAddress: string;
toTokenAddress: string;
source: RouterSwapTokenInfo & {
bridgeFeeAmount: string;
};
destination: RouterSwapTokenInfo;
partnerId: number;
estimatedTime: number;
slippageTolerance: number;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface RouterQuoteSendParams {
amount: string;
fromTokenAddress: string;
fromTokenChainId: string;
toTokenAddress: string;
toTokenChainId: string;
slippageTolerance?: number;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { RouterQuoteResponseConfig } from './router-quote-response-config';

export interface RouterSendTransactionParams extends RouterQuoteResponseConfig {
senderAddress: string;
receiverAddress: string;
refundAddress: string;
}

export interface RouterSendTransactionResponse extends RouterSendTransactionParams {
txn: {
from: string;
value: string;
to: string;
data: string;
gasPrice: string;
gasLimit: string;
};
}

export interface RouterTxStatusResponse {
status: 'completed' | 'pending';
dest_tx_hash: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { TX_STATUS } from 'src/core/blockchain/web3-public-service/web3-public/models/tx-status';
import { Injector } from 'src/core/injector/injector';
import { TxStatusData } from 'src/features/common/status-manager/models/tx-status-data';
import { CrossChainTradeData } from 'src/features/cross-chain/status-manager/models/cross-chain-trade-data';

import { RouterQuoteResponseConfig } from '../models/router-quote-response-config';
import { RouterQuoteSendParams } from '../models/router-quote-send-params';
import {
RouterSendTransactionParams,
RouterSendTransactionResponse,
RouterTxStatusResponse
} from '../models/router-send-transaction-params';

export class RouterApiService {
private static readonly ROUTER_ENDPOINT =
'https://api-beta.pathfinder.routerprotocol.com/api/v2';

private static readonly partnerId = 159;

public static async getQuote(
params: RouterQuoteSendParams
): Promise<RouterQuoteResponseConfig> {
return Injector.httpClient.get<RouterQuoteResponseConfig>(`${this.ROUTER_ENDPOINT}/quote`, {
params: { ...params, partnerId: this.partnerId }
});
}

public static async getSwapTx(
params: RouterSendTransactionParams
): Promise<RouterSendTransactionResponse> {
return Injector.httpClient.post<RouterSendTransactionResponse>(
`${this.ROUTER_ENDPOINT}/transaction`,
params
);
}

public static async getTxStatus(data: CrossChainTradeData): Promise<TxStatusData> {
const txData = await Injector.httpClient.get<RouterTxStatusResponse>(
`${this.ROUTER_ENDPOINT}/status`,
{
params: {
srcTxHash: data.srcTxHash
}
}
);
if (txData.status === 'completed') {
return {
hash: txData.dest_tx_hash,
status: TX_STATUS.SUCCESS
};
}
return {
hash: null,
status: TX_STATUS.PENDING
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import { MesonCrossChainProvider } from '../providers/meson-provider/meson-cross
import { OrbiterBridgeProvider } from '../providers/orbiter-bridge/orbiter-bridge-provider';
import { OwlToBridgeProvider } from '../providers/owl-to-bridge/owl-to-bridge-provider';
import { RangoCrossChainProvider } from '../providers/rango-provider/rango-cross-chain-provider';
import { RouterCrossChainProvider } from '../providers/router-provider/router-cross-chain-provider';
import { StargateV2CrossChainProvider } from '../providers/stargate-v2-provider/stargate-v2-cross-chain-provider';
//import { StargateCrossChainProvider } from '../providers/stargate-provider/stargate-cross-chain-provider';
// import { StargateCrossChainProvider } from '../providers/stargate-provider/stargate-cross-chain-provider';
import { TaikoBridgeProvider } from '../providers/taiko-bridge/taiko-bridge-provider';

const proxyProviders = [
Expand All @@ -35,7 +36,8 @@ const proxyProviders = [
ArchonBridgeProvider,
MesonCrossChainProvider,
OwlToBridgeProvider,
EddyBridgeProvider
EddyBridgeProvider,
RouterCrossChainProvider
] as const;

const nonProxyProviders = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export const CROSS_CHAIN_TRADE_TYPE = {
LAYERZERO: 'layerzero',
ARCHON_BRIDGE: 'archon_bridge',
MESON: 'meson',
EDDY_BRIDGE: 'eddy_bridge'
EDDY_BRIDGE: 'eddy_bridge',
ROUTER: 'router'
} as const;

export type CrossChainTradeType =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { BLOCKCHAIN_NAME } from 'src/core/blockchain/models/blockchain-name';

export const routerCrossChainSupportedChains = [
BLOCKCHAIN_NAME.ETHEREUM,
BLOCKCHAIN_NAME.OPTIMISM,
BLOCKCHAIN_NAME.ROOTSTOCK,
BLOCKCHAIN_NAME.BINANCE_SMART_CHAIN,
BLOCKCHAIN_NAME.POLYGON,
BLOCKCHAIN_NAME.MANTA_PACIFIC,
BLOCKCHAIN_NAME.XLAYER,
BLOCKCHAIN_NAME.FANTOM,
BLOCKCHAIN_NAME.BOBA,
BLOCKCHAIN_NAME.ZK_SYNC,
BLOCKCHAIN_NAME.METIS,
BLOCKCHAIN_NAME.POLYGON_ZKEVM,
BLOCKCHAIN_NAME.MANTLE,
BLOCKCHAIN_NAME.BASE,
BLOCKCHAIN_NAME.MODE,
BLOCKCHAIN_NAME.ARBITRUM,
BLOCKCHAIN_NAME.AVALANCHE,
BLOCKCHAIN_NAME.LINEA,
BLOCKCHAIN_NAME.BLAST,
BLOCKCHAIN_NAME.TAIKO,
BLOCKCHAIN_NAME.SCROLL,
BLOCKCHAIN_NAME.TRON,
BLOCKCHAIN_NAME.AURORA
// BLOCKCHAIN_NAME.SOLANA
];

export type RouterCrossChainSupportedBlockchains = (typeof routerCrossChainSupportedChains)[number];
Loading

0 comments on commit dbe2a24

Please sign in to comment.