Skip to content

Commit

Permalink
Create Blockchain Model.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 11, 2024
1 parent 77b6a7a commit 84f585b
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions pi-nexus-blockchain/models/Blockchain Model.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 84f585b

Please sign in to comment.