processWrite method for reuse in write call
MODIFY
src/model/base_token.ts
src/model/base_token.ts
import { Web3SideChainClient } from "./web3_side_chain_client";
import { ITransactionConfig, ITransactionOption, IContractInitParam } from "../interfaces";
import { BaseContractMethod } from "../abstracts";
-import { BaseWeb3Client } from "./web3_client";
-import { EXTRA_GAS_FOR_PROXY_CALL } from "../constant";
import { BaseContract } from "./eth_contract";
+import { eventBusPromise, merge, IEventBusPromise } from "../utils";
+import { EXTRA_GAS_FOR_PROXY_CALL } from "../constant";
interface ITransactionConfigParam {
txConfig: ITransactionConfig;
@@ -12,8 +12,6 @@ interface ITransactionConfigParam {
isParent?: boolean;
}
-
-
export class BaseToken {
contract: BaseContract;
@@ -25,6 +23,46 @@ export class BaseToken {
this.contract = this.getContract(contractParam);
}
+ protected processWrite(method: BaseContractMethod, option: ITransactionOption) {
+ const result = eventBusPromise((res, rej) => {
+ // tslint:disable-next-line
+ this.createTransactionConfig(
+ {
+ txConfig: option,
+ isWrite: true,
+ method,
+ isParent: true
+ }).then(config => {
+ if (option.returnTransaction) {
+ return res(
+ merge(config, {
+ data: method.encodeABI(),
+ to: this.contract.address
+ } as ITransactionConfig)
+ );
+ }
+ 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);
+ result.destroy();
+ };
+ }) as IEventBusPromise<any>;
+ });
+ return result;
+ }
+
getContract({ isParent, tokenAddress, abi }: IContractInitParam) {
const client = isParent ? this.client.parent.client :
this.client.child.client;
@@ -54,7 +92,7 @@ export class BaseToken {
!txConfig.chainId ? client.getChainId() : txConfig.chainId,
]);
- // txConfig.gas = isParent ? Number(gas) + EXTRA_GAS_FOR_PROXY_CALL : gas;
+ txConfig.gas = isParent ? Number(gas) + EXTRA_GAS_FOR_PROXY_CALL : gas;
txConfig.gasPrice = gasPrice;
txConfig.nonce = nonce;
txConfig.chainId = chainId;

MODIFY
src/plasma/erc20.ts
src/plasma/erc20.ts
@@ -41,43 +41,7 @@ export class ERC20 extends BaseToken {
this.depositManager.contract.address,
formatAmount(amount)
);
- const result = eventBusPromise((res, rej) => {
- // tslint:disable-next-line
- this.createTransactionConfig(
- {
- txConfig: option,
- isWrite: true,
- method,
- isParent: true
- }).then(config => {
- if (option.returnTransaction) {
- return res(
- merge(config, {
- data: method.encodeABI(),
- to: this.contract.address
- } as ITransactionConfig)
- );
- }
- 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);
- result.destroy();
- };
- }) as IEventBusPromise<any>;
- });
- return result;
+ return this.processWrite(method, option);
}
approveMax(option: ITransactionOption = {}) {

MODIFY
test/debug.js
test/debug.js
@@ -31,22 +31,16 @@ const execute = async () => {
const balance = await childErc20.getBalance(from);
console.log('balance', balance);
- // const rootTokenErc20 = matic.erc20(goerliTokenERC20);
- // const result = await rootTokenErc20.approve(
+ // const rootTokenErc20 = matic.erc20(goerliTokenERC20, true);
+ // const promise = rootTokenErc20.approve(
// '1000000000000000'
- // );
+ // )
+ // promise.on("txHash", (txHash) => {
+ // console.log("transaction hash args", txHash);
+ // });
+ // console.log("promise result", promise);
+ // const result = await promise;
// console.log("approve result", result);
- // console.log("result", result, result.on);
-
- // 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(_ => {

processWrite method for reuse in write call