fix spenderAddress initialization issue, throw error if spender is not provider in case of child
MODIFY
src/enums/error_type.ts
src/enums/error_type.ts
@@ -5,5 +5,6 @@ export enum ERROR_TYPE {
ProofAPINotSet = "proof_api_not_set",
TransactionOptionNotObject = "transation_object_not_object",
BurnTxNotCheckPointed = "burn_tx_not_checkpointed",
- EIP1559NotSupported = "eip-1559_not_supported"
+ EIP1559NotSupported = "eip-1559_not_supported",
+ NullSpenderAddress = "null_spender_address"
}
\ No newline at end of file

MODIFY
src/pos/erc20.ts
src/pos/erc20.ts
@@ -2,7 +2,7 @@ import { ITransactionOption } from "../interfaces";
import { Converter, Web3SideChainClient } from "../utils";
import { POSToken } from "./pos_token";
import { TYPE_AMOUNT } from "../types";
-import { Log_Event_Signature } from "../enums";
+import { ERROR_TYPE, Log_Event_Signature } from "../enums";
import { IAllowanceTransactionOption, IApproveTransactionOption, IPOSClientConfig, MAX_AMOUNT, promiseResolve } from "..";
import { IPOSContracts } from "../interfaces/pos_contracts";
@@ -42,8 +42,8 @@ export class ERC20 extends POSToken {
*/
getAllowance(userAddress: string, option: IAllowanceTransactionOption = {}) {
const spenderAddress = option.spenderAddress;
- const predicatePromise = spenderAddress ?
- this.getPredicateAddress() : promiseResolve(spenderAddress);
+
+ const predicatePromise = spenderAddress ? promiseResolve(spenderAddress) : this.getPredicateAddress();
return Promise.all([predicatePromise, this.getContract()]).then(result => {
const [predicateAddress, contract] = result;
@@ -56,10 +56,14 @@ export class ERC20 extends POSToken {
});
}
- approve(amount: TYPE_AMOUNT, option?: IApproveTransactionOption) {
+ approve(amount: TYPE_AMOUNT, option: IApproveTransactionOption = {}) {
const spenderAddress = option.spenderAddress;
- const predicatePromise = spenderAddress ?
- this.getPredicateAddress() : promiseResolve(spenderAddress);
+
+ if (!spenderAddress && !this.contractParam.isParent) {
+ this.client.logger.error(ERROR_TYPE.NullSpenderAddress).throw();
+ }
+
+ const predicatePromise = spenderAddress ? promiseResolve(spenderAddress) : this.getPredicateAddress();
return Promise.all([predicatePromise, this.getContract()]).then(result => {
const [predicateAddress, contract] = result;

MODIFY
src/utils/error_helper.ts
src/utils/error_helper.ts
@@ -39,6 +39,9 @@ export class ErrorHelper implements IError {
case ERROR_TYPE.EIP1559NotSupported:
errMsg = `${info ? 'Root' : 'Child'} chain doesn't support eip-1559`;
break;
+ case ERROR_TYPE.NullSpenderAddress:
+ errMsg = `Please provide spender address.`;
+ break;
default:
if (!this.type) {
this.type = ERROR_TYPE.Unknown;

MODIFY
test/specs/erc20.spec.ts
test/specs/erc20.spec.ts
@@ -118,7 +118,7 @@ describe('ERC20', () => {
});
- it('approve return tx', async () => {
+ it('approve parent return tx', async () => {
const result = await erc20Parent.approve('10', {
returnTransaction: true
});
@@ -128,6 +128,32 @@ describe('ERC20', () => {
});
+ it('approve parent return tx with spender address', async () => {
+ const spenderAddress = await erc20Parent.getPredicateAddress();
+ const result = await erc20Parent.approve('10', {
+ spenderAddress: spenderAddress,
+ returnTransaction: true
+ });
+
+ expect(result['to'].toLowerCase()).equal(erc20.parent.toLowerCase());
+ expect(result).to.have.property('data')
+
+ });
+
+ it('approve child return tx without spender address', async () => {
+ try {
+ const result = await erc20Child.approve('10');
+ expect(result['to'].toLowerCase()).equal(erc20.child.toLowerCase());
+ expect(result).to.have.property('data');
+ } catch (error) {
+ // console.log('error', error);
+ expect(error).eql({
+ type: 'null_spender_address',
+ message: 'Please provide spender address.'
+ });
+ }
+ });
+
it('deposit return tx', async () => {
const result = await erc20Parent.deposit(10, from, {
returnTransaction: true

fix spenderAddress initialization issue, throw error if spender is not provider in case of child