From 205be6576889393c4f5f4f2f0eecdfba9c3ad62c Mon Sep 17 00:00:00 2001 From: Mujahid Khan <106528609+mujahidkay@users.noreply.github.com> Date: Wed, 11 Dec 2024 15:50:30 +0500 Subject: [PATCH] add withdraw offer --- ui-fusdc-lp/src/App.tsx | 64 ++++++++++++++++++++++-- ui-fusdc-lp/src/components/Dashboard.tsx | 4 +- ui-fusdc-lp/src/components/Form.tsx | 12 +++-- 3 files changed, 70 insertions(+), 10 deletions(-) diff --git a/ui-fusdc-lp/src/App.tsx b/ui-fusdc-lp/src/App.tsx index b398544..2fddb3a 100644 --- a/ui-fusdc-lp/src/App.tsx +++ b/ui-fusdc-lp/src/App.tsx @@ -5,6 +5,7 @@ import { import { multiplyBy, parseRatio, + ceilDivideBy, } from "@agoric/zoe/src/contractSupport/ratio.js"; import { AmountMath } from "@agoric/ertp"; import { create } from "zustand"; @@ -20,7 +21,7 @@ import "./index.css"; import "@agoric/react-components/dist/style.css"; import { Navbar } from "./components/Navbar"; import Dashboard from "./components/Dashboard"; -import { useEffect } from "react"; +import { useEffect, useState } from "react"; type Wallet = Awaited>; @@ -35,6 +36,7 @@ interface AppState { fusdcInstance?: unknown; brands?: Record; purses?: Array; + metrics?: Record; } const useAppStore = create(() => ({})); @@ -58,6 +60,17 @@ const setup = async () => { }); } ); + + watcher.watchLatest>( + [Kind.Data, "published.fastUsdc.poolMetrics"], + (metrics) => { + console.log("Got metrics", metrics); + useAppStore.setState({ + metrics: {...metrics}, + }); + } + ); + }; const parseUSDCAmount = (amountString, usdc) => { const USDC_DECIMALS = 6; @@ -77,16 +90,16 @@ const connectWallet = async () => { } }; -const makeOffer = () => { +const makeDepositOffer = () => { const { wallet, fusdcInstance, brands } = useAppStore.getState(); if (!fusdcInstance) throw Error("no contract instance"); if (!(brands && brands.USDC)) throw Error("brands not available"); const proposal = { give: { - USDC: parseUSDCAmount(10, brands.USDC), + USDC: parseUSDCAmount('10', brands.USDC), }, }; - console.log("about to make offer"); + console.log("about to make offer", wallet); wallet?.makeOffer( { source: "agoricContract", @@ -109,7 +122,48 @@ const makeOffer = () => { ); }; +const makeWithdrawOffer = () => { + const { wallet, fusdcInstance, brands, metrics } = useAppStore.getState(); + if (!fusdcInstance) throw Error("no contract instance"); + if (!(brands && brands.USDC && brands.FastLP)) throw Error("brands not available"); + if (!(metrics && metrics.shareWorth)) throw Error("metrics not available"); + + const usdcAmount = parseUSDCAmount('10', brands.USDC) + const fastLPAmount = ceilDivideBy(usdcAmount, metrics.shareWorth); + const proposal = { + give: { + PoolShare: fastLPAmount, + }, + want: { + USDC: usdcAmount, + }, + }; + console.log('fastLPAmount', fastLPAmount); + console.log("about to make withdraw offer"); + wallet?.makeOffer( + { + source: "agoricContract", + instance: fusdcInstance, + callPipe: [["makeWithdrawInvitation", []]], + }, + proposal, + undefined, + (update: { status: string; data?: unknown }) => { + if (update.status === "error") { + alert(`Offer error: ${update.data}`); + } + if (update.status === "accepted") { + alert("Offer accepted"); + } + if (update.status === "refunded") { + alert("Offer rejected"); + } + } + ); +}; + function App() { + const [metrics, setMetrics] = useState>({}); useEffect(() => { setup(); }, []); @@ -145,7 +199,7 @@ function App() { defaultChainName="agoric-local" > - + diff --git a/ui-fusdc-lp/src/components/Dashboard.tsx b/ui-fusdc-lp/src/components/Dashboard.tsx index a40133d..77faf53 100644 --- a/ui-fusdc-lp/src/components/Dashboard.tsx +++ b/ui-fusdc-lp/src/components/Dashboard.tsx @@ -9,10 +9,10 @@ const headerData = [ { title: "Fees Earned (24h)", value: "$890", description: "USDC" }, ]; -const Dashboard = ({ makeOffer }) => ( +const Dashboard = ({ makeDepositOffer, makeWithdrawOffer }) => (
-
+
); diff --git a/ui-fusdc-lp/src/components/Form.tsx b/ui-fusdc-lp/src/components/Form.tsx index 11afec5..a920ffd 100644 --- a/ui-fusdc-lp/src/components/Form.tsx +++ b/ui-fusdc-lp/src/components/Form.tsx @@ -1,4 +1,4 @@ -const Form = ({ makeOffer }) => ( +const Form = ({ makeDepositOffer, makeWithdrawOffer }) => (

Deposit USDC

@@ -16,7 +16,7 @@ const Form = ({ makeOffer }) => ( className="btn btn-error w-full mt-4 bg-[#cd4246] text-white h-8 rounded" onClick={() => { console.log("clicked deposit"); - makeOffer(); + makeDepositOffer(); }} > Deposit @@ -38,7 +38,13 @@ const Form = ({ makeOffer }) => (

Max withdrawable: 117,778 USDC

-