@@ -963,6 +963,7 @@ <h3 class="font-bold text-orange-800 mb-2 text-sm">🎯 Target Price</h3>
963963 < script >
964964 const CONTRACT_ADDRESS = '0xb81fea65B45D743AB62a1A2B351f4f92fb1d4D16' ; // MangoSwap V4 - NEW CONTRACT
965965 const BASE_CHAIN_ID = '0x2105' ;
966+ const GAS_BUFFER_ETH = 0.001 ; // Reserves ETH for gas estimation
966967
967968 // PAYMASTER CONFIGURATION - Update this with your paymaster service URL
968969 const PAYMASTER_URL = 'https://api.developer.coinbase.com/rpc/v1/base/0XFTXVSOwf0H30PN84h07BmJgiJhNtRw' ;
@@ -1511,13 +1512,25 @@ <h3 class="font-bold text-orange-800 mb-2 text-sm">🎯 Target Price</h3>
15111512 document . getElementById ( 'tokenOut' ) . addEventListener ( 'change' , getQuote ) ;
15121513
15131514 document . getElementById ( 'maxBtn' ) . addEventListener ( 'click' , ( ) => {
1514- if ( ! isConnected ) {
1515- alert ( 'Please connect your wallet first' ) ;
1516- return ;
1517- }
1518- document . getElementById ( 'amountIn' ) . value = userBalanceFrom ;
1519- getQuote ( ) ;
1520- } ) ;
1515+ if ( ! isConnected ) {
1516+ alert ( 'Please connect your wallet first' ) ;
1517+ return ;
1518+ }
1519+
1520+ // For native ETH, leave a buffer for gas
1521+ const tokenIn = document . getElementById ( 'tokenIn' ) . value ;
1522+ const isNativeToken = tokenIn === '0x0000000000000000000000000000000000000000' ;
1523+ const gasBuffer = isNativeToken ? GAS_BUFFER_ETH : 0 ;
1524+ const maxAmount = Math . max ( 0 , userBalanceFrom - gasBuffer ) ;
1525+
1526+ if ( maxAmount <= 0 && isNativeToken ) {
1527+ alert ( `Please leave ${ GAS_BUFFER_ETH } ETH for gas fees.\n\nYour balance: ${ userBalanceFrom . toFixed ( 4 ) } ETH\nGas buffer needed: ${ GAS_BUFFER_ETH } ETH` ) ;
1528+ return ;
1529+ }
1530+
1531+ document . getElementById ( 'amountIn' ) . value = maxAmount . toFixed ( 6 ) ;
1532+ getQuote ( ) ;
1533+ } ) ;
15211534
15221535 document . getElementById ( 'swapArrow' ) . addEventListener ( 'click' , function ( ) {
15231536 const tokenInSelect = document . getElementById ( 'tokenIn' ) ;
@@ -2012,8 +2025,27 @@ <h3 class="font-bold text-orange-800 mb-2 text-sm">🎯 Target Price</h3>
20122025 if ( isInstant ) {
20132026 console . log ( 'Executing instant swap...' ) ;
20142027
2015- document . getElementById ( 'btnText' ) . innerHTML = '<span class="spinner"></span> Swapping...' ;
2016- document . getElementById ( 'scheduleSwapBtn' ) . disabled = true ;
2028+ if ( isInstant ) {
2029+ console . log ( 'Executing instant swap...' ) ;
2030+
2031+ // Check if user has enough balance including buffer for gas
2032+ const isNativeToken = tokenIn === '0x0000000000000000000000000000000000000000' ;
2033+ if ( isNativeToken ) {
2034+ const amountFloat = parseFloat ( amount ) ;
2035+ if ( amountFloat > ( userBalanceFrom - GAS_BUFFER_ETH ) ) {
2036+ const available = Math . max ( 0 , userBalanceFrom - GAS_BUFFER_ETH ) ;
2037+ alert ( `❌ Insufficient Balance\n\n` +
2038+ `Your balance: ${ userBalanceFrom . toFixed ( 4 ) } ETH\n` +
2039+ `Trying to swap: ${ amountFloat . toFixed ( 4 ) } ETH\n` +
2040+ `Gas buffer needed: ${ GAS_BUFFER_ETH } ETH\n\n` +
2041+ `💡 Maximum you can swap: ${ available . toFixed ( 4 ) } ETH\n\n` +
2042+ `Tip: Click the MAX button to fill in the correct amount!` ) ;
2043+ return ;
2044+ }
2045+ }
2046+
2047+ document . getElementById ( 'btnText' ) . innerHTML = '<span class="spinner"></span> Swapping...' ;
2048+ document . getElementById ( 'scheduleSwapBtn' ) . disabled = true ;
20172049
20182050 // Try batched swap first (GAS FREE with paymaster)
20192051 const batchedResult = await executeBatchedSwap ( tokenIn , tokenOut , amountWei , minOutWei ) ;
0 commit comments