Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(spaceward): build TxRaw without StargateClient #795

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading