-
Notifications
You must be signed in to change notification settings - Fork 316
feat: add openocean #5198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: add openocean #5198
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ea60886
feat: add openocean
7118c80
fix: use leftShift
4c471d5
fix: add openocean to ignoreWords
9cfc849
fix: remove annotation
9d63fdd
fix: clear openoceanIcon properties
76d5005
Merge branch 'DimensionDev:develop' into develop
3017042
Merge branch 'develop' into develop
guanbinrui d8796da
Merge branch 'develop' of https://github.com/DimensionDev/Maskbook in…
hanszhoou d888cbd
fix: conflict
hanszhoou ad2eee5
fix: conflict
hanszhoou 07eaf28
chore: qa
hanszhoou dd2ce10
chore: qa
hanszhoou 20f0ea8
chore: qa
hanszhoou f7835e2
Merge branch 'develop' of https://github.com/DimensionDev/Maskbook in…
hanszhoou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -289,6 +289,8 @@ | |
| "crwdns", | ||
| "dodoapi", | ||
| "getdodoroute", | ||
| "openocean", | ||
| "OPENOCEAN", | ||
| "getx", | ||
| "hookform", | ||
| "idiv", | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,39 @@ | ||
| import type { SwapOOData, SwapOORequest } from '../../types/openocean' | ||
| import { OPENOCEAN_BASE_URL } from '../../constants/openocean' | ||
| import { leftShift } from '@masknet/web3-shared-base' | ||
| import urlcat from 'urlcat' | ||
|
|
||
| export async function swapOO(request: SwapOORequest): Promise<SwapOOData> { | ||
| const response = await fetch( | ||
| urlcat(OPENOCEAN_BASE_URL, `/${request.chainId}/swap`, { | ||
| inTokenSymbol: request.toToken?.symbol, | ||
| inTokenAddress: request.fromToken?.address, | ||
| outTokenSymbol: request.toToken?.symbol, | ||
| outTokenAddress: request.toToken?.address, | ||
| amount: request.fromAmount, | ||
| gasPrice: 5000000000, | ||
| slippage: request.slippage, | ||
| disabledDexIds: '', | ||
| account: request.userAddr, | ||
| }), | ||
| ) | ||
| const payload = await response.json() | ||
| const { data, outAmount, minOutAmount, to, value } = payload | ||
| const _resAmount = leftShift(outAmount, request.toToken.decimals).toNumber() | ||
| const _fromAmount = leftShift(request.fromAmount, request.fromToken.decimals).toNumber() | ||
| return { | ||
| data, | ||
| targetApproveAddr: request.fromToken.address, | ||
| targetDecimals: request.fromToken.decimals, | ||
| resAmount: _resAmount, | ||
| fromAmount: _fromAmount, | ||
| resPricePerFromToken: +(_fromAmount / _resAmount).toFixed(8), | ||
| resPricePerToToken: +(_resAmount / _fromAmount).toFixed(8), | ||
| to, | ||
| value, | ||
| slippage: request.slippage, | ||
| fromTokenSymbol: request.fromToken.symbol, | ||
| toTokenSymbol: request.toToken.symbol, | ||
| minOutAmount, | ||
| } as SwapOOData | ||
| } | ||
This file contains hidden or 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 hidden or 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,13 @@ | ||
| import { NetworkType } from '@masknet/web3-shared-evm' | ||
|
|
||
| export const OPENOCEAN_BASE_URL = 'https://ethapi.openocean.finance/v2/' | ||
|
|
||
| export const networkNames: Record<NetworkType, string> = { | ||
|
guanbinrui marked this conversation as resolved.
|
||
| [NetworkType.Ethereum]: 'eth', | ||
| [NetworkType.Binance]: 'bsc', | ||
| [NetworkType.Polygon]: 'polygon', | ||
| [NetworkType.Arbitrum]: 'arbitrum', | ||
| [NetworkType.xDai]: 'xdai', | ||
| [NetworkType.Celo]: 'celo', | ||
| [NetworkType.Fantom]: 'fantom', | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
9 changes: 9 additions & 0 deletions
9
packages/mask/src/plugins/Trader/trader/openocean/useSlippageTolerance.ts
This file contains hidden or 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,9 @@ | ||
| import { clamp } from 'lodash-unified' | ||
| import { useValueRef } from '@masknet/shared' | ||
| import { SLIPPAGE_MAX, SLIPPAGE_MIN } from '../../constants' | ||
| import { currentSlippageSettings } from '../../settings' | ||
|
|
||
| export function useSlippageTolerance() { | ||
| const slippage_ = useValueRef(currentSlippageSettings) | ||
| return clamp(slippage_, SLIPPAGE_MIN, SLIPPAGE_MAX) | ||
| } |
65 changes: 65 additions & 0 deletions
65
packages/mask/src/plugins/Trader/trader/openocean/useTrade.ts
This file contains hidden or 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,65 @@ | ||
| import { | ||
| FungibleTokenDetailed, | ||
| isNativeTokenAddress, | ||
| useAccount, | ||
| useBlockNumber, | ||
| useRPCConstants, | ||
| useTokenConstants, | ||
| useTraderConstants, | ||
| } from '@masknet/web3-shared-evm' | ||
| import { useAsyncRetry } from 'react-use' | ||
| import { PluginTraderRPC } from '../../messages' | ||
| import type { TradeStrategy } from '../../types' | ||
| import { TargetChainIdContext } from '../useTargetChainIdContext' | ||
| import { useSlippageTolerance } from './useSlippageTolerance' | ||
| import { first } from 'lodash-unified' | ||
|
|
||
| export function useTrade( | ||
| strategy: TradeStrategy, | ||
| inputAmount: string, | ||
| outputAmount: string, | ||
| inputToken?: FungibleTokenDetailed, | ||
| outputToken?: FungibleTokenDetailed, | ||
| ) { | ||
| const { NATIVE_TOKEN_ADDRESS } = useTokenConstants() | ||
| const blockNumber = useBlockNumber() | ||
| const slippage = useSlippageTolerance() | ||
| const { targetChainId } = TargetChainIdContext.useContainer() | ||
| const { RPC } = useRPCConstants(targetChainId) | ||
| const providerURL = first(RPC) | ||
| const { OPENOCEAN_ETH_ADDRESS } = useTraderConstants(targetChainId) | ||
| const account = useAccount() | ||
|
|
||
| return useAsyncRetry(async () => { | ||
| if (!inputToken || !outputToken) return null | ||
| if (inputAmount === '0') return null | ||
| const sellToken = isNativeTokenAddress(inputToken.address) | ||
| ? { ...inputToken, address: OPENOCEAN_ETH_ADDRESS ?? '' } | ||
| : inputToken | ||
| const buyToken = isNativeTokenAddress(outputToken.address) | ||
| ? { ...outputToken, address: OPENOCEAN_ETH_ADDRESS ?? '' } | ||
| : outputToken | ||
| return PluginTraderRPC.swapOO({ | ||
| isNativeSellToken: isNativeTokenAddress(inputToken.address), | ||
| fromToken: sellToken, | ||
| toToken: buyToken, | ||
| fromAmount: inputAmount, | ||
| slippage, | ||
| userAddr: account, | ||
| rpc: providerURL, | ||
| chainId: targetChainId, | ||
| }) | ||
| }, [ | ||
| NATIVE_TOKEN_ADDRESS, | ||
| strategy, | ||
| inputAmount, | ||
| outputAmount, | ||
| inputToken?.address, | ||
| outputToken?.address, | ||
| slippage, | ||
| blockNumber, // refresh api each block | ||
| account, | ||
| providerURL, | ||
| targetChainId, | ||
| ]) | ||
| } |
89 changes: 89 additions & 0 deletions
89
packages/mask/src/plugins/Trader/trader/openocean/useTradeCallback.ts
This file contains hidden or 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,89 @@ | ||
| import { useCallback, useMemo, useState } from 'react' | ||
| import stringify from 'json-stable-stringify' | ||
| import { pick } from 'lodash-unified' | ||
| import type { TransactionConfig } from 'web3-core' | ||
| import { | ||
| TransactionState, | ||
| TransactionStateType, | ||
| useAccount, | ||
| useChainId, | ||
| useWeb3, | ||
| GasOptionConfig, | ||
| } from '@masknet/web3-shared-evm' | ||
| import type { SwapOOSuccessResponse, TradeComputed } from '../../types' | ||
|
|
||
| export function useTradeCallback( | ||
| tradeComputed: TradeComputed<SwapOOSuccessResponse> | null, | ||
| gasConfig?: GasOptionConfig, | ||
| ) { | ||
| const web3 = useWeb3() | ||
| const account = useAccount() | ||
| const chainId = useChainId() | ||
| const [tradeState, setTradeState] = useState<TransactionState>({ | ||
| type: TransactionStateType.UNKNOWN, | ||
| }) | ||
|
|
||
| // compose transaction config | ||
| const config = useMemo(() => { | ||
| if (!account || !tradeComputed?.trade_) return null | ||
| return { | ||
| from: account, | ||
| ...pick(tradeComputed.trade_, ['to', 'data', 'value']), | ||
| } as TransactionConfig | ||
| }, [account, tradeComputed]) | ||
|
|
||
| const tradeCallback = useCallback(async () => { | ||
| // validate config | ||
| if (!account || !config) { | ||
| setTradeState({ | ||
| type: TransactionStateType.UNKNOWN, | ||
| }) | ||
| return | ||
| } | ||
|
|
||
| // start waiting for provider to confirm tx | ||
| setTradeState({ | ||
| type: TransactionStateType.WAIT_FOR_CONFIRMING, | ||
| }) | ||
|
|
||
| // compose transaction config | ||
| const config_ = { | ||
| ...config, | ||
| gas: await web3.eth.estimateGas(config).catch((error) => { | ||
| setTradeState({ | ||
| type: TransactionStateType.FAILED, | ||
| error, | ||
| }) | ||
| throw error | ||
| }), | ||
| ...gasConfig, | ||
| } | ||
|
|
||
| // send transaction and wait for hash | ||
| return new Promise<string>((resolve, reject) => { | ||
| web3.eth.sendTransaction(config_, (error, hash) => { | ||
| if (error) { | ||
| setTradeState({ | ||
| type: TransactionStateType.FAILED, | ||
| error, | ||
| }) | ||
| reject(error) | ||
| } else { | ||
| setTradeState({ | ||
| type: TransactionStateType.HASH, | ||
| hash, | ||
| }) | ||
| resolve(hash) | ||
| } | ||
| }) | ||
| }) | ||
| }, [web3, account, chainId, stringify(config)]) | ||
|
|
||
| const resetCallback = useCallback(() => { | ||
| setTradeState({ | ||
| type: TransactionStateType.UNKNOWN, | ||
| }) | ||
| }, []) | ||
|
|
||
| return [tradeState, tradeCallback, resetCallback] as const | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.