Skip to content

Commit

Permalink
update token facade + changes for including tokenid
Browse files Browse the repository at this point in the history
Signed-off-by: Ivy Astrix <[email protected]>
  • Loading branch information
poi-son-ivy committed Mar 14, 2024
1 parent c5ca31a commit 9a1276d
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ export class UpdateTokenCommand {
client: Client,
updateParams: UpdateTokenRequestParams,
): Promise<TxReceipt> {
const updateTransaction = this.#createTransaction(
updateParams,
this.#tokenId,
);
const updateTransaction = this.#createTransaction(updateParams);

const signTx = await updateTransaction
.freezeWith(client)
Expand All @@ -62,9 +59,10 @@ export class UpdateTokenCommand {

#createTransaction(
updateParams: UpdateTokenRequestParams,
tokenId: string,
): TokenUpdateTransaction {
const transaction = new TokenUpdateTransaction().setTokenId(tokenId);
const transaction = new TokenUpdateTransaction().setTokenId(
updateParams.tokenId,
);

if (updateParams.name !== undefined) {
transaction.setTokenName(updateParams.name);
Expand All @@ -78,46 +76,54 @@ export class UpdateTokenCommand {
transaction.setTreasuryAccountId(updateParams.treasuryAccountId);
}

if (updateParams.adminKey !== undefined) {
transaction.setAdminKey(PublicKey.fromStringECDSA(updateParams.adminKey));
if (updateParams.adminPublicKey !== undefined) {
transaction.setAdminKey(
PublicKey.fromStringECDSA(updateParams.adminPublicKey),
);
}

if (updateParams.kycKey !== undefined) {
transaction.setKycKey(PublicKey.fromStringECDSA(updateParams.kycKey));
if (updateParams.kycPublicKey !== undefined) {
transaction.setKycKey(
PublicKey.fromStringECDSA(updateParams.kycPublicKey),
);
}

if (updateParams.freezeKey !== undefined) {
if (updateParams.freezePublicKey !== undefined) {
transaction.setFreezeKey(
PublicKey.fromStringECDSA(updateParams.freezeKey),
PublicKey.fromStringECDSA(updateParams.freezePublicKey),
);
}

if (updateParams.feeScheduleKey !== undefined) {
if (updateParams.feeSchedulePublicKey !== undefined) {
transaction.setFeeScheduleKey(
PublicKey.fromStringECDSA(updateParams.feeScheduleKey),
PublicKey.fromStringECDSA(updateParams.feeSchedulePublicKey),
);
}

if (updateParams.pauseKey !== undefined) {
transaction.setPauseKey(PublicKey.fromStringECDSA(updateParams.pauseKey));
if (updateParams.pausePublicKey !== undefined) {
transaction.setPauseKey(
PublicKey.fromStringECDSA(updateParams.pausePublicKey),
);
}

if (updateParams.wipeKey !== undefined) {
transaction.setWipeKey(PublicKey.fromStringECDSA(updateParams.wipeKey));
if (updateParams.wipePublicKey !== undefined) {
transaction.setWipeKey(
PublicKey.fromStringECDSA(updateParams.wipePublicKey),
);
}

if (updateParams.supplyKey !== undefined) {
if (updateParams.supplyPublicKey !== undefined) {
transaction.setSupplyKey(
PublicKey.fromStringECDSA(updateParams.supplyKey),
PublicKey.fromStringECDSA(updateParams.supplyPublicKey),
);
}

if (updateParams.expirationTime !== undefined) {
transaction.setExpirationTime(new Date(updateParams.expirationTime));
}

if (updateParams.memo !== undefined) {
transaction.setTokenMemo(updateParams.memo);
if (updateParams.tokenMemo !== undefined) {
transaction.setTokenMemo(updateParams.tokenMemo);
}

if (updateParams.autoRenewAccountId !== undefined) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*-
*
* 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.
*
*/

/* eslint-disable @typescript-eslint/restrict-template-expressions */

import { providerErrors } from '@metamask/rpc-errors';
import { divider, heading, text } from '@metamask/snaps-ui';
import _ from 'lodash';
import { HederaClientImplFactory } from '../../client/HederaClientImplFactory';
import { TxReceipt } from '../../types/hedera';
import { UpdateTokenRequestParams } from '../../types/params';
import { SnapDialogParams, WalletSnapParams } from '../../types/state';
import { SnapUtils } from '../../utils/SnapUtils';
import { UpdateTokenCommand } from '../../commands/hts/UpdateTokenCommand';

export class UpdateTokenFacade {
/**
* Updates prioerties for a token.
*
* @param walletSnapParams - Wallet snap params.
* @param updateTokenRequestParams - Parameters for updating a token.
* @returns Receipt of the transaction.
*/
public static async updateToken(
walletSnapParams: WalletSnapParams,
updateTokenRequestParams: UpdateTokenRequestParams,
): Promise<TxReceipt> {
const { origin, state } = walletSnapParams;

const { hederaEvmAddress, hederaAccountId, network } = state.currentAccount;

const { privateKey, publicKey, curve } =
state.accountState[hederaEvmAddress][network].keyStore;

const {
tokenId,
name,
symbol,
kycPublicKey,
freezePublicKey,
pausePublicKey,
wipePublicKey,
supplyPublicKey,
feeSchedulePublicKey,
expirationTime,
autoRenewAccountId = hederaAccountId,
tokenMemo = 'Created via Hedera Wallet Snap',
} = updateTokenRequestParams;

let txReceipt = {} as TxReceipt;
try {
const panelToShow = [
heading('Update a token'),
text(
'Learn more about updating tokens [here](https://docs.hedera.com/hedera/sdks-and-apis/sdks/token-service/update-a-token)',
),
text(`You are about to modify a token with the following details:`),
divider(),
text(`Id: ${tokenId}`),

text(`Name: ${name}`),
text(`Symbol: ${symbol}`),
];

panelToShow.push(
text(`Auto Renew Account ID: ${autoRenewAccountId}`),
text(`Token Memo: ${tokenMemo}`),
text(`Admin Key: ${publicKey}`),
text(`Treasury Account: ${hederaAccountId}`),
text(
`KYC Public Key: ${
_.isEmpty(kycPublicKey) ? 'Not set' : (kycPublicKey as string)
}`,
),
text(
`Freeze Public Key: ${
_.isEmpty(freezePublicKey) ? 'Not set' : (freezePublicKey as string)
}`,
),
text(
`Pause Public Key:${
_.isEmpty(pausePublicKey) ? 'Not set' : (pausePublicKey as string)
}`,
),
text(
`Wipe Public Key: ${
_.isEmpty(wipePublicKey) ? 'Not set' : (wipePublicKey as string)
}`,
),
text(
`Supply Public Key: ${
_.isEmpty(supplyPublicKey) ? 'Not set' : (supplyPublicKey as string)
}`,
),
text(
`Fee Schedule Public Key: ${
_.isEmpty(feeSchedulePublicKey)
? 'Not set'
: (feeSchedulePublicKey as string)
}`,
),
);
if (expirationTime) {
panelToShow.push(text(`Expiration Time: ${expirationTime}`));
}

const dialogParams: SnapDialogParams = {
type: 'confirmation',
content: await SnapUtils.generateCommonPanel(origin, panelToShow),
};
const confirmed = await SnapUtils.snapDialog(dialogParams);
if (!confirmed) {
console.error(`User rejected the transaction`);
throw providerErrors.userRejectedRequest();
}

const hederaClientFactory = new HederaClientImplFactory(
hederaAccountId,
network,
curve,
privateKey,
);

const hederaClient = await hederaClientFactory.createClient();
if (hederaClient === null) {
throw new Error('hedera client returned null');
}

const privateKeyObj = hederaClient.getPrivateKey();
if (privateKeyObj === null) {
throw new Error('private key object returned null');
}
const command = new UpdateTokenCommand(tokenId, privateKeyObj);

txReceipt = await command.execute(
hederaClient.getClient(),
updateTokenRequestParams,
);
} catch (error: any) {
const errMessage = `Error while trying to update a token: ${String(
error,
)}`;
console.error(errMessage);
throw providerErrors.unsupportedMethod(errMessage);
}

return txReceipt;
}
}
17 changes: 9 additions & 8 deletions packages/hedera-wallet-snap/packages/snap/src/types/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,19 @@ export type WipeTokenRequestParams = {
};

export type UpdateTokenRequestParams = {
tokenId: string;
name?: string;
symbol?: string;
treasuryAccountId?: string;
adminKey?: string;
kycKey?: string;
freezeKey?: string;
feeScheduleKey?: string;
pauseKey?: string;
wipeKey?: string;
supplyKey?: string;
adminPublicKey?: string;
kycPublicKey?: string;
freezePublicKey?: string;
feeSchedulePublicKey?: string;
pausePublicKey?: string;
wipePublicKey?: string;
supplyPublicKey?: string;
expirationTime?: string;
memo?: string;
tokenMemo?: string;
autoRenewAccountId?: string;
autoRenewPeriod?: number;
};

0 comments on commit 9a1276d

Please sign in to comment.