@@ -12,7 +12,8 @@ import {
12
12
ProviderType ,
13
13
} from '@masknet/web3-shared-evm'
14
14
import { ExtensionSite , getSiteType , isEnhanceableSiteType } from '@masknet/shared-base'
15
- import { isGreaterThan , toFixed } from '@masknet/web3-shared-base'
15
+ import { isGreaterThan , isZero , toFixed } from '@masknet/web3-shared-base'
16
+ import { SharedContextRef } from '../../../PluginContext/index.js'
16
17
import { SmartPayBundlerAPI } from '../../../SmartPay/index.js'
17
18
import { ConnectionReadonlyAPI } from '../apis/ConnectionReadonlyAPI.js'
18
19
import { ContractReadonlyAPI } from '../apis/ContractReadonlyAPI.js'
@@ -30,6 +31,7 @@ export class Popups implements Middleware<ConnectionContext> {
30
31
private Bundler = new SmartPayBundlerAPI ( )
31
32
32
33
private async getPaymentToken ( context : ConnectionContext ) {
34
+ const maskAddress = getMaskTokenAddress ( context . chainId )
33
35
try {
34
36
const smartPayChainId = await this . Bundler . getSupportedChainId ( )
35
37
if ( context . chainId !== smartPayChainId || ! context . owner ) return DEFAULT_PAYMENT_TOKEN_STATE
@@ -46,8 +48,6 @@ export class Popups implements Middleware<ConnectionContext> {
46
48
47
49
if ( ! signableConfig ?. maxFeePerGas ) return DEFAULT_PAYMENT_TOKEN_STATE
48
50
49
- const maskAddress = getMaskTokenAddress ( context . chainId )
50
-
51
51
const gas = await this . Web3 . estimateTransaction ?.( signableConfig , undefined , {
52
52
chainId : context . chainId ,
53
53
account : context . account ,
@@ -82,6 +82,14 @@ export class Popups implements Middleware<ConnectionContext> {
82
82
paymentToken : context . paymentToken ?? ! availableBalanceTooLow ? maskAddress : undefined ,
83
83
}
84
84
} catch ( error ) {
85
+ const nativeBalance = await this . Web3 . getNativeTokenBalance ( {
86
+ account : context . account ,
87
+ chainId : context . chainId ,
88
+ } )
89
+
90
+ if ( isZero ( nativeBalance ) )
91
+ return { allowMaskAsGas : true , paymentToken : context . paymentToken ?? maskAddress }
92
+
85
93
return {
86
94
allowMaskAsGas : false ,
87
95
paymentToken : undefined ,
@@ -107,7 +115,7 @@ export class Popups implements Middleware<ConnectionContext> {
107
115
isUndefined ,
108
116
)
109
117
110
- const response = await context . bridge . send ?. ( context . request , options )
118
+ const response = await SharedContextRef . value . send ( context . request , options )
111
119
const editor = ErrorEditor . from ( null , response )
112
120
113
121
if ( editor . presence ) {
0 commit comments