Skip to content

Commit

Permalink
Merge pull request #710 from alleslabs/feat/refactor-address
Browse files Browse the repository at this point in the history
feat: refactor addr types
  • Loading branch information
songwongtp committed Jan 3, 2024
2 parents f53be64 + f16c862 commit 0aa3919
Show file tree
Hide file tree
Showing 195 changed files with 1,118 additions and 1,087 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Improvements

- [#709](https://github.com/alleslabs/celatone-frontend/pull/709) refactor all address types

### Bug fixes

## v1.4.1
Expand Down
4 changes: 2 additions & 2 deletions src/lib/amplitude/track-event/trackInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import big from "big.js";
import { amp } from "../Amplitude";
import { AmpEvent } from "../types";
import type { SearchResultType } from "lib/services/searchService";
import type { MoveAccountAddr, Option, Token } from "lib/types";
import type { Addr, Option, Token } from "lib/types";
import { isHexModuleAddress, isHexWalletAddress } from "lib/utils";

export const trackUseMainSearch = (
Expand Down Expand Up @@ -197,7 +197,7 @@ export const trackUseToggle = (name: string, isActive: boolean) =>
amp.track(AmpEvent.USE_TOGGLE, { name, isActive });

export const trackUseModuleSelectionInputFill = (
address: MoveAccountAddr,
address: Addr,
manualModuleName: boolean,
manualFunctionName: boolean
) =>
Expand Down
6 changes: 3 additions & 3 deletions src/lib/app-fns/tx/clearAdmin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import { EstimatedFeeRender } from "lib/components/EstimatedFeeRender";
import { ExplorerLink } from "lib/components/ExplorerLink";
import { CustomIcon } from "lib/components/icon";
import { TxStreamPhase } from "lib/types";
import type { TxResultRendering, ContractAddr, HumanAddr } from "lib/types";
import type { BechAddr20, BechAddr32, TxResultRendering } from "lib/types";
import { feeFromStr } from "lib/utils";

import { catchTxError } from "./common";
import { postTx } from "./common/post";
import { sendingTx } from "./common/sending";

interface ClearAdminTxParams {
address: HumanAddr;
contractAddress: ContractAddr;
address: BechAddr20;
contractAddress: BechAddr32;
fee: StdFee;
memo?: string;
client: SigningCosmWasmClient;
Expand Down
6 changes: 3 additions & 3 deletions src/lib/app-fns/tx/execute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import { EstimatedFeeRender } from "lib/components/EstimatedFeeRender";
import { ExplorerLink } from "lib/components/ExplorerLink";
import { CustomIcon } from "lib/components/icon";
import type { Activity } from "lib/stores/contract";
import type { ContractAddr, HumanAddr, TxResultRendering } from "lib/types";
import type { BechAddr20, BechAddr32, TxResultRendering } from "lib/types";
import { TxStreamPhase } from "lib/types";
import { encode, feeFromStr, getCurrentDate } from "lib/utils";

import { catchTxError, postTx, sendingTx } from "./common";

interface ExecuteTxParams {
address: HumanAddr;
contractAddress: ContractAddr;
address: BechAddr20;
contractAddress: BechAddr32;
fee: StdFee;
msg: object;
funds: Coin[];
Expand Down
6 changes: 3 additions & 3 deletions src/lib/app-fns/tx/migrate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { Observable } from "rxjs";
import { EstimatedFeeRender } from "lib/components/EstimatedFeeRender";
import { ExplorerLink } from "lib/components/ExplorerLink";
import { CustomIcon } from "lib/components/icon";
import type { ContractAddr, HumanAddr, TxResultRendering } from "lib/types";
import type { BechAddr20, BechAddr32, TxResultRendering } from "lib/types";
import { TxStreamPhase } from "lib/types";
import { feeFromStr } from "lib/utils";

Expand All @@ -18,8 +18,8 @@ import { postTx } from "./common/post";
import { sendingTx } from "./common/sending";

interface MigrateTxParams {
sender: HumanAddr;
contractAddress: ContractAddr;
sender: BechAddr20;
contractAddress: BechAddr32;
codeId: number;
migrateMsg: object;
fee: StdFee;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/app-fns/tx/move/executeModule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import { EstimatedFeeRender } from "lib/components/EstimatedFeeRender";
import { ExplorerLink } from "lib/components/ExplorerLink";
import { CustomIcon } from "lib/components/icon";
import { TxStreamPhase } from "lib/types";
import type { HumanAddr, TxResultRendering } from "lib/types";
import type { BechAddr20, TxResultRendering } from "lib/types";
import { feeFromStr } from "lib/utils";

interface ExecuteModuleTxParams {
address: HumanAddr;
address: BechAddr20;
messages: EncodeObject[];
fee: StdFee;
client: SigningCosmWasmClient;
Expand Down
14 changes: 11 additions & 3 deletions src/lib/app-fns/tx/publish.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ import type { StdFee } from "@cosmjs/stargate";
import { pipe } from "@rx-stream/pipe";
import type { Observable } from "rxjs";

import type { PublishSucceedCallback } from "lib/app-provider/tx/publish";
import type { HumanAddr, TxResultRendering } from "lib/types";
import type { BechAddr20, Option, TxResultRendering } from "lib/types";

import { catchTxError, postTx, sendingTx } from "./common";

export interface PublishTxInternalResult {
txHash: string;
txFee: Option<string>;
}

export type PublishSucceedCallback = (
txResult: PublishTxInternalResult
) => void;

interface PublishModuleTxParams {
address: HumanAddr;
address: BechAddr20;
client: SigningCosmWasmClient;
onTxSucceed?: PublishSucceedCallback;
onTxFailed?: () => void;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/app-fns/tx/resend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import type { Observable } from "rxjs";
import { EstimatedFeeRender } from "lib/components/EstimatedFeeRender";
import { ExplorerLink } from "lib/components/ExplorerLink";
import { CustomIcon } from "lib/components/icon";
import type { HumanAddr, TxResultRendering } from "lib/types";
import type { BechAddr20, TxResultRendering } from "lib/types";
import { TxStreamPhase } from "lib/types";
import { feeFromStr } from "lib/utils";

import { catchTxError, postTx, sendingTx } from "./common";

interface ResendTxParams {
address: HumanAddr;
address: BechAddr20;
client: SigningCosmWasmClient;
fee: StdFee;
messages: EncodeObject[];
Expand Down
4 changes: 2 additions & 2 deletions src/lib/app-fns/tx/script.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import type { Observable } from "rxjs";
import { EstimatedFeeRender } from "lib/components/EstimatedFeeRender";
import { ExplorerLink } from "lib/components/ExplorerLink";
import { CustomIcon } from "lib/components/icon";
import type { HumanAddr, TxResultRendering } from "lib/types";
import type { BechAddr20, TxResultRendering } from "lib/types";
import { TxStreamPhase } from "lib/types";
import { feeFromStr } from "lib/utils";

import { catchTxError, postTx, sendingTx } from "./common";

interface DeployScriptTxParams {
address: HumanAddr;
address: BechAddr20;
client: SigningCosmWasmClient;
onTxSucceed?: () => void;
onTxFailed?: () => void;
Expand Down
30 changes: 23 additions & 7 deletions src/lib/app-fns/tx/upload.tsx → src/lib/app-fns/tx/storeCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,47 @@ import type { DeliverTxResponse, logs, StdFee } from "@cosmjs/stargate";
import { pipe } from "@rx-stream/pipe";
import type { Observable } from "rxjs";

import type { UploadSucceedCallback } from "lib/app-provider";
import { EstimatedFeeRender } from "lib/components/EstimatedFeeRender";
import { ExplorerLink } from "lib/components/ExplorerLink";
import { CustomIcon } from "lib/components/icon";
import { TxStreamPhase } from "lib/types";
import type { HumanAddr, TxResultRendering, ComposedMsg } from "lib/types";
import type {
BechAddr,
ComposedMsg,
Option,
TxResultRendering,
} from "lib/types";
import { feeFromStr, findAttr } from "lib/utils";

import { catchTxError } from "./common";
import { postTx } from "./common/post";
import { sendingTx } from "./common/sending";

interface UploadTxParams {
address: HumanAddr;
export interface StoreCodeTxInternalResult {
codeDisplayName: string;
codeId: string;
codeHash: string;
txHash: string;
txFee: Option<string>;
}

export type StoreCodeSucceedCallback = (
txResult: StoreCodeTxInternalResult
) => void;

interface StoreCodeTxParams {
address: BechAddr;
codeName: string;
messages: ComposedMsg[];
wasmFileName: string;
fee: StdFee;
memo?: string;
client: SigningCosmWasmClient;
isMigrate: boolean;
onTxSucceed: UploadSucceedCallback;
onTxSucceed: StoreCodeSucceedCallback;
}

export const uploadContractTx = ({
export const storeCodeTx = ({
address,
codeName,
messages,
Expand All @@ -37,7 +53,7 @@ export const uploadContractTx = ({
client,
isMigrate,
onTxSucceed,
}: UploadTxParams): Observable<TxResultRendering> => {
}: StoreCodeTxParams): Observable<TxResultRendering> => {
return pipe(
sendingTx(fee),
postTx<DeliverTxResponse>({
Expand Down
6 changes: 3 additions & 3 deletions src/lib/app-fns/tx/submitProposal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import type { Observable } from "rxjs";
import { EstimatedFeeRender } from "lib/components/EstimatedFeeRender";
import { ExplorerLink } from "lib/components/ExplorerLink";
import { CustomIcon } from "lib/components/icon";
import type { HumanAddr, Nullable, TxResultRendering } from "lib/types";
import type { BechAddr20, Nullable, TxResultRendering } from "lib/types";
import { TxStreamPhase } from "lib/types";
import { feeFromStr, findAttr } from "lib/utils";

import { catchTxError, postTx, sendingTx } from "./common";

interface SubmitWhitelistProposalTxParams {
address: HumanAddr;
address: BechAddr20;
client: SigningCosmWasmClient;
fee: StdFee;
messages: EncodeObject[];
Expand Down Expand Up @@ -106,7 +106,7 @@ export const submitWhitelistProposalTx = ({
};

interface SubmitStoreCodeProposalTxParams {
address: HumanAddr;
address: BechAddr20;
client: SigningCosmWasmClient;
fee: StdFee;
chainName: string;
Expand Down
12 changes: 6 additions & 6 deletions src/lib/app-fns/tx/updateAdmin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { EstimatedFeeRender } from "lib/components/EstimatedFeeRender";
import { ExplorerLink } from "lib/components/ExplorerLink";
import { CustomIcon } from "lib/components/icon";
import type {
Addr,
ContractAddr,
HumanAddr,
BechAddr,
BechAddr20,
BechAddr32,
TxResultRendering,
} from "lib/types";
import { TxStreamPhase } from "lib/types";
Expand All @@ -21,9 +21,9 @@ import { feeFromStr } from "lib/utils";
import { catchTxError, postTx, sendingTx } from "./common";

interface UpdateAdminTxParams {
address: HumanAddr;
contractAddress: ContractAddr;
newAdmin: Addr;
address: BechAddr20;
contractAddress: BechAddr32;
newAdmin: BechAddr;
fee: StdFee;
client: SigningCosmWasmClient;
onTxSucceed?: () => void;
Expand Down
14 changes: 12 additions & 2 deletions src/lib/app-provider/hooks/useCurrentChain.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import type { ChainContext as CosmosChainContext } from "@cosmos-kit/core";
import { useChain } from "@cosmos-kit/react";

import { useCelatoneApp } from "../contexts";
import type { BechAddr20, Option } from "lib/types";

export const useCurrentChain = () => {
interface ChainContext extends Omit<CosmosChainContext, "address"> {
address: Option<BechAddr20>;
}

export const useCurrentChain = (): ChainContext => {
const {
chainConfig: { registryChainName },
} = useCelatoneApp();

return useChain(registryChainName);
const { address, ...res } = useChain(registryChainName);
return {
address: address as Option<BechAddr20>,
...res,
};
};
6 changes: 3 additions & 3 deletions src/lib/app-provider/hooks/useDummyWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { useEffect, useState } from "react";

import { DUMMY_MNEMONIC } from "env";
import type { HumanAddr } from "lib/types";
import type { BechAddr20 } from "lib/types";

import { useCurrentChain } from "./useCurrentChain";

export const useDummyWallet = () => {
const { chain } = useCurrentChain();
const [dummyWallet, setDummyWallet] = useState<DirectSecp256k1HdWallet>();
const [dummyAddress, setDummyAddress] = useState<HumanAddr>();
const [dummyAddress, setDummyAddress] = useState<BechAddr20>();
useEffect(() => {
(async () => {
if (DUMMY_MNEMONIC) {
Expand All @@ -23,7 +23,7 @@ export const useDummyWallet = () => {
setDummyWallet(wallet);

const { address } = (await wallet.getAccounts())[0];
setDummyAddress(address as HumanAddr);
setDummyAddress(address as BechAddr20);
}
})();
}, [chain.bech32_prefix]);
Expand Down
6 changes: 3 additions & 3 deletions src/lib/app-provider/hooks/useExampleAddresses.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { toBech32 } from "@cosmjs/encoding";
import { useMemo } from "react";

import type { ContractAddr, HumanAddr } from "lib/types";
import type { BechAddr20, BechAddr32 } from "lib/types";
import { addrToValoper } from "lib/utils";

import { useCurrentChain } from "./useCurrentChain";
Expand All @@ -16,13 +16,13 @@ export const useExampleAddresses = () => {
const user = toBech32(
prefix,
new Uint8Array(bytes.slice(0, 20))
) as HumanAddr;
) as BechAddr20;

// reverse the bytes so the initial characters are different from the user address
const contract = toBech32(
prefix,
new Uint8Array(bytes.reverse())
) as ContractAddr;
) as BechAddr32;

const validator = addrToValoper(user);

Expand Down
Loading

2 comments on commit 0aa3919

@vercel
Copy link

@vercel vercel bot commented on 0aa3919 Jan 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 0aa3919 Jan 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.