Skip to content

Commit ae9a1f8

Browse files
committed
fix(Usage Modification): modification of usage
1 parent 3a0f4b2 commit ae9a1f8

File tree

2 files changed

+39
-40
lines changed

2 files changed

+39
-40
lines changed

example/basic-dapp/index.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -633,10 +633,12 @@ async function walletInitialized() {
633633

634634
const data = await provider.request({
635635
method: 'cfx_accounts',
636-
params: {
637-
availableNetwork: [1, 1029],
638-
scopes: ['baseInfo', 'identity'],
639-
},
636+
params: [
637+
{
638+
availableNetwork: [1, 1029],
639+
scopes: ['baseInfo', 'identity'],
640+
},
641+
],
640642
})
641643
const { chainId, networkId, address: alreadyAuthedAddresses, code } = data
642644
console.log(
@@ -668,10 +670,12 @@ async function walletInitialized() {
668670
connectButton.onclick = async () => {
669671
const data = await provider.request({
670672
method: 'cfx_accounts',
671-
params: {
672-
availableNetwork: [1, 1029],
673-
scopes: ['baseInfo', 'identity'],
674-
},
673+
params: [
674+
{
675+
availableNetwork: [1, 1029],
676+
scopes: ['baseInfo', 'identity'],
677+
},
678+
],
675679
})
676680
const { chainId, networkId, address: alreadyAuthedAddresses, code } = data
677681
console.log(
@@ -722,10 +726,7 @@ async function walletInitialized() {
722726
provider
723727
.request({
724728
method: 'cfx_sendTransaction',
725-
params: {
726-
payload,
727-
gatewayPayload,
728-
},
729+
params: [payload, gatewayPayload],
729730
})
730731
.then((result) => {
731732
getElement('send_native_token_result').innerHTML = `txhash: ${result}`
@@ -742,7 +743,7 @@ async function walletInitialized() {
742743
}
743744
console.log('getCFX tx', tx)
744745
provider
745-
.request({ method: 'cfx_sendTransaction', params: { payload: tx } })
746+
.request({ method: 'cfx_sendTransaction', params: [tx] })
746747
.then((result) => {
747748
getElement('get_cfx_result').innerHTML = result
748749
console.log('result', result)
@@ -775,7 +776,7 @@ async function walletInitialized() {
775776
provider
776777
.request({
777778
method: 'cfx_sendTransaction',
778-
params: { payload, gatewayPayload },
779+
params: [payload, gatewayPayload],
779780
})
780781
.then((result) => {
781782
getElement('gateway_test_result').innerHTML = result
@@ -803,7 +804,7 @@ async function walletInitialized() {
803804
gasPrice: 2,
804805
}
805806
provider
806-
.request({ method: 'cfx_sendTransaction', params: { payload: tx } })
807+
.request({ method: 'cfx_sendTransaction', params: [tx] })
807808
.then((result) => {
808809
getElement('approve_result').innerHTML = result
809810
console.log('result', result)
@@ -826,7 +827,7 @@ async function walletInitialized() {
826827
).data,
827828
}
828829
provider
829-
.request({ method: 'cfx_sendTransaction', params: { payload: tx } })
830+
.request({ method: 'cfx_sendTransaction', params: [tx] })
830831
.then((result) => {
831832
console.log('result', result)
832833
})
@@ -844,7 +845,7 @@ async function walletInitialized() {
844845
data: exampleContract.constructor('Example', 18, 'EP', 10000).data,
845846
}
846847
provider
847-
.request({ method: 'cfx_sendTransaction', params: { payload: tx } })
848+
.request({ method: 'cfx_sendTransaction', params: [tx] })
848849
.then((result) => {
849850
getElement('deploy_contract_result').innerHTML =
850851
result.transactionHash
@@ -877,7 +878,7 @@ async function walletInitialized() {
877878
addressName: addressName,
878879
}
879880
provider
880-
.request({ method: 'anyweb_importAccount', params: tx })
881+
.request({ method: 'anyweb_importAccount', params: [tx] })
881882
.then((result) => {
882883
getElement('import_address_result').innerHTML = result
883884
console.log('result', result)

src/provider.ts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@ import {
77
IAuthResult,
88
IBaseProviderOptions,
99
IIframeOptions,
10-
IPayload,
1110
IProvider,
1211
IProviderConnectInfo,
1312
IProviderMessage,
1413
IProviderRpcError,
1514
IRequestArguments,
16-
IRequestParams,
17-
IRequestParamsAccounts,
1815
} from './interface/provider'
1916
import { callIframe } from './utils/common'
2017
import config from '../package.json'
@@ -32,6 +29,7 @@ import { ConsoleLike } from './utils/types'
3229
export class Provider implements IProvider {
3330
logger: ConsoleLike
3431
public readonly appId: string
32+
private chainId = 1
3533

3634
events: {
3735
onConnect?: (connectInfo: IProviderConnectInfo) => void
@@ -97,12 +95,12 @@ export class Provider implements IProvider {
9795
if (!args || typeof args !== 'object' || Array.isArray(args)) {
9896
throw new Error('Invalid request arguments')
9997
}
100-
const { method, params, chainId } = args
98+
const { method, params } = args
10199
if (!method || method.trim().length === 0) {
102100
throw new Error('Method is required')
103101
}
104102
console.debug(`[AnyWeb] request ${method} with`, params)
105-
const result = await this.rawRequest(method, params, chainId)
103+
const result = await this.rawRequest(method, params)
106104
console.debug(`[AnyWeb] request(${method}):`, result)
107105
return result
108106
}
@@ -120,26 +118,21 @@ export class Provider implements IProvider {
120118
* Submits an RPC request
121119
* @param method
122120
* @param params
123-
* @param chainId
124121
* @protected
125122
*/
126-
protected async rawRequest(
127-
method: string,
128-
params?: IRequestParams,
129-
chainId = 1
130-
): Promise<unknown> {
123+
protected async rawRequest(method: string, params?: any): Promise<unknown> {
131124
switch (method) {
132125
case 'cfx_requestAccounts':
133126
return this.rawRequest('cfx_accounts')
134127
case 'cfx_accounts':
135-
params = params as IRequestParamsAccounts
136-
const scopes: string[] = params ? params.scopes || [] : []
128+
console.log({ params })
129+
const scopes = params[0].scopes
137130
const result = (await callIframe(
138131
'pages/dapp/auth',
139132
{
140133
appId: this.appId,
141-
params: params ? JSON.stringify(params) : '',
142-
chainId: chainId,
134+
params: params ? JSON.stringify(params[0]) : '',
135+
chainId: this.chainId,
143136
authType: 'account',
144137
scopes: scopes,
145138
},
@@ -166,7 +159,7 @@ export class Provider implements IProvider {
166159
case 'cfx_sendTransaction':
167160
try {
168161
let authType: IIframeOptions['authType']
169-
const { payload } = params as { payload: IPayload }
162+
const payload = params[0]
170163
const to = payload.to
171164
if (to) {
172165
authType =
@@ -182,8 +175,13 @@ export class Provider implements IProvider {
182175
'pages/dapp/auth',
183176
{
184177
appId: this.appId,
185-
chainId: chainId,
186-
params: params ? JSON.stringify(params) : '',
178+
chainId: this.chainId,
179+
params: params
180+
? JSON.stringify({
181+
payload: params[0],
182+
gatewayPayload: params[1],
183+
})
184+
: '',
187185
authType: authType,
188186
},
189187
this
@@ -198,8 +196,8 @@ export class Provider implements IProvider {
198196
'pages/dapp/auth',
199197
{
200198
appId: this.appId,
201-
chainId: chainId,
202-
params: params ? JSON.stringify(params) : JSON.stringify({}),
199+
chainId: this.chainId,
200+
params: params ? JSON.stringify(params[0]) : JSON.stringify({}),
203201
authType: 'importAccount',
204202
},
205203
this
@@ -215,7 +213,7 @@ export class Provider implements IProvider {
215213
'pages/index/home',
216214
{
217215
appId: this.appId,
218-
chainId: chainId,
216+
chainId: this.chainId,
219217
params: params ? JSON.stringify(params) : '',
220218
waitResult: false,
221219
},
@@ -226,7 +224,7 @@ export class Provider implements IProvider {
226224
'pages/dapp/auth',
227225
{
228226
appId: this.appId,
229-
chainId: chainId,
227+
chainId: this.chainId,
230228
params: params ? JSON.stringify(params) : '',
231229
authType: 'exit_accounts',
232230
},

0 commit comments

Comments
 (0)