Skip to content

Commit

Permalink
chore(ci): backmerge main → dev (#1631)
Browse files Browse the repository at this point in the history
Automatic merge from main to dev failed. This PR was created to help
resolve conflicts.
  • Loading branch information
mjlescano authored Jan 16, 2025
2 parents 4820f80 + 97f977f commit 4a18d63
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 55 deletions.
4 changes: 2 additions & 2 deletions packages/builder/src/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ export class ChainDefinition {
}

getPreset(ctx: ChainBuilderContext) {
return this.raw.preset ? template(this.raw.preset, ctx) : 'main';
return template(this.raw.preset || PackageReference.DEFAULT_PRESET, ctx);
}

getPackageRef(ctx: ChainBuilderContext) {
return new PackageReference(`${this.getName(ctx)}:${this.getVersion(ctx) || 'latest'}@${this.getPreset(ctx) || 'main'}`);
return PackageReference.from(this.getName(ctx), this.getVersion(ctx), this.getPreset(ctx));
}

getDeployers() {
Expand Down
45 changes: 23 additions & 22 deletions packages/website/src/features/Deploy/QueueFromGitOpsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,47 +131,41 @@ export default function QueueFromGitOps() {
partialDeployInfo
);

const hasDeployers = useMemo(() => {
return Boolean(cannonDefInfo?.def?.getDeployers()?.length);
}, [cannonDefInfo?.def]);

const cannonDefInfoError: string = gitInfo.gitUrl
? (cannonDefInfo?.error as any)?.toString()
: cannonfileUrlInput &&
'The format of your URL appears incorrect. Please double check and try again.';

const fullPackageRef = cannonDefInfo?.def?.getPackageRef(ctx) ?? null;
const pkgRef = useMemo(() => {
return cannonDefInfo?.def?.getPackageRef(ctx);
}, [cannonDefInfo?.def]);

const onChainPrevPkgQuery = useCannonFindUpgradeFromUrl(
prevPackageInputRef || fullPackageRef || undefined,
pkgRef,
currentSafe?.chainId,
cannonDefInfo?.def?.getDeployers()
hasDeployers ? cannonDefInfo?.def?.getDeployers() : undefined,
prevPackageInputRef || undefined
);

const prevDeployLocation = partialDeployIpfs
? `ipfs://${partialDeployIpfs}`
: onChainPrevPkgQuery.data || '';
const prevDeployLocation = onChainPrevPkgQuery.data || '';

const prevCannonDeployInfo = useCannonPackage(
prevDeployLocation,
currentSafe?.chainId
);

useEffect(() => {
if (previousPackageInput) {
if (PackageReference.isValid(previousPackageInput)) {
setPrevPackageInputRef(new PackageReference(previousPackageInput));
} else {
setPrevPackageInputRef(null);
}
}, [previousPackageInput]);

useEffect(() => {
if (!prevCannonDeployInfo.ipfsQuery.data?.deployInfo)
return setPreviousPackageInput('');

const name = prevCannonDeployInfo.ipfsQuery.data?.deployInfo?.def?.name;
const version =
prevCannonDeployInfo.ipfsQuery.data?.deployInfo?.def?.version || 'latest';
const preset = prevCannonDeployInfo.ipfsQuery.data?.deployInfo?.def?.preset;
setPreviousPackageInput(`${name}:${version}@${preset}`);
}, [prevCannonDeployInfo]);

// run the build and get the list of transactions we need to run
const { buildState, doBuild, resetState } = useCannonBuild(currentSafe);

Expand Down Expand Up @@ -199,6 +193,17 @@ export default function QueueFromGitOps() {
partialDeployInfo?.ipfsQuery.data?.deployInfo?.options,
]);

// After loading the deployment info, set a default value for the upgradeFrom field
useEffect(() => {
if (hasDeployers || !nextCannonDeployInfo?.def) {
return setPreviousPackageInput('');
}

const { name, preset } = nextCannonDeployInfo.def;
const { fullPackageRef } = PackageReference.from(name, 'latest', preset);
setPreviousPackageInput(fullPackageRef);
}, [nextCannonDeployInfo, hasDeployers]);

const writeToIpfsMutation = useCannonWriteDeployToIpfs();

useEffect(() => {
Expand Down Expand Up @@ -437,10 +442,6 @@ export default function QueueFromGitOps() {
prevCannonDeployInfo?.ipfsQuery.data?.deployInfo,
]);

const hasDeployers = useMemo(() => {
return Boolean(cannonDefInfo?.def?.getDeployers()?.length);
}, [cannonDefInfo?.def]);

const tomlRequiresPrevPackage = useMemo(
() =>
Boolean(
Expand Down
98 changes: 67 additions & 31 deletions packages/website/src/hooks/cannon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
findUpgradeFromPackage,
ChainBuilderContext,
RawChainDefinition,
CannonRegistry,
getIpfsUrl,
} from '@usecannon/builder';
import _ from 'lodash';
Expand Down Expand Up @@ -396,55 +397,89 @@ export function useCannonWriteDeployToIpfs() {
});
}

export function useCannonFindUpgradeFromUrl(packageRef?: PackageReference, chainId?: number, deployers?: Address[]) {
export function useCannonFindUpgradeFromUrl(
packageRef?: PackageReference,
chainId?: number,
deployers?: Address[],
upgradeFrom?: PackageReference // Optional, if not deployers given
) {
const registry = useCannonRegistry();
const publicClient = usePublicClient();

return useQuery({
enabled: !!packageRef && !!chainId && !!deployers?.length,
queryKey: ['cannon', 'find-upgrade-from', packageRef?.name, packageRef?.preset, packageRef?.version, chainId, deployers],
enabled: !!packageRef && !!chainId,
queryKey: [
'cannon',
'find-upgrade-from',
packageRef?.fullPackageRef,
chainId,
deployers?.join(','),
upgradeFrom?.fullPackageRef,
],
refetchOnWindowFocus: false,
queryFn: async () => {
return await findUpgradeFromPackage(
registry,
publicClient as Parameters<typeof findUpgradeFromPackage>[1],
packageRef!,
chainId!,
deployers || []
);
if (deployers?.length) {
const url = await findUpgradeFromPackage(
registry,
publicClient as Parameters<typeof findUpgradeFromPackage>[1],
packageRef!,
chainId!,
deployers!
);
return url;
} else if (upgradeFrom) {
const url = await _getCannonPackageRegistryUrl(registry, upgradeFrom.fullPackageRef, chainId!);
return url;
}

throw new Error(`Missing required parameters for ${packageRef?.fullPackageRef}`);
},
staleTime: 1000 * 60, // 1 minute
retry: false,
});
}

export function useCannonPackage(urlOrRef?: string | PackageReference, chainId?: number) {
async function _getCannonPackageRegistryUrl(registry: CannonRegistry, packageRefOrUrl: string, chainId: number) {
if (PackageReference.isValid(packageRefOrUrl)) {
const url = await registry.getUrl(packageRefOrUrl, chainId);
if (!url) throw new Error(`package not found: ${packageRefOrUrl} (${chainId})`);
return url;
} else if (getIpfsUrl(packageRefOrUrl)) {
return getIpfsUrl(packageRefOrUrl);
}

throw new Error(`package not found: ${packageRefOrUrl} (${chainId})`);
}

export function useCannonPackageRegistryUrl(urlOrRef?: string | PackageReference | null, chainId?: number) {
const normalizedUrlOrRef = useMemo(() => {
if (!urlOrRef) return undefined;
if (typeof urlOrRef !== 'string') return urlOrRef.fullPackageRef;
return urlOrRef;
if (typeof urlOrRef === 'string') return urlOrRef;
return urlOrRef.fullPackageRef!;
}, [urlOrRef]);

const connectedChainId = useChainId();
const registry = useCannonRegistry();
const settings = useStore((s) => s.settings);
const { addLog } = useLogs();

const packageChainId = chainId ?? connectedChainId;
const connectedChainId = useChainId();
const packageChainId = chainId || connectedChainId;

// Registry query with proper typing
const registryQuery = useQuery({
return useQuery({
queryKey: ['cannon', 'registry-url', normalizedUrlOrRef, packageChainId],
queryFn: async () => {
const ipfsUrl = getIpfsUrl(normalizedUrlOrRef);
if (ipfsUrl) return { url: ipfsUrl };
const url = await registry.getUrl(normalizedUrlOrRef!, packageChainId);
if (!url) throw new Error(`package not found: ${normalizedUrlOrRef} (${packageChainId})`);
return { url };
const url = await _getCannonPackageRegistryUrl(registry, normalizedUrlOrRef!, packageChainId);
return { chainId: packageChainId, url };
},
enabled: typeof normalizedUrlOrRef === 'string' && normalizedUrlOrRef.length > 3,
enabled: typeof normalizedUrlOrRef === 'string' && !!normalizedUrlOrRef && !!packageChainId,
refetchOnWindowFocus: false,
staleTime: 1000 * 60, // 1 minute
});
}

export function useCannonPackage(urlOrRef?: string | PackageReference, chainId?: number) {
const registry = useCannonRegistry();
const settings = useStore((s) => s.settings);
const { addLog } = useLogs();

// Registry query with proper typing
const registryQuery = useCannonPackageRegistryUrl(urlOrRef, chainId);

const ipfsQuery = useQuery<{
deployInfo: DeploymentInfo;
Expand Down Expand Up @@ -477,20 +512,21 @@ export function useCannonPackage(urlOrRef?: string | PackageReference, chainId?:
fullPackageRef,
};
},
refetchOnWindowFocus: false,
enabled: !!registryQuery.data?.url,
staleTime: 1000 * 60, // 1 minute
retry: false,
});

// Meta query with proper typing
const registryQueryMeta = useQuery<{ metaUrl: string } | null>({
queryKey: ['cannon', 'registry-meta', ipfsQuery.data?.fullPackageRef, packageChainId],
queryKey: ['cannon', 'registry-meta', ipfsQuery.data?.fullPackageRef, registryQuery.data?.chainId],
queryFn: async () => {
if (!ipfsQuery.data?.fullPackageRef) return null;
const metaUrl = await registry.getMetaUrl(ipfsQuery.data.fullPackageRef, packageChainId);
if (!ipfsQuery.data?.fullPackageRef || !registryQuery.data?.chainId) return null;
const metaUrl = await registry.getMetaUrl(ipfsQuery.data.fullPackageRef, registryQuery.data.chainId);
return metaUrl ? { metaUrl } : null;
},
enabled: !!ipfsQuery.data?.fullPackageRef,
enabled: !!ipfsQuery.data?.fullPackageRef && !!registryQuery.data?.chainId,
staleTime: 1000 * 60, // 1 minute
refetchOnWindowFocus: false,
});
Expand Down

0 comments on commit 4a18d63

Please sign in to comment.