Skip to content

Commit

Permalink
Merge pull request #298 from terra-money/fix/osmosis-gas-query
Browse files Browse the repository at this point in the history
feat: estimate osmosis gas at run time
  • Loading branch information
mwmerz authored Dec 26, 2023
2 parents b11972d + d4b097e commit 90a7117
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
4 changes: 2 additions & 2 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"manifest_version": 3,
"name": "Station Wallet",
"version": "7.4.12",
"version_name": "7.4.12",
"version": "7.4.13",
"version_name": "7.4.13",
"background": {
"service_worker": "background.js"
},
Expand Down
32 changes: 31 additions & 1 deletion src/data/queries/tx.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { QueryKey, useQuery, useQueryClient } from "react-query"
import { atom, useSetRecoilState } from "recoil"
import axios from "axios"
import { useNetworks } from "app/InitNetworks"
import { queryKey } from "../query"
import { useInterchainLCDClient } from "./lcdClient"
import axios from "axios"
import { RefetchOptions } from "../query"
import { CARBON_API } from "config/constants"

Expand All @@ -14,6 +15,10 @@ interface LatestTx {
onSuccess?: () => void
}

interface OsmosisGasResponse {
base_fee: string
}

export const latestTxState = atom<LatestTx>({
key: "latestTx",
default: { txhash: "", chainID: "" },
Expand Down Expand Up @@ -50,6 +55,31 @@ export const useTxInfo = ({ txhash, queryKeys, chainID }: LatestTx) => {
)
}

export const useOsmosisGas = () => {
const { networks } = useNetworks()

return useQuery(
[queryKey.tx.osmosisGas],
async () => {
try {
const { data } = await axios.get<OsmosisGasResponse>(
"osmosis/txfees/v1beta1/cur_eip_base_fee",
{
baseURL: networks["mainnet"]["osmosis-1"].lcd, // hard set for now.
}
)
return Number(data.base_fee)
} catch (e) {
return 0.0025
}
},
{
...RefetchOptions.INFINITY,
staleTime: 60 * 1000, // cache data for 1 min
}
)
}

export const useCarbonFees = () => {
return useQuery(
[queryKey.tx.fees],
Expand Down
2 changes: 1 addition & 1 deletion src/data/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const queryKey = mirror({
unbondings: "",
pool: "",
},
tx: { txInfo: "", create: "", fees: "" },
tx: { txInfo: "", create: "", fees: "", osmosisGas: "" },
wasm: { contractInfo: "", contractQuery: "" },
interchain: {
staking: {
Expand Down
19 changes: 15 additions & 4 deletions src/txs/Tx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ import { getLocalSetting, SettingKey } from "utils/localStorage"
import { combineState, RefetchOptions } from "data/query"
import { queryKey } from "data/query"
import { useNetwork } from "data/wallet"
import { isBroadcastingState, latestTxState } from "data/queries/tx"
import {
isBroadcastingState,
latestTxState,
useOsmosisGas,
} from "data/queries/tx"
import { useIsWalletEmpty } from "data/queries/bank"

import { Pre } from "components/general"
Expand Down Expand Up @@ -102,6 +106,7 @@ function Tx<TxValues>(props: Props<TxValues>) {
const isBroadcasting = useRecoilValue(isBroadcastingState)
const readNativeDenom = useNativeDenoms()
const { data: carbonFees } = useCarbonFees()
const { data: osmosisGas } = useOsmosisGas()

/* taxes */
const isClassic = networks[chain]?.isClassic
Expand Down Expand Up @@ -145,6 +150,7 @@ function Tx<TxValues>(props: Props<TxValues>) {
carbonFees?.costs["default_fee"]
)
}

const unsignedTx = await lcd.tx.create([{ address: key.address }], {
...simulationTx,
feeDenoms: [gasDenom],
Expand All @@ -171,14 +177,18 @@ function Tx<TxValues>(props: Props<TxValues>) {
(denom: CoinDenom) => {
const gasPrice = chain?.startsWith("carbon-")
? carbonFees?.prices[denom]
: chain?.startsWith("osmosis")
? (osmosisGas || 0.0025) * 10
: networks[chain]?.gasPrices[denom]

if (isNil(estimatedGas) || !gasPrice) return "0"

return new BigNumber(estimatedGas)
.times(gasPrice)
.integerValue(BigNumber.ROUND_CEIL)
.toString()
},
[estimatedGas, chain, networks, carbonFees]
[chain, carbonFees?.prices, osmosisGas, networks, estimatedGas]
)

const gasAmount = getGasAmount(gasDenom)
Expand Down Expand Up @@ -343,7 +353,6 @@ function Tx<TxValues>(props: Props<TxValues>) {

const renderFee = (descriptions?: Contents) => {
if (!estimatedGas) return null

return (
<Details>
<dl>
Expand Down Expand Up @@ -378,7 +387,9 @@ function Tx<TxValues>(props: Props<TxValues>) {
<dd>
{gasFee.amount && (
<Read
decimals={decimals}
decimals={
gasDenom ? readNativeDenom(gasDenom).decimals : decimals
}
{...gasFee}
denom={
gasFee.denom === token
Expand Down

0 comments on commit 90a7117

Please sign in to comment.