@@ -3,67 +3,45 @@ pragma solidity ^0.8.0;
33
44import {Script} from "forge-std/Script.sol " ;
55import {console} from "forge-std/console.sol " ;
6- import {FeesManager} from "socket-protocol/contracts/evmx/fees/FeesManager .sol " ;
6+ import "socket-protocol/contracts/evmx/interfaces/IFeesManager .sol " ;
77
88import {CounterAppGateway} from "../../src/counter/CounterAppGateway.sol " ;
99
1010/**
11- * @title WithdrawFees Script
11+ * @title WithdrawCredits Script
1212 * @notice Withdraws accumulated fees from EVMX to Arbitrum Sepolia
1313 * @dev This script:
1414 * 1. Checks available fees on EVMX
15- * 2. Switches to Arbitrum Sepolia to estimate gas costs
16- * 3. Calculates a safe amount to withdraw (fees minus estimated gas costs)
17- * 4. Performs the withdrawal if the amount is positive
18- * 5. Verifies final balance on Arbitrum Sepolia
15+ * 2. Performs the withdrawal if the amount is positive
1916 *
2017 * This demonstrates how developers can retrieve fees that their application has earned
21- * through Socket Protocol's fee system.
22- *
23- * Required environment variables:
24- * - EVMX_RPC: RPC URL for the EVMx network
25- * - ARBITRUM_SEPOLIA_RPC: RPC URL for Arbitrum Sepolia
26- * - PRIVATE_KEY: Private key of the deployer account
27- * - FEES_MANAGER: Address of Socket Protocol's FeesManager contract
28- * - APP_GATEWAY: Address of the deployed CounterAppGateway
29- * @notice Ensure your app has withdrawFeeTokens() function implemented. You can check its implementation in CounterAppGateway.sol
18+ * through SOCKET Protocol's fee system.
3019 */
31- contract WithdrawFees is Script {
20+ contract WithdrawCredits is Script {
3221 function run () external {
3322 // EVMX Check available fees
3423 vm.createSelectFork (vm.envString ("EVMX_RPC " ));
35- FeesManager feesManager = FeesManager (payable (vm.envAddress ("FEES_MANAGER " )));
36- address appGatewayAddress = vm.envAddress ("APP_GATEWAY " );
37- address token = vm.envAddress ("USDC " );
38-
39- CounterAppGateway appGateway = CounterAppGateway (appGatewayAddress);
40- uint256 availableFees = feesManager.getAvailableCredits (appGatewayAddress);
41- console.log ("Available fees: " , availableFees);
42-
43- if (availableFees > 0 ) {
44- // Switch to Arbitrum Sepolia to get gas price
45- vm.createSelectFork (vm.envString ("ARBITRUM_SEPOLIA_RPC " ));
46- uint256 privateKey = vm.envUint ("PRIVATE_KEY " );
47- address sender = vm.addr (privateKey);
48-
49- // Gas price from Arbitrum
50- uint256 arbitrumGasPrice = block .basefee + 0.1 gwei ; // With buffer
51- uint256 gasLimit = 5_000_000 ; // Estimate
52- uint256 estimatedGasCost = gasLimit * arbitrumGasPrice;
24+ IFeesManager feesManager = IFeesManager (payable (vm.envAddress ("FEES_MANAGER " )));
25+ address token = vm.envAddress ("ARBITRUM_USDC " );
26+ uint256 privateKey = vm.envUint ("PRIVATE_KEY " );
27+ address sender = vm.addr (privateKey);
5328
54- console. log ( " Arbitrum gas price (wei): " , arbitrumGasPrice );
55- console.log ("Gas limit : " , gasLimit );
56- console. log ( " Estimated gas cost: " , estimatedGasCost) ;
29+ uint256 availableCredits = feesManager. getAvailableCredits (sender );
30+ console.log ("Available credits : " , availableCredits );
31+ //consfeesManager.tokenOntokenOnChainBalances[42161][token] ;
5732
58- // Calculate amount to withdraw
59- uint256 amountToWithdraw = availableFees > estimatedGasCost ? availableFees - estimatedGasCost : 0 ;
33+ if (availableCredits > 0 ) {
34+ uint256 maxFees = 10000000000000000 ; // Static 1 cent USDC credit (18 decimals)
35+ // TODO: Also wrap native amount to be able to max withdraw
36+ uint256 amountToWithdraw = 900000000000000000 ; // availableCredits - maxFees;
6037
6138 if (amountToWithdraw > 0 ) {
62- // Switch back to EVMX to perform withdrawal
63- vm.createSelectFork (vm.envString ("EVMX_RPC " ));
6439 vm.startBroadcast (privateKey);
40+ AppGatewayApprovals[] memory approvals = new AppGatewayApprovals [](1 );
41+ approvals[0 ] = AppGatewayApprovals ({appGateway: address (feesManager), approval: true });
42+ feesManager.approveAppGateways (approvals);
6543 console.log ("Withdrawing amount: " , amountToWithdraw);
66- appGateway .withdrawCredits (421614 , token, amountToWithdraw, sender);
44+ feesManager .withdrawCredits (42161 , token, amountToWithdraw, maxFees , sender);
6745 vm.stopBroadcast ();
6846 } else {
6947 console.log ("Available fees less than estimated gas cost " );
0 commit comments