Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add action schema to track wallet and stake events #377

Merged
merged 4 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions common/data/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ 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 walletSchema from './schemas/wallet.schema.json'
import stakingActionSchema from './schemas/staking_action.schema.json'
import actionSchema from './schemas/action.schema.json'
import userAccountSchema from './schemas/user_account.schema.json'
import operatorStore from './mock/operator.store.json'
import validatorStore from './mock/validator.store.json'
Expand All @@ -19,8 +18,7 @@ export {
userSchema,
userAccountSchema,
eventSchema,
walletSchema,
stakingActionSchema,
actionSchema,
accountStore,
userStore,
operatorStore,
Expand Down
61 changes: 61 additions & 0 deletions common/data/src/schemas/action.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"$id": "https://casimir.co/agg.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"$comment": "analytics",
"title": "Action",
"type": "object",
"properties": {
"chain": {
"type": "string",
"description": "The chain which the event belongs to (e.g. iotex, ethereum)"
},
"network": {
"type": "string",
"description": "Network type (e.g. mainnet, testnet)"
},
"type": {
"type": "string",
"description": "Type of the event (e.g. wallet, stake)"
},
"action": {
"type": "string",
"description": "For wallet (e.g. received or sent) and for stake (e.g. stake_deposited, stake_rebalanced, withdrawal_initiated, withdrawal_fulfilled)"
},
"address": {
"type": "string",
"description": "Wallet address"
},
"amount": {
"type": "string",
"description": "Recorded amount depending on the event type"
},
"balance": {
"type": "string",
"description": "Wallet balance or the staked amount"
},
"gas": {
"type": "integer",
"description": "Gas used for the transaction"
},
"hash": {
"type": "string",
"description": "Transaction hash"
},
"price": {
"type": "string",
"description": "The exchange price of the coin at the time of the event"
},
"received_at": {
"type": "integer",
"description": "Timestamp of the event in unix format"
},
"rewards_all_time": {
"type": "string",
"description": "Total rewards earned"
},
"staking_fees": {
"type": "string",
"description": "Total staking fees"
}
}
}
9 changes: 5 additions & 4 deletions common/data/src/schemas/event.schema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"$id": "https://casimir.co/event.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"$comment": "analytics",
"title": "Event",
"type": "object",
"properties": {
Expand All @@ -10,7 +11,7 @@
},
"network": {
"type": "string",
"description": "The network which the event was received on (e.g. mainnet, testnet)"
"description": "Network type (e.g. mainnet, testnet)"
},
"provider": {
"type": "string",
Expand All @@ -33,8 +34,8 @@
"description": "The transaction hash"
},
"received_at": {
"type": "string",
"description": "The timestamp of the event recieved by the blockchain (format: Modified ISO 8601 e.g. 2015-03-04 22:44:30.652)"
"type": "integer",
"description": "Timestamp of the event in unix format"
},
"sender": {
"type": "string",
Expand All @@ -61,7 +62,7 @@
"description": "The exchange price of the coin at the time of the event"
},
"gas_fee": {
"type": "string",
"type": "integer",
"description": "The gas fee paid for the transaction"
}
}
Expand Down
32 changes: 0 additions & 32 deletions common/data/src/schemas/staking_action.schema.json

This file was deleted.

40 changes: 0 additions & 40 deletions common/data/src/schemas/wallet.schema.json

This file was deleted.

29 changes: 10 additions & 19 deletions infrastructure/cdk/src/providers/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Construct } from 'constructs'
import * as cdk from 'aws-cdk-lib'
import * as s3 from 'aws-cdk-lib/aws-s3'
import * as glue from '@aws-cdk/aws-glue-alpha'
import { Schema, eventSchema, walletSchema, stakingActionSchema } from '@casimir/data'
import { Schema, eventSchema, actionSchema } from '@casimir/data'
import { kebabCase, pascalCase, snakeCase } from '@casimir/helpers'
import { Config } from './config'
import { AnalyticsStackProps } from '../interfaces/StackProps'
Expand All @@ -21,8 +21,7 @@ export class AnalyticsStack extends cdk.Stack {

/** Get Glue Columns from JSON Schema for each table */
const eventColumns = new Schema(eventSchema).getGlueColumns()
const walletColumns = new Schema(walletSchema).getGlueColumns()
const stakingActionColumns = new Schema(stakingActionSchema).getGlueColumns()
const actionColumns = new Schema(actionSchema).getGlueColumns()

/** Create Glue DB */
const database = new glue.Database(this, config.getFullStackResourceName(this.name, 'database'), {
Expand All @@ -33,12 +32,11 @@ export class AnalyticsStack extends cdk.Stack {
const eventBucket = new s3.Bucket(this, config.getFullStackResourceName(this.name, 'event-bucket', config.dataVersion), {
bucketName: kebabCase(config.getFullStackResourceName(this.name, 'event-bucket', config.dataVersion)),
})
const walletBucket = new s3.Bucket(this, config.getFullStackResourceName(this.name, 'wallet-bucket', config.dataVersion), {
bucketName: kebabCase(config.getFullStackResourceName(this.name, 'wallet-bucket', config.dataVersion)),
})
const stakingActionBucket = new s3.Bucket(this, config.getFullStackResourceName(this.name, 'staking-action-bucket', config.dataVersion), {
bucketName: kebabCase(config.getFullStackResourceName(this.name, 'staking-action-bucket', config.dataVersion)),

const actionBucket = new s3.Bucket(this, config.getFullStackResourceName(this.name, 'action-bucket', config.dataVersion), {
bucketName: kebabCase(config.getFullStackResourceName(this.name, 'action-bucket', config.dataVersion)),
})

new s3.Bucket(this, config.getFullStackResourceName(this.name, 'output-bucket', config.dataVersion))

/** Create Glue tables */
Expand All @@ -49,18 +47,11 @@ export class AnalyticsStack extends cdk.Stack {
columns: eventColumns,
dataFormat: glue.DataFormat.JSON,
})
new glue.Table(this, config.getFullStackResourceName(this.name, 'wallet-table', config.dataVersion), {
database: database,
tableName: snakeCase(config.getFullStackResourceName(this.name, 'wallet-table', config.dataVersion)),
bucket: walletBucket,
columns: walletColumns,
dataFormat: glue.DataFormat.JSON,
})
new glue.Table(this, config.getFullStackResourceName(this.name, 'staking-action-table', config.dataVersion), {
new glue.Table(this, config.getFullStackResourceName(this.name, 'action-table', config.dataVersion), {
database: database,
tableName: snakeCase(config.getFullStackResourceName(this.name, 'staking-action-table', config.dataVersion)),
bucket: stakingActionBucket,
columns: stakingActionColumns,
tableName: snakeCase(config.getFullStackResourceName(this.name, 'action-table', config.dataVersion)),
bucket: actionBucket,
columns: actionColumns,
dataFormat: glue.DataFormat.JSON,
})
}
Expand Down
27 changes: 7 additions & 20 deletions infrastructure/cdk/test/analytics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as cdk from 'aws-cdk-lib'
import * as assertions from 'aws-cdk-lib/assertions'
import { Config } from '../src/providers/config'
import { AnalyticsStack } from '../src/providers/analytics'
import { Schema, eventSchema, walletSchema, stakingActionSchema } from '@casimir/data'
import { Schema, eventSchema, actionSchema } from '@casimir/data'

test('Analytics stack created', () => {
const config = new Config()
Expand All @@ -27,28 +27,15 @@ test('Analytics stack created', () => {
expect(columnName).toEqual(name)
}

const walletTable = Object.keys(resource).filter(key => key.includes('WalletTable'))[0]
const walletColumns = resource[walletTable].Properties.TableInput.StorageDescriptor.Columns
const walletGlueSchema = new Schema(walletSchema).getGlueColumns()
const actionTable = Object.keys(resource).filter(key => key.includes('ActionTable'))[0]
const actionColumns = resource[actionTable].Properties.TableInput.StorageDescriptor.Columns
const actionGlueSchema = new Schema(actionSchema).getGlueColumns()


for (const column of walletColumns) {
for (const column of actionColumns) {
const { Name: name, Type: type } = column
const columnName = Object.keys(walletSchema.properties).filter(key => key === name)[0]
const columnType = walletGlueSchema.filter(key => key.name === name)[0].type.inputString

expect(columnType).toEqual(type)
expect(columnName).toEqual(name)
}

const stakingActionTable = Object.keys(resource).filter(key => key.includes('StakingActionTable'))[0]
const stakingActionColumns = resource[stakingActionTable].Properties.TableInput.StorageDescriptor.Columns
const stakingActionGlueSchema = new Schema(stakingActionSchema).getGlueColumns()

for (const column of stakingActionColumns) {
const { Name: name, Type: type } = column
const columnName = Object.keys(stakingActionSchema.properties).filter(key => key === name)[0]
const columnType = stakingActionGlueSchema.filter(key => key.name === name)[0].type.inputString
const columnName = Object.keys(actionSchema.properties).filter(key => key === name)[0]
const columnType = actionGlueSchema.filter(key => key.name === name)[0].type.inputString

expect(columnType).toEqual(type)
expect(columnName).toEqual(name)
Expand Down