Sfoglia il codice sorgente

[xc-admin] delete product (#651)

* add delete product feature

* fix delete product bug

* refactor

* fix precommit
Daniel Chew 2 anni fa
parent
commit
5c16987153

+ 73 - 22
governance/xc_admin/packages/xc_admin_frontend/components/tabs/General.tsx

@@ -197,6 +197,13 @@ const General = () => {
               changes[symbol].new = { ...fileDataParsed[symbol] }
             }
           })
+          // check if any existing symbols are not in uploaded json
+          Object.keys(data).forEach((symbol) => {
+            if (!fileDataParsed[symbol]) {
+              changes[symbol] = { prev: {} }
+              changes[symbol].prev = { ...data[symbol] }
+            }
+          })
           setDataChanges(changes)
           openModal()
         }
@@ -342,6 +349,30 @@ const General = () => {
                 .instruction()
             )
           }
+        } else if (!newChanges) {
+          // if new is undefined, it means that the symbol is deleted
+          // create delete price account instruction
+          instructions.push(
+            await pythProgramClient.methods
+              .delPrice()
+              .accounts({
+                fundingAccount,
+                productAccount: new PublicKey(prev.address),
+                priceAccount: new PublicKey(prev.priceAccounts[0].address),
+              })
+              .instruction()
+          )
+          // create delete product account instruction
+          instructions.push(
+            await pythProgramClient.methods
+              .delProduct()
+              .accounts({
+                fundingAccount,
+                mappingAccount: rawConfig.mappingAccounts[0].address,
+                productAccount: new PublicKey(prev.address),
+              })
+              .instruction()
+          )
         } else {
           // check if metadata has changed
           if (
@@ -433,14 +464,13 @@ const General = () => {
   }
 
   const MetadataChangesRows = ({ changes }: { changes: any }) => {
-    const addNewPriceFeed =
-      changes.prev === undefined && changes.new !== undefined
+    const addPriceFeed = changes.prev === undefined && changes.new !== undefined
 
     return (
       <>
         {Object.keys(changes.new).map(
           (metadataKey) =>
-            (addNewPriceFeed ||
+            (addPriceFeed ||
               changes.prev[metadataKey] !== changes.new[metadataKey]) && (
               <tr key={metadataKey}>
                 <td className="base16 py-4 pl-6 pr-2 lg:pl-6">
@@ -451,7 +481,7 @@ const General = () => {
                 </td>
 
                 <td className="base16 py-4 pl-1 pr-2 lg:pl-6">
-                  {!addNewPriceFeed ? (
+                  {!addPriceFeed ? (
                     <>
                       <s>{changes.prev[metadataKey]}</s>
                       <br />{' '}
@@ -467,14 +497,13 @@ const General = () => {
   }
 
   const PriceAccountsChangesRows = ({ changes }: { changes: any }) => {
-    const addNewPriceFeed =
-      changes.prev === undefined && changes.new !== undefined
+    const addPriceFeed = changes.prev === undefined && changes.new !== undefined
     return (
       <>
         {changes.new.map((priceAccount: any, index: number) =>
           Object.keys(priceAccount).map((priceAccountKey) =>
             priceAccountKey === 'publishers' ? (
-              addNewPriceFeed ? (
+              addPriceFeed ? (
                 <PublisherKeysChangesRows
                   key={priceAccountKey}
                   changes={{
@@ -494,7 +523,7 @@ const General = () => {
                 )
               )
             ) : (
-              (addNewPriceFeed ||
+              (addPriceFeed ||
                 changes.prev[index][priceAccountKey] !==
                   priceAccount[priceAccountKey]) && (
                 <tr key={priceAccountKey}>
@@ -505,7 +534,7 @@ const General = () => {
                       .join(' ')}
                   </td>
                   <td className="base16 py-4 pl-1 pr-2 lg:pl-6">
-                    {!addNewPriceFeed ? (
+                    {!addPriceFeed ? (
                       <>
                         <s>{changes.prev[index][priceAccountKey]}</s>
                         <br />
@@ -523,14 +552,13 @@ const General = () => {
   }
 
   const PublisherKeysChangesRows = ({ changes }: { changes: any }) => {
-    const addNewPriceFeed =
-      changes.prev === undefined && changes.new !== undefined
-    const publisherKeysToAdd = addNewPriceFeed
+    const addPriceFeed = changes.prev === undefined && changes.new !== undefined
+    const publisherKeysToAdd = addPriceFeed
       ? changes.new
       : changes.new.filter(
           (newPublisher: string) => !changes.prev.includes(newPublisher)
         )
-    const publisherKeysToRemove = addNewPriceFeed
+    const publisherKeysToRemove = addPriceFeed
       ? []
       : changes.prev.filter(
           (prevPublisher: string) => !changes.new.includes(prevPublisher)
@@ -580,6 +608,19 @@ const General = () => {
     )
   }
 
+  const OldPriceFeedsRows = ({ priceFeedData }: { priceFeedData: any }) => {
+    return (
+      <>
+        <tr key={priceFeedData.metadata.symbol}>
+          <td className="base16 py-4 pl-6 pr-2 lg:pl-6">Symbol</td>
+          <td className="base16 py-4 pl-1 pr-2 lg:pl-6">
+            {priceFeedData.metadata.symbol}
+          </td>
+        </tr>
+      </>
+    )
+  }
+
   const ModalContent = ({ changes }: { changes: any }) => {
     return (
       <>
@@ -588,14 +629,18 @@ const General = () => {
             {/* compare changes.prev and changes.new and display the fields that are different */}
             {Object.keys(changes).map((key) => {
               const { prev, new: newChanges } = changes[key]
-              const addNewPriceFeed =
+              const addPriceFeed =
                 prev === undefined && newChanges !== undefined
-              const diff = addNewPriceFeed
-                ? []
-                : Object.keys(prev).filter(
-                    (k) =>
-                      JSON.stringify(prev[k]) !== JSON.stringify(newChanges[k])
-                  )
+              const deletePriceFeed =
+                prev !== undefined && newChanges === undefined
+              const diff =
+                addPriceFeed || deletePriceFeed
+                  ? []
+                  : Object.keys(prev).filter(
+                      (k) =>
+                        JSON.stringify(prev[k]) !==
+                        JSON.stringify(newChanges[k])
+                    )
               return (
                 <tbody key={key}>
                   <tr>
@@ -603,11 +648,17 @@ const General = () => {
                       className="base16 py-4 pl-6 pr-2 font-bold lg:pl-6"
                       colSpan={2}
                     >
-                      {addNewPriceFeed ? 'Add New Price Feed' : key}
+                      {addPriceFeed
+                        ? 'Add New Price Feed'
+                        : deletePriceFeed
+                        ? 'Delete Old Price Feed'
+                        : key}
                     </td>
                   </tr>
-                  {addNewPriceFeed ? (
+                  {addPriceFeed ? (
                     <NewPriceFeedsRows key={key} priceFeedData={newChanges} />
+                  ) : deletePriceFeed ? (
+                    <OldPriceFeedsRows key={key} priceFeedData={prev} />
                   ) : (
                     diff.map((k) =>
                       k === 'metadata' ? (

+ 1 - 1
governance/xc_admin/packages/xc_admin_frontend/components/tabs/Proposals.tsx

@@ -402,7 +402,7 @@ const Proposal = ({
             </button>
           </div>
         ) : proposalStatus === 'executeReady' ? (
-          <div className="flex items-center justify-between px-8 pt-3">
+          <div className="flex items-center justify-center space-x-8 pt-3">
             <button
               className="action-btn text-base"
               onClick={handleClickExecute}