Skip to content

Safe support for minting a Hypercert #483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Build and Test Frontend
on:
pull_request:
branches: [ dev, main, release ]
push:
branches: [ dev, main, release ]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Setup pnpm
uses: pnpm/action-setup@v4
with:
version: '9.2.0'

- name: Install dependencies
run: pnpm install

- name: Run tests
run: pnpm test

- name: Build frontend
run: pnpm build

- name: Run lint
run: pnpm lint
35 changes: 35 additions & 0 deletions .github/workflows/create-release-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Create Release PR
on:
push:
branches: [ main ]

permissions:
contents: write
pull-requests: write

jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Semantic Release Versioning
uses: cycjimmy/semantic-release-action@v4
with:
extra_plugins: |
@semantic-release/commit-analyzer
@semantic-release/release-notes-generator
@semantic-release/github
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Create Release PR
env:
GH_TOKEN: ${{ secrets.GHA_CREATE_PR }}
run: |
gh pr create \
--base release \
--head main \
--title "chore: new release" \
--body "Automated PR from main to release" \
--label "automated pr"
2 changes: 2 additions & 0 deletions components/global/extra-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ interface ExtraContentProps {
receipt?: TransactionReceipt;
}

// TODO: not really reusable for safe. breaks when minting hypercert from safe.
// We should make this reusable for all strategies.
export function ExtraContent({
message = "Your hypercert has been minted successfully!",
hypercertId,
Expand Down
40 changes: 22 additions & 18 deletions components/hypercert/hypercert-details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,34 +68,38 @@ export default async function HypercertDetails({
<Creator hypercert={hypercert} />
<ReadMore text={hypercert?.metadata?.description} length={280} />
<ExternalUrl url={hypercert?.metadata?.external_url} />
{(hypercert?.metadata?.work_timeframe_from as string) && (
<>
<Separator />
<h2 className="uppercase text-sm text-slate-500 font-medium tracking-wider">
TIME OF WORK
</h2>
<TimeFrame
from={hypercert.metadata?.work_timeframe_from}
to={hypercert.metadata?.work_timeframe_to}
/>
</>
)}
</section>
</article>

{(hypercert?.metadata?.work_timeframe_from ||
hypercert?.metadata?.work_scope) && (
<>
<Separator />
<section className="space-y-4 lg:flex lg:space-y-0 lg:space-x-8">
{hypercert?.metadata?.work_timeframe_from && (
<div className="flex flex-col gap-y-2 w-full max-w-[500px]">
<h2 className="uppercase text-sm text-slate-500 font-medium tracking-wider">
TIME OF WORK
</h2>
<TimeFrame
from={hypercert.metadata?.work_timeframe_from}
to={hypercert.metadata?.work_timeframe_to}
/>
</div>
)}
{hypercert?.metadata?.work_scope && (
<WorkScope hypercert={hypercert} />
)}
</section>
</>
)}
<Separator />
<section className="space-y-4 lg:flex lg:space-y-0 lg:space-x-8">
{hypercert?.metadata?.contributors && (
<Contributors hypercert={hypercert} />
)}
<Fractions hypercert={hypercert} />
</section>
{hypercert?.metadata?.work_scope && (
<>
<Separator />
<WorkScope hypercert={hypercert} />
</>
)}
<Separator />
</Suspense>
</section>
Expand Down
167 changes: 167 additions & 0 deletions hypercerts/EOAMintHypercertStrategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import { track } from "@vercel/analytics";
import { waitForTransactionReceipt } from "viem/actions";

import { createExtraContent } from "@/components/global/extra-content";
import { generateHypercertIdFromReceipt } from "@/lib/generateHypercertIdFromReceipt";
import { revalidatePathServerAction } from "@/app/actions/revalidatePathServerAction";

import {
MintHypercertParams,
MintHypercertStrategy,
} from "./MintHypercertStrategy";
import { Address, Chain } from "viem";
import { HypercertClient } from "@hypercerts-org/sdk";
import { UseWalletClientReturnType } from "wagmi";
import { useStepProcessDialogContext } from "@/components/global/step-process-dialog";
import { useQueueMintBlueprint } from "@/blueprints/hooks/queueMintBlueprint";

export class EOAMintHypercertStrategy extends MintHypercertStrategy {
constructor(
protected address: Address,
protected chain: Chain,
protected client: HypercertClient,
protected dialogContext: ReturnType<typeof useStepProcessDialogContext>,
protected queueMintBlueprint: ReturnType<typeof useQueueMintBlueprint>,
protected walletClient: UseWalletClientReturnType,
) {
super(address, chain, client, dialogContext, walletClient);
}

// FIXME: this is a long ass method. Break it down into smaller ones.
async execute({
metaData,
units,
transferRestrictions,
allowlistRecords,
blueprintId,
}: MintHypercertParams) {
const { setDialogStep, setSteps, setOpen, setTitle, setExtraContent } =
this.dialogContext;
const { mutateAsync: queueMintBlueprint } = this.queueMintBlueprint;
const { data: walletClient } = this.walletClient;

if (!this.client) {
setOpen(false);
throw new Error("No client found");
}

const isBlueprint = !!blueprintId;
setOpen(true);
setSteps([
{ id: "preparing", description: "Preparing to mint hypercert..." },
{ id: "minting", description: "Minting hypercert on-chain..." },
...(isBlueprint
? [{ id: "blueprint", description: "Queueing blueprint mint..." }]
: []),
{ id: "confirming", description: "Waiting for on-chain confirmation" },
{ id: "route", description: "Creating your new hypercert's link..." },
{ id: "done", description: "Minting complete!" },
]);
setTitle("Minting hypercert");
await setDialogStep("preparing", "active");
console.log("preparing...");

let hash;
try {
await setDialogStep("minting", "active");
console.log("minting...");
hash = await this.client.mintHypercert({
metaData,
totalUnits: units,
transferRestriction: transferRestrictions,
allowList: allowlistRecords,
});
} catch (error: unknown) {
console.error("Error minting hypercert:", error);
throw new Error(
`Failed to mint hypercert: ${error instanceof Error ? error.message : "Unknown error"}`,
);
}

if (!hash) {
throw new Error("No transaction hash returned");
}

if (blueprintId) {
try {
await setDialogStep("blueprint", "active");
await queueMintBlueprint({
blueprintId,
txHash: hash,
});
} catch (error: unknown) {
console.error("Error queueing blueprint mint:", error);
throw new Error(
`Failed to queue blueprint mint: ${error instanceof Error ? error.message : "Unknown error"}`,
);
}
}
await setDialogStep("confirming", "active");
console.log("Mint submitted", {
hash,
});
track("Mint submitted", {
hash,
});
let receipt;

try {
receipt = await waitForTransactionReceipt(walletClient!, {
confirmations: 3,
hash,
});
console.log({ receipt });
} catch (error: unknown) {
console.error("Error waiting for transaction receipt:", error);
await setDialogStep(
"confirming",
"error",
error instanceof Error ? error.message : "Unknown error",
);
throw new Error(
`Failed to confirm transaction: ${error instanceof Error ? error.message : "Unknown error"}`,
);
}

if (receipt?.status === "reverted") {
throw new Error("Transaction reverted: Minting failed");
}

await setDialogStep("route", "active");

let hypercertId;
try {
hypercertId = generateHypercertIdFromReceipt(receipt, this.chain.id);
console.log("Mint completed", {
hypercertId: hypercertId || "not found",
});
track("Mint completed", {
hypercertId: hypercertId || "not found",
});
console.log({ hypercertId });
} catch (error) {
console.error("Error generating hypercert ID:", error);
await setDialogStep(
"route",
"error",
error instanceof Error ? error.message : "Unknown error",
);
}

const extraContent = createExtraContent({
receipt,
hypercertId,
chain: this.chain,
});
setExtraContent(extraContent);

await setDialogStep("done", "completed");

await revalidatePathServerAction([
"/collections",
"/collections/edit/[collectionId]",
`/profile/${this.address}`,
{ path: `/`, type: "layout" },
]);
}
}
30 changes: 30 additions & 0 deletions hypercerts/MintHypercertStrategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Address, Chain } from "viem";
import {
HypercertClient,
HypercertMetadata,
TransferRestrictions,
AllowlistEntry,
} from "@hypercerts-org/sdk";
import { UseWalletClientReturnType } from "wagmi";

import { useStepProcessDialogContext } from "@/components/global/step-process-dialog";

export interface MintHypercertParams {
metaData: HypercertMetadata;
units: bigint;
transferRestrictions: TransferRestrictions;
allowlistRecords?: AllowlistEntry[] | string;
blueprintId?: number;
}

export abstract class MintHypercertStrategy {
constructor(
protected address: Address,
protected chain: Chain,
protected client: HypercertClient,
protected dialogContext: ReturnType<typeof useStepProcessDialogContext>,
protected walletClient: UseWalletClientReturnType,
) {}

abstract execute(params: MintHypercertParams): Promise<void>;
}
Loading
Loading