Skip to content

Commit

Permalink
refactored updating token fee schedules to a separate API / added a g…
Browse files Browse the repository at this point in the history
…lobal var for jest that was causing tests with imports that eventually hit log.js in node-forge to fail

Signed-off-by: Ivy Astrix <[email protected]>
  • Loading branch information
poi-son-ivy committed Mar 18, 2024
1 parent c9607ea commit 907a4e8
Show file tree
Hide file tree
Showing 10 changed files with 306 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/*-
*
* 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, TokenCustomFee } from '../../../types/snap';
import {
shouldDisplayReconnectButton,
updateTokenFeeSchedule,
} 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 UpdateTokenFeeSchedule: FC<Props> = ({
network,
mirrorNodeUrl,
setAccountInfo,
}) => {
const [state, dispatch] = useContext(MetaMaskContext);
const [loading, setLoading] = useState(false);
const { showModal } = useModal();
const [tokenId, setTokenId] = useState('');
const [feeCollectorAccountId, setTokenFeeCollectorAccountId] = useState('');
const [hbarAmount, setHbarAmount] = useState('');
const [tokenAmount, setTokenAmount] = useState('');
const [denominatingTokenId, setDenominatingTokenId] = useState('');
const [allCollectorsAreExempt, setCollectorsExempt] = useState(false);

const externalAccountRef = useRef<GetExternalAccountRef>(null);

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

const numberHbarAmount = Number(hbarAmount);
const numberTokenAmount = Number(tokenAmount);
const booleanAllCollectors = Boolean(allCollectorsAreExempt);

const tokenCustomFee = {
feeCollectorAccountId,
hbarAmount: numberHbarAmount,
tokenAmount: numberTokenAmount,
denominatingTokenId,
allCollectorsAreExempt: booleanAllCollectors,
} as TokenCustomFee;

const customFees = [tokenCustomFee];

const updateTokenFeeScheduleParams = {
tokenId,
customFees,
};

const response: any = await updateTokenFeeSchedule(
network,
mirrorNodeUrl,
updateTokenFeeScheduleParams,
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: 'updateTokenFeeSchedule',
description: `Update a token's fee schedule on Hedera using Hedera Token Service.`,
form: (
<>
<ExternalAccount ref={externalAccountRef} />
<label>
Enter the token id
<input
type="text"
style={{ width: '100%' }}
value={tokenId}
placeholder="Token ID"
onChange={(error) => setTokenId(error.target.value)}
/>
</label>
<br />
<label>
Enter the fee collector account id
<input
type="text"
style={{ width: '100%' }}
value={feeCollectorAccountId}
placeholder="Token Name"
onChange={(error) =>
setTokenFeeCollectorAccountId(error.target.value)
}
/>
</label>
<br />
<label>
Enter the hbar amount
<input
type="text"
style={{ width: '100%' }}
value={hbarAmount}
placeholder="HBar Amount"
onChange={(error) => setHbarAmount(error.target.value)}
/>
</label>
<br />
<label>
Enter the new token amount
<input
type="text"
style={{ width: '100%' }}
value={tokenAmount}
placeholder="Token Amount"
onChange={(error) => setTokenAmount(error.target.value)}
/>
</label>
<br />
<label>
Enter the denominating token id
<input
type="text"
style={{ width: '100%' }}
value={denominatingTokenId}
placeholder="Enter the new treasury account id"
onChange={(error) => setDenominatingTokenId(error.target.value)}
/>
</label>
<br />
<label>
Select whether collectors are exempt
<input
type="checkbox"
style={{ width: '100%' }}
value={allCollectorsAreExempt ? 'true' : 'false'}
placeholder="Are all collectors exempt?"
onChange={(changeEvent) =>
setCollectorsExempt(changeEvent.target.checked)
}
/>
</label>
</>
),
button: (
<SendHelloButton
buttonText="Update Token Fee Schedule"
onClick={handleUpdateTokenClick}
disabled={!state.installedSnap}
loading={loading}
/>
),
}}
disabled={!state.installedSnap}
fullWidth={
state.isFlask &&
Boolean(state.installedSnap) &&
!shouldDisplayReconnectButton(state.installedSnap)
}
/>
);
};

export { UpdateTokenFeeSchedule };
7 changes: 7 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 @@ -59,6 +59,7 @@ import { Account } from '../types/snap';
import { connectSnap, getSnap } from '../utils';
import { DeleteToken } from '../components/cards/hts/DeleteToken';
import { UpdateToken } from '../components/cards/hts/UpdateToken';
import { UpdateTokenFeeSchedule } from '../components/cards/hts/UpdateTokenFeeSchedule';

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

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

<DeleteToken
network={currentNetwork.value}
mirrorNodeUrl={mirrorNodeUrl}
Expand Down
5 changes: 5 additions & 0 deletions packages/hedera-wallet-snap/packages/site/src/types/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,8 @@ export type UpdateTokenRequestParams = {
autoRenewAccountId?: string;
autoRenewPeriod?: number;
};

export type UpdateTokenFeeScheduleRequestParams = {
tokenId: string;
customFees: TokenCustomFee[];
};
32 changes: 32 additions & 0 deletions packages/hedera-wallet-snap/packages/site/src/utils/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
StakeHbarRequestParams,
TransferCryptoRequestParams,
UpdateTokenRequestParams,
UpdateTokenFeeScheduleRequestParams,
WipeTokenRequestParams,
} from '../types/snap';

Expand Down Expand Up @@ -507,6 +508,37 @@ export const updateToken = async (
});
};

/**
* Invoke the "updateTokenFeeSchedule" method from the snap.
*
* @param network
* @param mirrorNodeUrl
* @param updateTokenFeeScheduleRequestParams
* @param externalAccountparams
*/
export const updateTokenFeeSchedule = async (
network: string,
mirrorNodeUrl: string,
updateTokenFeeScheduleRequestParams: UpdateTokenFeeScheduleRequestParams,
externalAccountparams?: ExternalAccountParams,
) => {
return await window.ethereum.request({
method: 'wallet_invokeSnap',
params: {
snapId: defaultSnapOrigin,
request: {
method: 'hts/updateTokenFeeSchedule',
params: {
network,
mirrorNodeUrl,
...updateTokenFeeScheduleRequestParams,
...externalAccountparams,
},
},
},
});
};

/**
* Invoke the "mintToken" method from the snap.
*
Expand Down
1 change: 1 addition & 0 deletions packages/hedera-wallet-snap/packages/snap/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = {
window: {
location: {
hostname: 'hedera-pulse',
href: 'http://localhost',
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { UpdateTokenCommand } from '../../commands/hts/UpdateTokenCommand';

export class UpdateTokenFacade {
/**
* Updates prioerties for a token.
* Updates priorities for a token.
*
* @param walletSnapParams - Wallet snap params.
* @param updateTokenRequestParams - Parameters for updating a token.
Expand All @@ -59,7 +59,6 @@ export class UpdateTokenFacade {
wipePublicKey,
supplyPublicKey,
feeSchedulePublicKey,
customFees,
expirationTime,
autoRenewAccountId = hederaAccountId,
tokenMemo = 'Created via Hedera Wallet Snap',
Expand Down
Loading

0 comments on commit 907a4e8

Please sign in to comment.