add: isApprovalNeeded
ADD
examples/hermez/erc20/is_aproval_needed.js
+const { getHermezClient, hermez, from } = require('../../utils_hermez');
+
+const execute = async () => {
+ const client = await getHermezClient();
+ const erc20Token = client.erc20(hermez.parent.erc20, true);
+
+ const result = await erc20Token.isApprovalNeeded();
+ console.log("result", result);
+
+}
+execute().then(() => {
+}).catch(err => {
+ console.error("err", err);
+}).finally(_ => {
+ process.exit(0);
+})
\ No newline at end of file

MODIFY
src/hermez/erc20.ts
src/hermez/erc20.ts
@@ -43,7 +43,7 @@ export class ERC20 extends HermezToken {
* @memberof ERC20
*/
getBalance(userAddress: string, option?: ITransactionOption) {
- if (this.contractAddress === ADDRESS_ZERO) {
+ if (this.contractParam.address === ADDRESS_ZERO) {
const client = this.contractParam.isParent ? this.client.parent : this.client.child;
return client.getBalance(userAddress);
} else {
@@ -58,6 +58,25 @@ export class ERC20 extends HermezToken {
}
+ /**
+ * is Approval needed to bridge tokens to other chains
+ *
+ * @returns
+ * @memberof ERC20
+ */
+ isApprovalNeeded() {
+ if (this.contractParam.address === ADDRESS_ZERO) {
+ return false;
+ }
+
+ const bridge = this.contractParam.isParent ? this.parentBridge : this.childBridge;
+
+ return bridge.wrappedTokenToTokenInfo(this.contractParam.address)
+ .then(tokenInfo => {
+ return tokenInfo[1] === ADDRESS_ZERO;
+ });
+ }
+
/**
* get allowance of user
*
@@ -68,8 +87,7 @@ export class ERC20 extends HermezToken {
*/
getAllowance(userAddress: string, option: IAllowanceTransactionOption = {}) {
this.checkForNonNative("getAllowance");
- const spenderAddress = option.spenderAddress ? option.spenderAddress : (
- this.contractParam.isParent ? this.parentBridge.contractAddress : this.childBridge.contractAddress);
+ const spenderAddress = option.spenderAddress ? option.spenderAddress : this.getBridgeAddress();
return this.getContract().then(contract => {
const method = contract.method(
@@ -91,8 +109,7 @@ export class ERC20 extends HermezToken {
*/
approve(amount: TYPE_AMOUNT, option: IApproveTransactionOption = {}) {
this.checkForNonNative("approve");
- const spenderAddress = option.spenderAddress ? option.spenderAddress : (
- this.contractParam.isParent ? this.parentBridge.contractAddress : this.childBridge.contractAddress);
+ const spenderAddress = option.spenderAddress ? option.spenderAddress : this.getBridgeAddress();
return this.getContract().then(contract => {
const method = contract.method(
@@ -399,7 +416,7 @@ export class ERC20 extends HermezToken {
name,
version: "1",
chainId,
- verifyingContract: this.contractAddress,
+ verifyingContract: this.contractParam.address,
},
message: {}
};
@@ -583,8 +600,7 @@ export class ERC20 extends HermezToken {
getPermitData(amount: TYPE_AMOUNT, option: IApproveTransactionOption = {}) {
this.checkForNonNative("getPermitData");
- const spenderAddress = option.spenderAddress ? option.spenderAddress : (
- this.contractParam.isParent ? this.parentBridge.contractAddress : this.childBridge.contractAddress);
+ const spenderAddress = option.spenderAddress ? option.spenderAddress : this.getBridgeAddress();
return this.getPermitData_(amount, spenderAddress);
}

MODIFY
test/config.js
test/config.js
-// const dotenv = require('dotenv').config();
+// const dotenv = require('dotenv');
// const path = require('path');
// dotenv.config({
// path: path.join(__dirname, '.env')

MODIFY
test/specs/hermez/erc20.spec.ts
test/specs/hermez/erc20.spec.ts
@@ -179,6 +179,12 @@ describe('ERC20', () => {
});
// APPROVE
+ it('approval needed', async () => {
+ const isApprovalNeeded = await erc20Parent.isApprovalNeeded();
+ console.log('isApprovalNeeded', isApprovalNeeded);
+ expect(isApprovalNeeded).to.be.an('boolean').equal(true);
+ });
+
it('approve parent return tx', async () => {
const result = await erc20Parent.approve('10000', {
returnTransaction: true

add: isApprovalNeeded