fix issue with write api for ethers
MODIFY
src/ethers/ethjs_method.ts
src/ethers/ethjs_method.ts
@@ -9,12 +9,21 @@ export class ContractMethod extends BaseContractMethod {
super(logger);
}
+ private toConfig_(config: ITransactionConfig = {}) {
+ return {
+ gasPrice: config.gasPrice,
+ gasLimit: config.gasLimit,
+ value: config.value,
+ nonce: config.nonce
+ };
+ }
+
encodeABI() {
return this.contract.interface.functions.encode[this.methodName](...this.args);
}
- estimateGas(tx: ITransactionConfig) {
- return this.contract.estimateGas[this.methodName](...this.args, tx).then(result => {
+ estimateGas(config: ITransactionConfig = {}) {
+ return this.contract.estimateGas[this.methodName](...this.args, this.toConfig_(config)).then(result => {
return result.toNumber();
});
}
@@ -28,8 +37,8 @@ export class ContractMethod extends BaseContractMethod {
});
}
- private getMethod_(config: ITransactionConfig) {
- return this.contract[this.methodName](...this.args, config);
+ private getMethod_(config: ITransactionConfig = {}) {
+ return this.contract[this.methodName](...this.args, this.toConfig_(config));
}
write(config: ITransactionConfig) {
@@ -39,14 +48,13 @@ export class ContractMethod extends BaseContractMethod {
onReceiptError: doNothing,
onTxError: doNothing
} as ISendResult;
- this.getMethod_(config).then(tx => {
- result.onTransactionHash(tx.transactionHash);
- // wait for 10 sec for confirmation
- // TODO - get the time from user
- return tx.wait(10000);
+ this.getMethod_(config).then(response => {
+ result.onTransactionHash(response.hash);
+ return response.wait();
}).then(receipt => {
result.onReceipt(receipt);
}).catch(err => {
+ console.log("error", err);
result.onTxError(err);
result.onReceiptError(err);
});

MODIFY
src/ethers/web3_client.ts
src/ethers/web3_client.ts
@@ -81,7 +81,7 @@ export class EtherWeb3Client extends BaseWeb3Client {
chainId: config.chainId,
data: config.data,
from: config.from as any,
- gasLimit: config.gas,
+ gasLimit: config.gasLimit,
gasPrice: config.gasPrice as any,
nonce: config.nonce,
to: config.to,

MODIFY
src/interfaces/transaction_config.ts
src/interfaces/transaction_config.ts
@@ -4,7 +4,7 @@ export interface ITransactionConfig {
from?: string | number;
to?: string;
value?: number | string | BN;
- gas?: number | string;
+ gasLimit?: number | string;
gasPrice?: number | string | BN;
data?: string;
nonce?: number;

MODIFY
src/pos/erc20.ts
src/pos/erc20.ts
@@ -4,7 +4,7 @@ import { formatAmount, Web3SideChainClient } from "../utils";
import { POSToken } from "./pos_token";
import { TYPE_AMOUNT } from "../types";
import { ExitManager } from "./exit_manager";
-import { Log_Event_Signature } from "../enums";
+import { Log_Event_Signature, ERROR_TYPE } from "../enums";
export class ERC20 extends POSToken {
@@ -33,6 +33,10 @@ export class ERC20 extends POSToken {
}
approve(amount: TYPE_AMOUNT, option?: ITransactionOption) {
+ if (!this.contractParam.isParent) {
+ this.client.logger.error(ERROR_TYPE.AllowedOnRoot, "approve").throw();
+ }
+
const contract = this.contract;
return this.getPredicateAddress().then(predicateAddress => {
const method = contract.method(
@@ -61,6 +65,10 @@ export class ERC20 extends POSToken {
* @memberof ERC20
*/
deposit(amount: TYPE_AMOUNT, userAddress: string, option?: ITransactionOption) {
+ if (!this.contractParam.isParent) {
+ this.client.logger.error(ERROR_TYPE.AllowedOnRoot, "deposit").throw();
+ }
+
const amountInABI = this.client.parent.encodeParameters(
[formatAmount(amount)],
['uint256'],
@@ -82,6 +90,10 @@ export class ERC20 extends POSToken {
* @memberof ERC20
*/
withdrawStart(amount: TYPE_AMOUNT, option?: ITransactionOption) {
+ if (this.contractParam.isParent) {
+ this.client.logger.error(ERROR_TYPE.AllowedOnChild, "withdrawStart").throw();
+ }
+
const contract = this.contract;
const method = contract.method(
"withdraw",
@@ -99,6 +111,10 @@ export class ERC20 extends POSToken {
* @memberof ERC20
*/
withdrawExit(burnTransactionHash: string, option?: ITransactionOption) {
+ if (!this.contractParam.isParent) {
+ this.client.logger.error(ERROR_TYPE.AllowedOnRoot, "withdrawExit").throw();
+ }
+
return this.exitManager.buildPayloadForExit(
burnTransactionHash,
Log_Event_Signature.Erc20Transfer,
@@ -121,6 +137,10 @@ export class ERC20 extends POSToken {
* @memberof ERC20
*/
withdrawExitFaster(burnTransactionHash: string, option?: ITransactionOption) {
+ if (!this.contractParam.isParent) {
+ this.client.logger.error(ERROR_TYPE.AllowedOnRoot, "withdrawExitFaster").throw();
+ }
+
return this.exitManager.buildPayloadForExit(
burnTransactionHash,
Log_Event_Signature.Erc20Transfer,
@@ -139,7 +159,7 @@ export class ERC20 extends POSToken {
* @returns
* @memberof ERC20
*/
- isExited(txHash: string) {
+ isWithdrawExited(txHash: string) {
if (!txHash) {
throw new Error(`txHash not provided`);
}

MODIFY
src/utils/base_token.ts
src/utils/base_token.ts
@@ -90,16 +90,16 @@ export class BaseToken {
const client = isParent ? this.client.parent :
this.client.child;
if (isWrite) {
- const [gas, gasPrice, nonce, chainId] = await Promise.all([
- !(txConfig.gas)
+ const [gasLimit, gasPrice, nonce, chainId] = await Promise.all([
+ !(txConfig.gasLimit)
? method.estimateGas({ from: txConfig.from, value: txConfig.value })
- : txConfig.gas,
+ : txConfig.gasLimit,
!txConfig.gasPrice ? client.getGasPrice() : txConfig.gasPrice,
!txConfig.nonce ? client.getTransactionCount(txConfig.from as string, 'pending') : txConfig.nonce,
!txConfig.chainId ? client.getChainId() : txConfig.chainId,
]);
console.log("calculated");
- txConfig.gas = isParent ? Number(gas) + EXTRA_GAS_FOR_PROXY_CALL : gas;
+ txConfig.gasLimit = isParent ? Number(gasLimit) + EXTRA_GAS_FOR_PROXY_CALL : gasLimit;
txConfig.gasPrice = gasPrice;
txConfig.nonce = nonce;
txConfig.chainId = chainId;

MODIFY
src/web3js/eth_method.ts
src/web3js/eth_method.ts
import { BaseContractMethod } from "../abstracts";
import { ITransactionConfig } from "../interfaces";
-import { TransactionObject } from "web3/eth/types";
+import { TransactionObject, Tx } from "web3/eth/types";
import { doNothing } from "../helpers";
import { Logger } from "../utils";
@@ -10,9 +10,24 @@ export class EthMethod extends BaseContractMethod {
super(logger);
}
+ private toConfig_(config: ITransactionConfig = {}) {
+ return {
+ chainId: config.chainId,
+ data: config.data,
+ from: config.from,
+ gas: config.gasLimit,
+ gasPrice: config.gasPrice,
+ nonce: config.nonce,
+ to: config.to,
+ value: config.value
+ } as Tx;
+ }
+
read<T>(tx: ITransactionConfig): Promise<T> {
this.logger.log("sending tx with config", tx);
- return this.method.call(tx as any);
+ return this.method.call(
+ this.toConfig_(tx)
+ );
}
write(tx: ITransactionConfig) {
@@ -24,8 +39,9 @@ export class EthMethod extends BaseContractMethod {
};
setTimeout(() => {
this.logger.log("sending tx with config", tx);
- this.method.send(tx as any).
- once("transactionHash", result.onTransactionHash).
+ this.method.send(
+ this.toConfig_(tx)
+ ).once("transactionHash", result.onTransactionHash).
once("receipt", result.onReceipt).
on("error", result.onTxError).
on("error", result.onReceiptError);

MODIFY
test/debug.js
test/debug.js
@@ -11,7 +11,7 @@ const from = user1.address;
const execute = async () => {
const privateKey = user1.privateKey;
const mumbaiERC720 = pos.child.erc20;
- const goerliERC721 = pos.parent.erc20;
+ const goerliERC720 = pos.parent.erc20;
// const matic = new POSClient({
// network: 'testnet',
// version: 'mumbai',
@@ -49,20 +49,24 @@ const execute = async () => {
}
});
- const mumbaiERC720Token = matic.erc20(mumbaiERC720);
+ // const mumbaiERC720Token = matic.erc20(mumbaiERC720);
+ const goerliERC720Token = matic.erc20(goerliERC720, true);
// return console.log("balance", await mumbaiERC720Token.getBalance(from));
-
- const tx = await mumbaiERC720Token.withdrawStart(
- 10,
- {
- nonce: 1974,
- // gasPrice: '1000',
- // gas: 100
- }
- );
+ const tx = await goerliERC720Token.approve(100);
console.log("txHash", await tx.getTransactionHash());
console.log("txReceipt", await tx.getReceipt());
+
+ // const tx = await mumbaiERC720Token.withdrawStart(
+ // 10,
+ // {
+ // // nonce: 1974,
+ // // gasPrice: '1000',
+ // // gas: 100
+ // }
+ // );
+ // console.log("txHash", await tx.getTransactionHash());
+ // console.log("txReceipt", await tx.getReceipt());
}
execute().then(_ => {

fix issue with write api for ethers