Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
coddeys committed Jun 21, 2024
1 parent 99287f9 commit 0c63011
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 71 deletions.
169 changes: 104 additions & 65 deletions islands/InvestorMint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,76 +30,78 @@ function useLucid(blockfrost: string) {
}

export default function PlatformMint(props: AppProps) {
const [amount, setAmount] = useState<number>(100);
const [bids, setBids] = useState<Bid[]>([]);
const { lucid, setupLucid } = useLucid(props.blockfrost);
const [waitingTx, setWaitingTx] = useState<boolean>(false);
const [policyId, setPolicyId] = useState<string | undefined>(undefined);
const [txHash, setTxHash] = useState<string | undefined>(undefined);
const tokenName = "EstateClub";

const updateAmount = (e) => setAmount(e.currentTarget.value);

const allBids = async () => {
const jsonResponse = await fetch("/api/bids");
const response = await jsonResponse.json();
const bids = response.map((x) => x.value);
setBids(bids);
};

const mintPlatformNFT = async () => {
const invest = async (bid, amount) => {
try {
setWaitingTx(true);

const lovelace = Number(5) * 1000000;
const mintRedeemer = Data.to(new Constr(0, []));

const utxos = await lucid?.wallet.getUtxos()!;
const utxo = utxos[0];
const outputReference = {
txHash: utxo.txHash,
outputIndex: utxo.outputIndex,
};

const contract = applyParams(
tokenName,
outputReference,
props.validators,
lucid!,
);

const assetName = `${contract!.policyId}${fromText(tokenName)}`;

const wAddr = await lucid.wallet.address();

const tx = await lucid!
.newTx()
.collectFrom([utxo])
.attachMintingPolicy(contract!.platformNFT)
.mintAssets(
{ [assetName]: BigInt(1) },
mintRedeemer,
)
.payToAddress(
wAddr,
{ lovelace: BigInt(lovelace) },
)
.complete();

const signedx = await tx.sign().complete();
const txHash = await signedx.submit();
const success = await lucid!.awaitTx(txHash);

if (success) {
setWaitingTx(false);
setPolicyId(contract!.policyId);
setTxHash(txHash);
}
console.log("Hello");
console.log(bid);
// const lovelace = Number(5) * 1000000;
// const mintRedeemer = Data.to(new Constr(0, []));

// const utxos = await lucid?.wallet.getUtxos()!;
// const utxo = utxos[0];
// const outputReference = {
// txHash: utxo.txHash,
// outputIndex: utxo.outputIndex,
// };

// const contract = applyParams(
// tokenName,
// outputReference,
// props.validators,
// lucid!,
// );

// const assetName = `${contract!.policyId}${fromText(tokenName)}`;

// const wAddr = await lucid.wallet.address();

// const tx = await lucid!
// .newTx()
// .collectFrom([utxo])
// .attachMintingPolicy(contract!.platformNFT)
// .mintAssets(
// { [assetName]: BigInt(1) },
// mintRedeemer,
// )
// .payToAddress(
// wAddr,
// { lovelace: BigInt(lovelace) },
// )
// .complete();

// const signedx = await tx.sign().complete();
// const txHash = await signedx.submit();
// const success = await lucid!.awaitTx(txHash);

// if (success) {
// setWaitingTx(false);
// setPolicyId(contract!.policyId);
// setTxHash(txHash);
// }
} catch (error) {
setWaitingTx(false);
console.error("error", error);
}
};

useEffect(() => {
fetch("/api/bids").then((res) => {
return res.json();
}).then((data) => {
setBids(data.map((x) => x.value));
});

if (lucid) {
window.cardano
.nami
Expand All @@ -114,8 +116,7 @@ export default function PlatformMint(props: AppProps) {
if (lucid) {
btn = <PlatformMintButton onClick={mintPlatformNFT} />;
} else {
btn = <PlatformMintButton onClick={allBids} />;
// btn = <SetupLucidButton onClick={setupLucid} isLoading={waitingTx} />;
btn = <SetupLucidButton onClick={setupLucid} isLoading={waitingTx} />;
}

let txInfo;
Expand All @@ -132,26 +133,64 @@ export default function PlatformMint(props: AppProps) {
<div>
{btn}
{loading}
<BidsView bids={bids} />
<BidsView
bids={bids}
onInput={updateAmount}
value={amount}
invest={invest}
/>
{txInfo}
</div>
);
}

function BidsView(props) {
const listBids = props.bids.map((bid) => (
<div className="card w-full bg-base-100 shadow-xl">
<div className="card-body">
<h2 className="card-title">
{bid.price}₳ | {bid.size / 100}
</h2>
<p>{bid.address}</p>
<a
href={`https://preview.cexplorer.io/address/${bid.contractAddress}`}
class="link"
>
{bid.contractAddress}
</a>
<div className="card-actions justify-end">
<input
type="number"
onInput={props.onInpur}
value={props.value}
placeholder="Price in ADA"
className="input input-bordered w-full max-w-xs"
/>
<button
className="btn btn-primary"
onClick={props.invest}
>
Invest Now
</button>
</div>
</div>
</div>
));
return (
<>
<p>{props.bids[0]?.address}</p>
</>
)
}
function Loading(props) {
return (
<div class="my-8">
<span className="loading loading-lg text-primary loading-spinner"></span>
<div class="flex flex-col my-8 gap-8">
{listBids}
</div>
);
}

// <div class="mockup-code pl-4">
// <pre class="whitespace-pre-wrap break-words">{bid.contract.script}</pre>
// </div>
function Loading(props) {
<div>
<span className="loading loading-lg text-primary loading-spinner"></span>
</div>;
}
function TxInfo(props) {
return (
<div className="">
Expand Down
3 changes: 1 addition & 2 deletions routes/api/bids/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ export const handler: Handlers<Bid | null> = {
return new Response(JSON.stringify(bid));
},


async GET(_req, ctx) {
const kv = await Deno.openKv(
"https://api.deno.com/databases/824bd08c-5681-4772-947f-b1ef806f3747/connect",
);
// const kv = await Deno.openKv();
const iter = (await kv.list({ prefix: ["bid"]} ));
const iter = await kv.list({ prefix: ["bid"] });
const bids = [];
for await (const res of iter) bids.push(res);
return new Response(JSON.stringify(bids));
Expand Down
8 changes: 4 additions & 4 deletions routes/investor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ export default function Home({ data }: PageProps<Data>) {
<Header blockfrost={blockfrost} />
<div class="h-[calc(100vh-68px)] bg-base-200">
<div class="mx-auto max-w-screen-xl px-4 lg:flex lg:items-center">
<div class="prose prose-sm md:prose-base w-full max-w-4xl flex-grow pt-10">
<div class="w-full max-w-4xl flex-grow pt-10">
<div class="">
<h1 class="">List of Properties</h1>
<h3 class="text-xl">
Choose the real estate obejct where do you want to invest
<h1 class="text-5xl font-bold mb-4">List of Properties</h1>
<h3 class="text-xl my-4">
Choose the real estate obejct where do you want to invest
</h3>
<InvestorMint
blockfrost={blockfrost}
Expand Down

0 comments on commit 0c63011

Please sign in to comment.