From a6262b2cb25a02127d99ab70023bacdfd573a764 Mon Sep 17 00:00:00 2001 From: TuanD Date: Mon, 1 Apr 2024 12:30:58 +0700 Subject: [PATCH] feat: rename to read/write method in evm provider (#818) --- .changeset/thick-falcons-worry.md | 5 +++ .../src/providers/disabled-provider.ts | 6 ++- .../src/providers/evm-provider.ts | 45 +++++++++++++++++-- .../src/providers/provider.ts | 19 +++++--- .../src/providers/ron-provider.ts | 45 +++++++++++++++++-- .../src/providers/sol-provider.ts | 6 ++- 6 files changed, 113 insertions(+), 13 deletions(-) create mode 100644 .changeset/thick-falcons-worry.md diff --git a/.changeset/thick-falcons-worry.md b/.changeset/thick-falcons-worry.md new file mode 100644 index 000000000..320d5e4a6 --- /dev/null +++ b/.changeset/thick-falcons-worry.md @@ -0,0 +1,5 @@ +--- +"@mochi-web3/connect-wallet-widget": minor +--- + +Rename to read/write method diff --git a/packages/web3/connect-wallet-widget/src/providers/disabled-provider.ts b/packages/web3/connect-wallet-widget/src/providers/disabled-provider.ts index 7f672b16f..2aebdc396 100644 --- a/packages/web3/connect-wallet-widget/src/providers/disabled-provider.ts +++ b/packages/web3/connect-wallet-widget/src/providers/disabled-provider.ts @@ -6,7 +6,11 @@ export class ProviderDisabled extends ChainProvider { return Object.assign(this) } - async method() { + async read() { + return '' + } + + async write() { return '' } diff --git a/packages/web3/connect-wallet-widget/src/providers/evm-provider.ts b/packages/web3/connect-wallet-widget/src/providers/evm-provider.ts index ad83a875d..a761addd3 100644 --- a/packages/web3/connect-wallet-widget/src/providers/evm-provider.ts +++ b/packages/web3/connect-wallet-widget/src/providers/evm-provider.ts @@ -3,7 +3,13 @@ import hexer from 'browser-string-hexer' import { createStore } from 'mipd' import { utils } from 'ethers' import isMobile from 'is-mobile' -import { msg, ChainProvider, TransferInput, Input } from './provider' +import { + msg, + ChainProvider, + TransferInput, + WriteInput, + ReadInput, +} from './provider' const eip6963Store = typeof window !== 'undefined' ? createStore() : null @@ -43,9 +49,42 @@ export class ProviderEVM extends ChainProvider { return Object.assign(this) } - async method(i: Input) { + async read(i: ReadInput) { try { - const { abi, to, from, args, method } = i + const { abi, to, from, args = [], method } = i + const iface = new utils.Interface(abi) + const data = iface.encodeFunctionResult(method, args) + + if (isMobile() && this.session.topic && this.signClient) { + return await this.signClient.request({ + topic: this.session.topic, + chainId: `eip155:${(+this.chainId).toString(10)}`, + request: { + method: 'eth_call', + params: [{ from, to, data }], + }, + }) + } + + return this.provider.request({ + method: 'eth_call', + params: [ + { + from, + to, + data, + }, + ], + }) + } catch (e) { + console.error('evm-provider:method', e) + return null + } + } + + async write(i: WriteInput) { + try { + const { abi, to, from, args = [], method } = i const iface = new utils.Interface(abi) const data = iface.encodeFunctionData(method, args) diff --git a/packages/web3/connect-wallet-widget/src/providers/provider.ts b/packages/web3/connect-wallet-widget/src/providers/provider.ts index 6a37bf13c..c7e48cb25 100644 --- a/packages/web3/connect-wallet-widget/src/providers/provider.ts +++ b/packages/web3/connect-wallet-widget/src/providers/provider.ts @@ -2,14 +2,22 @@ import { SVGProps } from 'react' import { SignClient } from '@walletconnect/sign-client' import { wcProfjectId } from '../constants' -export type Input = { +export type WriteInput = { abi: string method: string - args: (string | number)[] + args?: (string | number)[] from: string to: string } +export type ReadInput = { + abi: string + method: string + args?: (string | number)[] + from?: string + to: string +} + export type TransferInput = { chainId: string from: string @@ -24,7 +32,7 @@ export type ConnectResponse = { platform: string } | null -export abstract class ChainProvider { +export abstract class ChainProvider { public mobileProtocol: string = '' public provider: any public id: string = '' @@ -94,12 +102,13 @@ export abstract class ChainProvider { }) } - abstract method(i: I): Promise + abstract write(i: WI): Promise + abstract read(i: RI): Promise abstract transfer(args: TransferInput): Promise abstract connect(): Promise abstract connectMobile(): Promise abstract isInstalled(): Promise - abstract sync(storeGetter?: any): ChainProvider + abstract sync(storeGetter?: any): ChainProvider } export const msg = 'Please sign this message to prove wallet ownership' as const diff --git a/packages/web3/connect-wallet-widget/src/providers/ron-provider.ts b/packages/web3/connect-wallet-widget/src/providers/ron-provider.ts index e2dcfcfdb..f20ad1102 100644 --- a/packages/web3/connect-wallet-widget/src/providers/ron-provider.ts +++ b/packages/web3/connect-wallet-widget/src/providers/ron-provider.ts @@ -3,7 +3,13 @@ import hexer from 'browser-string-hexer' import { createStore } from 'mipd' import { utils } from 'ethers' import isMobile from 'is-mobile' -import { msg, ChainProvider, TransferInput, Input } from './provider' +import { + msg, + ChainProvider, + TransferInput, + WriteInput, + ReadInput, +} from './provider' const eip6963Store = typeof window !== 'undefined' ? createStore() : null @@ -44,9 +50,42 @@ export class ProviderRON extends ChainProvider { return Object.assign(this) } - async method(i: Input) { + async read(i: ReadInput) { try { - const { abi, to, from, args, method } = i + const { abi, to, from, args = [], method } = i + const iface = new utils.Interface(abi) + const data = iface.encodeFunctionResult(method, args) + + if (isMobile() && this.session.topic && this.signClient) { + return await this.signClient.request({ + topic: this.session.topic, + chainId: `eip155:${(+this.chainId).toString(10)}`, + request: { + method: 'eth_call', + params: [{ from, to, data }], + }, + }) + } + + return this.provider.request({ + method: 'eth_call', + params: [ + { + from, + to, + data, + }, + ], + }) + } catch (e) { + console.error('evm-provider:method', e) + return null + } + } + + async write(i: WriteInput) { + try { + const { abi, to, from, args = [], method } = i const iface = new utils.Interface(abi) const data = iface.encodeFunctionData(method, args) diff --git a/packages/web3/connect-wallet-widget/src/providers/sol-provider.ts b/packages/web3/connect-wallet-widget/src/providers/sol-provider.ts index ceaaddb7f..14abf22a2 100644 --- a/packages/web3/connect-wallet-widget/src/providers/sol-provider.ts +++ b/packages/web3/connect-wallet-widget/src/providers/sol-provider.ts @@ -57,7 +57,11 @@ export class ProviderSOL extends ChainProvider { } } - async method() { + async read() { + throw new Error('Not yet implemented') + } + + async write() { throw new Error('Not yet implemented') }