Skip to content

Commit

Permalink
Create operator data schema, pg table, and aggregate on getUser queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ccali11 committed Aug 1, 2023
1 parent 71dabdc commit 4646096
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 64 deletions.
8 changes: 4 additions & 4 deletions apps/web/src/composables/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import usePrice from '@/composables/price'
import useTrezor from '@/composables/trezor'
import useUsers from '@/composables/users'
import useWalletConnect from './walletConnect'
import { Account, BreakdownAmount, BreakdownString, ContractEventsByAddress, Pool, ProviderString, UserWithAccounts } from '@casimir/types'
import { Account, BreakdownAmount, BreakdownString, ContractEventsByAddress, Pool, ProviderString, UserWithAccountsAndOperators } from '@casimir/types'

const currentStaked = ref<BreakdownAmount>({
usd: '$0.00',
Expand Down Expand Up @@ -72,7 +72,7 @@ export default function useContracts() {

async function getCurrentStaked(): Promise<BreakdownAmount> {
const provider = new ethers.providers.JsonRpcProvider(ethereumUrl)
const addresses = (user.value as UserWithAccounts).accounts.map((account: Account) => account.address) as string[]
const addresses = (user.value as UserWithAccountsAndOperators).accounts.map((account: Account) => account.address) as string[]
try {
const promises = addresses.map((address) => manager.connect(provider).getUserStake(address))
const settledPromises = await Promise.allSettled(promises) as Array<PromiseFulfilledResult<ethers.BigNumber>>
Expand Down Expand Up @@ -193,7 +193,7 @@ export default function useContracts() {
async function getAllTimeStakingRewards() : Promise<BreakdownAmount> {
try {
/* Get User's Current Stake */
const addresses = (user.value as UserWithAccounts).accounts.map((account: Account) => account.address) as string[]
const addresses = (user.value as UserWithAccountsAndOperators).accounts.map((account: Account) => account.address) as string[]
const currentUserStakePromises = [] as Array<Promise<ethers.BigNumber>>
addresses.forEach(address => currentUserStakePromises.push(manager.connect(provider).getUserStake(address)))
const settledCurrentUserStakePromises = await Promise.allSettled(currentUserStakePromises) as Array<PromiseFulfilledResult<ethers.BigNumber>>
Expand Down Expand Up @@ -234,7 +234,7 @@ export default function useContracts() {

async function getTotalWalletBalance() : Promise<BreakdownAmount> {
const promises = [] as Array<Promise<any>>
const addresses = (user.value as UserWithAccounts).accounts.map((account: Account) => account.address) as string[]
const addresses = (user.value as UserWithAccountsAndOperators).accounts.map((account: Account) => account.address) as string[]
addresses.forEach((address) => { promises.push(getEthersBalance(address)) })
const totalWalletBalance = (await Promise.all(promises)).reduce((acc, curr) => acc + curr, 0)
const totalWalletBalanceUSD = totalWalletBalance * (await getCurrentPrice({ coin: 'ETH', currency: 'USD' }))
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/composables/ethers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ethers } from 'ethers'
import { EthersProvider } from '@casimir/types'
import { Account, TransactionRequest, UserWithAccounts } from '@casimir/types'
import { Account, TransactionRequest, UserWithAccountsAndOperators } from '@casimir/types'
import { GasEstimate, LoginCredentials, MessageRequest, ProviderString } from '@casimir/types'
import useAuth from '@/composables/auth'
import useContracts from '@/composables/contracts'
Expand Down Expand Up @@ -35,7 +35,7 @@ export default function useEthers() {
const { manager, refreshBreakdown } = useContracts()
const { user } = useUsers()
console.log('blockNumber :>> ', blockNumber)
const addresses = (user.value as UserWithAccounts).accounts.map((account: Account) => account.address) as string[]
const addresses = (user.value as UserWithAccountsAndOperators).accounts.map((account: Account) => account.address) as string[]
const block = await provider.getBlockWithTransactions(blockNumber)
const transactions = block.transactions
transactions.map(async (tx) => {
Expand Down
19 changes: 12 additions & 7 deletions apps/web/src/composables/users.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ref, readonly } from 'vue'
import { Account, AddAccountOptions, ProviderString, RemoveAccountOptions, UserWithAccounts, ApiResponse, UserAnalyticsData } from '@casimir/types'
import { Account, AddAccountOptions, ProviderString, RemoveAccountOptions, UserWithAccountsAndOperators, ApiResponse, UserAnalyticsData } from '@casimir/types'
import useEnvironment from '@/composables/environment'
import useEthers from './ethers'
import * as Session from 'supertokens-web-js/recipe/session'
Expand All @@ -10,7 +10,7 @@ const { usersUrl } = useEnvironment()

// 0xd557a5745d4560B24D36A68b52351ffF9c86A212
const session = ref<boolean>(false)
const user = ref<UserWithAccounts | undefined>(undefined)
const user = ref<UserWithAccountsAndOperators | undefined>(undefined)
const userAnalytics = ref<UserAnalyticsData>({
oneMonth: {
labels: [],
Expand Down Expand Up @@ -236,12 +236,17 @@ export default function useUsers() {
'Content-Type': 'application/json'
}
}
const response = await fetch(`${usersUrl}/analytics`, requestOptions)
const { error, message, data } = await response.json()
// TODO: Re-enable this when athena is ready
// const response = await fetch(`${usersUrl}/analytics`, requestOptions)
// const { error, message, data } = await response.json()
const error = false
const message = 'User analytics found'
const data = txData.value

if (error) throw new Error(message)

// TODO: Pass data from above when the API / data is ready
setRawAnalytics(txData.value)
setRawAnalytics(data)
computeUserAnalytics()
return { error, message, data }
} catch (error: any) {
Expand Down Expand Up @@ -297,8 +302,8 @@ export default function useUsers() {
return { error: false, message: `Account removed from user: ${userAccount}`, data: userAccount }
}

function setUser(newUser?: UserWithAccounts) {
user.value = newUser as UserWithAccounts
function setUser(newUser?: UserWithAccountsAndOperators) {
user.value = newUser as UserWithAccountsAndOperators
userAddresses.value = newUser?.accounts.map(account => account.address) as Array<string>
}

Expand Down
22 changes: 12 additions & 10 deletions common/data/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import accountSchema from './schemas/account.schema.json'
import nonceSchema from './schemas/nonce.schema.json'
import userSchema from './schemas/user.schema.json'
import eventSchema from './schemas/event.schema.json'
import accountStore from './mock/account.store.json'
import actionSchema from './schemas/action.schema.json'
import userAccountSchema from './schemas/user_account.schema.json'
import eventSchema from './schemas/event.schema.json'
import nonceSchema from './schemas/nonce.schema.json'
import operatorSchema from './schemas/operator.schema.json'
import operatorStore from './mock/operator.store.json'
import accountStore from './mock/account.store.json'
import userAccountSchema from './schemas/user_account.schema.json'
import userSchema from './schemas/user.schema.json'
import userStore from './mock/user.store.json'
import { Postgres } from '../../../services/users/src/providers/postgres'
import { JsonType, GlueType, PostgresType, Schema } from './providers/schema'
import { JsonSchema } from './interfaces/JsonSchema'

export {
accountSchema,
accountStore,
actionSchema,
eventSchema,
nonceSchema,
userSchema,
operatorSchema,
operatorStore,
userAccountSchema,
eventSchema,
actionSchema,
accountStore,
userSchema,
userStore,
operatorStore,
Postgres,
Schema
}
Expand Down
35 changes: 35 additions & 0 deletions common/data/src/schemas/operator.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"$id": "https://casimir.co/operator.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Operator",
"type": "object",
"properties": {
"account_id": {
"type": "integer",
"description": "The ID of the account the operator belongs to (FK)"
},
"id": {
"type": "serial",
"description": "The operator ID (PK)"
},
"user_id": {
"type": "integer",
"description": "The ID of the user who is an operator (FK)"
},
"created_at": {
"type": "string",
"description": "The operator creation date in ISO 8601 format"
},
"updated_at": {
"type": "string",
"description": "The operator last update date in ISO 8601 format"
}
},
"required": [
"account_id",
"id",
"user_id",
"created_at",
"updated_at"
]
}
12 changes: 7 additions & 5 deletions common/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Account } from './interfaces/Account'
import { AccountWithStakingInfo } from './interfaces/AccountWithStakingInfo'
import { AccountWithStakingAndOperatorInfo } from './interfaces/AccountWithStakingAndOperatorInfo'
import { AddAccountOptions } from './interfaces/AddAccountOptions'
import { AnalyticsData } from './interfaces/AnalyticsData'
import { ApiResponse } from './interfaces/ApiResponse'
Expand All @@ -22,20 +22,21 @@ import { GasEstimate } from './interfaces/GasEstimate'
import { LoginCredentials } from './interfaces/LoginCredentials'
import { MessageRequest } from './interfaces/MessageRequest'
import { Operator } from './interfaces/Operator'
import { OperatorAddedSuccess } from './interfaces/OperatorAddedSuccess'
import { Pool } from './interfaces/Pool'
import { PoolStatus } from './interfaces/PoolStatus'
import { ProviderString } from './interfaces/ProviderString'
import { TransactionRequest } from './interfaces/TransactionRequest'
import { RemoveAccountOptions } from './interfaces/RemoveAccountOptions'
import { User } from './interfaces/User'
import { UserAddedSuccess } from './interfaces/UserAddedSuccess'
import { UserWithAccounts } from './interfaces/UserWithAccounts'
import { UserWithAccountsAndOperators } from './interfaces/UserWithAccountsAndOperators'
import { UserAnalyticsData } from './interfaces/UserAnalyticsData'
import { Validator } from './interfaces/Validator'

export type {
Account,
AccountWithStakingInfo,
AccountWithStakingAndOperatorInfo,
ApiResponse,
AddAccountOptions,
AnalyticsData,
Expand All @@ -57,15 +58,16 @@ export type {
GasEstimate,
LoginCredentials,
MessageRequest,
Operator,
Operator,
OperatorAddedSuccess,
Pool,
ProviderString,
TransactionRequest,
RemoveAccountOptions,
User,
UserAddedSuccess,
UserAnalyticsData,
UserWithAccounts,
UserWithAccountsAndOperators,
Validator,
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Account, Pool } from '@casimir/types'
import { Account, Operator, Pool } from '@casimir/types'

export interface AccountWithStakingInfo extends Account {
export interface AccountWithStakingAndOperatorInfo extends Account {
/** The user's current staking pools and details (this interface/logic is in the web app wallet composable, but it will be moved to the processor, see https://github.com/consensusnetworks/casimir/blob/master/apps/web/src/composables/wallet.ts#L146) */
pools?: Pool[]
/** The total amount of stake rewards available to withdraw (ignore for now, see note on Account.pools) */
Expand All @@ -9,4 +9,6 @@ export interface AccountWithStakingInfo extends Account {
roi?: number
/** The total amount currently staked (ignore for now, see note on Account.pools) */
stake?: string
/** Operators associated with a given account address */
operators?: Operator[]
}
32 changes: 4 additions & 28 deletions common/types/src/interfaces/Operator.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,7 @@
export interface Operator {
accountId: number
id: number
id_str: string
declared_fee: number
previous_fee: number
fee: number // Todo check if this is bigint
name: string
public_key: string
owner_address: string
address: string
location: string
setup_provider: string
eth1_node_client: string
eth2_node_client: string
description: string
website_url: string
twitter_url: string
linkedin_url: string
logo: string
type: string
performance: {
'24h': number
'30d': number
},
is_active: number
is_valid: boolean
is_deleted: boolean
status: string // Todo check for enum (i.e., 'Active'...)
validators_count: number
url: string // Todo check if this is usable in DKG
userId: number
createdAt: string
updatedAt: string
}
7 changes: 7 additions & 0 deletions common/types/src/interfaces/OperatorAddedSuccess.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface OperatorAddedSuccess {
id: number
account_id: number
created_at: string
updated_at: string
user_id: number
}
30 changes: 30 additions & 0 deletions common/types/src/interfaces/OperatorDetails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export interface OperatorDetails {
id_str: string
declared_fee: number
previous_fee: number
fee: number // Todo check if this is bigint
name: string
public_key: string
owner_address: string
address: string
location: string
setup_provider: string
eth1_node_client: string
eth2_node_client: string
description: string
website_url: string
twitter_url: string
linkedin_url: string
logo: string
type: string
performance: {
'24h': number
'30d': number
},
is_active: number
is_valid: boolean
is_deleted: boolean
status: string // Todo check for enum (i.e., 'Active'...)
validators_count: number
url: string // Todo check if this is usable in DKG
}
6 changes: 0 additions & 6 deletions common/types/src/interfaces/UserWithAccounts.ts

This file was deleted.

8 changes: 8 additions & 0 deletions common/types/src/interfaces/UserWithAccountsAndOperators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { AccountWithStakingAndOperatorInfo, Operator, User } from '@casimir/types'

export interface UserWithAccountsAndOperators extends User {
/** An array of the user's accounts */
accounts: AccountWithStakingAndOperatorInfo[]
/** An array of the user's operators */
operators?: Operator[]
}

0 comments on commit 4646096

Please sign in to comment.