Skip to content

Commit 3fbe249

Browse files
committed
feat(support network change): support network change in SDK
1 parent 34927b0 commit 3fbe249

File tree

3 files changed

+80
-36
lines changed

3 files changed

+80
-36
lines changed

src/interface/provider.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,8 @@ export interface IIframeData {
9191
data: unknown
9292
success?: boolean
9393
}
94+
95+
export interface IIframeEventData {
96+
type: 'changeNetwork' | 'changeChain'
97+
data: unknown
98+
}

src/provider.ts

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,17 @@ export class Provider implements IProvider {
152152
if (this.address.length > 0) {
153153
return this.address
154154
}
155-
const result = (await callIframe('pages/dapp/auth', {
156-
appId: this.appId,
157-
params: params ? JSON.stringify(params) : '',
158-
chainId: (await this.request({ method: 'cfx_chainId' })) as string,
159-
authType: 'account',
160-
})) as IAuthResult
155+
console.log('cfx_accounts参数', paramsObj)
156+
const result = (await callIframe(
157+
'pages/dapp/auth',
158+
{
159+
appId: this.appId,
160+
params: params ? JSON.stringify(paramsObj) : '',
161+
chainId: (await this.request({ method: 'cfx_chainId' })) as string,
162+
authType: 'account',
163+
},
164+
this
165+
)) as IAuthResult
161166
setCache(result, this)
162167
this.events.onAccountsChanged &&
163168
this.events.onAccountsChanged(result.address)
@@ -168,47 +173,61 @@ export class Provider implements IProvider {
168173
return this.address
169174
case 'cfx_sendTransaction':
170175
try {
171-
return await callIframe('pages/dapp/auth', {
172-
appId: this.appId,
173-
chainId: (await this.request({ method: 'cfx_chainId' })) as string,
174-
params: params ? JSON.stringify(paramsObj) : '',
175-
authType:
176-
params && Object.keys(paramsObj).includes('to') && paramsObj['to']
177-
? getAddressType(paramsObj['to'] as string) ===
178-
AddressType.CONTRACT
179-
? 'callContract'
180-
: 'createTransaction'
181-
: 'createContract',
182-
})
176+
return await callIframe(
177+
'pages/dapp/auth',
178+
{
179+
appId: this.appId,
180+
chainId: (await this.request({
181+
method: 'cfx_chainId',
182+
})) as string,
183+
params: params ? JSON.stringify(paramsObj) : '',
184+
authType:
185+
params &&
186+
Object.keys(paramsObj).includes('to') &&
187+
paramsObj['to']
188+
? getAddressType(paramsObj['to'] as string) ===
189+
AddressType.CONTRACT
190+
? 'callContract'
191+
: 'createTransaction'
192+
: 'createContract',
193+
},
194+
this
195+
)
183196
} catch (e) {
184197
console.error('Error to sendTransaction', e)
185198
return e
186199
}
187200
case 'anyweb_importAccount':
188201
try {
189-
return await callIframe('pages/dapp/auth', {
190-
appId: this.appId,
191-
chainId: (await this.request({ method: 'cfx_chainId' })) as string,
192-
params: params ? JSON.stringify(paramsObj) : JSON.stringify([]),
193-
authType: 'importAccount',
194-
})
202+
return await callIframe(
203+
'pages/dapp/auth',
204+
{
205+
appId: this.appId,
206+
chainId: (await this.request({
207+
method: 'cfx_chainId',
208+
})) as string,
209+
params: params ? JSON.stringify(paramsObj) : JSON.stringify([]),
210+
authType: 'importAccount',
211+
},
212+
this
213+
)
195214
} catch (e) {
196215
console.error('Error to import Address', e)
197216
return e
198217
}
199218
case 'anyweb_version':
200219
return config.version
201220
case 'anyweb_home':
202-
return await callIframe('pages/index/home', {
203-
appId: this.appId,
204-
chainId: (await this.request({ method: 'cfx_chainId' })) as string,
205-
params: params
206-
? JSON.stringify(
207-
Array.isArray(params) && params.length > 0 ? params[0] : params
208-
)
209-
: '',
210-
waitResult: false,
211-
})
221+
return await callIframe(
222+
'pages/index/home',
223+
{
224+
appId: this.appId,
225+
chainId: (await this.request({ method: 'cfx_chainId' })) as string,
226+
params: params ? JSON.stringify(paramsObj) : '',
227+
waitResult: false,
228+
},
229+
this
230+
)
212231
default:
213232
return 'Unsupported method'
214233
}

src/utils/common.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
*/
55
import * as forge from 'node-forge'
66
import { BASE_URL } from '../config'
7-
import { IAuthResult, IIframeData, IIframeOptions } from '../interface/provider'
7+
import {
8+
IAuthResult,
9+
IIframeData,
10+
IIframeEventData,
11+
IIframeOptions,
12+
} from '../interface/provider'
813
import { Provider } from '../provider'
914

1015
export const getFrameWidth = () => {
@@ -230,7 +235,8 @@ export const callIframe = async (
230235
scope = [2],
231236
authType,
232237
waitResult = true,
233-
}: IIframeOptions
238+
}: IIframeOptions,
239+
provider: Provider
234240
) => {
235241
if (waitResult) {
236242
return new Promise<unknown>(async (resolve, reject) => {
@@ -276,6 +282,20 @@ export const callIframe = async (
276282
close()
277283
reject(new Error(callback.data as string))
278284
}
285+
} else if (callback.type === 'event') {
286+
const eventData = callback.data as IIframeEventData
287+
switch (eventData.type) {
288+
case 'changeNetwork':
289+
provider.events.onNetworkChanged &&
290+
provider.events.onNetworkChanged(String(eventData.data))
291+
break
292+
case 'changeChain':
293+
provider.events.onChainChanged &&
294+
provider.events.onChainChanged(String(eventData.data))
295+
break
296+
default:
297+
break
298+
}
279299
}
280300
}
281301
},

0 commit comments

Comments
 (0)