Skip to content

Commit

Permalink
CurrentStaked (Breakdown) being updated on new block
Browse files Browse the repository at this point in the history
  • Loading branch information
ccali11 committed Jun 27, 2023
1 parent b3ff7f8 commit e057860
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 36 deletions.
66 changes: 42 additions & 24 deletions apps/web/src/composables/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { ref, watch } from 'vue'
import { ref } from 'vue'
import { ethers } from 'ethers'
import { CasimirManager, CasimirViews } from '@casimir/ethereum/build/artifacts/types'
import CasimirManagerJson from '@casimir/ethereum/build/artifacts/src/v1/CasimirManager.sol/CasimirManager.json'
import CasimirViewsJson from '@casimir/ethereum/build/artifacts/src/v1/CasimirManager.sol/CasimirManager.json'
import useEnvironment from './environment'
import useUsers from './users'
import useEthers from './ethers'
import useLedger from './ledger'
import usePrice from '@/composables/price'
Expand All @@ -13,8 +12,6 @@ import useWalletConnect from './walletConnect'
import { Account, BreakdownAmount, Pool, ProviderString } from '@casimir/types'
import { ReadyOrStakeString } from '@/interfaces/ReadyOrStakeString'

const { user } = useUsers()

/** Manager contract */
const managerAddress = import.meta.env.PUBLIC_MANAGER_ADDRESS
const provider = new ethers.providers.JsonRpcProvider(import.meta.env.VITE_RPC_URL)
Expand All @@ -26,27 +23,27 @@ const views: CasimirViews = new ethers.Contract(viewsAddress, CasimirViewsJson.a

const { getCurrentPrice } = usePrice()

const currentStaked = ref<BreakdownAmount>({
usd: '$0.00',
exchange: '0 ETH'
})

const stakingRewards = ref({
usd: '$0.00',
exchange: '0 ETH'
})

const totalDeposited = ref({
usd: '$0.00',
exchange: '0 ETH'
})

export default function useContracts() {
const { ethereumURL } = useEnvironment()
const { ethersProviderList, getEthersBrowserSigner, listenForTransactions } = useEthers()
const { ethersProviderList, getEthersBrowserSigner } = useEthers()
const { getEthersLedgerSigner } = useLedger()
const { getEthersTrezorSigner } = useTrezor()
const { isWalletConnectSigner, getEthersWalletConnectSigner } = useWalletConnect()

const currentStaked = ref<BreakdownAmount>({
usd: '$0.00',
exchange: '0 ETH'
})

const stakingRewards = ref({
usd: '$0.00',
exchange: '0 ETH'
})

const totalDeposited = ref({
usd: '$0.00',
exchange: '0 ETH'
})

// watch(user, async () => {
// const promises = [] as any[]
Expand Down Expand Up @@ -223,12 +220,33 @@ export default function useContracts() {
}
}

currentStaked.value.exchange = '$ ' + (userEventTotals.StakeDeposited - userEventTotals.WithdrawalInitiated).toString()
currentStaked.value.usd = '$ ' + parseInt(currentStaked.value.exchange) * (await getCurrentPrice({ coin: 'ETH', currency: 'USD' }))
console.log('currentStaked in getUserContractEventsTotals :>> ', currentStaked)
return userEventTotals
}

async function setUserContractTotals(eventTotals: any) {
const exchangeCurrentStaked = eventTotals.StakeDeposited - eventTotals.WithdrawalInitiated
const usdCurrentStaked = exchangeCurrentStaked * (await getCurrentPrice({ coin: 'ETH', currency: 'USD' }))
const exchangeCurrentStakedRounded = Math.round(exchangeCurrentStaked * 100) / 100
const usdCurrentStakedRounded = Math.round(usdCurrentStaked * 100) / 100
currentStaked.value = {
exchange: exchangeCurrentStakedRounded.toString() + ' ETH',
usd: '$ ' + usdCurrentStakedRounded
}
}

// TODO: Add listener / subscription "StakeRebalanced(uint256 amount)" (to composable somewhere)

return { currentStaked, stakingRewards, totalDeposited, manager, deposit, getDepositFees, getPools, getUserContractEventsTotals, getUserStakeBalance, withdraw }
return {
currentStaked,
manager,
stakingRewards,
totalDeposited,
deposit,
getDepositFees,
getPools,
getUserContractEventsTotals,
getUserStakeBalance,
setUserContractTotals,
withdraw
}
}
19 changes: 7 additions & 12 deletions 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 } = useContracts()
const { getUserContractEventsTotals, setUserContractTotals } = useContracts()
const { user } = useUsers()

export default function useEthers() {
Expand Down Expand Up @@ -151,29 +151,24 @@ export default function useEthers() {
async function listenForTransactions() {
const provider = new ethers.providers.JsonRpcProvider(ethereumURL)
provider.on('block', async (blockNumber: number) => {
console.log('blockNumber :>> ', blockNumber)
const addresses = user.value?.accounts.map((account: Account) => account.address) as Array<string>
const block = await provider.getBlockWithTransactions(blockNumber)
const transactions = block.transactions
console.log('addresses :>> ', addresses)
console.log('transactions :>> ', transactions)
const promises = [] as Array<Promise<void>>
transactions.forEach(async (tx) => {
console.log('tx :>> ', tx)
if (addresses.includes(tx.from)) {
console.log('tx :>> ', tx)
const promises = [] as Array<Promise<any>>
transactions.map((tx) => {
if (addresses.includes(tx.from.toLowerCase())) {
promises.push(getUserContractEventsTotals(tx.from as string))
}
})
const result = await Promise.all(promises)
console.log('result :>> ', result)
const userEventTotals = await Promise.all(promises)
if (userEventTotals.length > 0) await setUserContractTotals(userEventTotals[0])
})
await new Promise(() => {
// Wait indefinitely using a Promise that never resolves
console.log('listening for blocks')
})
}


async function loginWithEthers(loginCredentials: LoginCredentials): Promise<void>{
const { provider, address, currency } = loginCredentials
const browserProvider = getBrowserProvider(provider)
Expand Down

0 comments on commit e057860

Please sign in to comment.