Skip to content

Commit

Permalink
fix(spaceward): build TxRaw without StargateClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Pitasi committed Sep 5, 2024
1 parent 5435212 commit 2ba5424
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
12 changes: 4 additions & 8 deletions spaceward/src/features/actions/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useChain } from "@cosmos-kit/react";
import { cosmos } from "@wardenprotocol/wardenjs";
import { createPersistantState } from "../../hooks/state";
import { useNewAction } from "../../hooks/useAction";
import { getSigningClient, TxOptions } from "../../hooks/useClient";
import { TxOptions, useTx } from "../../hooks/useClient";
import { env } from "../../env";
import { Action } from "@wardenprotocol/wardenjs/codegen/warden/act/v1beta1/action";
import { TransactionLike } from "ethers";
Expand Down Expand Up @@ -60,11 +60,9 @@ export const useActionsState = createPersistantState<
export function useEnqueueAction<Data>(
getMessage: ReturnType<typeof useNewAction<Data>>["getMessage"],
) {
const { address, getOfflineSignerDirect: getOfflineSigner } = useChain(
env.cosmoskitChainName,
);

const { address } = useChain(env.cosmoskitChainName);
const { setData } = useActionsState();
const { sign } = useTx();

async function addAction(
data: Parameters<typeof getMessage>[0],
Expand Down Expand Up @@ -100,11 +98,9 @@ export function useEnqueueAction<Data>(
}

const storeId = getActionId();
const signer = getOfflineSigner();
const client = await getSigningClient(signer);
const msg = getMessage(data, actionTimeoutHeight);
const fee = opts.fee || defaultFee;
const signedTx = await client.sign(address, [msg], fee, "");
const signedTx = await sign([msg], { fee });

setData({
[storeId]: {
Expand Down
31 changes: 20 additions & 11 deletions spaceward/src/hooks/useClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ export function useTx() {
const { address, getOfflineSignerDirect: getOfflineSigner, chain } = useChain(env.cosmoskitChainName);
const { toast } = useToast();

const sign = async (msgs: EncodeObject[], options: TxOptions) => {
const signer = getOfflineSigner();
const client = await getSigningClient(signer);

const fee = options.fee || defaultFee;
const txBody = TxBody.fromPartial({
messages: msgs,
memo: '',
});

return await buildTxRaw(chain.chain_id, client, signer, txBody, fee);
}

const tx = async (msgs: EncodeObject[], options: TxOptions) => {
if (!address) {
toast({
Expand All @@ -52,17 +65,13 @@ export function useTx() {
return;
}

let signed: Uint8Array;
const signer = getOfflineSigner();
const client = await getSigningClient(signer);

let signed: Uint8Array;
try {
const fee = options.fee || defaultFee;
const txBody = TxBody.fromPartial({
messages: msgs,
memo: '',
});
signed = await buildTxRaw(chain.chain_id, client, signer, txBody, fee);
const txRaw = await sign(msgs, options);
signed = TxRaw.encode(txRaw).finish();
} catch (e: unknown) {
console.error(e);
toast({
Expand All @@ -79,7 +88,7 @@ export function useTx() {
duration: 999999,
});

if (client && signed) {
if (signed) {
try {
const res = await client.broadcastTx(signed);
if (isDeliverTxSuccess(res)) {
Expand Down Expand Up @@ -122,7 +131,7 @@ export function useTx() {
}
};

return { tx };
return { tx, sign };
}

export function useQueryHooks() {
Expand Down Expand Up @@ -176,11 +185,11 @@ async function buildTxRaw(
const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, Number(account.accountNumber));
const { signature, signed } = await signer.signDirect(signerAddress, signDoc);

return TxRaw.encode({
return TxRaw.fromPartial({
bodyBytes: signed.bodyBytes,
authInfoBytes: signed.authInfoBytes,
signatures: [fromBase64(signature.signature)],
}).finish();
});
}

async function fetchAccount(address: string) {
Expand Down

0 comments on commit 2ba5424

Please sign in to comment.