-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: KayleCoder <kalecoder@outlook.com>
- Loading branch information
1 parent
f5920ca
commit 3e25b79
Showing
12 changed files
with
822 additions
and
28 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,12 @@ | ||
import axios from "axios"; | ||
import {CONFIGS} from "../config"; | ||
const rpcUrl = CONFIGS.chain.rpcUrl as string; | ||
export async function queryTxByHash(hash: string) { | ||
const result = await axios.post(rpcUrl, { | ||
method: "eth_getTransactionByHash", | ||
params: [hash], | ||
id: 1, | ||
jsonrpc: "2.0" | ||
}); | ||
return result; | ||
} |
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,23 @@ | ||
export const CONFIGS = { | ||
common: { | ||
env: getEnv('ENV', 'dev'), | ||
}, | ||
server: { | ||
port: getEnv('PORT', 3000) | ||
}, | ||
db: { | ||
host: getEnv('DB_HOST', 'localhost'), | ||
port: getEnv('DB_PORT', 15432), | ||
database: 'blobscan_dev', | ||
user: getEnv('DB_USER', 'ethda'), | ||
password: getEnv('DB_PASSWORD', ''), | ||
}, | ||
chain: { | ||
rpcUrl: getEnv('RPC_URL', 'https://rpc-devnet.ethda.io') | ||
} | ||
} | ||
|
||
export function getEnv(key: string, defaultValue: string | number): string | number { | ||
const result = process.env[key] || defaultValue; | ||
return typeof defaultValue === 'string' ? result : Number(result); | ||
} |
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,6 @@ | ||
import {Sequelize} from "sequelize"; | ||
import {CONFIGS} from "../config"; | ||
|
||
export const sequelize = new Sequelize(`postgresql://${CONFIGS.db.user}:${CONFIGS.db.password}@${CONFIGS.db.host}:${CONFIGS.db.port}/${CONFIGS.db.database}?schema=public`, { | ||
logging: CONFIGS.common.env === 'dev' | ||
}); |
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
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,32 @@ | ||
import {Transaction} from "../model/Transcation"; | ||
import * as _ from "lodash"; | ||
import {queryTxByHash} from "../chain"; | ||
import {sleep} from "../util/commonUtils"; | ||
import {HttpStatusCode} from "axios"; | ||
|
||
export async function indexTransactions() { | ||
while (true) { | ||
const transactions = await Transaction.findAll({ | ||
where: { | ||
re_indexed: 1 | ||
}, | ||
limit: 100 | ||
}); | ||
if (!_.isEmpty(transactions)) { | ||
for (const t of transactions) { | ||
const result = await queryTxByHash(t.getDataValue('hash')); | ||
if (result.status === HttpStatusCode.Ok) { | ||
await Transaction.update({ | ||
input_data: result.data.result.input, | ||
re_indexed: 0, | ||
}, { | ||
where: { | ||
hash: t.getDataValue('hash') | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
await sleep(3 * 1000); | ||
} | ||
} |
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,12 @@ | ||
import {sequelize} from "../db"; | ||
import {DataTypes} from "sequelize"; | ||
|
||
export const BlobsOnTransactions = sequelize.define("blobs_on_transactions", | ||
{ | ||
blob_hash: { type: DataTypes.STRING}, | ||
tx_hash: { type: DataTypes.STRING}, | ||
index: { type: DataTypes.INTEGER}, | ||
}, { | ||
timestamps: false, | ||
freezeTableName: true | ||
}); |
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,12 @@ | ||
import {sequelize} from "../db"; | ||
import {DataTypes} from "sequelize"; | ||
|
||
export const Transaction = sequelize.define("transaction", | ||
{ | ||
hash: { type: DataTypes.STRING, primaryKey: true}, | ||
input_data: { type: DataTypes.STRING}, | ||
re_indexed: { type: DataTypes.INTEGER}, | ||
}, { | ||
timestamps: false, | ||
freezeTableName: true | ||
}); |
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,7 @@ | ||
export function sleep(time: number) { | ||
return new Promise<void>((resolve, reject) => { | ||
setTimeout(() => { | ||
resolve(); | ||
}, time); | ||
}); | ||
} |
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,110 @@ | ||
import {QueryTypes, Sequelize, Transaction} from 'sequelize'; | ||
|
||
import * as _ from 'lodash'; | ||
import {sequelize} from "../db"; | ||
|
||
export function queryForCount( | ||
sql: string, | ||
replace: any[], | ||
transaction?: Transaction | ||
): Promise<number> { | ||
return sequelize | ||
.query(sql, { | ||
replacements: replace, | ||
type: QueryTypes.SELECT, | ||
raw: true, | ||
transaction, | ||
}) | ||
.then((r: any[]) => { | ||
if (!_.isEmpty(r)) { | ||
const res = r[0]; | ||
return res[Object.keys(res)[0]]; | ||
} | ||
}); | ||
} | ||
|
||
export function queryForArray( | ||
sql: string, | ||
replace: any[], | ||
transaction?: Transaction | ||
): Promise<any[]> { | ||
return sequelize | ||
.query(sql, { | ||
replacements: replace, | ||
type: QueryTypes.SELECT, | ||
transaction, | ||
}) | ||
.then((r: any[]) => { | ||
if (!_.isEmpty(r)) { | ||
return r; | ||
} | ||
return []; | ||
}); | ||
} | ||
|
||
export function queryForObj( | ||
sql: string, | ||
replace: any[], | ||
transaction?: Transaction | ||
): Promise<any> { | ||
return sequelize | ||
.query(sql, { | ||
replacements: replace, | ||
type: QueryTypes.SELECT, | ||
transaction, | ||
}) | ||
.then((r: any[]) => { | ||
if (!_.isEmpty(r)) { | ||
return r[0]; | ||
} | ||
return {}; | ||
}); | ||
} | ||
|
||
export function queryForUpdate( | ||
sql: string, | ||
replace: any[], | ||
transaction?: Transaction | ||
): Promise<number> { | ||
return sequelize | ||
.query(sql, { | ||
replacements: replace, | ||
type: QueryTypes.UPDATE, | ||
transaction, | ||
}) | ||
.then((r: any) => { | ||
return 0; | ||
}); | ||
} | ||
|
||
export function queryForInsert( | ||
sql: string, | ||
replace: any[], | ||
transaction?: Transaction | ||
): Promise<number> { | ||
return sequelize | ||
.query(sql, { | ||
replacements: replace, | ||
type: QueryTypes.INSERT, | ||
transaction, | ||
}) | ||
.then((r: any) => { | ||
return 0; | ||
}); | ||
} | ||
|
||
export function queryForDelete( | ||
sql: string, | ||
replace: any[], | ||
transaction?: Transaction | ||
): Promise<number> { | ||
return sequelize | ||
.query(sql, { | ||
replacements: replace, | ||
type: QueryTypes.DELETE, | ||
transaction, | ||
}) | ||
.then((r: any) => { | ||
return 0; | ||
}); | ||
} |
Oops, something went wrong.