Skip to content

Commit

Permalink
Initiate listener for contract events when user logs in
Browse files Browse the repository at this point in the history
  • Loading branch information
ccali11 committed Jun 27, 2023
1 parent aa9978f commit 1bcefe9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
2 changes: 2 additions & 0 deletions apps/web/src/composables/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion apps/web/src/composables/ethers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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<void>{
const { provider, address, currency } = loginCredentials
const browserProvider = getBrowserProvider(provider)
Expand Down Expand Up @@ -279,6 +289,7 @@ export default function useEthers() {
getEthersBrowserSigner,
getEthersBrowserProviderSelectedCurrency,
getGasPriceAndLimit,
listenForContractEvents,
listenForTransactions,
loginWithEthers,
requestEthersAccount,
Expand Down
3 changes: 2 additions & 1 deletion apps/web/src/composables/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const toAddress = ref<string>('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()
Expand Down Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions apps/web/src/pages/overview/components/Staking.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProviderString>('')
const selectedWallet = ref(null as null | string)
Expand Down Expand Up @@ -302,6 +300,13 @@ onUnmounted(() =>{
>
Stake
</button>
<button
class="card_button h-[37px] w-full mt-8"
:disabled="!(selectedWallet && formattedAmountToStake && !errorMessage)"
@click="withdraw({ amount: formattedAmountToStake, walletProvider: selectedProvider })"
>
Withdraw
</button>

<div
v-show="openTermsOfService"
Expand Down

0 comments on commit 1bcefe9

Please sign in to comment.