Skip to content

Commit

Permalink
feat: allow updating metadata (#1714)
Browse files Browse the repository at this point in the history
* feat: allow updating metadata

* connect wallet cta:
  • Loading branch information
saihaj authored Aug 8, 2024
1 parent 957e9ef commit d0cb82a
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion website/src/routes/publish.lazy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ const GetSubgraphInfo = graphql(/* GraphQL */ `
}
versions {
id
subgraphDeployment {
ipfsHash
}
metadata {
label
}
Expand Down Expand Up @@ -255,6 +258,20 @@ function DeploySubgraph({
enabled: !!subgraphId && !!chain,
});

function updateMetadata() {
if (!subgraphInfo) return false;

const version = form.watch('versionLabel');

const versionInfo = subgraphInfo.subgraph?.versions.find(
({ metadata }) => metadata?.label === version,
);

if (!versionInfo) return false;

return versionInfo.subgraphDeployment.ipfsHash === deploymentId;
}

function ensureNewVersion() {
// If there is no subgraph ID then it means we are just deploying a new subgraph
if (!subgraphId) return true;
Expand Down Expand Up @@ -307,6 +324,37 @@ function DeploySubgraph({
return;
}

// we are just updating the metadata
if (updateMetadata() && !ensureNewVersion()) {
const subgraphMeta = await uploadFileToIpfs({
path: '',
content: Buffer.from(
JSON.stringify(
subgraphMetadata({
...values,
}),
),
),
});

const hash = await writeContractAsync({
address: selectedChain.contracts.L2GNS.address as Address,
abi: L2GNSABI,
functionName: 'updateSubgraphMetadata',
args: [BigInt(subgraphInfo.subgraph.nftID), ipfsHexHash(subgraphMeta)],
});

const etherscanUrl = getEtherscanUrl({ chainId: selectedChain.chainId, hash });

window.open(etherscanUrl, '_blank');
setDeployed(true);
toast({
description: 'You are all set! You can go back to the CLI and close this window',
});

return;
}

try {
const hash = await writeContractAsync({
address: selectedChain.contracts.L2GNS.address as Address,
Expand Down Expand Up @@ -376,13 +424,15 @@ function DeploySubgraph({
chainSwitchPending ||
isPending ||
!form.formState.isValid ||
!ensureNewVersion() ||
!(ensureNewVersion() || updateMetadata()) ||
!isOwner();

const deployButtonCopy = (() => {
if (!address) return 'Need to connect wallet';
if (deployed) return 'Deployed';
if (chainSwitchPending) return 'Switching Chains...';
if (isPending) return 'Check Wallet...';
if (updateMetadata()) return 'Update Metadata';
return 'Deploy';
})();

Expand Down

0 comments on commit d0cb82a

Please sign in to comment.