File tree Expand file tree Collapse file tree 8 files changed +79
-2
lines changed
controllers/swapAndBridge Expand file tree Collapse file tree 8 files changed +79
-2
lines changed Original file line number Diff line number Diff line change @@ -1206,7 +1206,7 @@ export class SwapAndBridgeController extends EventEmitter implements ISwapAndBri
1206
1206
)
1207
1207
if ( ! native ) return 0n
1208
1208
1209
- if ( this . fromSelectedToken ?. address !== ZeroAddress ) return native . amount
1209
+ if ( this . fromSelectedToken ?. address !== ZeroAddress || amount === 0n ) return native . amount
1210
1210
1211
1211
// subtract the from amount from the portfolio available balance
1212
1212
if ( amount > native . amount ) return 0n
@@ -2465,6 +2465,33 @@ export class SwapAndBridgeController extends EventEmitter implements ISwapAndBri
2465
2465
} )
2466
2466
}
2467
2467
2468
+ // if the account cannot use other fee payment options than native
2469
+ // and the account doesn't have any native,
2470
+ // show an error
2471
+ const accountNativeBalance = this . #accountNativeBalance( 0n )
2472
+ if ( this . #selectedAccount. account && this . fromChainId && accountNativeBalance === 0n ) {
2473
+ const network = this . #networks. networks . find (
2474
+ ( net ) => net . chainId === BigInt ( this . fromChainId ! )
2475
+ )
2476
+ if ( network ) {
2477
+ const accountState =
2478
+ this . #accounts. accountStates [ this . #selectedAccount. account . addr ] [ this . fromChainId ]
2479
+ if ( accountState ) {
2480
+ const baseAcc = getBaseAccount (
2481
+ this . #selectedAccount. account ,
2482
+ accountState ,
2483
+ this . #keystore. getAccountKeys ( this . #selectedAccount. account ) ,
2484
+ network
2485
+ )
2486
+ if ( ! baseAcc . canPayWithTokens ( ) && ! baseAcc . canPayWithEOA ( ) ) {
2487
+ errors . push ( {
2488
+ title : `Insufficient ${ network . nativeAssetSymbol } to cover the gas fees`
2489
+ } )
2490
+ }
2491
+ }
2492
+ }
2493
+ }
2494
+
2468
2495
return errors
2469
2496
}
2470
2497
Original file line number Diff line number Diff line change @@ -54,6 +54,7 @@ export interface AccountOnchainState {
54
54
isSmarterEoa : boolean
55
55
delegatedContract : Hex | null
56
56
delegatedContractName : 'AMBIRE' | 'METAMASK' | 'UNKNOWN' | null
57
+ hasEoaWithNative : boolean
57
58
}
58
59
59
60
export type AccountStates = {
Original file line number Diff line number Diff line change @@ -86,6 +86,16 @@ export abstract class BaseAccount {
86
86
*/
87
87
abstract getNonceId ( ) : string
88
88
89
+ /**
90
+ * Can the account pay with tokens the transaction fee
91
+ */
92
+ abstract canPayWithTokens ( ) : boolean
93
+
94
+ /**
95
+ * Can the account pay with tokens the transaction fee
96
+ */
97
+ abstract canPayWithEOA ( ) : boolean
98
+
89
99
// this is specific for v2 accounts, hardcoding a false for all else
90
100
shouldIncludeActivatorCall ( broadcastOption : string ) {
91
101
return false
Original file line number Diff line number Diff line change @@ -117,4 +117,12 @@ export class EOA extends BaseAccount {
117
117
// EOAs have only an execution layer nonce
118
118
return this . accountState . eoaNonce ! . toString ( )
119
119
}
120
+
121
+ canPayWithTokens ( ) : boolean {
122
+ return false
123
+ }
124
+
125
+ canPayWithEOA ( ) : boolean {
126
+ return false
127
+ }
120
128
}
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import { Interface } from 'ethers'
3
3
import AmbireAccount from '../../../contracts/compiled/AmbireAccount.json'
4
4
import AmbireAccount7702 from '../../../contracts/compiled/AmbireAccount7702.json'
5
5
import { Hex } from '../../interfaces/hex'
6
+ import { has7702 } from '../7702/7702'
6
7
import { AccountOp , getSignableCalls } from '../accountOp/accountOp'
7
8
import { BROADCAST_OPTIONS } from '../broadcast/broadcast'
8
9
import {
@@ -197,4 +198,12 @@ export class EOA7702 extends BaseAccount {
197
198
// 7702 accounts have an execution layer nonce and an entry point nonce
198
199
return `${ this . accountState . eoaNonce ! . toString ( ) } -${ this . accountState . erc4337Nonce . toString ( ) } `
199
200
}
201
+
202
+ canPayWithTokens ( ) : boolean {
203
+ return has7702 ( this . network ) && this . network . erc4337 . hasPaymaster
204
+ }
205
+
206
+ canPayWithEOA ( ) : boolean {
207
+ return false
208
+ }
200
209
}
Original file line number Diff line number Diff line change @@ -98,4 +98,12 @@ export class V1 extends BaseAccount {
98
98
// v1 accounts can only have an ambire smart contract nonce
99
99
return this . accountState . nonce . toString ( )
100
100
}
101
+
102
+ canPayWithTokens ( ) : boolean {
103
+ return this . network . hasRelayer
104
+ }
105
+
106
+ canPayWithEOA ( ) : boolean {
107
+ return this . accountState . hasEoaWithNative
108
+ }
101
109
}
Original file line number Diff line number Diff line change @@ -173,4 +173,16 @@ export class V2 extends BaseAccount {
173
173
// v2 accounts have two nonces: ambire smart account & entry point nonce
174
174
return `${ this . accountState . nonce . toString ( ) } -${ this . accountState . erc4337Nonce . toString ( ) } `
175
175
}
176
+
177
+ canPayWithTokens ( ) : boolean {
178
+ if ( this . network . erc4337 . enabled ) {
179
+ return this . network . erc4337 . hasPaymaster
180
+ }
181
+
182
+ return this . network . hasRelayer
183
+ }
184
+
185
+ canPayWithEOA ( ) : boolean {
186
+ return this . accountState . hasEoaWithNative
187
+ }
176
188
}
Original file line number Diff line number Diff line change @@ -73,6 +73,7 @@ export async function getAccountState(
73
73
getEOAsCode ( eoas )
74
74
] )
75
75
76
+ const hasEoaWithNative = ! ! accountStateResult . find ( ( res : any ) => res . isEOA && res . balance > 0 )
76
77
const result : AccountOnchainState [ ] = accountStateResult . map ( ( accResult : any , index : number ) => {
77
78
const associatedKeys = accResult . associatedKeyPrivileges . map (
78
79
( privilege : string , keyIndex : number ) => {
@@ -130,7 +131,8 @@ export async function getAccountState(
130
131
account . associatedKeys . length > 0 && accResult . associatedKeyPrivileges . length === 0 ,
131
132
isSmarterEoa,
132
133
delegatedContract,
133
- delegatedContractName
134
+ delegatedContractName,
135
+ hasEoaWithNative
134
136
}
135
137
} )
136
138
You can’t perform that action at this time.
0 commit comments