Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
sim committed Jan 20, 2022
2 parents 337d4fb + 7f5156e commit 9063874
Show file tree
Hide file tree
Showing 15 changed files with 27 additions and 313 deletions.
50 changes: 0 additions & 50 deletions src/data/queries/treasury.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/data/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export const queryKey = mirror({
unbondings: "",
pool: "",
},
treasury: { taxRate: "", taxCap: "" },
tx: { txInfo: "", create: "" },
wasm: { contractInfo: "", contractQuery: "" },

Expand Down
8 changes: 2 additions & 6 deletions src/pages/dashboard/Dashboard.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@
gap: var(--grid-gap);

@include desktop {
grid-template-columns: repeat(5, 1fr);
grid-template-columns: repeat(4, 1fr);
}
}

@media (min-width: $breakpoint) and (max-width: (1400px - 0.02)) {
.header {
grid-template-columns: repeat(2, 1fr);
grid-template-rows: auto repeat(2, 1fr);
}

.price {
grid-column: 1 / span 2;
grid-template-rows: repeat(2, 1fr);
}
}
2 changes: 0 additions & 2 deletions src/pages/dashboard/Dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useTranslation } from "react-i18next"
import { Col, Page } from "components/layout"
import LunaPrice from "./LunaPrice"
import TaxRate from "./TaxRate"
import Issuance from "./Issuance"
import CommunityPool from "./CommunityPool"
import StakingRatio from "./StakingRatio"
Expand All @@ -16,7 +15,6 @@ const Dashboard = () => {
<Col>
<header className={styles.header}>
<LunaPrice />
<TaxRate />
<Issuance />
<CommunityPool />
<StakingRatio />
Expand Down
42 changes: 0 additions & 42 deletions src/pages/dashboard/TaxRate.tsx

This file was deleted.

86 changes: 9 additions & 77 deletions src/txs/Tx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ import { has } from "utils/num"
import { getAmount, sortCoins } from "utils/coin"
import { getErrorMessage } from "utils/error"
import { useCurrency } from "data/settings/Currency"
import { queryKey, combineState, RefetchOptions } from "data/query"
import { queryKey, RefetchOptions } from "data/query"
import { useAddress, useNetwork } from "data/wallet"
import { isBroadcastingState, latestTxState } from "data/queries/tx"
import { useBankBalance, useIsWalletEmpty } from "data/queries/bank"
import { getShouldTax, useTaxCap, useTaxRate } from "data/queries/treasury"

import { Pre } from "components/general"
import { Flex, Grid } from "components/layout"
Expand Down Expand Up @@ -57,8 +56,6 @@ interface Props<TxValues> {
estimationTxValues?: TxValues
createTx: (values: TxValues) => CreateTxOptions | undefined
gasAdjustment?: number
preventTax?: boolean
taxes?: Coins
excludeGasDenom?: (denom: string) => boolean

/* render */
Expand All @@ -83,7 +80,7 @@ function Tx<TxValues>(props: Props<TxValues>) {
const { token, decimals, amount, balance } = props
const { initialGasDenom, estimationTxValues, createTx } = props
const { gasAdjustment = DEFAULT_GAS_ADJUSTMENT } = props
const { preventTax, excludeGasDenom } = props
const { excludeGasDenom } = props
const { children, onChangeMax } = props
const { onPost, redirectAfterTx, queryKeys } = props

Expand All @@ -104,12 +101,6 @@ function Tx<TxValues>(props: Props<TxValues>) {
const bankBalance = useBankBalance()
const { gasPrices } = useTx()

/* queries: conditional */
const shouldTax = !preventTax && getShouldTax(token)
const { data: rate = "0", ...taxRateState } = useTaxRate(!shouldTax)
const { data: cap = "0", ...taxCapState } = useTaxCap(token)
const taxState = combineState(taxRateState, taxCapState)

/* simulation: estimate gas */
const simulationTx = estimationTxValues && createTx(estimationTxValues)
const key = {
Expand Down Expand Up @@ -173,7 +164,7 @@ function Tx<TxValues>(props: Props<TxValues>) {
const getNativeMax = () => {
if (!balance) return
const gasAmount = gasFee.denom === initialGasDenom ? gasFee.amount : "0"
return calcMax({ balance, rate, cap, gasAmount }).max
return calcMax({ balance, gasAmount })
}

const max = !gasFee.amount
Expand All @@ -187,14 +178,8 @@ function Tx<TxValues>(props: Props<TxValues>) {
if (max && isMax && onChangeMax) onChangeMax(toInput(max, decimals))
}, [decimals, isMax, max, onChangeMax])

/* tax */
const taxAmount =
token && amount && shouldTax
? calcMinimumTaxAmount(amount, { rate, cap })
: undefined

/* (effect): Log error on console */
const failed = getErrorMessage(taxState.error ?? estimatedGasState.error)
const failed = getErrorMessage(estimatedGasState.error)
useEffect(() => {
if (process.env.NODE_ENV === "development" && failed) {
console.groupCollapsed("Fee estimation failed")
Expand All @@ -212,10 +197,6 @@ function Tx<TxValues>(props: Props<TxValues>) {
const disabled =
passwordRequired && !password
? t("Enter password")
: taxState.isLoading
? t("Loading tax data...")
: taxState.error
? t("Failed to load tax data")
: estimatedGasState.isLoading
? t("Estimating fee...")
: estimatedGasState.error
Expand All @@ -242,10 +223,7 @@ function Tx<TxValues>(props: Props<TxValues>) {
if (!tx) throw new Error("Tx is not defined")

const gasCoins = new Coins([Coin.fromData(gasFee)])
const taxCoin = token && taxAmount && new Coin(token, taxAmount)
const taxCoins = props.taxes ?? taxCoin
const feeCoins = taxCoins ? gasCoins.add(taxCoins) : gasCoins
const fee = new Fee(estimatedGas, feeCoins)
const fee = new Fee(estimatedGas, gasCoins)

if (isWallet.multisig(wallet)) {
const unsignedTx = await auth.create({ ...tx, fee })
Expand Down Expand Up @@ -275,7 +253,6 @@ function Tx<TxValues>(props: Props<TxValues>) {
amount &&
new BigNumber(balance)
.minus(amount)
.minus(taxAmount ?? 0)
.minus((gasFee.denom === token && gasFee.amount) || 0)
.toString()

Expand Down Expand Up @@ -324,10 +301,6 @@ function Tx<TxValues>(props: Props<TxValues>) {
const renderFee = (descriptions?: Contents) => {
if (!estimatedGas) return null

const taxes = sortCoins(props.taxes ?? new Coins(), currency).filter(
({ amount }) => has(amount)
)

return (
<Details>
<dl>
Expand All @@ -338,28 +311,6 @@ function Tx<TxValues>(props: Props<TxValues>) {
</Fragment>
))}

{has(taxAmount) && (
<>
<dt>{t("Tax")}</dt>
<dd>
<Read amount={taxAmount} token={token} />
</dd>
</>
)}

{!!taxes.length && (
<>
<dt>{t("Tax")}</dt>
<dd>
{taxes.map((coin) => (
<p key={coin.denom}>
<Read {...coin} />
</p>
))}
</dd>
</>
)}

<dt className={styles.gas}>
{t("Gas")}
{availableGasDenoms.length > 1 && (
Expand Down Expand Up @@ -503,37 +454,18 @@ export const getInitialGasDenom = (bankBalance: Coins, token?: Token) => {

interface Params {
balance: Amount
rate: string
cap: Amount
gasAmount: Amount
}

// Receive tax and gas information and return the maximum payment amount
export const calcMax = ({ balance, rate, cap, gasAmount }: Params) => {
// Receive gas and return the maximum payment amount
export const calcMax = ({ balance, gasAmount }: Params) => {
const available = new BigNumber(balance).minus(gasAmount)

const tax = calcMinimumTaxAmount(available, {
rate: new BigNumber(rate).div(new BigNumber(1).plus(rate)),
cap,
})

const max = BigNumber.max(new BigNumber(available).minus(tax ?? 0), 0)
const max = BigNumber.max(new BigNumber(available), 0)
.integerValue(BigNumber.ROUND_FLOOR)
.toString()

return { max, tax }
}

export const calcMinimumTaxAmount = (
amount: BigNumber.Value,
{ rate, cap }: { rate: BigNumber.Value; cap: BigNumber.Value }
) => {
const tax = BigNumber.min(
new BigNumber(amount).times(rate),
cap
).integerValue(BigNumber.ROUND_FLOOR)

return tax.gt(0) ? tax.toString() : undefined
return max
}

/* hooks */
Expand Down
26 changes: 3 additions & 23 deletions src/txs/swap/MultipleSwapContext.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import { FC, useMemo } from "react"
import { useTranslation } from "react-i18next"
import { zipObj } from "ramda"
import { isDenomTerraNative } from "@terra.kitchen/utils"
import { getAmount, sortCoins } from "utils/coin"
import createContext from "utils/createContext"
import { useCurrency } from "data/settings/Currency"
import { combineState } from "data/query"
import { useBankBalance, useTerraNativeLength } from "data/queries/bank"
import { useTaxCaps, useTaxRate } from "data/queries/treasury"
import { readNativeDenom } from "data/token"
import { Card } from "components/layout"
import { Wrong } from "components/feedback"

interface MultipleSwap {
taxRate: string
taxCaps: Record<Denom, Amount>
available: TokenItemWithBalance[]
}

Expand All @@ -31,42 +26,27 @@ const MultipleSwapContext: FC = ({ children }) => {
.map(({ denom }) => denom)
.filter(isDenomTerraNative)

/* treasury */
const { data: taxRate, ...taxRateState } = useTaxRate()
const taxCapsState = useTaxCaps(denoms)
const taxCaps = taxCapsState.every(({ isSuccess }) => isSuccess)
? zipObj(
denoms,
taxCapsState.map(({ data }) => {
if (!data) throw new Error()
return data
})
)
: undefined

const available = useMemo(() => {
return denoms.map((denom) => {
const balance = getAmount(bankBalance, denom)
return { ...readNativeDenom(denom), balance }
})
}, [bankBalance, denoms])

const state = combineState(taxRateState, ...taxCapsState)

const render = () => {
if (length < 2)
return <Wrong>{t("Multiple swap requires at least 2 coins")}</Wrong>

if (!(taxRate && taxCaps && available)) return null
if (!available) return null

return (
<MultipleSwapProvider value={{ taxRate, taxCaps, available }}>
<MultipleSwapProvider value={{ available }}>
{children}
</MultipleSwapProvider>
)
}

return <Card {...state}>{render()}</Card>
return <Card>{render()}</Card>
}

export default MultipleSwapContext
1 change: 0 additions & 1 deletion src/txs/swap/SwapForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ const SwapForm = () => {
initialGasDenom,
estimationTxValues,
createTx,
preventTax: mode === SwapMode.ONCHAIN,
onPost: () => {
// add custom token on ask cw20
if (!(askAsset && AccAddress.validate(askAsset) && askTokenItem)) return
Expand Down
Loading

0 comments on commit 9063874

Please sign in to comment.