add abstract class for contract method
ADD
src/abstracts/contract_method.ts
+import { ITransactionConfig, ISendResult } from "@/interfaces";
+
+export abstract class BaseContractMethod {
+ abstract read<T>(tx: ITransactionConfig,): Promise<T>;
+ abstract write(tx: ITransactionConfig,): ISendResult;
+ abstract estimateGas(tx: ITransactionConfig,): Promise<number>;
+}
\ No newline at end of file

ADD
src/abstracts/index.ts
+export * from "./contract_method";
\ No newline at end of file

MODIFY
src/inherited/eth_contract.ts
src/inherited/eth_contract.ts
import { BaseContract } from "@/model";
import Contract from "web3/eth/contract";
-import { ITransactionConfig, ITransactionResult } from "@/interfaces";
-import { doNothing } from "@/helpers";
+import { EthMethod } from "./eth_method";
export class Web3Contract extends BaseContract {
contract: Contract;
@@ -11,28 +10,10 @@ export class Web3Contract extends BaseContract {
this.contract = contract;
}
- read<T>(methodName: string, tx: ITransactionConfig, ...args): Promise<T> {
- console.log("args", arguments);
- return this.contract.methods[methodName](...args).call();
- }
-
- write(methodName: string, tx: ITransactionConfig, ...args) {
- const result = {
- onTransactionHash: (doNothing as any),
- onReceipt: doNothing,
- onError: doNothing
- };
- const method = this.contract.methods[methodName](...args);
- setTimeout(() => {
- method.send(tx as any).
- once("transactionHash", result.onTransactionHash).
- once("receipt", result.onTransactionHash).
- once("error", result.onError);
- }, 0);
- return result;
- }
-
- createTransaction(methodName: string, tx: ITransactionConfig, ...args) {
- return this.contract.methods[methodName](...args) as ITransactionResult;
+ method(methodName: string, ...args) {
+ console.log("args method", arguments);
+ return new EthMethod(
+ this.contract.methods[methodName](...args)
+ );
}
}
\ No newline at end of file

ADD
src/inherited/eth_method.ts
+import { BaseContractMethod } from "@/abstracts";
+import { ITransactionConfig } from "@/interfaces";
+import { TransactionObject } from "web3/eth/types";
+import { doNothing } from "@/helpers";
+
+export class EthMethod extends BaseContractMethod {
+
+ constructor(private method: TransactionObject<any>) {
+ super();
+ }
+
+ read<T>(tx: ITransactionConfig): Promise<T> {
+ console.log("args", arguments);
+ return this.method.call(tx as any);
+ }
+
+ write(tx: ITransactionConfig) {
+ const result = {
+ onTransactionHash: (doNothing as any),
+ onReceipt: doNothing,
+ onError: doNothing
+ };
+ setTimeout(() => {
+ this.method.send(tx as any).
+ once("transactionHash", result.onTransactionHash).
+ once("receipt", result.onTransactionHash).
+ once("error", result.onError);
+ }, 0);
+ return result;
+ }
+
+ estimateGas(tx: ITransactionConfig): Promise<number> {
+ console.log("args", arguments);
+ return this.method.estimateGas(tx as any);
+ }
+
+}
\ No newline at end of file

MODIFY
src/model/eth_contract.ts
src/model/eth_contract.ts
import { ITransactionConfig, ISendResult, ITransactionResult } from "../interfaces";
+import { BaseContractMethod } from "@/abstracts";
export abstract class BaseContract {
@@ -6,7 +7,5 @@ export abstract class BaseContract {
}
- abstract read<T>(methodName: string, tx: ITransactionConfig, ...args): Promise<T>;
- abstract write(methodName: string, tx: ITransactionConfig, ...args): ISendResult;
- abstract createTransaction(methodName: string, tx?: ITransactionConfig, ...args): ITransactionResult;
+ abstract method(methodName: string, ...args): BaseContractMethod;
}
\ No newline at end of file

MODIFY
src/plasma/erc20.ts
src/plasma/erc20.ts
@@ -14,32 +14,38 @@ export class ERC20 extends BaseToken {
getBalance(tokenAddress: string, userAddress: string, isParent?: boolean) {
const contract = this.getContract(tokenAddress, isParent);
+ const method = contract.method(
+ "balanceOf",
+ userAddress
+ );
return createTransactionConfig(
{
txConfig: {},
defaultTxConfig: this.parentDefaultConfig,
}).then(config => {
console.log("config", config);
- return contract.read<string>("balanceOf", config, userAddress);
+ return method.read<string>(config);
});
}
- approve(tokenAddress: string, amount: BN | string | number, txConfig?: ITransactionConfig) {
+ approve(tokenAddress: string, amount: BN | string | number, txConfig: ITransactionConfig = {}) {
const contract = this.getContract(tokenAddress, true);
const methodName = "approve";
+ const method = contract.method(
+ methodName,
+ this.depositManager.contract.address,
+ formatAmount(amount)
+ );
return createTransactionConfig(
{
txConfig,
defaultTxConfig: this.parentDefaultConfig,
- txResult: contract.createTransaction(methodName),
client: this.client.parent.client,
- isWrite: true
+ isWrite: true,
+ method
}).then(config => {
- return contract.write(
- "approve",
+ return method.write(
config,
- this.depositManager.contract.address,
- formatAmount(amount)
);
});
}

MODIFY
src/utils/create_tx_config.ts
src/utils/create_tx_config.ts
import { ITransactionResult, ITransactionConfig } from "@/interfaces";
import { BaseWeb3Client } from "@/model";
+import { BaseContractMethod } from "@/abstracts";
interface ITransactionConfigParam {
txConfig: ITransactionConfig;
defaultTxConfig: ITransactionConfig;
- txResult?: ITransactionResult;
+ method?: BaseContractMethod;
client?: BaseWeb3Client;
isWrite?: boolean;
}
-export const createTransactionConfig = async ({ txConfig, defaultTxConfig, txResult, client, isWrite }: ITransactionConfigParam) => {
+export const createTransactionConfig = async ({ txConfig, defaultTxConfig, method, client, isWrite }: ITransactionConfigParam) => {
txConfig = Object.assign(defaultTxConfig, txConfig || {});
if (isWrite) {
const [gas, gasPrice, nonce, chainId] = await Promise.all([
!(txConfig.gas)
- ? txResult.estimateGas({ from: txConfig.from, value: txConfig.value })
+ ? method.estimateGas({ from: txConfig.from, value: txConfig.value })
: txConfig.gas,
!txConfig.gasPrice ? client.getGasPrice() : txConfig.gasPrice,
!txConfig.nonce ? client.getTransactionCount(txConfig.from as string, 'pending') : txConfig.nonce,

MODIFY
test/debug.js
test/debug.js
@@ -7,6 +7,7 @@ const from = env.user1_address
const execute = async () => {
const privateKey = process.env.user1_privateKey;
+ const erc20TokenAddress = env.PLASMA_MUMBAI_ERC20;
const matic = new PlasmaClient({
network: 'testnet',
version: 'mumbai',
@@ -26,6 +27,12 @@ const execute = async () => {
const balance = await matic.erc20.getBalance(env.PLASMA_MUMBAI_ERC20, from)
console.log('balance', balance)
+
+ // const result = await matic.erc20.approve(erc20TokenAddress, '1000000000000000');
+
+ // result.onError = function () {
+ // console.log("error args", arguments);
+ // }
}
execute().then(_ => {

add abstract class for contract method