From 8aad787da9c88155f31790a78c95669ac044b5dc Mon Sep 17 00:00:00 2001 From: boneyard93501 <4523011+boneyard93501@users.noreply.github.com> Date: Wed, 29 Sep 2021 23:37:01 -0500 Subject: [PATCH] update db --- data/snapshot.db | Bin 0 -> 8192 bytes src/index.ts | 13 ++++++++--- src/local_db.ts | 59 ++++++++++++++++++++++++++++++++++------------- 3 files changed, 53 insertions(+), 19 deletions(-) diff --git a/data/snapshot.db b/data/snapshot.db index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..69bc9579fd441c4faad27bb62b174d92f8105c6e 100644 GIT binary patch literal 8192 zcmeI#%}N6?5C`z22!fz^^Hi9lf(YutqgL6A^rH5@r0!UQ-6SMqu~+epd>Y@z?9#1T zK|RZV*bpWko5}B%o2#J}raK>NBXq2`mDcKvNGYXr>*dx(ldpA2?wkC#(CXmyZ8NXZ zTdyjA2nav`0uX=z1Rwwb2tWV=5P-m61)eYU`cA*EUnXM4Q%=r=bnm6>dl%QmU{ug( za6T-k8>QV1T0}Qg+KR|J;R#0y(bgu;>4E3_%auH1C#9*X$SDyCKgsH3vJ+?IG4i)* z8%_?7%F5ra>R0uX=z1Rwwb2tWV=5P$##An=a`-T^VOZ7cu) literal 0 HcmV?d00001 diff --git a/src/index.ts b/src/index.ts index c2460c7..910d0d0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,10 @@ import { ethers } from "ethers"; import { TypedDataUtils } from 'ethers-eip712'; // https://github.com/0xsequence/ethers-eip712 import { eip_validation, Response } from "./eip_processor"; -import { get_db, crate_table, insert_event } from './local_db'; +import { get_db, create_table, insert_event } from './local_db'; + + + const DB_PATH = './data/snapshot.db'; @@ -47,8 +50,12 @@ async function main() { // return (resp_str, signed_response); - var db = get_db('./data/snapshot.db'); - // crate_table(db); + var db = await get_db(DB_PATH); + await create_table(db); + await insert_event(db, JSON.parse(eip712_doc), response, signed_response); + + + db.close(); } main(); diff --git a/src/local_db.ts b/src/local_db.ts index 91b4100..3e05745 100644 --- a/src/local_db.ts +++ b/src/local_db.ts @@ -12,7 +12,7 @@ export async function get_db(db_path: any) { db_path = ':memory'; } - let db = new sqlite3.Database(db_path, (err: any) => { + var db = new sqlite3.Database(db_path, (err: any) => { if (err) { return console.error("failure to get sqlite: ", err.message); } @@ -21,16 +21,16 @@ export async function get_db(db_path: any) { } -export async function crate_table(db: any) { - const stmt = `create table if not exists snapshot.db ( - snapshot_id: integer primary key, - event_address: text, - event_signature: text, +export async function create_table(db: any) { + const stmt = `create table if not exists snapshot ( + snapshot_id integer primary key, + event_address text, + event_signature text, eip712_doc text, - peer_id: text, - timestamp: integer; - eip_validation: boolean; - ts_validation: boolean; + peer_id text, + timestamp integer, + eip_validation boolean, + ts_validation boolean, signed_response text )`; @@ -38,15 +38,42 @@ export async function crate_table(db: any) { } export async function insert_event(db: any, eip_obj: any, response_obj: Response, signed_msg: string) { - const stmt = `insert into snapshot.db ( - values (?,?,?,?,?,?,?,?,?) - )`; - const values = [eip_obj.data.message.snapshot, eip_obj.address, eip_obj.sig, JSON.stringify(eip_obj.data), response_obj.peer_id, response_obj.timestamp, response_obj.eip_validation, response_obj.ts_validation, signed_msg]; - db.run(stmt, values, function (err: any) { + const stmt = `insert into snapshot values (?,?,?,?,?,?,?,?,?)`; + var cursor = db.prepare(stmt); + + const vals = [eip_obj.data.message.snapshot, eip_obj.address, eip_obj.sig, JSON.stringify(eip_obj.data), response_obj.peer_id, response_obj.timestamp, response_obj.eip_validation, response_obj.ts_validation, signed_msg]; + for (let v of vals) { + console.log(typeof (v)); + } + + cursor.run(stmt, vals, function (err: any) { if (err) { - return console.log(err.message); + return console.log("insert error: ", err.message); } // console.log(`A row has been inserted with row id: ${this.lastID}`); // console.log(`A row has been inserted with row id: ${db.}`); }); + cursor.finalize(); } + + +/* + +var db = new sqlite3.Database(DB_PATH); + db.serialize(function () { + const create_stmt = "CREATE TABLE lorem (info TEXT)"; + db.run(create_stmt); + + const ins_stmt = "INSERT INTO lorem VALUES (?)"; + var stmt = db.prepare("INSERT INTO lorem VALUES (?)"); + for (var i = 0; i < 10; i++) { + stmt.run("Ipsum " + i); + } + stmt.finalize(); + + db.each("SELECT rowid AS id, info FROM lorem", function (err: any, row: any) { + console.log(row.id + ": " + row.info); + }); + }); + +*/ \ No newline at end of file