Skip to content

Commit

Permalink
unsubscribe_contract_logs method
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklatkovich committed Dec 26, 2019
1 parent 37db2ff commit f8f8943
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 61 deletions.
41 changes: 23 additions & 18 deletions docs/Constants.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,24 +228,29 @@ console.log(constants.OPERATIONS_IDS); // operation id
SIDECHAIN_ETH_CREATE_ADDRESS: 39,
SIDECHAIN_ETH_APPROVE_ADDRESS: 40,
SIDECHAIN_ETH_DEPOSIT: 41,
SIDECHAIN_ETH_WITHDRAW: 42,
SIDECHAIN_ETH_APPROVE_WITHDRAW: 43,
SIDECHAIN_ISSUE: 44, // VIRTUAL
SIDECHAIN_BURN: 45, // VIRTUAL
SIDECHAIN_ERC20_REGISTER_TOKEN: 46,
SIDECHAIN_ERC20_DEPOSIT_TOKEN: 47,
SIDECHAIN_ERC20_WITHDRAW_TOKEN: 48,
SIDECHAIN_ERC20_APPROVE_TOKEN_WITHDRAW: 49,
SIDECHAIN_ERC20_ISSUE: 50, // VIRTUAL
SIDECHAIN_ERC20_BURN: 51, // VIRTUAL
SIDECHAIN_BTC_CREATE_ADDRESS: 52,
SIDECHAIN_BTC_CREATE_INTERMEDIATE_DEPOSIT: 53,
SIDECHAIN_BTC_INTERMEDIATE_DEPOSIT: 54,
SIDECHAIN_BTC_DEPOSIT: 55,
SIDECHAIN_BTC_WITHDRAW: 56,
SIDECHAIN_BTC_APPROVE_WITHDRAW: 57,
SIDECHAIN_BTC_AGGREGATE: 58,
BLOCK_REWARD: 59,// VIRTUAL
SIDECHAIN_ETH_SEND_DEPOSIT: 42,
SIDECHAIN_ETH_WITHDRAW: 43,
SIDECHAIN_ETH_SEND_WITHDRAW: 44,
SIDECHAIN_ETH_APPROVE_WITHDRAW: 45,
SIDECHAIN_ETH_UPDATE_CONTRACT_ADDRESS: 46,
SIDECHAIN_ISSUE: 47, // VIRTUAL
SIDECHAIN_BURN: 48, // VIRTUAL
SIDECHAIN_ERC20_REGISTER_TOKEN: 49,
SIDECHAIN_ERC20_DEPOSIT_TOKEN: 50,
SIDECHAIN_ERC20_SEND_DEPOSIT: 51,
SIDECHAIN_ERC20_WITHDRAW_TOKEN: 52,
SIDECHAIN_ERC20_SEND_WITHDRAW: 53,
SIDECHAIN_ERC20_APPROVE_TOKEN_WITHDRAW: 54,
SIDECHAIN_ERC20_ISSUE: 55, // VIRTUAL
SIDECHAIN_ERC20_BURN: 56, // VIRTUAL
SIDECHAIN_BTC_CREATE_ADDRESS: 57,
SIDECHAIN_BTC_CREATE_INTERMEDIATE_DEPOSIT: 58,
SIDECHAIN_BTC_INTERMEDIATE_DEPOSIT: 59,
SIDECHAIN_BTC_DEPOSIT: 60,
SIDECHAIN_BTC_WITHDRAW: 61,
SIDECHAIN_BTC_AGGREGATE: 62,
SIDECHAIN_BTC_APPROVE_AGGREGATE: 63,
BLOCK_REWARD: 64, // VIRTUAL
}
*/
```
34 changes: 0 additions & 34 deletions [email protected]

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "echojs-lib",
"version": "1.10.0-rc.0",
"version": "1.10.0-rc.1",
"description": "Pure JavaScript ECHO library for node.js",
"main": "./dist/index.js",
"types": "./types/index.d.ts",
Expand Down
6 changes: 6 additions & 0 deletions src/echo/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { toRawContractLogsFilterOptions } from '../utils/converters';
/** @typedef {import("bignumber.js").default} BigNumber */
/** @typedef {import("../../types/interfaces/vm/types").Log} Log */
/** @typedef {import("./ws-api/database-api").SidechainType} SidechainType */
/** @typedef {typeof basic.integers["uint64"]["__TInput__"]} UInt64 */

/** @typedef {
* {
Expand Down Expand Up @@ -1954,6 +1955,11 @@ class API {
}, toRawContractLogsFilterOptions(opts));
}

/** @param {UInt64} subscribeId */
async unsubscribeContractLogs(subscribeId) {
return this.wsApi.database.unsubscribeContractLogs(basic.integers.uint64.toRaw(subscribeId));
}

/**
* @method getContractResult
*
Expand Down
3 changes: 3 additions & 0 deletions src/echo/ws-api/database-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,9 @@ class DatabaseAPI {
*/
subscribeContractLogs(cb, options) { return this.db.exec('subscribe_contract_logs', [cb, options]); }

/** @param {number|string} cbId */
unsubscribeContractLogs(cbId) { return this.db.exec('unsubscribe_contract_logs', [cbId]); }

/**
* @method getContractResult
*
Expand Down
23 changes: 18 additions & 5 deletions test/api/database/subscribe-contract-logs.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "assert";
import { Echo } from "../../..";
import { Echo, BigNumber } from "../../..";
import { url } from "../../_test-data";
import { deploy, emit, emit1 } from "../../_event-emitter-contract";

Expand All @@ -17,10 +17,24 @@ describe('subscribeContractLogs', () => {
after(async () => echo.disconnect());
describe('when no any options are provided', () => {
/** @type {number|string} */
let cbId;
let subscribeId;
/** @type {Log[]} */
let emits = [];
it('should succeed', async () => cbId = await echo.api.subscribeContractLogs((logs) => emits.push(...logs)));
it('should succeed', async () => {
subscribeId = await echo.api.subscribeContractLogs((logs) => emits.push(...logs));
});
describe('should returns subscribeId', () => {
/** @type {BigNumber} */
let subIdBN;
before('should returns subscribeId', () => {
assert.ok(subscribeId !== undefined);
subIdBN = new BigNumber(subscribeId);
});
it('number', () => assert.ok(!new BigNumber(subscribeId).isNaN()));
it('integer', () => assert.ok(new BigNumber(subscribeId).isInteger()));
it('non-negative', () => assert.ok(new BigNumber(subscribeId).gte(0)));
it('less than 2**64', () => assert.ok(new BigNumber(subscribeId).lt(new BigNumber(2).pow(64))));
});
describe('when different contracts emit', () => {
before(() => emits = []);
it('should not rejects', async function () {
Expand All @@ -37,7 +51,6 @@ describe('subscribeContractLogs', () => {
assert.deepStrictEqual(new Set([contract1, contract2]), new Set(emits.map((log) => log[1].address)));
});
});
// TODO: unsubscribe
after(async () => { if (cbId !== undefined); });
after(async () => { if (subscribeId !== undefined) await echo.api.unsubscribeContractLogs(subscribeId); });
});
});
5 changes: 4 additions & 1 deletion test/cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ describe('cache', () => {
});

it('should not clean more often then minCleaningTime', async () => {
let tickTimeoutId;
try {
const promises = [];
const expirationTime = 100;
Expand All @@ -204,7 +205,8 @@ describe('cache', () => {
res();
} else {
if (currentCacheSize !== 0) {
setTimeout(() => {
tickTimeoutId = setTimeout(() => {
delete tickTimeoutId;
tick(res, callCount + 1, currentCacheSize);
}, tickInterval);
}
Expand Down Expand Up @@ -238,6 +240,7 @@ describe('cache', () => {
});

} catch (e) {
if (tickTimeoutId !== undefined) clearTimeout(tickTimeoutId);
throw(e);
}
})
Expand Down
10 changes: 8 additions & 2 deletions types/echo/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,17 @@ export default class Api {

/**
* @param cb
* @param options Contract logs filter options (see {@link ContractLogsFilterOptions})
* @returns Callback id which should be referenced in {@link unsubscribeContractLogs}
* @param options Contract logs filter options (see `ContractLogsFilterOptions` method)
* @returns Callback id which should be referenced in `unsubscribeContractLogs`
*/
subscribeContractLogs(cb: (result: Log[]) => any, options?: ContractLogsFilterOptions): Promise<number|string>;

/**
* Unsubscribe from contract log subscription
* @param subscribeId Subscribe id (returns by `subscribeContractLogs`)
*/
unsubscribeContractLogs(subscribeId: typeof uint64["__TInput__"]): Promise<void>;

validateTransaction(tr: Object): Promise<any>;
verifyAuthority(tr: Object): Promise<any>;
verifyAccountAuthority(accountNameOrId: Object, signers: Array<string>): Promise<any>;
Expand Down

0 comments on commit f8f8943

Please sign in to comment.