-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |