Skip to content
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

chore: release v1.586.0 #6793

Merged
merged 3 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/Modals/Send/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AssetId, ChainId } from '@shapeshiftoss/caip'
import type { AccountId, AssetId, ChainId } from '@shapeshiftoss/caip'
import { CHAIN_NAMESPACE, fromAccountId, fromAssetId, fromChainId } from '@shapeshiftoss/caip'
import type {
CosmosSdkChainId,
Expand Down Expand Up @@ -36,7 +36,7 @@ export type EstimateFeesInput = {
from?: string
to: string
sendMax: boolean
accountId: string
accountId: AccountId
contractAddress: string | undefined
}

Expand Down
1 change: 1 addition & 0 deletions src/components/Sweep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const Sweep = ({
isSuccess: isEstimatedFeesDataSuccess,
} = useGetEstimatedFeesQuery({
amountCryptoPrecision: '0',
feeAssetId: assetId,
assetId,
to: fromAddress ?? '',
sendMax: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Center } from '@chakra-ui/react'
import type { AccountId } from '@shapeshiftoss/caip'
import { toAssetId } from '@shapeshiftoss/caip'
import type { Asset } from '@shapeshiftoss/types'
import { useQuery } from '@tanstack/react-query'
import { DefiModalContent } from 'features/defi/components/DefiModal/DefiModalContent'
import { DefiModalHeader } from 'features/defi/components/DefiModal/DefiModalHeader'
import type {
Expand All @@ -10,8 +11,9 @@ import type {
} from 'features/defi/contexts/DefiManagerProvider/DefiCommon'
import { DefiAction, DefiStep } from 'features/defi/contexts/DefiManagerProvider/DefiCommon'
import qs from 'qs'
import { useCallback, useEffect, useMemo, useReducer, useState } from 'react'
import { useCallback, useEffect, useMemo, useReducer } from 'react'
import { useTranslate } from 'react-polyglot'
import { reactQueries } from 'react-queries'
import { useSelector } from 'react-redux'
import type { AccountDropdownProps } from 'components/AccountDropdown/AccountDropdown'
import { CircularProgress } from 'components/CircularProgress/CircularProgress'
Expand All @@ -20,7 +22,6 @@ import { Steps } from 'components/DeFi/components/Steps'
import { Sweep } from 'components/Sweep'
import { useBrowserRouter } from 'hooks/useBrowserRouter/useBrowserRouter'
import { useWallet } from 'hooks/useWallet/useWallet'
import { getThorchainFromAddress } from 'lib/utils/thorchain'
import { getThorchainSaversPosition } from 'state/slices/opportunitiesSlice/resolvers/thorchainsavers/utils'
import type { StakingId } from 'state/slices/opportunitiesSlice/types'
import { serializeUserStakingId, toOpportunityId } from 'state/slices/opportunitiesSlice/utils'
Expand Down Expand Up @@ -51,8 +52,6 @@ export const ThorchainSaversDeposit: React.FC<YearnDepositProps> = ({
accountId,
onAccountIdChange: handleAccountIdChange,
}) => {
const [fromAddress, setFromAddress] = useState<string | null>(null)

const {
state: { wallet },
} = useWallet()
Expand Down Expand Up @@ -126,21 +125,16 @@ export const ThorchainSaversDeposit: React.FC<YearnDepositProps> = ({
selectPortfolioAccountMetadataByAccountId(state, accountFilter),
)

useEffect(() => {
if (!(accountId && wallet && accountMetadata)) return
;(async () => {
const _fromAddress = await getThorchainFromAddress({
accountId,
getPosition: getThorchainSaversPosition,
assetId,
wallet,
accountMetadata,
})

if (!_fromAddress) return
setFromAddress(_fromAddress)
})()
}, [accountId, accountMetadata, assetId, fromAddress, wallet])
const { data: fromAddress } = useQuery({
...reactQueries.common.thorchainFromAddress({
accountId: accountId!,
getPosition: getThorchainSaversPosition,
assetId,
wallet: wallet!,
accountMetadata: accountMetadata!,
}),
enabled: Boolean(accountId && wallet && accountMetadata),
})

const makeHandleSweepBack = useCallback(
(onNext: StepComponentProps['onNext']) => () => onNext(DefiStep.Info),
Expand All @@ -162,7 +156,7 @@ export const ThorchainSaversDeposit: React.FC<YearnDepositProps> = ({
<Deposit
{...ownProps}
accountId={accountId}
fromAddress={fromAddress}
fromAddress={fromAddress ?? null}
onAccountIdChange={handleAccountIdChange}
/>
),
Expand All @@ -172,7 +166,7 @@ export const ThorchainSaversDeposit: React.FC<YearnDepositProps> = ({
component: ({ onNext }) => (
<Sweep
accountId={accountId}
fromAddress={fromAddress}
fromAddress={fromAddress ?? null}
assetId={assetId}
onBack={makeHandleSweepBack(onNext)}
onSweepSeen={makeHandleSweepSeen(onNext)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { AccountId } from '@shapeshiftoss/caip'
import { fromAccountId, fromAssetId, toAssetId } from '@shapeshiftoss/caip'
import { CONTRACT_INTERACTION } from '@shapeshiftoss/chain-adapters'
import type { Asset } from '@shapeshiftoss/types'
import { getConfig } from 'config'
import { getOrCreateContractByType } from 'contracts/contractManager'
import { ContractType } from 'contracts/types'
import { Approve as ReusableApprove } from 'features/defi/components/Approve/Approve'
Expand All @@ -22,12 +21,11 @@ import { useBrowserRouter } from 'hooks/useBrowserRouter/useBrowserRouter'
import { useErrorHandler } from 'hooks/useErrorToast/useErrorToast'
import { usePoll } from 'hooks/usePoll/usePoll'
import { useWallet } from 'hooks/useWallet/useWallet'
import { bn, bnOrZero } from 'lib/bignumber/bignumber'
import { bnOrZero } from 'lib/bignumber/bignumber'
import { toBaseUnit } from 'lib/math'
import { trackOpportunityEvent } from 'lib/mixpanel/helpers'
import { MixPanelEvent } from 'lib/mixpanel/types'
import { assetIdToPoolAssetId } from 'lib/swapper/swappers/ThorchainSwapper/utils/poolAssetHelpers/poolAssetHelpers'
import { useRouterContractAddress } from 'lib/swapper/swappers/ThorchainSwapper/utils/useRouterContractAddress'
import { MAX_ALLOWANCE } from 'lib/swapper/swappers/utils/constants'
import { isSome, isToken } from 'lib/utils'
import {
Expand All @@ -36,8 +34,8 @@ import {
createBuildCustomTxInput,
getErc20Allowance,
} from 'lib/utils/evm'
import { getInboundAddressDataForChain } from 'lib/utils/thorchain/getInboundAddressDataForChain'
import { getMaybeThorchainSaversDepositQuote } from 'state/slices/opportunitiesSlice/resolvers/thorchainsavers/utils'
import { useGetThorchainSaversDepositQuoteQuery } from 'lib/utils/thorchain/hooks/useGetThorchainSaversDepositQuoteQuery'
import { useSendThorTx } from 'lib/utils/thorchain/hooks/useSendThorTx'
import { DefiProvider } from 'state/slices/opportunitiesSlice/types'
import { serializeUserStakingId, toOpportunityId } from 'state/slices/opportunitiesSlice/utils'
import {
Expand Down Expand Up @@ -107,10 +105,19 @@ export const Approve: React.FC<ApproveProps> = ({ accountId, onNext }) => {
selectMarketDataByAssetIdUserCurrency(state, feeAsset?.assetId ?? ''),
)

const { routerContractAddress: saversRouterContractAddress } = useRouterContractAddress({
feeAssetId: feeAsset?.assetId ?? '',
skip: !isTokenDeposit || !feeAsset?.assetId,
excludeHalted: true,
const { data: thorchainSaversDepositQuote } = useGetThorchainSaversDepositQuoteQuery({
asset,
amountCryptoBaseUnit: toBaseUnit(state?.deposit.cryptoAmount, asset.precision),
})

const { inboundAddress } = useSendThorTx({
assetId,
accountId: accountId ?? null,
amountCryptoBaseUnit: toBaseUnit(state?.deposit.cryptoAmount, asset.precision),
memo: thorchainSaversDepositQuote?.memo ?? null,
fromAddress: '',
action: 'depositSavers',
enableEstimateFees: false,
})

const handleApprove = useCallback(async () => {
Expand All @@ -120,22 +127,14 @@ export const Approve: React.FC<ApproveProps> = ({ accountId, onNext }) => {
!wallet ||
!accountId ||
!dispatch ||
!saversRouterContractAddress ||
!opportunityData
!opportunityData ||
!inboundAddress
)
return

dispatch({ type: ThorchainSaversDepositActionType.SET_LOADING, payload: true })

try {
const daemonUrl = getConfig().REACT_APP_THORCHAIN_NODE_URL
const maybeInboundAddressData = await getInboundAddressDataForChain(
daemonUrl,
feeAsset?.assetId,
)
if (maybeInboundAddressData.isErr())
throw new Error(maybeInboundAddressData.unwrapErr().message)

const amountCryptoBaseUnit = toBaseUnit(state.deposit.cryptoAmount, asset.precision)

const poolId = assetIdToPoolAssetId({ assetId: asset.assetId })
Expand All @@ -153,7 +152,7 @@ export const Approve: React.FC<ApproveProps> = ({ accountId, onNext }) => {
const data = encodeFunctionData({
abi: contract.abi,
functionName: 'approve',
args: [getAddress(saversRouterContractAddress), BigInt(amountToApprove)],
args: [getAddress(inboundAddress), BigInt(amountToApprove)],
})

const adapter = assertGetEvmChainAdapter(chainId)
Expand All @@ -175,7 +174,7 @@ export const Approve: React.FC<ApproveProps> = ({ accountId, onNext }) => {
fn: () =>
getErc20Allowance({
address: fromAssetId(assetId).assetReference,
spender: saversRouterContractAddress,
spender: inboundAddress,
from: fromAccountId(accountId).account,
chainId: asset.chainId,
}),
Expand All @@ -186,64 +185,6 @@ export const Approve: React.FC<ApproveProps> = ({ accountId, onNext }) => {
maxAttempts: 60,
})

const estimatedDepositGasCryptoPrecision = await (async () => {
const maybeQuote = await getMaybeThorchainSaversDepositQuote({
asset,
amountCryptoBaseUnit,
})
if (maybeQuote.isErr()) throw new Error(maybeQuote.unwrapErr())
const quote = maybeQuote.unwrap()
const thorContract = getOrCreateContractByType({
address: saversRouterContractAddress!,
type: ContractType.ThorRouter,
chainId: asset.chainId,
})

const data = encodeFunctionData({
abi: thorContract.abi,
functionName: 'depositWithExpiry',
args: [
getAddress(quote.inbound_address),
getAddress(fromAssetId(assetId).assetReference),
BigInt(amountCryptoBaseUnit),
quote.memo,
BigInt(quote.expiry),
],
})

const adapter = assertGetEvmChainAdapter(chainId)

const customTxInput = await createBuildCustomTxInput({
accountNumber,
adapter,
data,
value: '0', // this is not a token send, but a smart contract call so we don't send anything here, THOR router does
to: saversRouterContractAddress!,
wallet,
})

const fees = await adapter.getFeeData({
to: customTxInput.to,
value: customTxInput.value,
chainSpecific: {
from: fromAccountId(accountId).account,
data: customTxInput.data,
},
})

const fastFeeCryptoBaseUnit = fees.fast.txFee
const fastFeeCryptoPrecision = bnOrZero(
bn(fastFeeCryptoBaseUnit).div(bn(10).pow(feeAsset.precision)),
)

return fastFeeCryptoPrecision.toString()
})()

dispatch({
type: ThorchainSaversDepositActionType.SET_DEPOSIT,
payload: { estimatedGasCryptoPrecision: estimatedDepositGasCryptoPrecision },
})

trackOpportunityEvent(
MixPanelEvent.DepositApprove,
{
Expand All @@ -268,12 +209,10 @@ export const Approve: React.FC<ApproveProps> = ({ accountId, onNext }) => {
assets,
chainId,
dispatch,
feeAsset?.assetId,
feeAsset.precision,
inboundAddress,
onNext,
opportunityData,
poll,
saversRouterContractAddress,
showErrorToast,
state?.deposit.cryptoAmount,
state?.deposit.fiatAmount,
Expand Down Expand Up @@ -316,7 +255,7 @@ export const Approve: React.FC<ApproveProps> = ({ accountId, onNext }) => {

const handleCancel = useCallback(() => history.push('/'), [history])

if (!saversRouterContractAddress || !state || !dispatch) return null
if (!isTokenDeposit || !inboundAddress || !state || !dispatch) return null

return (
<ReusableApprove
Expand All @@ -335,7 +274,7 @@ export const Approve: React.FC<ApproveProps> = ({ accountId, onNext }) => {
isExactAllowance={state.isExactAllowance}
onCancel={handleCancel}
onConfirm={handleApprove}
spenderContractAddress={saversRouterContractAddress}
spenderContractAddress={inboundAddress}
onToggle={onExactAllowanceToggle}
/>
)
Expand Down
Loading
Loading