|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import {Script} from "forge-std/Script.sol"; |
| 5 | +import {console} from "forge-std/console.sol"; |
| 6 | +import "socket-protocol/contracts/evmx/interfaces/IFeesManager.sol"; |
| 7 | + |
| 8 | +import {CounterAppGateway} from "../../src/counter/CounterAppGateway.sol"; |
| 9 | + |
| 10 | +/** |
| 11 | + * @title WithdrawCredits Script |
| 12 | + * @notice Withdraws accumulated fees from EVMX to Arbitrum Sepolia |
| 13 | + * @dev This script: |
| 14 | + * 1. Checks available fees on EVMX |
| 15 | + * 2. Performs the withdrawal if the amount is positive |
| 16 | + * |
| 17 | + * This demonstrates how developers can retrieve fees that their application has earned |
| 18 | + * through SOCKET Protocol's fee system. |
| 19 | + */ |
| 20 | +contract WithdrawCredits is Script { |
| 21 | + function run() external { |
| 22 | + // EVMX Check available fees |
| 23 | + vm.createSelectFork(vm.envString("EVMX_RPC")); |
| 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); |
| 28 | + |
| 29 | + uint256 availableCredits = feesManager.getAvailableCredits(sender); |
| 30 | + console.log("Available credits:", availableCredits); |
| 31 | + //consfeesManager.tokenOntokenOnChainBalances[42161][token]; |
| 32 | + |
| 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 = availableCredits - maxFees; |
| 37 | + |
| 38 | + if (amountToWithdraw > 0) { |
| 39 | + vm.startBroadcast(privateKey); |
| 40 | + AppGatewayApprovals[] memory approvals = new AppGatewayApprovals[](1); |
| 41 | + approvals[0] = AppGatewayApprovals({appGateway: address(feesManager), approval: true}); |
| 42 | + feesManager.approveAppGateways(approvals); |
| 43 | + console.log("Withdrawing amount:", amountToWithdraw); |
| 44 | + feesManager.withdrawCredits(42161, token, amountToWithdraw, maxFees, sender); |
| 45 | + vm.stopBroadcast(); |
| 46 | + } else { |
| 47 | + console.log("Available fees less than estimated gas cost"); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments