Skip to content

Commit

Permalink
Withdraw fund implemented and add multicall for deposit and update
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelAR committed Nov 8, 2024
1 parent ba5c614 commit f6e15ef
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 18 deletions.
7 changes: 5 additions & 2 deletions frontend/gostarkme-web/components/modules/Fund/Fund.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import FundDonate from "./FundDonate";
import starknetlogo from "@/public/icons/starklogo.png";
import { FundVote } from "./FundVote";

import { useEffect, useState } from "react";
import { FUND_MANAGER_ADDR, upVotesNeeded } from "@/constants";
import Divider from "@/components/ui/Divider";
Expand All @@ -13,6 +14,7 @@ import { useAtomValue } from "jotai";
import { Contract } from "starknet";
import { clickedFundState } from "@/state/nFunds";
import LoadingSpinner from "@/components/ui/LoadingSpinner";
import { FundWithdraw } from "./FundWithdraw";

const Fund = () => {

Expand Down Expand Up @@ -46,7 +48,8 @@ const Fund = () => {
currentBalance = BigInt(currentBalance) / BigInt(10 ** 18);

let goal = await fundContract.getGoal();

goal = BigInt(goal) / BigInt(10 ** 18);

let upVotes = await fundContract.getUpVotes();

let evidenceLink = await fundContract.get_evidence_link();
Expand Down Expand Up @@ -96,7 +99,7 @@ const Fund = () => {
{Number(fund.state) === 0 && <p>Fund is currently innactive.</p>}
{Number(fund.state) === 1 && <FundVote upVotes={fund.upVotes} upVotesNeeded={upVotesNeeded} addr={fund.addr} setLoading={setLoading} getDetails={getDetails} />}
{Number(fund.state) === 2 && <FundDonate currentBalance={fund.currentBalance} goal={fund.goal} addr={fund.addr} icon={starknetlogo} />}
{Number(fund.state) === 3 && <p>Fund is currently closed.</p>}
{Number(fund.state) === 3 && <FundWithdraw currentBalance={fund.currentBalance} goal={fund.goal} addr={fund.addr} setLoading={setLoading} getDetails={getDetails} />}
{Number(fund.state) === 4 && <p>Fund was already withdrawed.</p>}
</section>
}
Expand Down
34 changes: 22 additions & 12 deletions frontend/gostarkme-web/components/modules/Fund/FundDonate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { walletStarknetkitLatestAtom } from "@/state/connectedWallet";
import { useAtomValue, useSetAtom } from "jotai";
import Image, { StaticImageData } from "next/image";
import { useState } from "react";
import { Contract, InvokeFunctionResponse } from "starknet";
import { CallData, Contract, InvokeFunctionResponse, cairo } from "starknet";
import { useRouter } from "next/navigation";
import { latestTxAtom } from "@/state/latestTx";

Expand Down Expand Up @@ -42,17 +42,27 @@ const FundDonate = ({ currentBalance, goal, addr, icon }: FundDonateProps) => {
setError("The amount cannot be negative.");
} else {
setError("");
const erc20 = new Contract(strkAbi, addrSTRK, provider);
const transferCall = erc20.populate('transfer', {
recipient: addr,
amount: BigInt(amount * 10 ** 18)
});
wallet?.account?.execute(transferCall)
.then(async (resp: InvokeFunctionResponse) => {
setLatestTx({ txHash: resp.transaction_hash, type: "donation" });
router.push("/app/confirmation");
})
.catch((e: any) => { console.log("error increase balance =", e) });
await wallet?.account.execute([
{
contractAddress: addrSTRK,
entrypoint: 'transfer',
calldata: CallData.compile({
recipient: addr,
amount: cairo.uint256(amount * 10 ** 18 )
}),
},
{
contractAddress: addr,
entrypoint: 'update_receive_donation',
calldata: CallData.compile({
strks: cairo.uint256(amount * 10 ** 18 )
}),
},
]).then(async (resp: InvokeFunctionResponse) => {
setLatestTx({ txHash: resp.transaction_hash, type: "vote" });
router.push("/app/confirmation");
})
.catch((e: any) => { });
}
};

Expand Down
50 changes: 50 additions & 0 deletions frontend/gostarkme-web/components/modules/Fund/FundWithdraw.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { calculatePorcentage } from "@/app/utils";
import { Button } from "@/components/ui/Button";
import ProgressBar from "@/components/ui/ProgressBar";
import { fundAbi } from "@/contracts/abis/fund";
import { walletStarknetkitLatestAtom } from "@/state/connectedWallet";
import { latestTxAtom } from "@/state/latestTx";
import { useAtomValue, useSetAtom } from "jotai";
import { Contract, InvokeFunctionResponse } from "starknet";
import { useRouter } from "next/navigation";

interface FundWithdrawProps {
addr: string,
setLoading: (load: boolean) => void,
getDetails: () => void,
currentBalance: number;
goal: number;
}

export const FundWithdraw = ({ currentBalance, goal, addr, setLoading, getDetails }: FundWithdrawProps) => {

const wallet = useAtomValue(walletStarknetkitLatestAtom);

const progress = calculatePorcentage(currentBalance, goal);

const setLatestTx = useSetAtom(latestTxAtom);

const router = useRouter();

async function withdraw() {
setLoading(true);
const fundContract = new Contract(fundAbi, addr, wallet?.account);
fundContract.withdraw()
.then(async (resp: InvokeFunctionResponse) => {
setLatestTx({ txHash: resp.transaction_hash, type: "vote" });
router.push("/app/confirmation");
})
.catch((e: any) => { getDetails() });
}

return (
<div className="flex flex-col">
<ProgressBar progress={progress} />
<div className="flex justify-center my-2">
<p className="text-center mx-2">{currentBalance.toString()} / {goal.toString()} </p>
<p>&#127775;</p>
</div>
<Button label="Withdraw" onClick={withdraw}></Button>
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Button } from "@/components/ui/Button";
import { useState } from "react";
import FundingStep from "./FundingStep";
import DescriptionStep from "./DescriptionStep";
import { Contract, wallet, InvokeFunctionResponse, shortString } from "starknet";
import { Contract, wallet, InvokeFunctionResponse, shortString, cairo } from "starknet";
import { fundManager } from "@/contracts/abis/fundManager";
import { FUND_MANAGER_ADDR } from "@/constants";
import { useAtom, useAtomValue, useSetAtom } from "jotai";
Expand Down Expand Up @@ -66,7 +66,7 @@ const Stages = () => {

async function newFund() {
const fundManagerContract = new Contract(fundManager, FUND_MANAGER_ADDR, wallet?.account);
fundManagerContract.newFund(fundingName, goal, evidenceLink, contactHandle, fundingDescription)
fundManagerContract.newFund(fundingName, cairo.uint256(Number(goal) * Number(10) ** Number(18) ) , evidenceLink, contactHandle, fundingDescription)
.then(async (resp: InvokeFunctionResponse) => {
setLatesTx({ txHash: resp.transaction_hash, type: "newfund" });
setActualFund({id: 0, name: fundingName});
Expand Down
2 changes: 1 addition & 1 deletion frontend/gostarkme-web/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const ARGENT_WEBWALLET_URL =
process.env.NEXT_PUBLIC_ARGENT_WEBWALLET_URL || "https://web.argent.xyz"

export const FUND_MANAGER_ADDR =
"0x020f8eb5d312650408d83081f76ae6daef87151bcec5d6b2b14620fc2f868057"
"0x0166080bd78e151e02bd97ae394839c784ef6c66ddf5668782a4cff69d22ef6f"

export const navItems = [
{ label: 'My Profile', href: '/app/myprofile' },
Expand Down
2 changes: 1 addition & 1 deletion frontend/gostarkme-web/contracts/abis/fund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export const fundAbi = [
},
{
"type": "function",
"name": "receiveDonation",
"name": "update_receive_donation",
"inputs": [
{
"name": "strks",
Expand Down

0 comments on commit f6e15ef

Please sign in to comment.