take tokenAddress in methods - store in token instance
MODIFY
src/constant.ts
src/constant.ts
@@ -5,3 +5,5 @@ export let Web3Client: typeof MaticWeb3Client;
export const setWeb3Client = (web3Client) => {
Web3Client = web3Client;
}
+
+export const EXTRA_GAS_FOR_PROXY_CALL = 1000000

MODIFY
src/inherited/eth_method.ts
src/inherited/eth_method.ts
@@ -21,10 +21,25 @@ export class EthMethod extends BaseContractMethod {
onError: doNothing
};
setTimeout(() => {
+ console.log("sending tx", tx);
+ // {
+ // chainId: tx.chainId,
+ // data: tx.data,
+ // from: tx.from.toString(),
+ // gas: tx.gas,
+ // gasPrice: tx.gasPrice.toString(),
+ // nonce: tx.nonce,
+ // to: tx.to,
+ // value: tx.value?.toString()
+ // }
+ try {
this.method.send(tx as any).
once("transactionHash", result.onTransactionHash).
once("receipt", result.onTransactionHash).
once("error", result.onError);
+ } catch (error) {
+
+ }
}, 0);
return result;
}

MODIFY
src/interfaces/index.ts
src/interfaces/index.ts
@@ -4,3 +4,4 @@ export * from "./method";
export * from "./transaction_config";
export * from "./send_result";
export * from "./transaction";
+export * from "./transaction_option";
\ No newline at end of file

ADD
src/interfaces/transaction_option.ts
+import { ITransactionConfig } from "./transaction_config";
+
+export interface ITransactionOption extends ITransactionConfig {
+ isParent?: boolean;
+ returnTransaction?: boolean;
+}
\ No newline at end of file

MODIFY
src/model/base_token.ts
src/model/base_token.ts
import { Web3SideChainClient } from "./web3_side_chain_client";
+import { ITransactionConfig, ITransactionOption } from "@/interfaces";
+import { BaseContractMethod } from "@/abstracts";
+import { BaseWeb3Client } from "./web3_client";
+import { EXTRA_GAS_FOR_PROXY_CALL } from "@/constant";
+import { BaseContract } from "./eth_contract";
+
+interface ITransactionConfigParam {
+ txConfig: ITransactionConfig;
+ method?: BaseContractMethod;
+ isWrite?: boolean;
+ isParent?: boolean;
+}
+
+
export class BaseToken {
constructor(
+ private tokenAddress: string,
public client: Web3SideChainClient,
public abi
- ) { }
+ ) {
+
+ }
- getContract(address: string, isParent: boolean) {
+ getContract({ isParent }: ITransactionOption) {
const client = isParent ? this.client.parent.client :
this.client.child.client;
- return client.getContract(address, this.abi);
+ return client.getContract(this.tokenAddress, this.abi);
}
get parentDefaultConfig() {
@@ -22,4 +39,27 @@ export class BaseToken {
return this.client.config.child.defaultConfig;
}
+ createTransactionConfig = async ({ txConfig, method, isParent, isWrite }: ITransactionConfigParam) => {
+ txConfig = Object.assign(this.parentDefaultConfig, txConfig || {});
+ console.log("txConfig", txConfig);
+ const client = isParent ? this.client.parent.client :
+ this.client.child.client;
+ if (isWrite) {
+ const [gas, gasPrice, nonce, chainId] = await Promise.all([
+ !(txConfig.gas)
+ ? 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,
+ !txConfig.chainId ? client.getChainId() : txConfig.chainId,
+ ]);
+
+ // txConfig.gas = isParent ? Number(gas) + EXTRA_GAS_FOR_PROXY_CALL : gas;
+ txConfig.gasPrice = gasPrice;
+ txConfig.nonce = nonce;
+ txConfig.chainId = chainId;
+ }
+ return txConfig;
+ };
+
}
\ No newline at end of file

MODIFY
src/plasma/erc20.ts
src/plasma/erc20.ts
import { BaseToken, Web3SideChainClient } from "@/model";
import BN from "bn.js";
-import { ITransactionConfig } from "@/interfaces";
+import { ITransactionConfig, ITransactionOption } from "@/interfaces";
import { formatAmount, IEventBusPromise, eventBusPromise } from "../utils";
import { createTransactionConfig } from "@/utils/create_tx_config";
import { DepositManager } from "./deposit_manager";
export class ERC20 extends BaseToken {
- constructor(client: Web3SideChainClient, abi: string,
+ constructor(
+ tokenAddress: string,
+ client: Web3SideChainClient,
public depositManager: DepositManager) {
- super(client, abi);
+ super(tokenAddress, client, client.getABI('ChildERC20'));
}
- getBalance(tokenAddress: string, userAddress: string, isParent?: boolean) {
- const contract = this.getContract(tokenAddress, isParent);
+ getBalance(userAddress: string, option: ITransactionOption = {}) {
+ const contract = this.getContract(option);
const method = contract.method(
"balanceOf",
userAddress
@@ -21,31 +23,30 @@ export class ERC20 extends BaseToken {
return createTransactionConfig(
{
txConfig: {},
- defaultTxConfig: this.parentDefaultConfig,
+ defaultTxConfig: this.childDefaultConfig,
}).then(config => {
console.log("config", config);
return method.read<string>(config);
});
}
- approve(tokenAddress: string, amount: BN | string | number, txConfig: ITransactionConfig = {}) {
- const contract = this.getContract(tokenAddress, true);
- const methodName = "approve";
+ approve(amount: BN | string | number, option: ITransactionOption = {}) {
+ option.isParent = true;
+ const contract = this.getContract(option);
const method = contract.method(
- methodName,
+ "approve",
this.depositManager.contract.address,
formatAmount(amount)
);
console.log("arguments approve", arguments);
const result = eventBusPromise((res, rej) => {
// tslint:disable-next-line
- createTransactionConfig(
+ this.createTransactionConfig(
{
- txConfig,
- defaultTxConfig: this.parentDefaultConfig,
- client: this.client.parent.client,
+ txConfig: option,
isWrite: true,
- method
+ method,
+ isParent: true
}).then(config => {
console.log("config", config);
const methodResult = method.write(

MODIFY
src/plasma/index.ts
src/plasma/index.ts
@@ -5,7 +5,7 @@ import { DepositManager } from "./deposit_manager";
export class PlasmaClient {
- erc20: ERC20;
+
private client_: Web3SideChainClient;
@@ -13,6 +13,14 @@ export class PlasmaClient {
depositManager: DepositManager;
+ erc20(tokenAddress: string) {
+ return new ERC20(
+ tokenAddress,
+ this.client_,
+ this.depositManager
+ );
+ }
+
constructor(config: IPlasmaClientConfig) {
this.client_ = new Web3SideChainClient(config);
@@ -32,12 +40,6 @@ 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
test/debug.js
test/debug.js
@@ -7,7 +7,8 @@ const from = env.user1_address
const execute = async () => {
const privateKey = process.env.user1_privateKey;
- const erc20TokenAddress = env.PLASMA_MUMBAI_ERC20;
+ const erc20TokenMumbaiAddress = env.PLASMA_MUMBAI_ERC20;
+ const goerliTokenERC20 = env.PLASMA_GOERLI_ERC20
const matic = new PlasmaClient({
network: 'testnet',
version: 'mumbai',
@@ -25,13 +26,18 @@ const execute = async () => {
}
});
- // const balance = await matic.erc20.getBalance(env.PLASMA_MUMBAI_ERC20, from)
+ const childErc20 = matic.erc20(erc20TokenMumbaiAddress);
+
+ const balance = await childErc20.getBalance(from);
+ console.log('balance', balance);
+
+ // const balance = await matic.erc20.getBalance(erc20TokenMumbaiAddress, from)
// console.log('balance', balance)
- const result = matic.erc20.approve(erc20TokenAddress, '1000000000000000');
- console.log("result", result, result.on);
+ // const result = matic.erc20.approve(goerliTokenERC20, '1000000000000000');
+ // console.log("result", result, result.on);
- await result;
+ // await result;
// result.onError = function () {
// console.log("error args", arguments);
// }

take tokenAddress in methods - store in token instance