-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
278 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"use server"; | ||
|
||
import { claimDao } from "@/lib/cobuild/publishers"; | ||
import { useConfig } from "@/lib/config"; | ||
|
||
export default async function withdraw(from, cell, config) { | ||
config = config ?? useConfig(); | ||
|
||
try { | ||
const buildingPacket = await claimDao(config)({ from, cell }); | ||
return { | ||
buildingPacket, | ||
}; | ||
} catch (err) { | ||
return { | ||
error: err.toString(), | ||
}; | ||
} | ||
} |
137 changes: 137 additions & 0 deletions
137
src/app/accounts/[address]/claim/[txHash]/[index]/form.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
"use client"; | ||
|
||
import { useState } from "react"; | ||
import { useRouter } from "next/navigation"; | ||
import { Alert, Button } from "flowbite-react"; | ||
|
||
import claim from "@/actions/claim"; | ||
import useTipHeader from "@/hooks/use-tip-header"; | ||
import useHeader from "@/hooks/use-header"; | ||
import useHeaderByNumber from "@/hooks/use-header-by-number"; | ||
import useCell from "@/hooks/use-cell"; | ||
|
||
import Capacity from "@/components/capacity"; | ||
import { | ||
DaoCycleProgress, | ||
DaoCycleProgressHint, | ||
daoCycleProgressColor, | ||
} from "@/components/dao-cycle-progress"; | ||
import * as dao from "@/lib/dao"; | ||
import Loading from "./loading"; | ||
import SignForm from "../../../sign-form"; | ||
import SubmitBuildingPacket from "../../../submit-building-packet"; | ||
|
||
function CellDetailsDisplay({ cell, depositHeader, withdrawHeader }) { | ||
return ( | ||
<dl> | ||
<dt>Base</dt> | ||
<dd> | ||
<Capacity value={cell.cellOutput.capacity} /> | ||
</dd> | ||
<dt>Reward</dt> | ||
<dd> | ||
+<Capacity value={dao.reward(cell, depositHeader, withdrawHeader)} /> | ||
</dd> | ||
</dl> | ||
); | ||
} | ||
|
||
function CellDetails({ cell, pending, onConfirm }) { | ||
const tipHeader = useTipHeader(); | ||
const depositBlockNumber = dao.getDepositBlockNumberFromWithdrawCell(cell); | ||
const depositHeader = useHeaderByNumber(depositBlockNumber); | ||
const withdrawHeader = useHeader(cell.blockHash); | ||
|
||
if (!tipHeader || !depositHeader || !withdrawHeader) { | ||
return <Loading />; | ||
} | ||
|
||
const waitingDuration = dao.estimateWithdrawWaitingDurationUntil( | ||
tipHeader, | ||
depositHeader, | ||
withdrawHeader, | ||
); | ||
|
||
return ( | ||
<> | ||
<p> | ||
{waitingDuration ? ( | ||
`Waiting for ${waitingDuration.humanize()}` | ||
) : ( | ||
<Button | ||
color="green" | ||
isProcessing={pending} | ||
disabled={pending} | ||
onClick={() => onConfirm(cell)} | ||
> | ||
Claim Now | ||
</Button> | ||
)} | ||
</p> | ||
<CellDetailsDisplay | ||
{...{ cell, tipHeader, withdrawHeader, depositHeader }} | ||
/> | ||
</> | ||
); | ||
} | ||
|
||
function LoadCell({ outPoint, pending, onConfirm }) { | ||
const cell = useCell(outPoint); | ||
const childProps = { cell, pending, onConfirm }; | ||
return cell ? <CellDetails {...childProps} /> : <Loading />; | ||
} | ||
|
||
export default function ClaimForm({ address, outPoint, config }) { | ||
const router = useRouter(); | ||
const [formState, setFormState] = useState({}); | ||
const [pending, setPending] = useState(false); | ||
const [signedBuildingPacket, setSignedBuildingPacket] = useState(null); | ||
const back = () => router.back(); | ||
const onConfirm = async (cell) => { | ||
setPending(true); | ||
try { | ||
setFormState(await claim(address, cell)); | ||
} catch (err) { | ||
setFormState({ error: err.toString() }); | ||
} | ||
setPending(false); | ||
}; | ||
|
||
if ( | ||
formState.buildingPacket === null || | ||
formState.buildingPacket === undefined | ||
) { | ||
const childProps = { outPoint, pending, onConfirm }; | ||
return ( | ||
<> | ||
{formState.error ? ( | ||
<Alert className="mb-5" color="failure"> | ||
{formState.error} | ||
</Alert> | ||
) : null} | ||
<LoadCell {...childProps} /> | ||
</> | ||
); | ||
} else if ( | ||
signedBuildingPacket === null || | ||
signedBuildingPacket === undefined | ||
) { | ||
return ( | ||
<SignForm | ||
address={address} | ||
buildingPacket={formState.buildingPacket} | ||
ckbChainConfig={config.ckbChainConfig} | ||
onSubmit={setSignedBuildingPacket} | ||
onCancel={back} | ||
/> | ||
); | ||
} else { | ||
return ( | ||
<SubmitBuildingPacket | ||
buildingPacket={signedBuildingPacket} | ||
ckbChainConfig={config.ckbChainConfig} | ||
onClose={back} | ||
/> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Spinner } from "flowbite-react"; | ||
|
||
export default function Loading() { | ||
return <Spinner />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { useConfig } from "@/lib/config"; | ||
|
||
import ClaimForm from "./form"; | ||
|
||
export default function Claim({ params: { address, txHash, index }, config }) { | ||
config = config ?? useConfig(); | ||
|
||
const outPoint = { | ||
txHash: `0x${txHash}`, | ||
index: `0x${index.toString(16)}`, | ||
}; | ||
|
||
const childProps = { address, outPoint, config }; | ||
|
||
return ( | ||
<main> | ||
<h2>Claim</h2> | ||
<ClaimForm {...childProps} /> | ||
</main> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters