From 6a1a0370d0145543b518c72c2141d4ebbbaf655c Mon Sep 17 00:00:00 2001 From: Burnt Val <117188229+BurntVal@users.noreply.github.com> Date: Tue, 29 Oct 2024 18:00:35 -0400 Subject: [PATCH] Refactor/auto gas (#227) * default to auto fee on signAndBroadcast * overwrite signAndBroadcast method to handle simulation and default to auto fee * restore signAndBroadcast; introduce gasPrice param in provider; comments on setting fee in demo app * move gasPrice default logic to context * changeset --- .changeset/perfect-hounds-hear.md | 6 ++++++ apps/demo-app/src/app/layout.tsx | 3 ++- apps/demo-app/src/app/page.tsx | 19 +++++++++++++------ .../src/components/Abstraxion/index.tsx | 2 ++ .../components/AbstraxionContext/index.tsx | 14 +++++++++++++- .../src/hooks/useAbstraxionSigningClient.ts | 14 ++++++++++---- 6 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 .changeset/perfect-hounds-hear.md diff --git a/.changeset/perfect-hounds-hear.md b/.changeset/perfect-hounds-hear.md new file mode 100644 index 00000000..4da6b47d --- /dev/null +++ b/.changeset/perfect-hounds-hear.md @@ -0,0 +1,6 @@ +--- +"@burnt-labs/abstraxion": minor +"demo-app": minor +--- + +introduce gasPrice param; provide documentation on "auto" fee use diff --git a/apps/demo-app/src/app/layout.tsx b/apps/demo-app/src/app/layout.tsx index ff657102..3cae284c 100644 --- a/apps/demo-app/src/app/layout.tsx +++ b/apps/demo-app/src/app/layout.tsx @@ -32,7 +32,8 @@ const legacyConfig = { }; const treasuryConfig = { - treasury: "xion17ah4x9te3sttpy2vj5x6hv4xvc0td526nu0msf7mt3kydqj4qs2s9jhe90", // Example XION treasury contract + treasury: "xion1h82c0efsxxq4pgua754u6xepfu6avglup20fl834gc2ah0ptgn5s2zffe9", // Example XION treasury contract with /cosmwasm.wasm.v1.MsgExecuteContract grant + // gasPrice: "0uxion", // This defaults to "0uxion" on testnet and "0.025uxion" on mainnet. If you feel the need to change the gasPrice when connecting to signer, set this value. Please stick to the string format seen in example // Optional params to activate mainnet config // rpcUrl: "https://rpc.xion-mainnet-1.burnt.com:443", // restUrl: "https://api.xion-mainnet-1.burnt.com:443", diff --git a/apps/demo-app/src/app/page.tsx b/apps/demo-app/src/app/page.tsx index c2eafddd..a41a02d2 100644 --- a/apps/demo-app/src/app/page.tsx +++ b/apps/demo-app/src/app/page.tsx @@ -58,18 +58,25 @@ export default function Page(): JSX.Element { }; try { + // Use "auto" fee for most transactions const claimRes = await client?.execute( account.bech32Address, seatContractAddress, msg, - { - amount: [{ amount: "0", denom: "uxion" }], - gas: "500000", - }, - "", - [], + "auto", ); + // Default cosmsjs gas multiplier for simulation is 1.4 + // If you're finding that transactions are undersimulating, you can bump up the gas multiplier by setting fee to a number, ex. 1.5 + // Fee amounts shouldn't stray too far away from the defaults + // Example: + // const claimRes = await client?.execute( + // account.bech32Address, + // seatContractAddress, + // msg, + // 1.5, + // ); + setExecuteResult(claimRes); } catch (error) { // eslint-disable-next-line no-console -- No UI exists yet to display errors diff --git a/packages/abstraxion/src/components/Abstraxion/index.tsx b/packages/abstraxion/src/components/Abstraxion/index.tsx index ec4aa154..96b24f66 100644 --- a/packages/abstraxion/src/components/Abstraxion/index.tsx +++ b/packages/abstraxion/src/components/Abstraxion/index.tsx @@ -69,6 +69,7 @@ export interface AbstraxionConfig { bank?: SpendLimit[]; callbackUrl?: string; treasury?: string; + gasPrice?: string; } export function AbstraxionProvider({ @@ -87,6 +88,7 @@ export function AbstraxionProvider({ bank={config.bank} callbackUrl={config.callbackUrl} treasury={config.treasury} + gasPrice={config.gasPrice} > {children} diff --git a/packages/abstraxion/src/components/AbstraxionContext/index.tsx b/packages/abstraxion/src/components/AbstraxionContext/index.tsx index 1d54150a..46793351 100644 --- a/packages/abstraxion/src/components/AbstraxionContext/index.tsx +++ b/packages/abstraxion/src/components/AbstraxionContext/index.tsx @@ -1,6 +1,7 @@ import type { ReactNode } from "react"; import { createContext, useCallback, useEffect, useState } from "react"; -import { testnetChainInfo } from "@burnt-labs/constants"; +import { testnetChainInfo, xionGasValues } from "@burnt-labs/constants"; +import { GasPrice } from "@cosmjs/stargate"; import { SignArbSecp256k1HdWallet } from "@burnt-labs/abstraxion-core"; import { abstraxionAuth } from "../Abstraxion"; @@ -34,6 +35,7 @@ export interface AbstraxionContextProps { stake?: boolean; bank?: SpendLimit[]; treasury?: string; + gasPrice: GasPrice; logout: () => void; } @@ -50,6 +52,7 @@ export function AbstraxionContextProvider({ bank, callbackUrl, treasury, + gasPrice, }: { children: ReactNode; contracts?: ContractGrantDescription[]; @@ -60,6 +63,7 @@ export function AbstraxionContextProvider({ bank?: SpendLimit[]; callbackUrl?: string; treasury?: string; + gasPrice?: string; }): JSX.Element { const [abstraxionError, setAbstraxionError] = useState(""); const [isConnected, setIsConnected] = useState(false); @@ -70,6 +74,13 @@ export function AbstraxionContextProvider({ >(undefined); const [granterAddress, setGranterAddress] = useState(""); const [dashboardUrl, setDashboardUrl] = useState(""); + let gasPriceDefault: GasPrice; + const { gasPrice: gasPriceConstant } = xionGasValues; + if (rpcUrl.includes("mainnet")) { + gasPriceDefault = GasPrice.fromString(gasPriceConstant); + } else { + gasPriceDefault = GasPrice.fromString("0uxion"); + } const configureInstance = useCallback(() => { abstraxionAuth.configureAbstraxionInstance( @@ -160,6 +171,7 @@ export function AbstraxionContextProvider({ bank, treasury, logout, + gasPrice: gasPrice ? GasPrice.fromString(gasPrice) : gasPriceDefault, }} > {children} diff --git a/packages/abstraxion/src/hooks/useAbstraxionSigningClient.ts b/packages/abstraxion/src/hooks/useAbstraxionSigningClient.ts index 1a9668a5..acddd2fc 100644 --- a/packages/abstraxion/src/hooks/useAbstraxionSigningClient.ts +++ b/packages/abstraxion/src/hooks/useAbstraxionSigningClient.ts @@ -1,5 +1,5 @@ import { useContext, useEffect, useState } from "react"; -import { testnetChainInfo } from "@burnt-labs/constants"; +import { testnetChainInfo, xionGasValues } from "@burnt-labs/constants"; import { GasPrice } from "@cosmjs/stargate"; import { GranteeSignerClient, @@ -15,8 +15,14 @@ export const useAbstraxionSigningClient = (): { | undefined; readonly logout: (() => void) | undefined; } => { - const { isConnected, abstraxionAccount, granterAddress, rpcUrl, logout } = - useContext(AbstraxionContext); + const { + isConnected, + abstraxionAccount, + granterAddress, + rpcUrl, + logout, + gasPrice, + } = useContext(AbstraxionContext); const [signArbWallet, setSignArbWallet] = useState< SignArbSecp256k1HdWallet | undefined >(undefined); @@ -50,7 +56,7 @@ export const useAbstraxionSigningClient = (): { rpcUrl || testnetChainInfo.rpc, abstraxionAccount, { - gasPrice: GasPrice.fromString("0uxion"), + gasPrice, granterAddress, granteeAddress, },