From a7828f4d35b7050484f5f6bff367a11c4d9f426f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Sworze=C5=84?= Date: Fri, 8 Mar 2024 16:25:24 +0100 Subject: [PATCH] move generateAnchor to untils --- govtool/frontend/src/context/wallet.tsx | 42 +++++++------------ govtool/frontend/src/utils/generateAnchor.tsx | 9 ++++ govtool/frontend/src/utils/index.ts | 1 + 3 files changed, 25 insertions(+), 27 deletions(-) create mode 100644 govtool/frontend/src/utils/generateAnchor.tsx diff --git a/govtool/frontend/src/context/wallet.tsx b/govtool/frontend/src/context/wallet.tsx index 18f00d5de..d5eda8cf7 100644 --- a/govtool/frontend/src/context/wallet.tsx +++ b/govtool/frontend/src/context/wallet.tsx @@ -10,8 +10,6 @@ import { } from "react"; import { Address, - Anchor, - AnchorDataHash, BigNum, Certificate, CertificatesBuilder, @@ -33,7 +31,6 @@ import { TransactionUnspentOutput, TransactionUnspentOutputs, TransactionWitnessSet, - URL, Value, VoteDelegation, Voter, @@ -59,20 +56,21 @@ import { PATHS } from "@consts"; import { CardanoApiWallet, VoterInfo, Protocol } from "@models"; import type { StatusModalState } from "@organisms"; import { - getPubDRepID, - WALLET_LS_KEY, - DELEGATE_TRANSACTION_KEY, - REGISTER_TRANSACTION_KEY, + checkIsMaintenanceOn, DELEGATE_TO_KEY, - PROTOCOL_PARAMS_KEY, + DELEGATE_TRANSACTION_KEY, + generateAnchor, getItemFromLocalStorage, - setItemToLocalStorage, - removeItemFromLocalStorage, + getPubDRepID, openInNewTab, + PROTOCOL_PARAMS_KEY, + REGISTER_SOLE_VOTER_TRANSACTION_KEY, + REGISTER_TRANSACTION_KEY, + removeItemFromLocalStorage, SANCHO_INFO_KEY, + setItemToLocalStorage, VOTE_TRANSACTION_KEY, - checkIsMaintenanceOn, - REGISTER_SOLE_VOTER_TRANSACTION_KEY, + WALLET_LS_KEY, } from "@utils"; import { getEpochParams, getTransactionStatus } from "@services"; import { @@ -1096,9 +1094,7 @@ function CardanoProvider(props: Props) { let dRepRegCert; // If there is an anchor if (cip95MetadataURL && cip95MetadataHash) { - const url = URL.new(cip95MetadataURL); - const hash = AnchorDataHash.from_hex(cip95MetadataHash); - const anchor = Anchor.new(url, hash); + const anchor = generateAnchor(cip95MetadataURL, cip95MetadataHash); // Create cert object using one Ada as the deposit dRepRegCert = DrepRegistration.new_with_anchor( dRepCred, @@ -1141,9 +1137,7 @@ function CardanoProvider(props: Props) { let dRepUpdateCert; // If there is an anchor if (cip95MetadataURL && cip95MetadataHash) { - const url = URL.new(cip95MetadataURL); - const hash = AnchorDataHash.from_hex(cip95MetadataHash); - const anchor = Anchor.new(url, hash); + const anchor = generateAnchor(cip95MetadataURL, cip95MetadataHash); // Create cert object using one Ada as the deposit dRepUpdateCert = DrepUpdate.new_with_anchor(dRepCred, anchor); } else { @@ -1217,9 +1211,7 @@ function CardanoProvider(props: Props) { let votingProcedure; if (cip95MetadataURL && cip95MetadataHash) { - const url = URL.new(cip95MetadataURL); - const hash = AnchorDataHash.from_hex(cip95MetadataHash); - const anchor = Anchor.new(url, hash); + const anchor = generateAnchor(cip95MetadataURL, cip95MetadataHash); // Create cert object using one Ada as the deposit votingProcedure = VotingProcedure.new_with_anchor( votingChoice, @@ -1259,9 +1251,7 @@ function CardanoProvider(props: Props) { const infoAction = InfoAction.new(); const infoGovAct = GovernanceAction.new_info_action(infoAction); // Create an anchor - const anchorURL = URL.new(url); - const anchorHash = AnchorDataHash.from_hex(hash); - const anchor = Anchor.new(anchorURL, anchorHash); + const anchor = generateAnchor(url, hash); const rewardAddr = RewardAddress.from_address( Address.from_bech32(rewardAddress) @@ -1315,9 +1305,7 @@ function CardanoProvider(props: Props) { const treasuryGovAct = GovernanceAction.new_treasury_withdrawals_action(treasuryAction); // Create an anchor - const anchorURL = URL.new(url); - const anchorHash = AnchorDataHash.from_hex(hash); - const anchor = Anchor.new(anchorURL, anchorHash); + const anchor = generateAnchor(url, hash); const rewardAddr = RewardAddress.from_address( Address.from_bech32(rewardAddress) diff --git a/govtool/frontend/src/utils/generateAnchor.tsx b/govtool/frontend/src/utils/generateAnchor.tsx new file mode 100644 index 000000000..3f457e6e6 --- /dev/null +++ b/govtool/frontend/src/utils/generateAnchor.tsx @@ -0,0 +1,9 @@ +import {Anchor, AnchorDataHash, URL } from "@emurgo/cardano-serialization-lib-asmjs"; + +export const generateAnchor = (url: string, hash: string) => { + const metadataUrl = URL.new(url); + const urlHash = AnchorDataHash.from_hex(hash); + const anchor = Anchor.new(metadataUrl, urlHash); + + return anchor; +}; \ No newline at end of file diff --git a/govtool/frontend/src/utils/index.ts b/govtool/frontend/src/utils/index.ts index 08a870600..313353de7 100644 --- a/govtool/frontend/src/utils/index.ts +++ b/govtool/frontend/src/utils/index.ts @@ -4,6 +4,7 @@ export * from "./callAll"; export * from "./checkIsMaintenanceOn"; export * from "./checkIsWalletConnected"; export * from "./formatDate"; +export * from "./generateAnchor"; export * from "./getDRepID"; export * from "./getGovActionId"; export * from "./getLengthInBytes";