Skip to content
This repository has been archived by the owner on Sep 6, 2023. It is now read-only.

add VenlyConnector to packages/connectors (v1) #432

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stupid-bulldogs-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@wagmi/connectors': minor
---

Added `VenlyConnector`
1 change: 1 addition & 0 deletions packages/connectors/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ ledger/**
metaMask/**
mock/**
safe/**
venly/**
walletConnect/**
walletConnectLegacy/**
1 change: 1 addition & 0 deletions packages/connectors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const config = createConfig({
- [`MockConnector`](/packages/connectors/src/mock.ts)
- [`SafeConnector`](/packages/connectors/src/safe.ts)
- [`WalletConnectConnector`](/packages/connectors/src/walletConnect.ts)
- [`VenlyConnector`](/packages/connectors/src/venly.ts)

## Contributing

Expand Down
6 changes: 6 additions & 0 deletions packages/connectors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@ledgerhq/connect-kit-loader": "^1.1.0",
"@safe-global/safe-apps-provider": "^0.17.1",
"@safe-global/safe-apps-sdk": "^8.0.0",
"@venly/web3-provider": "^3.2.3",
"@walletconnect/ethereum-provider": "2.9.2",
"@walletconnect/utils": "2.9.2",
"@walletconnect/legacy-provider": "^2.0.0",
Expand Down Expand Up @@ -68,6 +69,10 @@
"types": "./dist/safe.d.ts",
"default": "./dist/safe.js"
},
"./venly": {
"types": "./dist/venly.d.ts",
"default": "./dist/venly.js"
},
"./walletConnect": {
"types": "./dist/walletConnect.d.ts",
"default": "./dist/walletConnect.js"
Expand All @@ -85,6 +90,7 @@
"/metaMask",
"/mock",
"/safe",
"/venly",
"/walletConnect",
"/walletConnectLegacy",
"/dist"
Expand Down
16 changes: 16 additions & 0 deletions packages/connectors/src/venly.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, expect, it } from 'vitest'

import { testChains } from '../test'
import { VenlyConnector } from './venly'

describe('VenlyConnector', () => {
it('inits', () => {
const connector = new VenlyConnector({
chains: testChains,
options: {
clientId: 'wagmi',
},
})
expect(connector.name).toEqual('Venly')
})
})
185 changes: 185 additions & 0 deletions packages/connectors/src/venly.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import { VenlyProvider, VenlyProviderOptions } from '@venly/web3-provider'
import type { Chain } from '@wagmi/chains'
import {
SwitchChainError,
UserRejectedRequestError,
createWalletClient,
custom,
getAddress,
numberToHex,
} from 'viem'

import { Connector } from './base'
import { ChainNotConfiguredForConnectorError } from './errors'
import type { WalletClient } from './types'
import { normalizeChainId } from './utils/normalizeChainId'

type Options = Omit<VenlyProviderOptions, 'reloadOnDisconnect'> & {
/**
* Fallback Ethereum JSON RPC URL
* @default ""
*/
jsonRpcUrl?: string
/**
* Fallback Ethereum Chain ID
* @default 1
*/
chainId?: number
/**
* Whether or not to reload dapp automatically after disconnect.
*/
reloadOnDisconnect?: boolean
}

export class VenlyConnector extends Connector<VenlyProvider, Options> {
readonly id = 'venly'
readonly name = 'Venly'
readonly ready = true

client: VenlyProvider = new VenlyProvider()
// client?: VenlyProvider
provider: any

constructor({ chains, options }: { chains?: Chain[]; options: Options }) {
super({
chains,
options,
})
}

async connect({ chainId }: { chainId?: number } = {}) {
try {
if (!this.provider)
this.provider = await this.client.createProvider(this.options)

this.provider.on('accountsChanged', this.onAccountsChanged)
this.provider.on('chainChanged', this.onChainChanged)
this.provider.on('disconnect', this.onDisconnect)

this.emit('message', { type: 'connecting' })

// Switch to chain if provided
let id = await this.getChainId()
let unsupported = this.isChainUnsupported(id)
chainId = chainId ?? this.chains[0]?.id
if (chainId && id !== chainId) {
const chain = await this.switchChain(chainId)
id = chain.id
unsupported = this.isChainUnsupported(id)
}
const account = await this.getAccount()

return {
account,
chain: { id, unsupported },
}
} catch (error) {
if (
/(user closed modal|accounts received is empty)/i.test(
(error as Error).message,
)
)
throw new UserRejectedRequestError(error as Error)
throw error
}
}

async disconnect() {
if (!this.provider) return

await this.client.logout()
this.provider.removeListener('accountsChanged', this.onAccountsChanged)
this.provider.removeListener('chainChanged', this.onChainChanged)
this.provider.removeListener('disconnect', this.onDisconnect)

this.provider = null
}

async getAccount() {
const accounts = await this.provider.request({
method: 'eth_accounts',
})
// return checksum address
return getAddress(accounts[0] as string)
}

async getChainId() {
const chainId = await this.provider.request({
method: 'eth_chainId',
})
return normalizeChainId(chainId)
}

getProvider() {
return this.provider
}

async getWalletClient({ chainId }: { chainId?: number } = {}): Promise<WalletClient> {
const provider = this.getProvider()
const account = await this.getAccount()
const chain = this.chains.find((x) => x.id === chainId)
if (!provider) throw new Error('provider is required.')
return createWalletClient({
account,
chain,
transport: custom(provider),
})
}

async isAuthorized() {
try {
this.provider = await this.client.createProvider({
...this.options,
skipAuthentication: true,
})
const authResult = await this.client.checkAuthenticated()
return authResult.isAuthenticated
} catch {
return false
}
}

async switchChain(chainId: number) {
const id = numberToHex(chainId)

try {
await this.provider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: id }],
})
return (
this.chains.find((x) => x.id === chainId) ?? {
id: chainId,
name: `Chain ${id}`,
network: `${id}`,
nativeCurrency: { name: 'Ether', decimals: 18, symbol: 'ETH' },
rpcUrls: { default: { http: [''] }, public: { http: [''] } },
}
)
} catch (error) {
const chain = this.chains.find((x) => x.id === chainId)
if (!chain)
throw new ChainNotConfiguredForConnectorError({
chainId,
connectorId: this.id,
})

throw new SwitchChainError(error as Error)
}
}

protected onAccountsChanged = (accounts: string[]) => {
if (accounts.length === 0) this.emit('disconnect')
else this.emit('change', { account: getAddress(accounts[0] as string) })
}

protected onChainChanged = (chainId: number | string) => {
const id = normalizeChainId(chainId)
const unsupported = this.isChainUnsupported(id)
this.emit('change', { chain: { id, unsupported } })
}

protected onDisconnect = () => {
this.emit('disconnect')
}
}
1 change: 1 addition & 0 deletions packages/connectors/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default defineConfig(
'src/metaMask.ts',
'src/mock/index.ts',
'src/safe.ts',
'src/venly.ts',
'src/walletConnect.ts',
'src/walletConnectLegacy.ts',
],
Expand Down
Loading
Loading