Skip to content

Commit

Permalink
Added APIs for pause/unpasuing a token (#148)
Browse files Browse the repository at this point in the history
Signed-off-by: Himalayan Dev <kipkap1001@gmail.com>
  • Loading branch information
himalayan-dev authored Mar 16, 2024
1 parent 240fb91 commit 3f78919
Show file tree
Hide file tree
Showing 18 changed files with 6,576 additions and 14,036 deletions.
2 changes: 1 addition & 1 deletion packages/hedera-wallet-snap/jest.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/// <reference types="jest-extended" />
// / <reference types="jest-extended" />
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
MetamaskActions,
} from '../../../contexts/MetamaskContext';
import useModal from '../../../hooks/useModal';
import { Account, DeleteTokenRequestParams } from '../../../types/snap';
import { Account, PauseOrDeleteTokenRequestParams } from '../../../types/snap';
import { deleteToken, shouldDisplayReconnectButton } from '../../../utils';
import { Card, SendHelloButton } from '../../base';
import ExternalAccount, {
Expand Down Expand Up @@ -52,7 +52,7 @@ const DeleteToken: FC<Props> = ({ network, mirrorNodeUrl, setAccountInfo }) => {

const deleteTokenParams = {
tokenId,
} as DeleteTokenRequestParams;
} as PauseOrDeleteTokenRequestParams;

const response: any = await deleteToken(
network,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*-
*
* Hedera Wallet Snap
*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import { FC, useContext, useRef, useState } from 'react';
import {
MetaMaskContext,
MetamaskActions,
} from '../../../contexts/MetamaskContext';
import useModal from '../../../hooks/useModal';
import { Account, PauseOrDeleteTokenRequestParams } from '../../../types/snap';
import { pauseToken, shouldDisplayReconnectButton } from '../../../utils';
import { Card, SendHelloButton } from '../../base';
import ExternalAccount, {
GetExternalAccountRef,
} from '../../sections/ExternalAccount';

type Props = {
network: string;
mirrorNodeUrl: string;
setAccountInfo: React.Dispatch<React.SetStateAction<Account>>;
};

const PauseToken: FC<Props> = ({ network, mirrorNodeUrl, setAccountInfo }) => {
const [state, dispatch] = useContext(MetaMaskContext);
const [loading, setLoading] = useState(false);
const { showModal } = useModal();
const [tokenId, setTokenId] = useState<string>();

const externalAccountRef = useRef<GetExternalAccountRef>(null);

const handlePauseTokenClick = async () => {
setLoading(true);
try {
const externalAccountParams =
externalAccountRef.current?.handleGetAccountParams();

const pauseTokenParams = {
tokenId,
} as PauseOrDeleteTokenRequestParams;

const response: any = await pauseToken(
network,
mirrorNodeUrl,
pauseTokenParams,
externalAccountParams,
);

const { receipt, currentAccount } = response;

setAccountInfo(currentAccount);
console.log('receipt: ', receipt);

showModal({
title: 'Transaction Receipt',
content: JSON.stringify({ receipt }, null, 4),
});
} catch (error) {
console.error(error);
dispatch({ type: MetamaskActions.SetError, payload: error });
}
setLoading(false);
};

return (
<Card
content={{
title: 'pauseToken',
description:
'Pause a token. This will prevent all transactions from occurring on the token.',
form: (
<>
<ExternalAccount ref={externalAccountRef} />
<label>
Enter the token ID to pause
<input
type="text"
style={{ width: '100%' }}
value={tokenId}
placeholder="Token Id"
onChange={(error) => setTokenId(error.target.value)}
/>
</label>
<br />
</>
),
button: (
<SendHelloButton
buttonText="Pause"
onClick={handlePauseTokenClick}
disabled={!state.installedSnap}
loading={loading}
/>
),
}}
disabled={!state.installedSnap}
fullWidth={
state.isFlask &&
Boolean(state.installedSnap) &&
!shouldDisplayReconnectButton(state.installedSnap)
}
/>
);
};

export { PauseToken };
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*-
*
* Hedera Wallet Snap
*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import { FC, useContext, useRef, useState } from 'react';
import {
MetaMaskContext,
MetamaskActions,
} from '../../../contexts/MetamaskContext';
import useModal from '../../../hooks/useModal';
import { Account, PauseOrDeleteTokenRequestParams } from '../../../types/snap';
import { shouldDisplayReconnectButton, unpauseToken } from '../../../utils';
import { Card, SendHelloButton } from '../../base';
import ExternalAccount, {
GetExternalAccountRef,
} from '../../sections/ExternalAccount';

type Props = {
network: string;
mirrorNodeUrl: string;
setAccountInfo: React.Dispatch<React.SetStateAction<Account>>;
};

const UnpauseToken: FC<Props> = ({
network,
mirrorNodeUrl,
setAccountInfo,
}) => {
const [state, dispatch] = useContext(MetaMaskContext);
const [loading, setLoading] = useState(false);
const { showModal } = useModal();
const [tokenId, setTokenId] = useState<string>();

const externalAccountRef = useRef<GetExternalAccountRef>(null);

const handleUnpauseTokenClick = async () => {
setLoading(true);
try {
const externalAccountParams =
externalAccountRef.current?.handleGetAccountParams();

const unpauseTokenParams = {
tokenId,
} as PauseOrDeleteTokenRequestParams;

const response: any = await unpauseToken(
network,
mirrorNodeUrl,
unpauseTokenParams,
externalAccountParams,
);

const { receipt, currentAccount } = response;

setAccountInfo(currentAccount);
console.log('receipt: ', receipt);

showModal({
title: 'Transaction Receipt',
content: JSON.stringify({ receipt }, null, 4),
});
} catch (error) {
console.error(error);
dispatch({ type: MetamaskActions.SetError, payload: error });
}
setLoading(false);
};

return (
<Card
content={{
title: 'unpauseToken',
description:
'Unpause a token that was previously disabled from participating in transactions.',
form: (
<>
<ExternalAccount ref={externalAccountRef} />
<label>
Enter the token ID to unpause
<input
type="text"
style={{ width: '100%' }}
value={tokenId}
placeholder="Token Id"
onChange={(error) => setTokenId(error.target.value)}
/>
</label>
<br />
</>
),
button: (
<SendHelloButton
buttonText="Unpause"
onClick={handleUnpauseTokenClick}
disabled={!state.installedSnap}
loading={loading}
/>
),
}}
disabled={!state.installedSnap}
fullWidth={
state.isFlask &&
Boolean(state.installedSnap) &&
!shouldDisplayReconnectButton(state.installedSnap)
}
/>
);
};

export { UnpauseToken };
16 changes: 15 additions & 1 deletion packages/hedera-wallet-snap/packages/site/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ import { UnstakeHbar } from '../components/cards/UnstakeHbar';
import { AssociateTokens } from '../components/cards/hts/AssociateTokens';
import { BurnToken } from '../components/cards/hts/BurnToken';
import { CreateToken } from '../components/cards/hts/CreateToken';
import { DeleteToken } from '../components/cards/hts/DeleteToken';
import { DissociateTokens } from '../components/cards/hts/DissociateTokens';
import { FreezeAccount } from '../components/cards/hts/FreezeAccount';
import { MintToken } from '../components/cards/hts/MintToken';
import { PauseToken } from '../components/cards/hts/PauseToken';
import { UnfreezeAccount } from '../components/cards/hts/UnfreezeAccount';
import { UnpauseToken } from '../components/cards/hts/UnpauseToken';
import { WipeToken } from '../components/cards/hts/WipeToken';
import { networkOptions } from '../config/constants';
import {
Expand All @@ -57,7 +60,6 @@ import {
import { MetaMaskContext, MetamaskActions } from '../contexts/MetamaskContext';
import { Account } from '../types/snap';
import { connectSnap, getSnap } from '../utils';
import {DeleteToken} from "../components/cards/hts/DeleteToken";

const Index = () => {
const [state, dispatch] = useContext(MetaMaskContext);
Expand Down Expand Up @@ -225,6 +227,18 @@ const Index = () => {
setAccountInfo={setAccountInfo}
/>

<PauseToken
network={currentNetwork.value}
mirrorNodeUrl={mirrorNodeUrl}
setAccountInfo={setAccountInfo}
/>

<UnpauseToken
network={currentNetwork.value}
mirrorNodeUrl={mirrorNodeUrl}
setAccountInfo={setAccountInfo}
/>

<AssociateTokens
network={currentNetwork.value}
mirrorNodeUrl={mirrorNodeUrl}
Expand Down
8 changes: 4 additions & 4 deletions packages/hedera-wallet-snap/packages/site/src/types/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ export type BurnTokenRequestParams = {
serialNumbers?: number[];
};

export type PauseOrDeleteTokenRequestParams = {
tokenId: string;
};

export type AssociateTokensRequestParams = {
tokenIds: string[];
};
Expand All @@ -171,10 +175,6 @@ export type DissociateTokensRequestParams = {
tokenIds: string[];
};

export type DeleteTokenRequestParams = {
tokenId: string | undefined;
};

export type FreezeAccountRequestParams = {
tokenId: string;
accountId: string;
Expand Down
Loading

0 comments on commit 3f78919

Please sign in to comment.