Add requestJson

This commit is contained in:
abefernan 2023-07-21 19:52:24 +02:00
parent 1621d26211
commit 3ceaa89f2c
4 changed files with 16 additions and 20 deletions

View File

@ -1,10 +1,10 @@
import { EncodeObject } from "@cosmjs/proto-signing";
import { Account, calculateFee } from "@cosmjs/stargate";
import { assert } from "@cosmjs/utils";
import axios from "axios";
import { NextRouter, withRouter } from "next/router";
import { useRef, useState } from "react";
import { useChains } from "../../../context/ChainsContext";
import { requestJson } from "../../../lib/request";
import { exportMsgToJson, gasOfTx } from "../../../lib/txMsgHelpers";
import { DbTransaction } from "../../../types";
import { MsgTypeUrl, MsgTypeUrls } from "../../../types/txMsg";
@ -74,10 +74,8 @@ const CreateTxForm = ({ router, senderAddress, accountOnChain }: CreateTxFormPro
memo,
};
const {
data: { transactionID },
} = await axios.post("/api/transaction", {
dataJSON: JSON.stringify(tx),
const transactionID = await requestJson("/api/transaction", {
body: { dataJSON: JSON.stringify(tx) },
});
router.push(`/${chain.registryName}/${senderAddress}/transaction/${transactionID}`);

View File

@ -4,10 +4,10 @@ import { LedgerSigner } from "@cosmjs/ledger-amino";
import { SigningStargateClient } from "@cosmjs/stargate";
import { assert } from "@cosmjs/utils";
import TransportWebUSB from "@ledgerhq/hw-transport-webusb";
import axios from "axios";
import { useCallback, useLayoutEffect, useState } from "react";
import { useChains } from "../../context/ChainsContext";
import { getConnectError } from "../../lib/errorHelpers";
import { requestJson } from "../../lib/request";
import { DbSignature, DbTransaction, WalletAccount } from "../../types";
import HashView from "../dataViews/HashView";
import Button from "../inputs/Button";
@ -173,10 +173,7 @@ const TransactionSigning = (props: TransactionSigningProps) => {
signature: bases64EncodedSignature,
address: signerAddress,
};
const _res = await axios.post(
`/api/transaction/${props.transactionID}/signature`,
signature,
);
await requestJson(`/api/transaction/${props.transactionID}/signature`, { body: signature });
props.addSignature(signature);
setSigning("signed");
}

View File

@ -6,8 +6,8 @@ import {
} from "@cosmjs/amino";
import { Account, StargateClient } from "@cosmjs/stargate";
import { assert } from "@cosmjs/utils";
import axios from "axios";
import { checkAddress } from "./displayHelpers";
import { requestJson } from "./request";
/**
* Turns array of compressed Secp256k1 pubkeys
@ -41,9 +41,9 @@ const createMultisigFromCompressedSecp256k1Pubkeys = async (
chainId,
};
const res = await axios.post(`/api/chain/${chainId}/multisig`, multisig);
console.log(res.data);
return res.data.address;
const res = await requestJson(`/api/chain/${chainId}/multisig`, { body: multisig });
console.log(res);
return res.address;
};
/**
@ -80,12 +80,13 @@ const getMultisigAccount = async (
pubkey = accountOnChain.pubkey;
} else {
console.log("No pubkey on chain for: ", address);
const res = await axios.get(`/api/chain/${chainId}/multisig/${address}`);
if (res.status !== 200) {
try {
const res = await requestJson(`/api/chain/${chainId}/multisig/${address}`);
pubkey = JSON.parse(res.pubkeyJSON);
} catch {
throw new Error("Multisig has no pubkey on node, and was not created using this tool.");
}
pubkey = JSON.parse(res.data.pubkeyJSON);
}
return [pubkey, accountOnChain];

View File

@ -2,7 +2,6 @@ import { MultisigThresholdPubkey } from "@cosmjs/amino";
import { fromBase64 } from "@cosmjs/encoding";
import { Account, StargateClient, makeMultisignedTxBytes } from "@cosmjs/stargate";
import { assert } from "@cosmjs/utils";
import axios from "axios";
import { GetServerSideProps } from "next";
import { useRouter } from "next/router";
import { useCallback, useEffect, useState } from "react";
@ -16,6 +15,7 @@ import StackableContainer from "../../../../components/layout/StackableContainer
import { useChains } from "../../../../context/ChainsContext";
import { findTransactionByID } from "../../../../lib/graphqlHelpers";
import { getMultisigAccount } from "../../../../lib/multisigHelpers";
import { requestJson } from "../../../../lib/request";
import { dbTxFromJson } from "../../../../lib/txMsgHelpers";
import { DbSignature } from "../../../../types";
@ -129,8 +129,8 @@ const TransactionPage = ({
const broadcaster = await StargateClient.connect(chain.nodeAddress);
const result = await broadcaster.broadcastTx(signedTxBytes);
console.log(result);
const _res = await axios.post(`/api/transaction/${transactionID}`, {
txHash: result.transactionHash,
await requestJson(`/api/transaction/${transactionID}`, {
body: { txHash: result.transactionHash },
});
setTransactionHash(result.transactionHash);
// eslint-disable-next-line @typescript-eslint/no-explicit-any