Skip to content

Commit

Permalink
support viem
Browse files Browse the repository at this point in the history
  • Loading branch information
KABBOUCHI committed Sep 28, 2023
1 parent 9da073d commit 9d74a5f
Showing 3 changed files with 40 additions and 20 deletions.
50 changes: 34 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -32,8 +32,24 @@ const walletconnect = new WalletConnectConnector({
qrcode: true,
})

// web3.js v1
setWeb3LibraryCallback((provider) => new Web3(provider))

// ethers.js v5
setWeb3LibraryCallback((provider) => new Web3Provider(provider, "any"))

// viem
setWeb3LibraryCallback((provider, _connector, account) => ({
public: createPublicClient({
transport: custom(provider),
}),
wallet: createWalletClient({
account,
chain: null as unknown as Chain,
transport: custom(provider),
}),
}))

defineComponent({
setup() {
const { active, activate, account, library } = useWeb3()
@@ -55,9 +71,11 @@ defineComponent({
},
})
```

#### Typescript:

using generic:

```js
import Web3 from 'web3'

@@ -71,48 +89,48 @@ const { library } = useWeb3<Web3Provider>()
```

using global types:

```ts
// global.d.ts
import type Web3 from "web3";
import type Web3 from 'web3'

declare module "@instadapp/vue-web3" {
declare module '@instadapp/vue-web3' {
interface IVueWeb3Library extends Web3 {}
}
```


#### Nuxt 3

```bash
yarn add @instadapp/vue-web3-nuxt -D
```

```ts
// nuxt.config.ts
// nuxt.config.ts
export default defineNuxtConfig({
modules: [
'@instadapp/vue-web3-nuxt'
],
web3 :{
autoImport: false, // default `true`
}
modules: ['@instadapp/vue-web3-nuxt'],
web3: {
autoImport: false, // default `true`
},
})
```

If you disabled `@instadapp/vue-web3-nuxt` auto import:

```ts
//composables/useWeb3.ts
import Web3 from "web3";
import Web3 from 'web3'
// import { Web3Provider } from "@ethersproject/providers";
import { useWeb3 as useWeb3Generic } from "@instadapp/vue-web3";
import { useWeb3 as useWeb3Generic } from '@instadapp/vue-web3'

const useWeb3 = () => useWeb3Generic<Web3>();
const useWeb3 = () => useWeb3Generic<Web3>()
// const useWeb3 = () => useWeb3Generic<Web3Provider>();

export { useWeb3 };
export { useWeb3 }
```

<br />
---
## <br />

<br />

Demo (Nuxt 2): https://github.com/KABBOUCHI/nuxt-vue-web3
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@instadapp/vue-web3",
"version": "0.10.1",
"version": "0.11.0",
"description": "Vue web3 composition api",
"license": "MIT",
"main": "index.js",
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -37,7 +37,9 @@ const active = computed(
)
const library = shallowRef()

let getLibrary: any = (provider?: any, connector?: any) => (): any => null
let getLibrary: any =
(provider: any, connector: any, account: `0x${string}`) => (): any =>
null

export const setWeb3LibraryCallback = (
cb: (provider?: any, connector?: any) => any,
@@ -160,13 +162,13 @@ export const useWeb3 = <TVueWeb3Library extends IVueWeb3Library>() => {
library.value = undefined
}

watch([active, provider, connector, chainId], () => {
watch([active, provider, connector, chainId, account], () => {
library.value =
active.value &&
chainId.value !== undefined &&
Number.isInteger(chainId.value) &&
!!connector.value
? getLibrary(provider.value, connector.value)
? getLibrary(provider.value, connector.value, account.value)
: undefined
})

0 comments on commit 9d74a5f

Please sign in to comment.