From 1bcefe9aa33ab95732cba29f844a98554cac9e2c Mon Sep 17 00:00:00 2001 From: Christopher Cali Date: Tue, 27 Jun 2023 10:16:44 -0700 Subject: [PATCH] Initiate listener for contract events when user logs in --- apps/web/src/composables/contracts.ts | 2 ++ apps/web/src/composables/ethers.ts | 13 ++++++++++++- apps/web/src/composables/wallet.ts | 3 ++- apps/web/src/pages/overview/components/Staking.vue | 11 ++++++++--- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/apps/web/src/composables/contracts.ts b/apps/web/src/composables/contracts.ts index e4e3d50ba..212b5c3e9 100644 --- a/apps/web/src/composables/contracts.ts +++ b/apps/web/src/composables/contracts.ts @@ -211,6 +211,8 @@ export default function useContracts() { return acc }, {} as { [key: string]: number }) + console.log('userEventTotals :>> ', userEventTotals) + for (const item of items) { for (const action of item) { const { args, event } = action diff --git a/apps/web/src/composables/ethers.ts b/apps/web/src/composables/ethers.ts index 9c1497924..520eaf89b 100644 --- a/apps/web/src/composables/ethers.ts +++ b/apps/web/src/composables/ethers.ts @@ -9,7 +9,7 @@ import useUsers from '@/composables/users' const { createSiweMessage, signInWithEthereum } = useAuth() const { ethereumURL } = useEnvironment() -const { getUserContractEventsTotals, setUserContractTotals } = useContracts() +const { manager, getUserContractEventsTotals, setUserContractTotals } = useContracts() const { user } = useUsers() export default function useEthers() { @@ -183,6 +183,16 @@ export default function useEthers() { }) } + async function listenForContractEvents() { + manager.on('StakeDeposited', async (event: any) => { + console.log('got to StakeDeposited!') + }) + + manager.on('StakeRebalanced', async (event: any) => { + console.log('got to StakeRebalanced!') + }) + } + async function loginWithEthers(loginCredentials: LoginCredentials): Promise{ const { provider, address, currency } = loginCredentials const browserProvider = getBrowserProvider(provider) @@ -279,6 +289,7 @@ export default function useEthers() { getEthersBrowserSigner, getEthersBrowserProviderSelectedCurrency, getGasPriceAndLimit, + listenForContractEvents, listenForTransactions, loginWithEthers, requestEthersAccount, diff --git a/apps/web/src/composables/wallet.ts b/apps/web/src/composables/wallet.ts index ac47957c6..6d29db34f 100644 --- a/apps/web/src/composables/wallet.ts +++ b/apps/web/src/composables/wallet.ts @@ -36,7 +36,7 @@ const toAddress = ref('0x728474D29c2F81eb17a669a7582A2C17f1042b57') export default function useWallet() { const { getUserStakeBalance, getUserContractEventsTotals, setUserContractTotals } = useContracts() - const { estimateEIP1559GasFee, ethersProviderList, getEthersAddressWithBalance, getEthersBalance, sendEthersTransaction, signEthersMessage, listenForTransactions, loginWithEthers, getEthersBrowserProviderSelectedCurrency, switchEthersNetwork } = useEthers() + const { estimateEIP1559GasFee, ethersProviderList, getEthersAddressWithBalance, getEthersBalance, sendEthersTransaction, signEthersMessage, listenForContractEvents, listenForTransactions, loginWithEthers, getEthersBrowserProviderSelectedCurrency, switchEthersNetwork } = useEthers() const { getLedgerAddress, loginWithLedger, sendLedgerTransaction, signLedgerMessage } = useLedger() const { solanaProviderList, sendSolanaTransaction, signSolanaMessage } = useSolana() const { getTrezorAddress, loginWithTrezor, sendTrezorTransaction, signTrezorMessage } = useTrezor() @@ -74,6 +74,7 @@ export default function useWallet() { if (error) throw new Error(message || 'There was an error getting the user') setUser(retrievedUser) setPrimaryAddress(user?.value?.address as string) + listenForContractEvents() listenForTransactions() loadingUserWallets.value = false } else { // Add account if it doesn't already exist diff --git a/apps/web/src/pages/overview/components/Staking.vue b/apps/web/src/pages/overview/components/Staking.vue index 609694dfe..9698d766c 100644 --- a/apps/web/src/pages/overview/components/Staking.vue +++ b/apps/web/src/pages/overview/components/Staking.vue @@ -3,16 +3,14 @@ import { ref, onMounted, onUnmounted, watch } from 'vue' import { FormattedWalletOption, ProviderString } from '@casimir/types' import VueFeather from 'vue-feather' import useEthers from '@/composables/ethers' -import usePrice from '@/composables/price' import useUsers from '@/composables/users' import useContracts from '@/composables/contracts' import TermsOfService from '@/components/TermsOfService.vue' const { getEthersBalance } = useEthers() -const { getCurrentPrice } = usePrice() const { user } = useUsers() -const { deposit } = useContracts() +const { deposit, withdraw } = useContracts() const selectedProvider = ref('') const selectedWallet = ref(null as null | string) @@ -302,6 +300,13 @@ onUnmounted(() =>{ > Stake +