create eventBusPromise
MODIFY
package.json
package.json
"typescript": "^3.8.3",
"typings": "^2.1.1",
"web3": "^1.5.0",
+ "web3-core-promievent": "^1.5.0",
"webpack": "^4.43.0",
"webpack-cli": "^4.2.0",
"yargs": "^16.2.0"

MODIFY
src/plasma/erc20.ts
src/plasma/erc20.ts
import { BaseToken, Web3SideChainClient } from "@/model";
import BN from "bn.js";
import { ITransactionConfig } from "@/interfaces";
-import { formatAmount } from "@/utils";
+import { formatAmount, IEventBusPromise, eventBusPromise } from "../utils";
import { createTransactionConfig } from "@/utils/create_tx_config";
import { DepositManager } from "./deposit_manager";
@@ -36,7 +36,10 @@ export class ERC20 extends BaseToken {
this.depositManager.contract.address,
formatAmount(amount)
);
- return createTransactionConfig(
+ console.log("arguments approve", arguments);
+ const result = eventBusPromise((res, rej) => {
+ // tslint:disable-next-line
+ createTransactionConfig(
{
txConfig,
defaultTxConfig: this.parentDefaultConfig,
@@ -44,10 +47,27 @@ export class ERC20 extends BaseToken {
isWrite: true,
method
}).then(config => {
- return method.write(
+ console.log("config", config);
+ const methodResult = method.write(
config,
);
+ methodResult.onTransactionHash = (txHash) => {
+ result.emit("txHash", txHash);
+ };
+ methodResult.onError = (err, receipt) => {
+ rej({
+ error: err,
+ receipt
});
+ };
+ methodResult.onReceipt = (receipt) => {
+ result.emit("receipt", receipt);
+ res(receipt);
+ };
+ }) as IEventBusPromise<any>;
+ });
+ return result;
+
}
}
\ No newline at end of file

MODIFY
src/plasma/index.ts
src/plasma/index.ts
@@ -15,11 +15,7 @@ export class PlasmaClient {
constructor(config: IPlasmaClientConfig) {
this.client_ = new Web3SideChainClient(config);
- this.erc20 = new ERC20(
- this.client_,
- this.client_.getABI('ChildERC20'),
- this.depositManager
- );
+
const mainContracts = this.client_.mainContracts;
@@ -36,6 +32,12 @@ export class PlasmaClient {
config.depositManager,
this.client_.getABI("DepositManager")
);
+
+ this.erc20 = new ERC20(
+ this.client_,
+ this.client_.getABI('ChildERC20'),
+ this.depositManager
+ );
}
}
\ No newline at end of file

MODIFY
src/utils/create_tx_config.ts
src/utils/create_tx_config.ts
@@ -12,7 +12,7 @@ interface ITransactionConfigParam {
export const createTransactionConfig = async ({ txConfig, defaultTxConfig, method, client, isWrite }: ITransactionConfigParam) => {
txConfig = Object.assign(defaultTxConfig, txConfig || {});
-
+ console.log("txConfig", txConfig);
if (isWrite) {
const [gas, gasPrice, nonce, chainId] = await Promise.all([
!(txConfig.gas)

MODIFY
src/utils/event_bus.ts
src/utils/event_bus.ts
+// Create a constructor that uses `Promise` as its super and does the `super` call
+// via `Reflect.construct`
+const myPromise = function (executor) {
+ return Reflect.construct(Promise, [executor], myPromise);
+};
+// Assign the statics (`resolve`, `reject`, etc.) to the new constructor
+Object.assign(
+ myPromise,
+ (Object as any).fromEntries(
+ Reflect.ownKeys(Promise)
+ .filter(key => key !== "length" && key !== "name")
+ .map(key => [key, Promise[key]])
+ )
+);
+
+// Create the prototype, add methods to it
+myPromise.prototype = Object.create(Promise.prototype);
+myPromise.prototype.constructor = myPromise;
+
+export interface IEventBusPromise<T> extends Promise<T> {
+ on(event: string, cb: Function);
+ emit(event: string, ...args);
+}
+
+export const eventBusPromise = function <T>(executor: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void) {
+ const promise: IEventBusPromise<T> = new Promise(executor) as any;
+ const eventBus = new EventBus();
+ promise.on = eventBus.on.bind(eventBus);
+ promise.emit = eventBus.emit.bind(eventBus);
+ return promise;
+};
+
export class EventBus {
constructor(ctx?) {

MODIFY
test/debug.js
test/debug.js
@@ -25,14 +25,22 @@ const execute = async () => {
}
});
- const balance = await matic.erc20.getBalance(env.PLASMA_MUMBAI_ERC20, from)
- console.log('balance', balance)
+ // const balance = await matic.erc20.getBalance(env.PLASMA_MUMBAI_ERC20, from)
+ // console.log('balance', balance)
- // const result = await matic.erc20.approve(erc20TokenAddress, '1000000000000000');
+ const result = matic.erc20.approve(erc20TokenAddress, '1000000000000000');
+ console.log("result", result, result.on);
+ await result;
// result.onError = function () {
// console.log("error args", arguments);
// }
+ // result.onTransactionHash = function () {
+ // console.log("transaction hash args", arguments);
+ // }
+ // result.onReceipt = function(){
+ // console.log("onReceipt hash args", arguments);
+ // }
}
execute().then(_ => {

create eventBusPromise