Skip to content

Commit

Permalink
overwrite signAndBroadcast method to handle simulation and default to…
Browse files Browse the repository at this point in the history
… auto fee
  • Loading branch information
BurntVal committed Oct 11, 2024
1 parent d79fba7 commit 67e5152
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 19 deletions.
2 changes: 1 addition & 1 deletion apps/demo-app/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const legacyConfig = {
};

const treasuryConfig = {
treasury: "xion17ah4x9te3sttpy2vj5x6hv4xvc0td526nu0msf7mt3kydqj4qs2s9jhe90", // Example XION treasury contract
treasury: "xion1h82c0efsxxq4pgua754u6xepfu6avglup20fl834gc2ah0ptgn5s2zffe9", // Example XION treasury contract with /cosmwasm.wasm.v1.MsgExecuteContract grant
// Optional params to activate mainnet config
// rpcUrl: "https://rpc.xion-mainnet-1.burnt.com:443",
// restUrl: "https://api.xion-mainnet-1.burnt.com:443",
Expand Down
42 changes: 27 additions & 15 deletions apps/demo-app/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import {
} from "@burnt-labs/abstraxion";
import { Button } from "@burnt-labs/ui";
import "@burnt-labs/ui/dist/index.css";
import type { ExecuteResult } from "@cosmjs/cosmwasm-stargate";
import type { DeliverTxResponse } from "@cosmjs/cosmwasm-stargate";
import { SignArb } from "../components/sign-arb.tsx";

const seatContractAddress =
"xion1z70cvc08qv5764zeg3dykcyymj5z6nu4sqr7x8vl4zjef2gyp69s9mmdka";

type ExecuteResultOrUndefined = ExecuteResult | undefined;

export default function Page(): JSX.Element {
// Abstraxion hooks
const { data: account } = useAbstraxionAccount();
Expand All @@ -28,8 +26,9 @@ export default function Page(): JSX.Element {
React.Dispatch<React.SetStateAction<boolean>>,
] = useModal();
const [loading, setLoading] = useState(false);
const [executeResult, setExecuteResult] =
useState<ExecuteResultOrUndefined>(undefined);
const [executeResult, setExecuteResult] = useState<
DeliverTxResponse | undefined
>(undefined);

const blockExplorerUrl = `https://explorer.burnt.com/xion-testnet-1/tx/${executeResult?.transactionHash}`;

Expand Down Expand Up @@ -58,17 +57,30 @@ export default function Page(): JSX.Element {
};

try {
const claimRes = await client?.execute(
account.bech32Address,
seatContractAddress,
msg,
{
amount: [{ amount: "0", denom: "uxion" }],
gas: "500000",
const msgSerialized = new TextEncoder().encode(JSON.stringify(msg));
const msgExecuteContract = {
typeUrl: "/cosmwasm.wasm.v1.MsgExecuteContract",
value: {
sender: account.bech32Address,
contract: seatContractAddress,
msg: msgSerialized,
funds: [],
},
"",
[],
);
};

const claimRes = await client?.signAndBroadcast(account.bech32Address, [
msgExecuteContract,
]);

// OR
// const claimRes = await client?.execute(
// account.bech32Address,
// seatContractAddress,
// msg,
// "auto",
// "",
// [],
// );

setExecuteResult(claimRes);
} catch (error) {
Expand Down
51 changes: 48 additions & 3 deletions packages/abstraxion-core/src/GranteeSignerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@ import {
EncodeObject,
OfflineSigner,
} from "@cosmjs/proto-signing";
import type { Account, SignerData, StdFee } from "@cosmjs/stargate";
import type { TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx";
import {
calculateFee,
GasPrice,
type Account,
type SignerData,
type StdFee,
} from "@cosmjs/stargate";
import { TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx";
import { MsgExec } from "cosmjs-types/cosmos/authz/v1beta1/tx";
import {
HttpEndpoint,
Tendermint37Client,
TendermintClient,
} from "@cosmjs/tendermint-rpc";
import { customAccountFromAny } from "@burnt-labs/signers";
import { xionGasValues } from "@burnt-labs/constants";

interface GranteeSignerOptions {
readonly granterAddress: string;
Expand Down Expand Up @@ -111,7 +118,45 @@ export class GranteeSignerClient extends SigningCosmWasmClient {
];
}

return super.signAndBroadcast(signerAddress, messages, fee, memo);
let usedFee: StdFee;
const {
gasPrice: gasPriceString,
gasAdjustment,
gasAdjustmentMargin,
} = xionGasValues;
if (fee == "auto" || typeof fee === "number") {
const gasEstimation = await this.simulate(signerAddress, messages, memo);
const multiplier = typeof fee == "number" ? fee : gasAdjustment;
const gasPrice = GasPrice.fromString(gasPriceString);
// usedFee = calculateFee(Math.round(gasEstimation * multiplier), gasPrice);
const calculatedFee = calculateFee(gasEstimation, gasPrice);
let gas = Math.ceil(
parseInt(calculatedFee.gas) * multiplier + gasAdjustmentMargin,
).toString();

const chainId = await this.getChainId();
if (/testnet/.test(chainId)) {
usedFee = { amount: [{ amount: "0", denom: "uxion" }], gas };
} else {
usedFee = { amount: calculatedFee.amount, gas };
}
} else {
usedFee = fee;
}

const txRaw = await this.sign(
signerAddress,
messages,
usedFee,
memo,
undefined,
);
const txBytes = TxRaw.encode(txRaw).finish();
return this.broadcastTx(
txBytes,
this.broadcastTimeoutMs,
this.broadcastPollIntervalMs,
);
}

public async sign(
Expand Down

0 comments on commit 67e5152

Please sign in to comment.