Skip to content

Commit

Permalink
feat: rename to read/write method in evm provider (#818)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuanddd committed Apr 1, 2024
1 parent f926772 commit a6262b2
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-falcons-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mochi-web3/connect-wallet-widget": minor
---

Rename to read/write method
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ export class ProviderDisabled extends ChainProvider {
return Object.assign(this)
}

async method() {
async read() {
return ''
}

async write() {
return ''
}

Expand Down
45 changes: 42 additions & 3 deletions packages/web3/connect-wallet-widget/src/providers/evm-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down
19 changes: 14 additions & 5 deletions packages/web3/connect-wallet-widget/src/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,7 +32,7 @@ export type ConnectResponse = {
platform: string
} | null

export abstract class ChainProvider<I = Input> {
export abstract class ChainProvider<RI = ReadInput, WI = WriteInput> {
public mobileProtocol: string = ''
public provider: any
public id: string = ''
Expand Down Expand Up @@ -94,12 +102,13 @@ export abstract class ChainProvider<I = Input> {
})
}

abstract method(i: I): Promise<void | string>
abstract write(i: WI): Promise<void | string>
abstract read(i: RI): Promise<void | any>
abstract transfer(args: TransferInput): Promise<string>
abstract connect(): Promise<ConnectResponse>
abstract connectMobile(): Promise<ConnectResponse>
abstract isInstalled(): Promise<boolean>
abstract sync(storeGetter?: any): ChainProvider<I>
abstract sync(storeGetter?: any): ChainProvider<WI>
}

export const msg = 'Please sign this message to prove wallet ownership' as const
45 changes: 42 additions & 3 deletions packages/web3/connect-wallet-widget/src/providers/ron-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}

Expand Down

0 comments on commit a6262b2

Please sign in to comment.