From 84f585be9c10fd44f3481a71c209fbe16125d14e Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Sun, 11 Aug 2024 15:25:57 +0700 Subject: [PATCH] Create Blockchain Model.js --- .../models/Blockchain Model.js | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 pi-nexus-blockchain/models/Blockchain Model.js diff --git a/pi-nexus-blockchain/models/Blockchain Model.js b/pi-nexus-blockchain/models/Blockchain Model.js new file mode 100644 index 000000000..7b117389f --- /dev/null +++ b/pi-nexus-blockchain/models/Blockchain Model.js @@ -0,0 +1,47 @@ +import { Model, DataTypes } from 'sequelize'; +import { sequelize } from '../database'; +import { NexusModel } from './NexusModel'; + +class BlockchainModel extends Model { + static init(sequelize) { + super.init({ + id: { + type: DataTypes.UUID, + defaultValue: DataTypes.UUIDV4, + primaryKey: true, + }, + networkId: { + type: DataTypes.STRING, + allowNull: false, + }, + chainId: { + type: DataTypes.STRING, + allowNull: false, + }, + blockNumber: { + type: DataTypes.BIGINT, + allowNull: false, + defaultValue: 0, + }, + }, { + sequelize, + modelName: 'Blockchain', + tableName: 'blockchains', + timestamps: true, + underscored: true, + }); + } + + static associate(models) { + this.hasMany(models.Nexus, { foreignKey: 'blockchainId', onDelete: 'CASCADE' }); + } + + async updateBlockNumber() { + const web3 = new Web3(new Web3.providers.HttpProvider(`https://mainnet.infura.io/v3/YOUR_PROJECT_ID`)); + const blockNumber = await web3.eth.getBlockNumber(); + this.blockNumber = blockNumber; + await this.save(); + } +} + +export default BlockchainModel;