-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: removed WC package, and refactor events
- Loading branch information
1 parent
b457675
commit 1b006ea
Showing
33 changed files
with
276 additions
and
277 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
Oops, something went wrong.