Skip to content

Commit

Permalink
refactoring recieve steps
Browse files Browse the repository at this point in the history
  • Loading branch information
diyahir committed May 8, 2024
1 parent 67e8ef7 commit 1cb695c
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 131 deletions.
6 changes: 3 additions & 3 deletions packages/nextjs/components/HistoryTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const HistoryTable = () => {
const { transactions, addTransaction, toastSuccess, toastError } = useLightningApp();
const [expandedRow, setExpandedRow] = useState<number | null>(null); // State to manage expanded row index
const { data: walletClient } = useWalletClient();
const { data: yourContract } = useScaffoldContract({
const { data: htlcContract } = useScaffoldContract({
contractName: "HashedTimelock",
walletClient,
});
Expand Down Expand Up @@ -46,8 +46,8 @@ export const HistoryTable = () => {
if (transaction.hashLockTimestamp > Date.now() / 1000) {
return;
}
if (!yourContract) return;
yourContract.write
if (!htlcContract) return;
htlcContract.write
.refund([transaction.contractId as `0x${string}`], {})
.then(tx => {
console.log(tx);
Expand Down
131 changes: 6 additions & 125 deletions packages/nextjs/components/RecieveModalPopup.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"use client";

import { useEffect, useRef, useState } from "react";
import { AddressInput, IntegerInput } from "./scaffold-eth";
import { Step1 } from "./recieve-steps/Step1";
import { Step2 } from "./recieve-steps/Step2";
import { Step3 } from "./recieve-steps/Step3";
import {
InitiationRequest,
KIND,
Expand All @@ -11,10 +13,8 @@ import {
} from "@lightning-evm-bridge/shared";
import { waitForTransaction } from "@wagmi/core";
import axios from "axios";
import { PaymentRequestObject, decode } from "bolt11";
import { randomBytes } from "crypto";
import { sha256 } from "js-sha256";
import QRCode from "qrcode.react";
import { useWalletClient } from "wagmi";
import { useLightningApp } from "~~/hooks/LightningProvider";
import { useScaffoldContract } from "~~/hooks/scaffold-eth";
Expand Down Expand Up @@ -161,7 +161,6 @@ function RecieveModal({ isOpen, onClose }: RecieveModalProps) {
function onClickQRCode() {
navigator.clipboard.writeText(invoice);
toastSuccess("Lightning Invoice Copied");
// setActiveStep(activeStep + 1);
}

function onClickContinue() {
Expand Down Expand Up @@ -279,7 +278,7 @@ function RecieveModal({ isOpen, onClose }: RecieveModalProps) {
</ol>

{activeStep === 1 &&
step1({
Step1({
amount,
invoice,
recipientAddress,
Expand All @@ -289,9 +288,9 @@ function RecieveModal({ isOpen, onClose }: RecieveModalProps) {
onClickQRCode,
})}

{activeStep === 2 && step2({ invoice, onClickQRCode })}
{activeStep === 2 && Step2({ invoice, onClickQRCode })}

{activeStep === 3 && step3({ txHash })}
{activeStep === 3 && Step3({ txHash })}
</div>
</div>
</div>
Expand All @@ -300,122 +299,4 @@ function RecieveModal({ isOpen, onClose }: RecieveModalProps) {
);
}

export function step1({
amount,
invoice,
recipientAddress,
setRecipientAddress,
setAmount,
onClickContinue,
onClickQRCode,
}: {
amount: bigint;
invoice: string;
recipientAddress: string;
setRecipientAddress: (val: string) => void;
setAmount: (val: bigint) => void;
onClickContinue: () => void;
onClickQRCode: () => void;
}) {
let paymentRequest: PaymentRequestObject = {
satoshis: Number(0),
tags: [{ tagName: "payment_hash", data: "abc123" }],
};
if (invoice !== "") {
paymentRequest = decode(invoice);
}

function isGenerateQRDisabled(): boolean {
return amount === BigInt(0) || invoice !== "";
}
return (
<div className="flex flex-col text-start w-full gap-2">
<div className="flex-col">
<span className="text-sm text-gray-500">Recipient Address</span>
<AddressInput
placeholder="0x123...321"
value={recipientAddress}
onChange={newAddress => {
setRecipientAddress(newAddress);
}}
disabled={invoice !== ""}
/>
</div>
<div className="flex-col">
<span className="text-sm text-gray-500">Amount (sats)</span>
<IntegerInput
value={amount}
onChange={val => setAmount(BigInt(val))}
disableMultiplyBy1e18
disabled={invoice !== ""}
/>
</div>
<button
className="btn btn-secondary rounded-none w-full"
disabled={isGenerateQRDisabled()}
onClick={() => onClickContinue()}
>
Generate Service Fee Invoice
</button>
{invoice && (
<div className="flex flex-col w-full gap-2 h-full">
<button
className="btn btn-neutral rounded-none text-center w-full"
onClick={() => {
onClickQRCode();
}}
>
Copy Invoice
</button>
<div className="flex flex-col items-center">
<QRCode size={250} value={invoice} className="" />
</div>
<div className="flex flex-col">
<span className="text-center text-gray-500">Service Fee: {paymentRequest.satoshis} sats</span>
</div>
</div>
)}
</div>
);
}

function step2({ invoice, onClickQRCode }: { invoice: string; onClickQRCode: () => void }) {
let paymentRequest: PaymentRequestObject = {
satoshis: Number(0),
tags: [{ tagName: "payment_hash", data: "abc123" }],
};
if (invoice !== "") {
paymentRequest = decode(invoice);
}
return (
<div className="flex flex-col text-start cursor-pointer w-full" onClick={() => onClickQRCode()}>
&nbsp;
<div className="flex flex-col self-center gap-2">
<QRCode size={250} value={invoice} />
<button className="btn btn-neutral rounded-none w-full text-center" onClick={() => onClickQRCode()}>
Copy Invoice
</button>
<div className="flex flex-col">
<span className="text-center text-gray-500">Invoice: {paymentRequest.satoshis} sats</span>
</div>
</div>
</div>
);
}

function step3({ txHash }: { txHash: string }) {
return (
<div className="flex flex-col text-start w-full gap-2">
<a
href={`https://3xpl.com/botanix/transaction/${txHash}`}
target="_blank"
rel="noreferrer"
className="btn btn-secondary rounded-none w-full"
>
View Transaction
</a>
</div>
);
}

export default RecieveModal;
7 changes: 4 additions & 3 deletions packages/nextjs/components/SendModalPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type SendModalProps = {
isOpen: boolean;
onClose: () => void;
};

function SendModal({ isOpen, onClose }: SendModalProps) {
const { addTransaction, transactions, toastError } = useLightningApp();
const [invoice, setInvoice] = useState<string>("");
Expand Down Expand Up @@ -47,7 +48,7 @@ function SendModal({ isOpen, onClose }: SendModalProps) {
}, [transactions]);

const { data: walletClient } = useWalletClient();
const { data: yourContract } = useScaffoldContract({
const { data: htlcContract } = useScaffoldContract({
contractName: "HashedTimelock",
walletClient,
});
Expand Down Expand Up @@ -78,9 +79,9 @@ function SendModal({ isOpen, onClose }: SendModalProps) {

function submitPayment() {
console.log("submitting payment");
if (!yourContract) return;
if (!htlcContract) return;
if (!lnInvoiceRef.current) return;
yourContract.write
htlcContract.write
.newContract(
[
process.env.LSP_ADDRESS ?? "0xf89335a26933d8Dd6193fD91cAB4e1466e5198Bf",
Expand Down
82 changes: 82 additions & 0 deletions packages/nextjs/components/recieve-steps/Step1.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { AddressInput, IntegerInput } from "../scaffold-eth";
import { PaymentRequestObject, decode } from "bolt11";
import QRCode from "qrcode.react";

export function Step1({
amount,
invoice,
recipientAddress,
setRecipientAddress,
setAmount,
onClickContinue,
onClickQRCode,
}: {
amount: bigint;
invoice: string;
recipientAddress: string;
setRecipientAddress: (val: string) => void;
setAmount: (val: bigint) => void;
onClickContinue: () => void;
onClickQRCode: () => void;
}) {
let paymentRequest: PaymentRequestObject = {
satoshis: Number(0),
tags: [{ tagName: "payment_hash", data: "abc123" }],
};
if (invoice !== "") {
paymentRequest = decode(invoice);
}

function isGenerateQRDisabled(): boolean {
return amount === BigInt(0) || invoice !== "";
}
return (
<div className="flex flex-col text-start w-full gap-2">
<div className="flex-col">
<span className="text-sm text-gray-500">Recipient Address</span>
<AddressInput
placeholder="0x123...321"
value={recipientAddress}
onChange={newAddress => {
setRecipientAddress(newAddress);
}}
disabled={invoice !== ""}
/>
</div>
<div className="flex-col">
<span className="text-sm text-gray-500">Amount (sats)</span>
<IntegerInput
value={amount}
onChange={val => setAmount(BigInt(val))}
disableMultiplyBy1e18
disabled={invoice !== ""}
/>
</div>
<button
className="btn btn-secondary rounded-none w-full"
disabled={isGenerateQRDisabled()}
onClick={() => onClickContinue()}
>
Generate Service Fee Invoice
</button>
{invoice && (
<div className="flex flex-col w-full gap-2 h-full">
<button
className="btn btn-neutral rounded-none text-center w-full"
onClick={() => {
onClickQRCode();
}}
>
Copy Invoice
</button>
<div className="flex flex-col items-center">
<QRCode size={250} value={invoice} className="" />
</div>
<div className="flex flex-col">
<span className="text-center text-gray-500">Service Fee: {paymentRequest.satoshis} sats</span>
</div>
</div>
)}
</div>
);
}
26 changes: 26 additions & 0 deletions packages/nextjs/components/recieve-steps/Step2.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { PaymentRequestObject, decode } from "bolt11";
import QRCode from "qrcode.react";

export function Step2({ invoice, onClickQRCode }: { invoice: string; onClickQRCode: () => void }) {
let paymentRequest: PaymentRequestObject = {
satoshis: Number(0),
tags: [{ tagName: "payment_hash", data: "abc123" }],
};
if (invoice !== "") {
paymentRequest = decode(invoice);
}
return (
<div className="flex flex-col text-start cursor-pointer w-full" onClick={() => onClickQRCode()}>
&nbsp;
<div className="flex flex-col self-center gap-2">
<QRCode size={250} value={invoice} />
<button className="btn btn-neutral rounded-none w-full text-center" onClick={() => onClickQRCode()}>
Copy Invoice
</button>
<div className="flex flex-col">
<span className="text-center text-gray-500">Invoice: {paymentRequest.satoshis} sats</span>
</div>
</div>
</div>
);
}
14 changes: 14 additions & 0 deletions packages/nextjs/components/recieve-steps/Step3.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function Step3({ txHash }: { txHash: string }) {
return (
<div className="flex flex-col text-start w-full gap-2">
<a
href={`https://3xpl.com/botanix/transaction/${txHash}`}
target="_blank"
rel="noreferrer"
className="btn btn-secondary rounded-none w-full"
>
View Transaction
</a>
</div>
);
}

0 comments on commit 1cb695c

Please sign in to comment.