implement root and child check in all api
MODIFY
src/plasma/erc20.ts
src/plasma/erc20.ts
import { TYPE_AMOUNT } from "../types";
import { Log_Event_Signature } from "../enums";
-import { BaseContract } from "../abstracts";
import { IPlasmaContracts, ITransactionOption } from "../interfaces";
import { BaseToken, Converter, promiseResolve, Web3SideChainClient } from "../utils";
-import { ErcPredicate } from "./erc20_predicate";
import { PlasmaToken } from "./plasma_token";
export class ERC20 extends PlasmaToken {
@@ -39,7 +37,7 @@ export class ERC20 extends PlasmaToken {
}
approve(amount: TYPE_AMOUNT, option: ITransactionOption = {}) {
- this.checkForParent("approve");
+ this.checkForRoot_("approve");
return this.getContract().then(contract => {
const method = contract.method(
"approve",
@@ -59,7 +57,7 @@ export class ERC20 extends PlasmaToken {
}
deposit(amount: TYPE_AMOUNT, userAddress: string, option: ITransactionOption = {}) {
- this.checkForParent("deposit");
+ this.checkForRoot_("deposit");
return this.contracts_.depositManager.getContract().then(contract => {
const method = contract.method(
@@ -73,7 +71,7 @@ export class ERC20 extends PlasmaToken {
}
withdrawStart(amount: TYPE_AMOUNT, option?: ITransactionOption) {
- this.checkForChild("withdrawStart");
+ this.checkForChild_("withdrawStart");
return this.getContract().then(tokenContract => {
@@ -86,6 +84,8 @@ export class ERC20 extends PlasmaToken {
}
private withdrawChallenge_(burnTxHash: string, isFast: boolean, option: ITransactionOption) {
+ this.checkForRoot_("withdrawChallenge");
+
return Promise.all([
this.getPredicate(),
this.contracts_.exitManager.buildPayloadForExit(

MODIFY
src/plasma/erc721.ts
src/plasma/erc721.ts
@@ -66,6 +66,8 @@ export class ERC721 extends PlasmaToken {
* @param options
*/
safeDeposit(tokenId: string | number, userAddress: string, options?: ITransactionOption) {
+ this.checkForRoot_("safeDeposit");
+
return this.getContract().then(contract => {
const method = contract.method(
"safeTransferFrom",
@@ -79,6 +81,9 @@ export class ERC721 extends PlasmaToken {
}
withdrawStart(tokenId: string | number, options: ITransactionOption = {}) {
+ this.checkForChild_("withdrawStart");
+
+
return this.getContract().then(contract => {
const method = contract.method(
"withdraw",
@@ -94,6 +99,8 @@ export class ERC721 extends PlasmaToken {
private withdrawChallenge_(burnTxHash: string, isFast: boolean, option: ITransactionOption) {
+ this.checkForRoot_("withdrawChallenge");
+
return Promise.all([
this.getPredicate(),
this.contracts_.exitManager.buildPayloadForExit(

MODIFY
src/pos/erc20.ts
src/pos/erc20.ts
@@ -35,9 +35,8 @@ export class ERC20 extends POSToken {
}
approve(amount: TYPE_AMOUNT, option?: ITransactionOption) {
- if (!this.contractParam.isParent) {
- this.client.logger.error(ERROR_TYPE.AllowedOnRoot, "approve").throw();
- }
+ this.checkForRoot_("approve");
+
return Promise.all([this.getPredicateAddress(), this.getContract()]).then(result => {
const [predicateAddress, contract] = result;
const method = contract.method(
@@ -66,9 +65,8 @@ 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();
- }
+ this.checkForRoot_("deposit");
+
const amountInABI = this.client.parent.encodeParameters(
[Converter.toHex(amount)],
@@ -91,9 +89,8 @@ 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();
- }
+ this.checkForChild_("withdrawStart");
+
return this.getContract().then(contract => {
const method = contract.method(
@@ -113,9 +110,7 @@ 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();
- }
+ this.checkForRoot_("withdrawExit");
return this.exitManager.buildPayloadForExit(
burnTransactionHash,
@@ -139,9 +134,8 @@ 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();
- }
+ this.checkForRoot_("withdrawExitFaster");
+
return this.exitManager.buildPayloadForExit(
burnTransactionHash,

MODIFY
src/pos/erc721.ts
src/pos/erc721.ts
@@ -99,6 +99,8 @@ export class ERC721 extends POSToken {
}
isApproved(tokenId: string, option?: ITransactionOption) {
+ this.checkForRoot_("isApproved");
+
return this.getContract().then(contract => {
const method = contract.method(
"getApproved",
@@ -114,6 +116,8 @@ export class ERC721 extends POSToken {
}
isApprovedAll(userAddress: string, option?: ITransactionOption) {
+ this.checkForRoot_("isApprovedAll");
+
return Promise.all([this.getContract(), this.getPredicateAddress()]).then(result => {
const [contract, predicateAddress] = result;
const method = contract.method(
@@ -127,6 +131,8 @@ export class ERC721 extends POSToken {
}
approve(tokenId: TYPE_AMOUNT, option?: ITransactionOption) {
+ this.checkForRoot_("approve");
+
return Promise.all([this.getContract(), this.getPredicateAddress()]).then(result => {
const [contract, predicateAddress] = result;
const method = contract.method(
@@ -139,6 +145,8 @@ export class ERC721 extends POSToken {
}
approveAll(option?: ITransactionOption) {
+ this.checkForRoot_("approveAll");
+
return Promise.all([this.getContract(), this.getPredicateAddress()]).then(result => {
const [contract, predicateAddress] = result;
const method = contract.method(
@@ -152,6 +160,8 @@ export class ERC721 extends POSToken {
deposit(tokenId: TYPE_AMOUNT, userAddress: string, option?: ITransactionOption) {
+ this.checkForRoot_("deposit");
+
const amountInABI = this.client.parent.encodeParameters(
[Converter.toHex(tokenId)],
['uint256'],
@@ -165,6 +175,8 @@ export class ERC721 extends POSToken {
}
depositMany(tokenIds: TYPE_AMOUNT[], userAddress: string, option?: ITransactionOption) {
+ this.checkForRoot_("depositMany");
+
const tokensInUint256 = this.validateMany__(tokenIds);
const amountInABI = this.client.parent.encodeParameters(
@@ -180,6 +192,9 @@ export class ERC721 extends POSToken {
}
withdrawStart(tokenId: TYPE_AMOUNT, option?: ITransactionOption) {
+ this.checkForChild_("withdrawStart");
+
+
return this.getContract().then(contract => {
const method = contract.method(
"withdraw",
@@ -190,6 +205,9 @@ export class ERC721 extends POSToken {
}
withdrawStartMany(tokenIds: TYPE_AMOUNT[], option?: ITransactionOption) {
+ this.checkForChild_("withdrawStartMany");
+
+
const tokensInUint256 = this.validateMany__(tokenIds);
return this.getContract().then(contract => {
const method = contract.method(
@@ -201,6 +219,9 @@ export class ERC721 extends POSToken {
}
withdrawExit(burnTransactionHash: string, option?: ITransactionOption) {
+ this.checkForRoot_("withdrawExit");
+
+
return this.exitManager.buildPayloadForExit(
burnTransactionHash,
Log_Event_Signature.Erc721Transfer,
@@ -213,6 +234,9 @@ export class ERC721 extends POSToken {
}
withdrawExitMany(burnTransactionHash: string, option?: ITransactionOption) {
+ this.checkForRoot_("withdrawExitMany");
+
+
return this.exitManager.buildPayloadForExit(
burnTransactionHash,
Log_Event_Signature.Erc721BatchTransfer,
@@ -225,6 +249,9 @@ export class ERC721 extends POSToken {
}
withdrawExitFaster(burnTransactionHash: string, option?: ITransactionOption) {
+ this.checkForRoot_("withdrawExitFaster");
+
+
return this.exitManager.buildPayloadForExit(
burnTransactionHash,
Log_Event_Signature.Erc721Transfer,
@@ -237,6 +264,9 @@ export class ERC721 extends POSToken {
}
withdrawExitFasterMany(burnTransactionHash: string, option?: ITransactionOption) {
+ this.checkForRoot_("withdrawExitFasterMany");
+
+
return this.exitManager.buildPayloadForExit(
burnTransactionHash,
Log_Event_Signature.Erc721BatchTransfer,

MODIFY
src/utils/base_token.ts
src/utils/base_token.ts
@@ -192,20 +192,6 @@ export class BaseToken {
return txConfig;
}
- protected checkForParent(methodName) {
- if (!this.contractParam.isParent) {
- this.client.logger.error(
- ERROR_TYPE.AllowedOnRoot, methodName
- ).throw();
- }
- }
-
- protected checkForChild(methodName) {
- if (this.contractParam.isParent) {
- this.client.logger.error(ERROR_TYPE.AllowedOnChild, methodName).throw();
- }
- }
-
protected transferERC20_(to: string, amount: TYPE_AMOUNT, option?: ITransactionOption) {
return this.getContract().then(contract => {
const method = contract.method(
@@ -233,4 +219,16 @@ export class BaseToken {
});
}
+ protected checkForRoot_(methodName) {
+ if (!this.contractParam.isParent) {
+ this.client.logger.error(ERROR_TYPE.AllowedOnRoot, methodName).throw();
+ }
+ }
+
+ protected checkForChild_(methodName) {
+ if (this.contractParam.isParent) {
+ this.client.logger.error(ERROR_TYPE.AllowedOnChild, "withdrawStart").throw();
+ }
+ }
+
}
\ No newline at end of file

implement root and child check in all api