[xc-admin] delete product (#651)

* add delete product feature

* fix delete product bug

* refactor

* fix precommit
This commit is contained in:
Daniel Chew 2023-03-03 13:17:58 +09:00 committed by GitHub
parent 35a2fb61b0
commit 5c16987153
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 23 deletions

View File

@ -197,6 +197,13 @@ const General = () => {
changes[symbol].new = { ...fileDataParsed[symbol] } 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) setDataChanges(changes)
openModal() openModal()
} }
@ -342,6 +349,30 @@ const General = () => {
.instruction() .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 { } else {
// check if metadata has changed // check if metadata has changed
if ( if (
@ -433,14 +464,13 @@ const General = () => {
} }
const MetadataChangesRows = ({ changes }: { changes: any }) => { const MetadataChangesRows = ({ changes }: { changes: any }) => {
const addNewPriceFeed = const addPriceFeed = changes.prev === undefined && changes.new !== undefined
changes.prev === undefined && changes.new !== undefined
return ( return (
<> <>
{Object.keys(changes.new).map( {Object.keys(changes.new).map(
(metadataKey) => (metadataKey) =>
(addNewPriceFeed || (addPriceFeed ||
changes.prev[metadataKey] !== changes.new[metadataKey]) && ( changes.prev[metadataKey] !== changes.new[metadataKey]) && (
<tr key={metadataKey}> <tr key={metadataKey}>
<td className="base16 py-4 pl-6 pr-2 lg:pl-6"> <td className="base16 py-4 pl-6 pr-2 lg:pl-6">
@ -451,7 +481,7 @@ const General = () => {
</td> </td>
<td className="base16 py-4 pl-1 pr-2 lg:pl-6"> <td className="base16 py-4 pl-1 pr-2 lg:pl-6">
{!addNewPriceFeed ? ( {!addPriceFeed ? (
<> <>
<s>{changes.prev[metadataKey]}</s> <s>{changes.prev[metadataKey]}</s>
<br />{' '} <br />{' '}
@ -467,14 +497,13 @@ const General = () => {
} }
const PriceAccountsChangesRows = ({ changes }: { changes: any }) => { const PriceAccountsChangesRows = ({ changes }: { changes: any }) => {
const addNewPriceFeed = const addPriceFeed = changes.prev === undefined && changes.new !== undefined
changes.prev === undefined && changes.new !== undefined
return ( return (
<> <>
{changes.new.map((priceAccount: any, index: number) => {changes.new.map((priceAccount: any, index: number) =>
Object.keys(priceAccount).map((priceAccountKey) => Object.keys(priceAccount).map((priceAccountKey) =>
priceAccountKey === 'publishers' ? ( priceAccountKey === 'publishers' ? (
addNewPriceFeed ? ( addPriceFeed ? (
<PublisherKeysChangesRows <PublisherKeysChangesRows
key={priceAccountKey} key={priceAccountKey}
changes={{ changes={{
@ -494,7 +523,7 @@ const General = () => {
) )
) )
) : ( ) : (
(addNewPriceFeed || (addPriceFeed ||
changes.prev[index][priceAccountKey] !== changes.prev[index][priceAccountKey] !==
priceAccount[priceAccountKey]) && ( priceAccount[priceAccountKey]) && (
<tr key={priceAccountKey}> <tr key={priceAccountKey}>
@ -505,7 +534,7 @@ const General = () => {
.join(' ')} .join(' ')}
</td> </td>
<td className="base16 py-4 pl-1 pr-2 lg:pl-6"> <td className="base16 py-4 pl-1 pr-2 lg:pl-6">
{!addNewPriceFeed ? ( {!addPriceFeed ? (
<> <>
<s>{changes.prev[index][priceAccountKey]}</s> <s>{changes.prev[index][priceAccountKey]}</s>
<br /> <br />
@ -523,14 +552,13 @@ const General = () => {
} }
const PublisherKeysChangesRows = ({ changes }: { changes: any }) => { const PublisherKeysChangesRows = ({ changes }: { changes: any }) => {
const addNewPriceFeed = const addPriceFeed = changes.prev === undefined && changes.new !== undefined
changes.prev === undefined && changes.new !== undefined const publisherKeysToAdd = addPriceFeed
const publisherKeysToAdd = addNewPriceFeed
? changes.new ? changes.new
: changes.new.filter( : changes.new.filter(
(newPublisher: string) => !changes.prev.includes(newPublisher) (newPublisher: string) => !changes.prev.includes(newPublisher)
) )
const publisherKeysToRemove = addNewPriceFeed const publisherKeysToRemove = addPriceFeed
? [] ? []
: changes.prev.filter( : changes.prev.filter(
(prevPublisher: string) => !changes.new.includes(prevPublisher) (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 }) => { const ModalContent = ({ changes }: { changes: any }) => {
return ( return (
<> <>
@ -588,13 +629,17 @@ const General = () => {
{/* compare changes.prev and changes.new and display the fields that are different */} {/* compare changes.prev and changes.new and display the fields that are different */}
{Object.keys(changes).map((key) => { {Object.keys(changes).map((key) => {
const { prev, new: newChanges } = changes[key] const { prev, new: newChanges } = changes[key]
const addNewPriceFeed = const addPriceFeed =
prev === undefined && newChanges !== undefined prev === undefined && newChanges !== undefined
const diff = addNewPriceFeed const deletePriceFeed =
prev !== undefined && newChanges === undefined
const diff =
addPriceFeed || deletePriceFeed
? [] ? []
: Object.keys(prev).filter( : Object.keys(prev).filter(
(k) => (k) =>
JSON.stringify(prev[k]) !== JSON.stringify(newChanges[k]) JSON.stringify(prev[k]) !==
JSON.stringify(newChanges[k])
) )
return ( return (
<tbody key={key}> <tbody key={key}>
@ -603,11 +648,17 @@ const General = () => {
className="base16 py-4 pl-6 pr-2 font-bold lg:pl-6" className="base16 py-4 pl-6 pr-2 font-bold lg:pl-6"
colSpan={2} colSpan={2}
> >
{addNewPriceFeed ? 'Add New Price Feed' : key} {addPriceFeed
? 'Add New Price Feed'
: deletePriceFeed
? 'Delete Old Price Feed'
: key}
</td> </td>
</tr> </tr>
{addNewPriceFeed ? ( {addPriceFeed ? (
<NewPriceFeedsRows key={key} priceFeedData={newChanges} /> <NewPriceFeedsRows key={key} priceFeedData={newChanges} />
) : deletePriceFeed ? (
<OldPriceFeedsRows key={key} priceFeedData={prev} />
) : ( ) : (
diff.map((k) => diff.map((k) =>
k === 'metadata' ? ( k === 'metadata' ? (

View File

@ -402,7 +402,7 @@ const Proposal = ({
</button> </button>
</div> </div>
) : proposalStatus === 'executeReady' ? ( ) : 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 <button
className="action-btn text-base" className="action-btn text-base"
onClick={handleClickExecute} onClick={handleClickExecute}