-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added UniSwap Autoroutes to dashboard (#467)
- Loading branch information
1 parent
1fff788
commit bf702c4
Showing
8 changed files
with
866 additions
and
85 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { AlphaRouter } from '@uniswap/smart-order-router'; | ||
import { Token, Percent, TradeType, CurrencyAmount } from '@uniswap/sdk-core'; | ||
import BigNumber from '../../bignumber'; | ||
import getProvider from '../../provider'; | ||
import { getDecimalChainIdByNetworkType } from '../../network'; | ||
import { getTokenAddressByNetworkAndSymbol, getTokenDecimalsBySymbol } from '../../tokens'; | ||
import { getCollateralConfigBySymbol } from '../../constants/COLLATERALS'; | ||
|
||
const getUniswapTokenBySymbol = async function (network: string, symbol: string): Promise<Token> { | ||
const tokenAddress = await getTokenAddressByNetworkAndSymbol(network, symbol); | ||
const tokenDecimals = getTokenDecimalsBySymbol(symbol); | ||
const decimalChainId = getDecimalChainIdByNetworkType(network); | ||
return new Token(decimalChainId, tokenAddress, tokenDecimals, symbol); | ||
}; | ||
|
||
export const getUniswapAutoRoute = async function ( | ||
network: string, | ||
collateralSymbol: string, | ||
inputAmount: string | number = 1, | ||
walletAddress?: string | ||
) { | ||
const collateralConfig = getCollateralConfigBySymbol(collateralSymbol); | ||
const provider = await getProvider(network); | ||
const router = new AlphaRouter({ chainId: 1, provider }); | ||
const inputToken = await getUniswapTokenBySymbol(network, collateralConfig.symbol); | ||
const outputToken = await getUniswapTokenBySymbol(network, 'DAI'); | ||
|
||
const inputAmountInteger = new BigNumber(inputAmount).shiftedBy(collateralConfig.decimals).toFixed(0); | ||
const inputAmountWithCurrency = CurrencyAmount.fromRawAmount(inputToken, inputAmountInteger); | ||
|
||
// get auto route | ||
const route = await router.route( | ||
inputAmountWithCurrency, | ||
outputToken, | ||
TradeType.EXACT_INPUT, | ||
{ | ||
recipient: walletAddress || '0x000000000000000000000000000000000000dEaD', // use given address or "dead" address as fallback | ||
slippageTolerance: new Percent(10, 100), | ||
deadline: Math.floor(Date.now() / 1000 + 1800), | ||
}, | ||
{ | ||
maxSplits: 0, | ||
} | ||
); | ||
if (!route) { | ||
throw new Error(`Could not get auto route for collateral "${collateralSymbol}".`); | ||
} | ||
return route; | ||
}; | ||
|
||
export const fetchAutoRouteInformation = async function ( | ||
network: string, | ||
collateralSymbol: string, | ||
inputAmount: string | number = 1, | ||
walletAddress?: string | ||
) { | ||
try { | ||
const token = await getUniswapTokenBySymbol(network, collateralSymbol); | ||
const autoRouteData = await getUniswapAutoRoute(network, collateralSymbol, inputAmount, walletAddress); | ||
const routes = autoRouteData.route[0].tokenPath.map(p => p.symbol); | ||
|
||
return { | ||
totalPrice: new BigNumber(autoRouteData.quote.toFixed(token.decimals)), | ||
routes, | ||
errorMessage: undefined, | ||
}; | ||
} catch (error: any) { | ||
return { | ||
totalPrice: undefined, | ||
routes: undefined, | ||
errorMessage: error.toString(), | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters