Skip to content

Commit

Permalink
Feat/kyc flag api (#196)
Browse files Browse the repository at this point in the history
Signed-off-by: Himalayan Dev <[email protected]>
Signed-off-by: Himalayan Dev <[email protected]>
  • Loading branch information
himalayan-dev authored Mar 18, 2024
1 parent 8590a47 commit 3e72195
Show file tree
Hide file tree
Showing 19 changed files with 15,398 additions and 6,268 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*-
*
* 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,
FreezeOrEnableKYCAccountRequestParams,
} from '../../../types/snap';
import {
disableKYCAccount,
shouldDisplayReconnectButton,
} from '../../../utils';
import { Card, SendHelloButton } from '../../base';
import { GetExternalAccountRef } from '../../sections/ExternalAccount';

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

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

const externalAccountRef = useRef<GetExternalAccountRef>(null);

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

const disableKYCAccountParams = {
tokenId,
accountId,
} as FreezeOrEnableKYCAccountRequestParams;

const response: any = await disableKYCAccount(
network,
mirrorNodeUrl,
disableKYCAccountParams,
externalAccountParams,
);

const { receipt, currentAccount } = response;

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

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

return (
<Card
content={{
title: 'disableKYCAccount',
description:
'Revokes the KYC flag to the Hedera account for the given Hedera token.',
form: (
<>
<label>
Enter the token Id to use
<input
type="string"
style={{ width: '100%' }}
value={tokenId}
placeholder="Enter Token Id(0.0.x)"
onChange={(e) => setTokenId(e.target.value)}
/>
</label>
<br />

<label>
Enter the account Id to revoke KYC from
<input
type="string"
style={{ width: '100%' }}
value={accountId}
placeholder="Enter Account Id(0.0.x)"
onChange={(e) => setAccountId(e.target.value)}
/>
</label>
<br />
</>
),
button: (
<SendHelloButton
buttonText="Revoke KYC"
onClick={handleDisableKYCAccountClick}
disabled={!state.installedSnap}
loading={loading}
/>
),
}}
disabled={!state.installedSnap}
fullWidth={
state.isFlask &&
Boolean(state.installedSnap) &&
!shouldDisplayReconnectButton(state.installedSnap)
}
/>
);
};

export { DisableKYCAccount };
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*-
*
* 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,
FreezeOrEnableKYCAccountRequestParams,
} from '../../../types/snap';
import { enableKYCAccount, shouldDisplayReconnectButton } from '../../../utils';
import { Card, SendHelloButton } from '../../base';
import { GetExternalAccountRef } from '../../sections/ExternalAccount';

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

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

const externalAccountRef = useRef<GetExternalAccountRef>(null);

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

const enableKYCAccountParams = {
tokenId,
accountId,
} as FreezeOrEnableKYCAccountRequestParams;

const response: any = await enableKYCAccount(
network,
mirrorNodeUrl,
enableKYCAccountParams,
externalAccountParams,
);

const { receipt, currentAccount } = response;

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

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

return (
<Card
content={{
title: 'enableKYCAccount',
description:
'Grants KYC to the Hedera accounts for the given Hedera token.',
form: (
<>
<label>
Enter the token Id to use
<input
type="string"
style={{ width: '100%' }}
value={tokenId}
placeholder="Enter Token Id(0.0.x)"
onChange={(e) => setTokenId(e.target.value)}
/>
</label>
<br />

<label>
Enter the account Id to grant KYC to
<input
type="string"
style={{ width: '100%' }}
value={accountId}
placeholder="Enter Account Id(0.0.x)"
onChange={(e) => setAccountId(e.target.value)}
/>
</label>
<br />
</>
),
button: (
<SendHelloButton
buttonText="Grant KYC"
onClick={handleEnableKYCAccountClick}
disabled={!state.installedSnap}
loading={loading}
/>
),
}}
disabled={!state.installedSnap}
fullWidth={
state.isFlask &&
Boolean(state.installedSnap) &&
!shouldDisplayReconnectButton(state.installedSnap)
}
/>
);
};

export { EnableKYCAccount };
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import {
MetamaskActions,
} from '../../../contexts/MetamaskContext';
import useModal from '../../../hooks/useModal';
import { Account, FreezeAccountRequestParams } from '../../../types/snap';
import {
Account,
FreezeOrEnableKYCAccountRequestParams,
} from '../../../types/snap';
import { freezeAccount, shouldDisplayReconnectButton } from '../../../utils';
import { Card, SendHelloButton } from '../../base';
import { GetExternalAccountRef } from '../../sections/ExternalAccount';
Expand Down Expand Up @@ -57,7 +60,7 @@ const FreezeAccount: FC<Props> = ({
const freezeAccountParams = {
tokenId,
accountId,
} as FreezeAccountRequestParams;
} as FreezeOrEnableKYCAccountRequestParams;

const response: any = await freezeAccount(
network,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import {
MetamaskActions,
} from '../../../contexts/MetamaskContext';
import useModal from '../../../hooks/useModal';
import { Account, FreezeAccountRequestParams } from '../../../types/snap';
import {
Account,
FreezeOrEnableKYCAccountRequestParams,
} from '../../../types/snap';
import { shouldDisplayReconnectButton, unfreezeAccount } from '../../../utils';
import { Card, SendHelloButton } from '../../base';
import { GetExternalAccountRef } from '../../sections/ExternalAccount';
Expand Down Expand Up @@ -57,7 +60,7 @@ const UnfreezeAccount: FC<Props> = ({
const unfreezeAccountParams = {
tokenId,
accountId,
} as FreezeAccountRequestParams;
} as FreezeOrEnableKYCAccountRequestParams;

const response: any = await unfreezeAccount(
network,
Expand Down
14 changes: 14 additions & 0 deletions packages/hedera-wallet-snap/packages/site/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ 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 { DisableKYCAccount } from '../components/cards/hts/DisableKYCAccount';
import { DissociateTokens } from '../components/cards/hts/DissociateTokens';
import { EnableKYCAccount } from '../components/cards/hts/EnableKYCAccount';
import { FreezeAccount } from '../components/cards/hts/FreezeAccount';
import { MintToken } from '../components/cards/hts/MintToken';
import { PauseToken } from '../components/cards/hts/PauseToken';
Expand Down Expand Up @@ -263,6 +265,18 @@ const Index = () => {
setAccountInfo={setAccountInfo}
/>

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

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

<WipeToken
network={currentNetwork.value}
mirrorNodeUrl={mirrorNodeUrl}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export type DissociateTokensRequestParams = {
tokenIds: string[];
};

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

0 comments on commit 3e72195

Please sign in to comment.