Skip to content

Commit

Permalink
refactor: removed WC package, and refactor events
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenvechain committed Nov 10, 2023
1 parent b457675 commit 1b006ea
Show file tree
Hide file tree
Showing 33 changed files with 276 additions and 277 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish-wallet-kit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
packages: write
strategy:
matrix:
package: [ wallet-kit,react-wallet-kit,wallet-connect,vanilla-wallet-kit ]
package: [ wallet-kit,react-wallet-kit,vanilla-wallet-kit ]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
Expand Down
7 changes: 5 additions & 2 deletions apps/sample-vanilla-app/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable no-undef */
import { configureThorModal } from '@vechainfoundation/vanilla-wallet-kit';
import { DAppKit } from '@vechainfoundation/vanilla-wallet-kit';

const walletConnectOptions = {
projectId: 'a0b855ceaf109dbc8426479a4c3d38d8',
Expand All @@ -18,4 +18,7 @@ const vechainWalletKitOptions = {
walletConnectOptions,
};

configureThorModal(vechainWalletKitOptions);
const connex = DAppKit.configure(vechainWalletKitOptions);

// eslint-disable-next-line no-console
console.log(`Connex configured`, connex.thor.genesis.id);
2 changes: 1 addition & 1 deletion apps/sample-vanilla-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"scripts": {
"build": "parcel build index.html --public-url /vechain-dapp-kit/vanilla",
"clean": "rm -rf dist",
"dev": "parcel index.html --open --port 3001",
"dev": "parcel index.html --open --no-cache --port 3001",
"purge": "yarn clean && rm -rf node_modules",
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down
12 changes: 3 additions & 9 deletions apps/sample-vue-app/src/connex/ConnexProvider.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<script lang="ts">
import { defineComponent, provide, reactive, readonly, toRefs } from 'vue';
import {
ConnectResponse,
MultiWalletConnex,
WalletSource,
} from '@vechainfoundation/wallet-kit';
import { ConnectResponse, WalletSource } from '@vechainfoundation/wallet-kit';
import type Connex from '@vechain/connex';
import {
ConnexSymbol,
Expand All @@ -13,7 +9,7 @@ import {
} from '@/connex/keys';
import { WalletActions, WalletState } from '@/connex/types';
import { WalletConnectOptions } from '@vechainfoundation/wallet-connect';
import { configureThorModal } from '@vechainfoundation/vanilla-wallet-kit';
import { DAppKit } from '@vechainfoundation/vanilla-wallet-kit';
const initWallets = (hasWcOptions: boolean) => {
const wallets: WalletSource[] = ['sync2'];
Expand Down Expand Up @@ -51,7 +47,7 @@ export default defineComponent({
account: null,
});
const connex = new MultiWalletConnex({
const connex = DAppKit.configure({
nodeUrl: 'https://mainnet.vechain.org/',
walletConnectOptions,
});
Expand All @@ -68,8 +64,6 @@ export default defineComponent({
connex.wallet.onDisconnected(onDisconnected);
connex.wallet.onSourceChanged(onSourceChanged);
configureThorModal(connex);
const setAccount = (addr: string) => {
walletState.account = addr;
};
Expand Down
2 changes: 1 addition & 1 deletion packages/react-wallet-kit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const nodeOptions: Omit<Options, 'signer'> = {
- Optional: Configure wallet connect options

```typescript
import type { WalletConnectOptions } from '@vechainfoundation/wallet-connect';
import type { WalletConnectOptions } from '@vechainfoundation/wallet-kit';

const walletConnectOptions: WalletConnectOptions = {
projectId: '<PROJECT_ID>', // Create your project here: https://cloud.walletconnect.com/sign-up
Expand Down
1 change: 0 additions & 1 deletion packages/react-wallet-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"@vechain/connex-framework": "2.1.0",
"@vechain/picasso": "^2.1.1",
"@vechainfoundation/vanilla-wallet-kit": "*",
"@vechainfoundation/wallet-connect": "*",
"@vechainfoundation/wallet-kit": "*",
"react-icons": "^4.11.0",
"styled-components": "^6.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/react-wallet-kit/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Options } from '@vechain/connex';
import type React from 'react';
import type { WalletConnectOptions } from '@vechainfoundation/wallet-connect';
import type {
ConnectResponse,
WalletConnectOptions,
WalletSource,
} from '@vechainfoundation/wallet-kit';

Expand Down
2 changes: 1 addition & 1 deletion packages/vanilla-wallet-kit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ yarn add @vechainfoundation/vanilla-wallet-kit
- In your root `index.ts`

```typescript
import type { WalletConnectOptions } from '@vechainfoundation/wallet-connect';
import type { WalletConnectOptions } from '@vechainfoundation/wallet-kit';
import type { Options } from '@vechain/connex';
import { configureThorModal } from '@vechainfoundation/vanilla-wallet-kit';

Expand Down
4 changes: 2 additions & 2 deletions packages/vanilla-wallet-kit/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable no-undef */
import { configureThorModal } from './dist';
import { DAppKit } from './dist';

const walletConnectOptions = {
projectId: 'a0b855ceaf109dbc8426479a4c3d38d8',
Expand All @@ -18,4 +18,4 @@ const vechainWalletKitOptions = {
walletConnectOptions,
};

configureThorModal(vechainWalletKitOptions);
DAppKit.configure(vechainWalletKitOptions);
3 changes: 1 addition & 2 deletions packages/vanilla-wallet-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
"scripts": {
"build": "tsup",
"clean": "rm -rf dist .turbo",
"dev": "parcel index.html",
"dev": "parcel --no-cache index.html",
"format": "prettier \"**/*.{cjs,html,js,json,md,ts}\" --ignore-path ./.eslintignore --write",
"lint": "eslint 'src/**/*.ts'",
"purge": "yarn clean && rm -rf node_modules",
"watch": "tsup --watch"
},
"dependencies": {
"@vechainfoundation/wallet-connect": "*",
"@vechainfoundation/wallet-kit": "*",
"@wagmi/core": "^1.4.5",
"@web3modal/ethereum": "^2.7.1",
Expand Down
107 changes: 23 additions & 84 deletions packages/vanilla-wallet-kit/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,94 +1,33 @@
import type { WalletSource } from '@vechainfoundation/wallet-kit';
import type { ConnexOptions } from '@vechainfoundation/wallet-kit';
import { MultiWalletConnex } from '@vechainfoundation/wallet-kit';
import type { ConnexOptions } from '@vechainfoundation/wallet-kit/src';
import type { WCModal } from '@vechainfoundation/wallet-connect';
import type { OpenOptions } from '@vechainfoundation/wallet-connect/src/types';
import './components';
import type { SourceInfo } from './constants';
import { CustomWalletConnectModal } from './modal';

export type VechainWalletKitOptions = MultiWalletConnex | ConnexOptions;
let connex: MultiWalletConnex | null = null;

class CustomWalletConnectModal implements WCModal {
openModal(options: OpenOptions): Promise<void> {
const { uri } = options;
dispatchEvent(new CustomEvent('vwk-open-wc-modal', { detail: uri }));
return Promise.resolve();
}
closeModal(): void {
// NOT USED because it is controlled from inside the component
// dispatchEvent(new CustomEvent('vwk-close-wc-modal'));
}
subscribeModal() {
// NOT USED because it is controlled from inside the component
// eslint-disable-next-line @typescript-eslint/no-empty-function
return (): void => {};
}
}

class VechainWalletKit {
connex: MultiWalletConnex;
account: string | null = null;

constructor(options: VechainWalletKitOptions) {
if ('thor' in options) {
this.connex = options;
} else {
this.connex = new MultiWalletConnex(
this.addCustomWalletConnectModalIfNotPresent(options),
);
const DAppKit = {
configure(options: ConnexOptions): MultiWalletConnex {
if (!options.customWcModal) {
options.customWcModal = CustomWalletConnectModal.getInstance();
}
}

addCustomWalletConnectModalIfNotPresent(
options: ConnexOptions,
): ConnexOptions {
return {
...options,
customWcModal:
options.customWcModal || new CustomWalletConnectModal(),
};
}

setSource = (wallet: WalletSource): void => {
this.connex.wallet.setSource(wallet);
};
}
connex = new MultiWalletConnex(options);

class VechainWalletKitModal {
public walletKit: VechainWalletKit;
return connex;
},

constructor(walletKit: VechainWalletKit) {
this.walletKit = walletKit;
}
get connex(): MultiWalletConnex {
if (!connex) {
// eslint-disable-next-line no-console
console.error('🚨🚨🚨 DAppKit not configured 🚨🚨🚨');
throw new Error('DAppKit not configured');
}

initModalListeners(): void {
addEventListener('vwk-source-card-clicked', (event) => {
const source = (event as CustomEvent).detail as SourceInfo;
this.walletKit.setSource(source.id);
this.walletKit.connex.wallet
.connect()
.then(({ account }) => {
this.walletKit.account = account;
})
.catch((error) => {
// eslint-disable-next-line no-console
console.error(error);
});
});
const disconnect = (): void => {
this.walletKit.connex.wallet.disconnect().catch((error) => {
// eslint-disable-next-line no-console
console.error(error);
});
};
addEventListener('vwk-close-wc-modal', disconnect);
}
}
return connex;
},

export const configureThorModal = (
walletKitOptions: VechainWalletKitOptions,
): void => {
const vechainWalletKit = new VechainWalletKit(walletKitOptions);
const vechainWalletKitModal = new VechainWalletKitModal(vechainWalletKit);
vechainWalletKitModal.initModalListeners();
get modal(): CustomWalletConnectModal {
return CustomWalletConnectModal.getInstance();
},
};

export { DAppKit };
Loading

0 comments on commit 1b006ea

Please sign in to comment.