create error helper
MODIFY
package-lock.json
package-lock.json
}
},
"@maticnetwork/meta": {
- "version": "2.4.16",
- "resolved": "https://registry.npmjs.org/@maticnetwork/meta/-/meta-2.4.16.tgz",
- "integrity": "sha512-N8dgOEtetBWP0Cn33F7F9ZqL+bDwK6LHWPTHlVHR1X3CrrroC/I0jMkDWQdxmTuAkkyCFI34g4qdqh5QrbNnwA=="
+ "version": "2.4.18",
+ "resolved": "https://registry.npmjs.org/@maticnetwork/meta/-/meta-2.4.18.tgz",
+ "integrity": "sha512-ahxaZ1bdKUhDiwIG9dYHqfPMmnm1OT4BmUMrYjU3XPfhPYO0TEz/tAZFW980hoOHF8DfgI1YLi4DGJmfxerXrQ=="
},
"@nodelib/fs.scandir": {
"version": "2.1.4",

MODIFY
package.json
package.json
},
"homepage": "https://github.com/maticnetwork/matic.js#readme",
"dependencies": {
- "@maticnetwork/meta": "^2.4.16",
+ "@maticnetwork/meta": "^2.4.18",
"assert": "^2.0.0",
"bn.js": "5.0.0",
"eth-sig-util": "^2.5.3",

ADD
src/enums/error_type.ts
+export enum ERROR_TYPE {
+ AllowedOnRoot = "allowed_on_root",
+ AllowedOnChild = "allowed_on_child",
+ Unknown = "unknown"
+}
\ No newline at end of file

MODIFY
src/enums/index.ts
src/enums/index.ts
export * from "./log_event_signature";
+export * from "./error_type";
\ No newline at end of file

MODIFY
src/helpers/contract_write_result.ts
src/helpers/contract_write_result.ts
@@ -7,11 +7,11 @@ export class ContractWriteResult {
constructor(result: ISendResult) {
this.txHashPromise = new Promise((res, rej) => {
result.onTransactionHash = res;
- result.onError = rej;
+ result.onTxError = rej;
});
this.receiptPromise = new Promise((res, rej) => {
result.onReceipt = res;
- result.onError = rej;
+ result.onReceiptError = rej;
});
}

MODIFY
src/index.ts
src/index.ts
import { defaultExport } from "./default";
export * from "./utils";
+export * from "./enums";
export * from "./pos";
export * from "./plasma";
export * from "./interfaces";
export * from "./types";
export * from "./constant";
export * from "./abstracts";
+export { initService } from "./services";
export default defaultExport;

MODIFY
src/inherited/eth_method.ts
src/inherited/eth_method.ts
@@ -19,7 +19,8 @@ export class EthMethod extends BaseContractMethod {
const result = {
onTransactionHash: (doNothing as any),
onReceipt: doNothing,
- onError: doNothing
+ onReceiptError: doNothing,
+ onTxError: doNothing
};
setTimeout(() => {
LOGGER.log("sending tx with config", tx);
@@ -38,7 +39,8 @@ export class EthMethod extends BaseContractMethod {
this.method.send(tx as any).
once("transactionHash", result.onTransactionHash).
once("receipt", result.onReceipt).
- once("error", result.onError);
+ on("error", result.onTxError).
+ on("error", result.onReceiptError);
// } catch (error) {
// }
@@ -51,7 +53,7 @@ export class EthMethod extends BaseContractMethod {
}
encodeABI() {
- return this.encodeABI();
+ return this.method.encodeABI();
}
}
\ No newline at end of file

ADD
src/interfaces/error.ts
+import { ERROR_TYPE } from "../enums";
+
+export interface IError {
+ type: ERROR_TYPE;
+ message: string;
+}
\ No newline at end of file

MODIFY
src/interfaces/index.ts
src/interfaces/index.ts
@@ -15,3 +15,4 @@ export * from "./rpc_request_payload";
export * from "./rpc_response_payload";
export * from "./map_promise_option";
export * from "./base_client_config";
+export * from "./error";
\ No newline at end of file

MODIFY
src/interfaces/send_result.ts
src/interfaces/send_result.ts
export interface ISendResult {
onTransactionHash: (txHash: string) => void;
onReceipt: Function;
- onError: Function;
+ onReceiptError: Function;
+ onTxError: Function;
}
\ No newline at end of file

MODIFY
src/utils/base_token.ts
src/utils/base_token.ts
@@ -84,7 +84,8 @@ export class BaseToken {
}
protected async createTransactionConfig({ txConfig, method, isParent, isWrite }: ITransactionConfigParam) {
- txConfig = Object.assign(isParent ? this.parentDefaultConfig : this.childDefaultConfig, txConfig || {});
+ const defaultConfig = isParent ? this.parentDefaultConfig : this.childDefaultConfig;
+ txConfig = Object.assign(defaultConfig, (txConfig || {}));
console.log("txConfig", txConfig, isParent, isWrite);
const client = isParent ? this.client.parent :
this.client.child;

ADD
src/utils/error_helper.ts
+import { ERROR_TYPE } from "../enums";
+import { IError } from "../interfaces";
+
+export class ErrorHelper implements IError {
+ type: ERROR_TYPE;
+ message: string;
+
+ constructor(type: ERROR_TYPE, info?) {
+ this.type = type;
+ this.message = this.getMsg_(info);
+ }
+
+ throw() {
+ throw this.get();
+ }
+
+ get() {
+ return {
+ message: this.message,
+ type: this.type
+ } as IError;
+ }
+
+ private getMsg_(info) {
+ let errMsg: string;
+ switch (this.type) {
+ case ERROR_TYPE.AllowedOnChild:
+ errMsg = `The action ${info} is allowed only on child token.`;
+ break;
+ case ERROR_TYPE.AllowedOnRoot:
+ errMsg = `The action ${info} is allowed only on root token.`;
+ break;
+ default:
+ if (!this.type) {
+ this.type = ERROR_TYPE.Unknown;
+ }
+ errMsg = this.message;
+ break;
+ }
+ return errMsg;
+ }
+}
\ No newline at end of file

MODIFY
src/utils/logger.ts
src/utils/logger.ts
+import { ERROR_TYPE } from "../enums";
+import { ErrorHelper } from "./error_helper";
+
export class Logger {
private isEnabled: boolean;
@@ -11,4 +14,8 @@ export class Logger {
console.log(...message);
}
}
+
+ error(type: ERROR_TYPE, info?) {
+ return new ErrorHelper(type, info);
+ }
}
\ No newline at end of file

create error helper