From c55c654bbac68d6c87af0ba0aa39929f0cc619d3 Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Wed, 14 Jun 2023 20:33:20 +0200 Subject: [PATCH 01/11] Simplify Tx types --- types/txMsg.ts | 89 +++++++++++++++++--------------------------------- 1 file changed, 30 insertions(+), 59 deletions(-) diff --git a/types/txMsg.ts b/types/txMsg.ts index 469d6f2..3c7afc9 100644 --- a/types/txMsg.ts +++ b/types/txMsg.ts @@ -1,65 +1,36 @@ import { MsgSend } from "cosmjs-types/cosmos/bank/v1beta1/tx"; -import { MsgSetWithdrawAddress, MsgWithdrawDelegatorReward } from "cosmjs-types/cosmos/distribution/v1beta1/tx"; -import { MsgBeginRedelegate, MsgDelegate, MsgUndelegate } from "cosmjs-types/cosmos/staking/v1beta1/tx"; +import { + MsgSetWithdrawAddress, + MsgWithdrawDelegatorReward, +} from "cosmjs-types/cosmos/distribution/v1beta1/tx"; +import { + MsgBeginRedelegate, + MsgDelegate, + MsgUndelegate, +} from "cosmjs-types/cosmos/staking/v1beta1/tx"; import { MsgCreateVestingAccount } from "cosmjs-types/cosmos/vesting/v1beta1/tx"; import { MsgTransfer } from "cosmjs-types/ibc/applications/transfer/v1/tx"; -export type MsgType = - | "send" - | "delegate" - | "undelegate" - | "redelegate" - | "claimRewards" - | "setWithdrawAddress" - | "createVestingAccount" - | "msgTransfer"; +export const MsgTypeUrls = { + Send: "/cosmos.bank.v1beta1.MsgSend", + SetWithdrawAddress: "/cosmos.distribution.v1beta1.MsgSetWithdrawAddress", + WithdrawDelegatorReward: "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward", + BeginRedelegate: "/cosmos.staking.v1beta1.MsgBeginRedelegate", + Delegate: "/cosmos.staking.v1beta1.MsgDelegate", + Undelegate: "/cosmos.staking.v1beta1.MsgUndelegate", + CreateVestingAccount: "/cosmos.vesting.v1beta1.MsgCreateVestingAccount", + Transfer: "/ibc.applications.transfer.v1.MsgTransfer", +} as const; -export type TxMsg = - | TxMsgSend - | TxMsgDelegate - | TxMsgUndelegate - | TxMsgRedelegate - | TxMsgClaimRewards - | TxMsgSetWithdrawAddress - | TxMsgCreateVestingAccount - | TxMsgTransfer; +export type MsgTypeUrl = (typeof MsgTypeUrls)[keyof typeof MsgTypeUrls]; -export interface TxMsgSend { - readonly typeUrl: "/cosmos.bank.v1beta1.MsgSend"; - readonly value: MsgSend; -} - -export interface TxMsgDelegate { - readonly typeUrl: "/cosmos.staking.v1beta1.MsgDelegate"; - readonly value: MsgDelegate; -} - -export interface TxMsgUndelegate { - readonly typeUrl: "/cosmos.staking.v1beta1.MsgUndelegate"; - readonly value: MsgUndelegate; -} - -export interface TxMsgRedelegate { - readonly typeUrl: "/cosmos.staking.v1beta1.MsgBeginRedelegate"; - readonly value: MsgBeginRedelegate; -} - -export interface TxMsgClaimRewards { - readonly typeUrl: "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward"; - readonly value: MsgWithdrawDelegatorReward; -} - -export interface TxMsgSetWithdrawAddress { - readonly typeUrl: "/cosmos.distribution.v1beta1.MsgSetWithdrawAddress"; - readonly value: MsgSetWithdrawAddress; -} - -export interface TxMsgCreateVestingAccount { - readonly typeUrl: "/cosmos.vesting.v1beta1.MsgCreateVestingAccount"; - readonly value: MsgCreateVestingAccount; -} - -export interface TxMsgTransfer { - readonly typeUrl: "/ibc.applications.transfer.v1.MsgTransfer"; - readonly value: MsgTransfer; -} +export const MsgCodecs = { + [MsgTypeUrls.Send]: MsgSend, + [MsgTypeUrls.SetWithdrawAddress]: MsgSetWithdrawAddress, + [MsgTypeUrls.WithdrawDelegatorReward]: MsgWithdrawDelegatorReward, + [MsgTypeUrls.BeginRedelegate]: MsgBeginRedelegate, + [MsgTypeUrls.Delegate]: MsgDelegate, + [MsgTypeUrls.Undelegate]: MsgUndelegate, + [MsgTypeUrls.CreateVestingAccount]: MsgCreateVestingAccount, + [MsgTypeUrls.Transfer]: MsgTransfer, +}; From 76df022af848412ced36e8f94fbcea30d4428c3d Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Wed, 14 Jun 2023 20:36:18 +0200 Subject: [PATCH 02/11] Adapt Tx helpers --- lib/txMsgHelpers.ts | 159 ++++++++------------------------------------ 1 file changed, 26 insertions(+), 133 deletions(-) diff --git a/lib/txMsgHelpers.ts b/lib/txMsgHelpers.ts index bbbcb92..4cee151 100644 --- a/lib/txMsgHelpers.ts +++ b/lib/txMsgHelpers.ts @@ -1,153 +1,60 @@ import { EncodeObject } from "@cosmjs/proto-signing"; -import Long from "long"; import { DbTransaction } from "../types"; -import { - MsgType, - TxMsg, - TxMsgClaimRewards, - TxMsgCreateVestingAccount, - TxMsgDelegate, - TxMsgRedelegate, - TxMsgSend, - TxMsgSetWithdrawAddress, - TxMsgTransfer, - TxMsgUndelegate, -} from "../types/txMsg"; +import { MsgCodecs, MsgTypeUrl, MsgTypeUrls } from "../types/txMsg"; -const isTxMsgSend = (msg: TxMsg | EncodeObject): msg is TxMsgSend => - msg.typeUrl === "/cosmos.bank.v1beta1.MsgSend" && - "value" in msg && - "fromAddress" in msg.value && - "toAddress" in msg.value && - "amount" in msg.value && - !!msg.value.fromAddress && - !!msg.value.toAddress && - !!msg.value.amount.length; - -const isTxMsgDelegate = (msg: TxMsg | EncodeObject): msg is TxMsgDelegate => - msg.typeUrl === "/cosmos.staking.v1beta1.MsgDelegate" && - "value" in msg && - "delegatorAddress" in msg.value && - "validatorAddress" in msg.value && - "amount" in msg.value && - !!msg.value.delegatorAddress && - !!msg.value.validatorAddress && - !!msg.value.amount; - -const isTxMsgUndelegate = (msg: TxMsg | EncodeObject): msg is TxMsgUndelegate => - msg.typeUrl === "/cosmos.staking.v1beta1.MsgUndelegate" && - "value" in msg && - "delegatorAddress" in msg.value && - "validatorAddress" in msg.value && - "amount" in msg.value && - !!msg.value.delegatorAddress && - !!msg.value.validatorAddress && - !!msg.value.amount; - -const isTxMsgRedelegate = (msg: TxMsg | EncodeObject): msg is TxMsgRedelegate => - msg.typeUrl === "/cosmos.staking.v1beta1.MsgBeginRedelegate" && - "value" in msg && - "delegatorAddress" in msg.value && - "validatorSrcAddress" in msg.value && - "validatorDstAddress" in msg.value && - "amount" in msg.value && - !!msg.value.delegatorAddress && - !!msg.value.validatorSrcAddress && - !!msg.value.validatorDstAddress && - !!msg.value.amount; - -const isTxMsgClaimRewards = (msg: TxMsg | EncodeObject): msg is TxMsgClaimRewards => - msg.typeUrl === "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward" && - "value" in msg && - "delegatorAddress" in msg.value && - "validatorAddress" in msg.value && - !!msg.value.delegatorAddress && - !!msg.value.validatorAddress; - -const isTxMsgSetWithdrawAddress = (msg: TxMsg | EncodeObject): msg is TxMsgSetWithdrawAddress => - msg.typeUrl === "/cosmos.distribution.v1beta1.MsgSetWithdrawAddress" && - "value" in msg && - "delegatorAddress" in msg.value && - "withdrawAddress" in msg.value && - !!msg.value.delegatorAddress && - !!msg.value.withdrawAddress; - -const isTxMsgCreateVestingAccount = (msg: TxMsg | EncodeObject): msg is TxMsgCreateVestingAccount => - msg.typeUrl === "/cosmos.vesting.v1beta1.MsgCreateVestingAccount" && - "value" in msg && - "fromAddress" in msg.value && - "toAddress" in msg.value && - "amount" in msg.value && - "endTime" in msg.value && - "delayed" in msg.value && - !!msg.value.fromAddress && - !!msg.value.toAddress && - !!msg.value.amount.length && - !!msg.value.endTime && - typeof msg.value.delayed === "boolean"; - -const isTxMsgTransfer = (msg: TxMsg | EncodeObject): msg is TxMsgTransfer => - msg.typeUrl === "/ibc.applications.transfer.v1.MsgTransfer" && - "value" in msg && - "sourcePort" in msg.value && - "sourceChannel" in msg.value && - "sender" in msg.value && - "receiver" in msg.value && - !!msg.value.sourcePort && - !!msg.value.sourceChannel && - !!msg.value.sender && - !!msg.value.receiver; - -const gasOfMsg = (msgType: MsgType): number => { +const gasOfMsg = (msgType: MsgTypeUrl): number => { switch (msgType) { - case "send": + case MsgTypeUrls.Send: return 100_000; - case "delegate": + case MsgTypeUrls.SetWithdrawAddress: return 100_000; - case "undelegate": + case MsgTypeUrls.WithdrawDelegatorReward: return 100_000; - case "redelegate": + case MsgTypeUrls.BeginRedelegate: return 100_000; - case "claimRewards": + case MsgTypeUrls.Delegate: return 100_000; - case "setWithdrawAddress": + case MsgTypeUrls.Undelegate: return 100_000; - case "createVestingAccount": + case MsgTypeUrls.CreateVestingAccount: return 100_000; - case "msgTransfer": + case MsgTypeUrls.Transfer: return 100_000; default: throw new Error("Unknown msg type"); } }; -const gasOfTx = (msgTypes: readonly MsgType[]): number => { +export const gasOfTx = (msgTypes: readonly MsgTypeUrl[]): number => { const txFlatGas = 100_000; const totalTxGas = msgTypes.reduce((acc, msgType) => acc + gasOfMsg(msgType), txFlatGas); return totalTxGas; }; -const importMsgType = (msg: EncodeObject): EncodeObject => { - if (isTxMsgCreateVestingAccount(msg)) { - return { ...msg, value: { ...msg.value, endTime: Long.fromValue(msg.value.endTime) } }; - } +export const isKnownMsgTypeUrl = (typeUrl: string): typeUrl is MsgTypeUrl => + Object.values(MsgTypeUrls).includes(typeUrl as MsgTypeUrl); - if (isTxMsgTransfer(msg)) { - return { - ...msg, - value: { ...msg.value, timeoutTimestamp: Long.fromValue(msg.value.timeoutTimestamp) }, - }; +export const exportMsgToJson = (msg: EncodeObject): EncodeObject => { + if (isKnownMsgTypeUrl(msg.typeUrl)) { + return { ...msg, value: MsgCodecs[msg.typeUrl].toJSON(msg.value) }; } return msg; }; -const importMsgTypes = (msgs: readonly EncodeObject[]): EncodeObject[] => msgs.map(importMsgType); +const importMsgFromJson = (msg: EncodeObject): EncodeObject => { + if (isKnownMsgTypeUrl(msg.typeUrl)) { + const parsedValue = MsgCodecs[msg.typeUrl].fromJSON(msg.value); + return { ...msg, value: parsedValue }; + } -const dbTxFromJson = (txJson: string): DbTransaction | null => { + return msg; +}; + +export const dbTxFromJson = (txJson: string): DbTransaction | null => { try { const dbTx: DbTransaction = JSON.parse(txJson); - dbTx.msgs = importMsgTypes(dbTx.msgs); + dbTx.msgs = dbTx.msgs.map(importMsgFromJson); return dbTx; } catch (error) { @@ -160,17 +67,3 @@ const dbTxFromJson = (txJson: string): DbTransaction | null => { return null; } }; - -export { - isTxMsgSend, - isTxMsgDelegate, - isTxMsgUndelegate, - isTxMsgRedelegate, - isTxMsgClaimRewards, - isTxMsgSetWithdrawAddress, - isTxMsgCreateVestingAccount, - isTxMsgTransfer, - gasOfMsg, - gasOfTx, - dbTxFromJson, -}; From 6413149f6196755d7486232b2d2cad73d2bd041a Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Wed, 14 Jun 2023 20:37:12 +0200 Subject: [PATCH 03/11] Adapt details forms --- .../TxMsgClaimRewardsDetails.tsx | 10 ++--- .../TxMsgCreateVestingAccountDetails.tsx | 24 +++++------ .../TransactionInfo/TxMsgDelegateDetails.tsx | 17 +++++--- .../TxMsgRedelegateDetails.tsx | 21 +++++---- .../TransactionInfo/TxMsgSendDetails.tsx | 12 +++--- .../TxMsgSetWithdrawAddressDetails.tsx | 10 ++--- .../TransactionInfo/TxMsgTransferDetails.tsx | 25 ++++++----- .../TxMsgUndelegateDetails.tsx | 17 +++++--- .../dataViews/TransactionInfo/index.tsx | 43 +++++++++++-------- 9 files changed, 99 insertions(+), 80 deletions(-) diff --git a/components/dataViews/TransactionInfo/TxMsgClaimRewardsDetails.tsx b/components/dataViews/TransactionInfo/TxMsgClaimRewardsDetails.tsx index 9ecdd65..5df2193 100644 --- a/components/dataViews/TransactionInfo/TxMsgClaimRewardsDetails.tsx +++ b/components/dataViews/TransactionInfo/TxMsgClaimRewardsDetails.tsx @@ -1,19 +1,19 @@ -import { TxMsgClaimRewards } from "../../../types/txMsg"; +import { MsgWithdrawDelegatorReward } from "cosmjs-types/cosmos/distribution/v1beta1/tx"; import HashView from "../HashView"; interface TxMsgClaimRewardsDetailsProps { - readonly msg: TxMsgClaimRewards; + readonly msgValue: MsgWithdrawDelegatorReward; } -const TxMsgClaimRewardsDetails = ({ msg }: TxMsgClaimRewardsDetailsProps) => ( +const TxMsgClaimRewardsDetails = ({ msgValue }: TxMsgClaimRewardsDetailsProps) => ( <>
  • MsgWithdrawDelegatorReward

  • -
    - +
    +