Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
doitian committed Dec 25, 2023
1 parent 8284c35 commit 52fdfc0
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/actions/fetch-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { configFromEnv } from "@/lib/config";
import { fetchAssets as innerFetchAssets } from "@/lib/cobuild/assets-manager";

export default async function fetchAssets(address) {
const config = configFromEnv(process.env);
export default async function fetchAssets(address, config) {
config = config ?? configFromEnv(process.env);
return await innerFetchAssets(config, address);
}
4 changes: 2 additions & 2 deletions src/actions/transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { parseUnit } from "@ckb-lumos/bi";
import { transferCkb } from "@/lib/cobuild/publishers";
import { configFromEnv } from "@/lib/config";

export default async function transfer(_prevState, formData) {
const config = configFromEnv(process.env);
export default async function transfer(_prevState, formData, config) {
config = config ?? configFromEnv(process.env);

const from = formData.get("from");
const to = formData.get("to");
Expand Down
4 changes: 3 additions & 1 deletion src/app/accounts/[address]/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import Assets from "./assets";
import Loading from "./loading";
import AccountHeader from "./account-header";

export default function Account({ params: { address } }) {
export default function Account({ params: { address }, config }) {
config = config ?? configFromEnv(process.env);

return (
<>
<header>
Expand Down
13 changes: 11 additions & 2 deletions src/app/accounts/[address]/sign-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@

import { Button } from "flowbite-react";

export default function SignForm({ address, buildingPacket }) {
import BuildingPacketReview from "@/lib/cobuild/react/building-packet-review";
import { applyLockActions } from "@/lib/cobuild/lock-actions";

export default function SignForm({ address, buildingPacket, config }) {
const appliedBuildingPacket = applyLockActions(
address,
buildingPacket,
config,
);

return (
<>
<Button>Sign</Button>
<pre>{JSON.stringify(buildingPacket, null, 2)}</pre>
<BuildingPacketReview buildingPacket={appliedBuildingPacket} />
</>
);
}
12 changes: 8 additions & 4 deletions src/app/accounts/[address]/transfer/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import transfer from "@/actions/transfer";
import Loading from "../loading";
import SignForm from "../sign-form";

export function TransferFormStep1({ formAction, formState, address }) {
export function TransactionForm({ formAction, formState, address }) {
const [balance, setBalance] = useState();
useEffect(() => {
fetchAssets(address).then(({ ckbBalance }) => setBalance(ckbBalance));
Expand Down Expand Up @@ -59,13 +59,17 @@ export function TransferFormStep1({ formAction, formState, address }) {
);
}

export default function TransferForm({ address }) {
export default function TransferForm({ address, config }) {
const [formState, formAction] = useFormState(transfer, {});

return formState.buildingPacket === null ||
formState.buildingPacket === undefined ? (
<TransferFormStep1 {...{ formAction, formState, address }} />
<TransactionForm {...{ formAction, formState, address }} />
) : (
<SignForm address={address} buildingPacket={formState.buildingPacket} />
<SignForm
address={address}
buildingPacket={formState.buildingPacket}
config={config}
/>
);
}
6 changes: 4 additions & 2 deletions src/app/accounts/[address]/transfer/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { configFromEnv } from "@/lib/config";

import TransferForm from "./form";

export default function Transfer({ params: { address } }) {
export default function Transfer({ params: { address }, config }) {
config = config ?? configFromEnv(process.env);

return (
<section>
<h2>Transfer</h2>
<TransferForm address={address} />
<TransferForm address={address} config={config} />
</section>
);
}
4 changes: 1 addition & 3 deletions src/app/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import { Button } from "flowbite-react";
import { useRouter } from "next/navigation";

import * as joyid from "@/lib/wallet/joyid";
import { configFromEnv } from "@/lib/config";

export default function RootClientPage() {
const config = configFromEnv(process.env);
export default function RootClientPage({ config }) {
const router = useRouter();

const connect = async () => {
Expand Down
6 changes: 4 additions & 2 deletions src/app/page.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { configFromEnv } from "@/lib/config";
import RootHeader from "./header";

export default function RootPage() {
return <RootHeader />;
export default function RootPage({ config }) {
config = config ?? configFromEnv(process.env);
return <RootHeader config={config} />;
}
14 changes: 14 additions & 0 deletions src/lib/cobuild/lock-actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { addressToScript } from "@ckb-lumos/helpers";

export function applyLockActions(address, buildingPacket, config) {
const lock = addressToScript(address, { config: config.ckbChainConfig });
if (lock.codeHash === config.SCRIPTS.JOYID_COBUILD_POC.CODE_HASH) {
return applyJoyidCobuildPocLockActions(lock, buildingPacket, config);
}

return buildingPacket;
}

function applyJoyidCobuildPocLockActions(lock, buildingPacket, config) {
// Assume that the tx uses Cobuild WitnessLayout structure
}
4 changes: 4 additions & 0 deletions src/lib/cobuild/react/building-packet-review.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// TODO: present the buildingPacket
export default function BuildingPacketReview({ buildingPacket }) {
return <pre>{JSON.stringify(buildingPacket, null, 2)}</pre>;
}

0 comments on commit 52fdfc0

Please sign in to comment.