Skip to content

Commit

Permalink
Fix the monitor notification handling (#81)
Browse files Browse the repository at this point in the history
to have the correct events and logs information
  • Loading branch information
inwonkim committed Aug 29, 2023
1 parent 88c2979 commit 652b9e2
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
14 changes: 14 additions & 0 deletions lib/data/Formatter/BlockNotification.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import BigNumber from "bignumber.js";
import { Converter } from "../index";
import EventLog from "./EventLog";

export default class BlockNotification {
readonly hash: string;
readonly height: BigNumber;
readonly indexes: BigNumber[][];
readonly events: BigNumber[][][];
readonly logs: EventLog[][][];

constructor(data) {
this.hash = data.hash;
Expand All @@ -29,5 +31,17 @@ export default class BlockNotification {
}
}
}
if (data.logs) {
this.logs = [];
for (let i = 0; i < data.logs.length; i++) {
this.logs[i] = [];
for (let j = 0; j < data.logs[i].length; j++) {
this.logs[i][j] = [];
for (let k = 0; k < data.logs[i][j].length; k++) {
this.logs[i][j][k] = data.logs[i][j][k];
}
}
}
}
}
}
21 changes: 21 additions & 0 deletions lib/data/Formatter/EventLog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2023 ICON Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export default interface EventLog {
scoreAddress: string;
indexed: string[];
data: string[];
}
10 changes: 8 additions & 2 deletions lib/data/Formatter/EventNotification.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import BigNumber from "bignumber.js";
import { Converter } from "../index";
import EventLog from "./EventLog";

export default class EventNotification {
readonly hash: string;
readonly height: BigNumber;
readonly index: BigNumber;
readonly events: BigNumber[];
readonly logs: EventLog[];

constructor(data) {
this.hash = data.hash;
this.height = Converter.toBigNumber(data.height);
this.index = Converter.toBigNumber(data.index);
this.events = [];
this.logs = [];
if (data.events) {
this.events = [];
for (let i = 0; i < data.events; i++)
for (let i = 0; i < data.events.length; i++)
this.events[i] = Converter.toBigNumber(data.events[i]);
}
if (data.logs) {
for (let i = 0; i < data.logs.length; i++) this.logs[i] = data.logs[i];
}
}
}
10 changes: 7 additions & 3 deletions lib/transport/monitor/BlockMonitorSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import { Converter } from "../../data";
export default class BlockMonitorSpec implements MonitorSpec {
readonly height: BigNumber;
readonly eventFilters?: EventFilter[];
readonly logs?: boolean;

constructor(height: BigNumber, eventFilters?: EventFilter[]) {
constructor(height: BigNumber, eventFilters?: EventFilter[], logs?: boolean) {
this.height = height;
this.eventFilters = eventFilters;
this.logs = logs;
}

getPath(): string {
Expand All @@ -34,11 +36,13 @@ export default class BlockMonitorSpec implements MonitorSpec {

getParam(): object {
const height = Converter.toHex(this.height);
const params = { height };
if (this.logs) params["logs"] = "0x1";
if (!this.eventFilters || this.eventFilters.length === 0) {
return { height };
return params;
}
return {
height: height,
...params,
eventFilters: this.eventFilters.map((v) => v.toObject()),
};
}
Expand Down
5 changes: 5 additions & 0 deletions quickstart/example/html/MonitorExample.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<br />
<div>
<h3>1. monitor block</h3>
<p>
<div><label for="M01-url">URL:</label><input type="text" id="M01-url" size="60" /></div>
<div><label for="M01-addr">Addr:</label><input type="text" id="M01-addr" size="45" /></div>
<div><label for="M01-event">Event:</label><input type="text" id="M01-event" size="60" /></div>
<p>
<button id="M01-1">start monitoring</button>
<button id="M01-2">stop</button>
<p><span id="M01-3"></span></p>
Expand Down
10 changes: 8 additions & 2 deletions quickstart/example/js/MonitorExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@ class MonitorExample {
}

async startMonitorBlock() {
const url = getInputValue('M01-url');
const addr = getInputValue('M01-addr');
const event = getInputValue('M01-event');
const provider: HttpProvider = new HttpProvider(url);
this.iconService = new IconService(provider);
const block = await this.iconService.getLastBlock().execute();
const height = block.height;
const spec = new BlockMonitorSpec(Converter.toBigNumber(height + 1));
const eventFilter = new EventFilter(event, addr);
const spec = new BlockMonitorSpec(Converter.toBigNumber(height + 1), [eventFilter], true);
const onevent = (data: BlockNotification) => {
document.getElementById("M01-3").innerHTML = `block height : ${data.height}, block hash : ${data.hash}`;
document.getElementById("M01-3").innerHTML = JSON.stringify(data);
}
const onerror = (error) => {
console.log(error);
Expand Down

0 comments on commit 652b9e2

Please sign in to comment.