Monitor mempool for txns
This commit is contained in:
parent
b65a7736ee
commit
ac2880bb23
|
@ -5,7 +5,7 @@
|
||||||
"tasks": [
|
"tasks": [
|
||||||
{
|
{
|
||||||
"type": "npm",
|
"type": "npm",
|
||||||
"script": "dev",
|
"script": "start",
|
||||||
"group": { "kind": "build", "isDefault": true },
|
"group": { "kind": "build", "isDefault": true },
|
||||||
"problemMatcher": []
|
"problemMatcher": []
|
||||||
},
|
},
|
||||||
|
|
|
@ -2636,7 +2636,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zecwalletlitelib"
|
name = "zecwalletlitelib"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
source = "git+https://github.com/adityapk00/zecwallet-light-cli?rev=623be1872b92edf441212d4a68b45e63e3036c2c#623be1872b92edf441212d4a68b45e63e3036c2c"
|
source = "git+https://github.com/adityapk00/zecwallet-light-cli?rev=bcf881d4ff29ff267c09d6abc4244ec09717da5a#bcf881d4ff29ff267c09d6abc4244ec09717da5a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"arr_macro",
|
"arr_macro",
|
||||||
"base58",
|
"base58",
|
||||||
|
|
|
@ -15,6 +15,6 @@ default-features = false
|
||||||
features = ["napi-6"]
|
features = ["napi-6"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
zecwalletlitelib = { git = "https://github.com/adityapk00/zecwallet-light-cli", rev = "623be1872b92edf441212d4a68b45e63e3036c2c" }
|
zecwalletlitelib = { git = "https://github.com/adityapk00/zecwallet-light-cli", rev = "bcf881d4ff29ff267c09d6abc4244ec09717da5a" }
|
||||||
#zecwalletlitelib = { path = "../../zecwallet-light-cli/lib" }
|
#zecwalletlitelib = { path = "../../zecwallet-light-cli/lib" }
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
|
|
|
@ -25,7 +25,7 @@ lazy_static! {
|
||||||
}
|
}
|
||||||
|
|
||||||
register_module!(mut m, {
|
register_module!(mut m, {
|
||||||
m.export_function("litelib_say_hello", litelib_say_hello)?;
|
//m.export_function("litelib_say_hello", litelib_say_hello)?;
|
||||||
m.export_function("litelib_wallet_exists", litelib_wallet_exists)?;
|
m.export_function("litelib_wallet_exists", litelib_wallet_exists)?;
|
||||||
m.export_function("litelib_initialize_new", litelib_initialize_new)?;
|
m.export_function("litelib_initialize_new", litelib_initialize_new)?;
|
||||||
m.export_function("litelib_initialize_existing", litelib_initialize_existing)?;
|
m.export_function("litelib_initialize_existing", litelib_initialize_existing)?;
|
||||||
|
@ -38,13 +38,13 @@ register_module!(mut m, {
|
||||||
Ok(())
|
Ok(())
|
||||||
});
|
});
|
||||||
|
|
||||||
fn litelib_say_hello(mut cx: FunctionContext) -> JsResult<JsString> {
|
// fn litelib_say_hello(mut cx: FunctionContext) -> JsResult<JsString> {
|
||||||
let to = cx.argument::<JsString>(0)?.value(&mut cx);
|
// let to = cx.argument::<JsString>(0)?.value(&mut cx);
|
||||||
|
|
||||||
let ret = format!("Hello {}", to);
|
// let ret = format!("Hello {}", to);
|
||||||
|
|
||||||
Ok(cx.string(ret))
|
// Ok(cx.string(ret))
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Check if there is an existing wallet
|
// Check if there is an existing wallet
|
||||||
fn litelib_wallet_exists(mut cx: FunctionContext) -> JsResult<JsBoolean> {
|
fn litelib_wallet_exists(mut cx: FunctionContext) -> JsResult<JsBoolean> {
|
||||||
|
@ -84,10 +84,10 @@ fn litelib_initialize_new(mut cx: FunctionContext) -> JsResult<JsString> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
LIGHTCLIENT
|
let lc = Arc::new(lightclient);
|
||||||
.lock()
|
LightClient::start_mempool_monitor(lc.clone());
|
||||||
.unwrap()
|
|
||||||
.replace(Some(Arc::new(lightclient)));
|
LIGHTCLIENT.lock().unwrap().replace(Some(lc));
|
||||||
|
|
||||||
// Return the wallet's seed
|
// Return the wallet's seed
|
||||||
seed
|
seed
|
||||||
|
@ -123,10 +123,10 @@ fn litelib_initialize_new_from_phrase(mut cx: FunctionContext) -> JsResult<JsStr
|
||||||
// Initialize logging
|
// Initialize logging
|
||||||
let _ = lightclient.init_logging();
|
let _ = lightclient.init_logging();
|
||||||
|
|
||||||
LIGHTCLIENT
|
let lc = Arc::new(lightclient);
|
||||||
.lock()
|
LightClient::start_mempool_monitor(lc.clone());
|
||||||
.unwrap()
|
|
||||||
.replace(Some(Arc::new(lightclient)));
|
LIGHTCLIENT.lock().unwrap().replace(Some(lc));
|
||||||
|
|
||||||
format!("OK")
|
format!("OK")
|
||||||
};
|
};
|
||||||
|
@ -157,10 +157,10 @@ fn litelib_initialize_existing(mut cx: FunctionContext) -> JsResult<JsString> {
|
||||||
// Initialize logging
|
// Initialize logging
|
||||||
let _ = lightclient.init_logging();
|
let _ = lightclient.init_logging();
|
||||||
|
|
||||||
LIGHTCLIENT
|
let lc = Arc::new(lightclient);
|
||||||
.lock()
|
LightClient::start_mempool_monitor(lc.clone());
|
||||||
.unwrap()
|
|
||||||
.replace(Some(Arc::new(lightclient)));
|
LIGHTCLIENT.lock().unwrap().replace(Some(lc));
|
||||||
|
|
||||||
format!("OK")
|
format!("OK")
|
||||||
};
|
};
|
||||||
|
|
64
src/rpc.ts
64
src/rpc.ts
|
@ -23,8 +23,12 @@ export default class RPC {
|
||||||
fnSetAllAddresses: (a: string[]) => void;
|
fnSetAllAddresses: (a: string[]) => void;
|
||||||
fnSetZecPrice: (p?: number) => void;
|
fnSetZecPrice: (p?: number) => void;
|
||||||
refreshTimerID?: NodeJS.Timeout;
|
refreshTimerID?: NodeJS.Timeout;
|
||||||
|
updateTimerId?: NodeJS.Timeout;
|
||||||
|
|
||||||
|
updateDataLock: boolean;
|
||||||
|
|
||||||
lastBlockHeight: number;
|
lastBlockHeight: number;
|
||||||
|
lastTxId?: string;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
fnSetTotalBalance: (tb: TotalBalance) => void,
|
fnSetTotalBalance: (tb: TotalBalance) => void,
|
||||||
|
@ -43,13 +47,19 @@ export default class RPC {
|
||||||
this.lastBlockHeight = 0;
|
this.lastBlockHeight = 0;
|
||||||
|
|
||||||
this.refreshTimerID = undefined;
|
this.refreshTimerID = undefined;
|
||||||
|
this.updateTimerId = undefined;
|
||||||
|
this.updateDataLock = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
async configure(rpcConfig: RPCConfig) {
|
async configure(rpcConfig: RPCConfig) {
|
||||||
this.rpcConfig = rpcConfig;
|
this.rpcConfig = rpcConfig;
|
||||||
|
|
||||||
if (!this.refreshTimerID) {
|
if (!this.refreshTimerID) {
|
||||||
this.refreshTimerID = setInterval(() => this.refresh(false), 60 * 1000);
|
this.refreshTimerID = setInterval(() => this.refresh(false), 3 * 60 * 1000); // 3 mins
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.updateTimerId) {
|
||||||
|
this.updateTimerId = setInterval(() => this.updateData(), 3 * 1000); // 3 secs
|
||||||
}
|
}
|
||||||
|
|
||||||
// Immediately call the refresh after configure to update the UI
|
// Immediately call the refresh after configure to update the UI
|
||||||
|
@ -61,6 +71,11 @@ export default class RPC {
|
||||||
clearInterval(this.refreshTimerID);
|
clearInterval(this.refreshTimerID);
|
||||||
this.refreshTimerID = undefined;
|
this.refreshTimerID = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.updateTimerId) {
|
||||||
|
clearInterval(this.updateTimerId);
|
||||||
|
this.updateTimerId = undefined;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static getDefaultFee(): number {
|
static getDefaultFee(): number {
|
||||||
|
@ -96,6 +111,36 @@ export default class RPC {
|
||||||
console.log(`Deinitialize status: ${str}`);
|
console.log(`Deinitialize status: ${str}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async updateData() {
|
||||||
|
//console.log("Update data triggered");
|
||||||
|
if (this.updateDataLock) {
|
||||||
|
//console.log("Update lock, returning");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.updateDataLock = true;
|
||||||
|
const latest_txid = RPC.getLastTxid();
|
||||||
|
console.log(`Latest: ${latest_txid}, prev = ${this.lastTxId}`);
|
||||||
|
if (this.lastTxId !== latest_txid) {
|
||||||
|
const latestBlockHeight = await this.fetchInfo();
|
||||||
|
this.lastBlockHeight = latestBlockHeight;
|
||||||
|
this.lastTxId = latest_txid;
|
||||||
|
|
||||||
|
//console.log("Update data fetching new txns");
|
||||||
|
|
||||||
|
// And fetch the rest of the data.
|
||||||
|
this.fetchTotalBalance();
|
||||||
|
this.fetchTandZTransactions(latestBlockHeight);
|
||||||
|
this.getZecPrice();
|
||||||
|
|
||||||
|
// Save the wallet
|
||||||
|
RPC.doSave();
|
||||||
|
|
||||||
|
//console.log(`Finished update data at ${latestBlockHeight}`);
|
||||||
|
}
|
||||||
|
this.updateDataLock = false;
|
||||||
|
}
|
||||||
|
|
||||||
async refresh(fullRefresh: boolean) {
|
async refresh(fullRefresh: boolean) {
|
||||||
const latestBlockHeight = await this.fetchInfo();
|
const latestBlockHeight = await this.fetchInfo();
|
||||||
|
|
||||||
|
@ -115,16 +160,17 @@ export default class RPC {
|
||||||
// We are synced. Cancel the poll timer
|
// We are synced. Cancel the poll timer
|
||||||
clearInterval(pollerID);
|
clearInterval(pollerID);
|
||||||
|
|
||||||
// Save the wallet
|
|
||||||
RPC.doSave();
|
|
||||||
|
|
||||||
// And fetch the rest of the data.
|
// And fetch the rest of the data.
|
||||||
this.fetchTotalBalance();
|
this.fetchTotalBalance();
|
||||||
this.fetchTandZTransactions(latestBlockHeight);
|
this.fetchTandZTransactions(latestBlockHeight);
|
||||||
this.getZecPrice();
|
this.getZecPrice();
|
||||||
|
|
||||||
this.lastBlockHeight = latestBlockHeight;
|
this.lastBlockHeight = latestBlockHeight;
|
||||||
// All done, set up next fetch
|
|
||||||
|
// Save the wallet
|
||||||
|
RPC.doSave();
|
||||||
|
|
||||||
|
// All done
|
||||||
console.log(`Finished full refresh at ${latestBlockHeight}`);
|
console.log(`Finished full refresh at ${latestBlockHeight}`);
|
||||||
}
|
}
|
||||||
}, 1000);
|
}, 1000);
|
||||||
|
@ -248,6 +294,13 @@ export default class RPC {
|
||||||
this.fnSetAllAddresses(allAddresses);
|
this.fnSetAllAddresses(allAddresses);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static getLastTxid(): string {
|
||||||
|
const lastTxid = native.litelib_execute("lasttxid", "");
|
||||||
|
const lastTxidJSON = JSON.parse(lastTxid);
|
||||||
|
|
||||||
|
return lastTxidJSON.last_txid;
|
||||||
|
}
|
||||||
|
|
||||||
static getPrivKeyAsString(address: string): string {
|
static getPrivKeyAsString(address: string): string {
|
||||||
const privKeyStr = native.litelib_execute("export", address);
|
const privKeyStr = native.litelib_execute("export", address);
|
||||||
const privKeyJSON = JSON.parse(privKeyStr);
|
const privKeyJSON = JSON.parse(privKeyStr);
|
||||||
|
@ -287,6 +340,7 @@ export default class RPC {
|
||||||
fetchTandZTransactions(latestBlockHeight: number) {
|
fetchTandZTransactions(latestBlockHeight: number) {
|
||||||
const listStr = native.litelib_execute("list", "");
|
const listStr = native.litelib_execute("list", "");
|
||||||
const listJSON = JSON.parse(listStr);
|
const listJSON = JSON.parse(listStr);
|
||||||
|
//console.log(listJSON);
|
||||||
|
|
||||||
let txlist: Transaction[] = listJSON.map((tx: any) => {
|
let txlist: Transaction[] = listJSON.map((tx: any) => {
|
||||||
const transaction = new Transaction();
|
const transaction = new Transaction();
|
||||||
|
|
|
@ -2118,6 +2118,11 @@
|
||||||
dependencies:
|
dependencies:
|
||||||
source-map "^0.6.1"
|
source-map "^0.6.1"
|
||||||
|
|
||||||
|
"@types/url-parse@^1.4.3":
|
||||||
|
version "1.4.3"
|
||||||
|
resolved "https://registry.yarnpkg.com/@types/url-parse/-/url-parse-1.4.3.tgz#fba49d90f834951cb000a674efee3d6f20968329"
|
||||||
|
integrity sha512-4kHAkbV/OfW2kb5BLVUuUMoumB3CP8rHqlw48aHvFy5tf9ER0AfOonBlX29l/DD68G70DmyhRlSYfQPSYpC5Vw==
|
||||||
|
|
||||||
"@types/verror@^1.10.3":
|
"@types/verror@^1.10.3":
|
||||||
version "1.10.4"
|
version "1.10.4"
|
||||||
resolved "https://registry.yarnpkg.com/@types/verror/-/verror-1.10.4.tgz#805c0612b3a0c124cf99f517364142946b74ba3b"
|
resolved "https://registry.yarnpkg.com/@types/verror/-/verror-1.10.4.tgz#805c0612b3a0c124cf99f517364142946b74ba3b"
|
||||||
|
|
Loading…
Reference in New Issue