Skip to content

Commit 00a809b

Browse files
committed
Fixed withdrawal functionality
1 parent 9d9e798 commit 00a809b

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

‎frontend/lib/forestOnchain.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Abi } from "viem";
33

44
// 👉 Replace this with your actual deployed address
55
export const FOREST_ONCHAIN_ADDRESS =
6-
"0x732b3f7eff51a10A515BC2e2666f9b334B1e78E9" as const;
6+
"0xB48e40b165A4cC7A54A955888E5232B6c4f05bdb" as const;
77

88
// 👉 Paste the ABI generated by Foundry here (from out/ForestOnchain.sol/ForestOnchain.json)
99
export const FOREST_ONCHAIN_ABI = [
@@ -222,6 +222,13 @@ export const FOREST_ONCHAIN_ABI = [
222222
"outputs": [],
223223
"stateMutability": "nonpayable"
224224
},
225+
{
226+
"type": "function",
227+
"name": "totalStaked",
228+
"inputs": [],
229+
"outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }],
230+
"stateMutability": "view"
231+
},
225232
{
226233
"type": "function",
227234
"name": "withdraw",

‎src/ForestOnchain.sol‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ contract ForestOnchain {
5757
uint constant MAX_SESSION_DURATION = 60 minutes;
5858
uint constant MIN_SESSION_DURATION = 20 minutes;
5959
uint constant MIN_NUM_TREES_PER_GOAL = 1;
60+
uint256 public totalStaked;
6061

6162
event ActivityAdded(string indexed activityType);
6263
event SessionStarted(
@@ -160,6 +161,7 @@ contract ForestOnchain {
160161
}
161162
uint stakeAmount = goal.stakeAmount;
162163
delete userGoals[msg.sender][_activityType];
164+
totalStaked -= stakeAmount;
163165
(bool success, ) = payable(msg.sender).call{value: stakeAmount}("");
164166
if (!success) {
165167
revert ForestOnchain__TransferFailed();
@@ -180,6 +182,10 @@ contract ForestOnchain {
180182
if (_amount > address(this).balance) {
181183
revert ForestOnchain__InsufficientFunds();
182184
}
185+
uint256 available = address(this).balance - totalStaked;
186+
if (_amount > available) {
187+
revert ForestOnchain__InsufficientFunds();
188+
}
183189
(bool success, ) = _to.call{value: _amount}("");
184190
if (!success) {
185191
revert ForestOnchain__TransferFailed();
@@ -340,6 +346,7 @@ contract ForestOnchain {
340346
stakeAmount
341347
);
342348
}
349+
totalStaked += stakeAmount;
343350
userGoals[msg.sender][_activityType].activityType = _activityType;
344351
userGoals[msg.sender][_activityType].startTime = block.timestamp;
345352
userGoals[msg.sender][_activityType].endTime =

0 commit comments

Comments
 (0)