diff --git a/src/facades/TradingWalletFacade.js b/src/facades/TradingWalletFacade.js index 6383ec9..5f25c4f 100644 --- a/src/facades/TradingWalletFacade.js +++ b/src/facades/TradingWalletFacade.js @@ -33,6 +33,14 @@ class TradingWalletFacade { } async depositTokenAsync(personalWalletAddress, tradingWalletAddress, quantity, tokenAddress, privateKey) { + this.log.info({ + fn: 'depositTokenAsync', + personalWalletAddress, + tradingWalletAddress, + tokenAddress, + quantity, + }, 'Deposit token.') + let approveToZeroTransactionHash = null let approveTransactionHash = null @@ -53,14 +61,14 @@ class TradingWalletFacade { } const allowance = await this.erc20TokenService.getAllowanceAsync(personalWalletAddress, tradingWalletAddress) - - if (quantity > allowance && allowance > 0) { + const allowanceToInt = +allowance + if (quantity > allowanceToInt && allowanceToInt > 0) { this.log.info({ personalWalletAddress, tradingWalletAddress, quantity, tokenAddress, - allowance, + allowanceToInt, fn: 'depositTokenAsync', }, 'The quantity to deposit is not completely allowed!') @@ -72,26 +80,15 @@ class TradingWalletFacade { zeroQuantity, privateKey, ) - - this.log.info({ - approveToZeroTransactionHash, - fn: 'depositTokenAsync', - }, - 'Approve to zero quantity done successfully.') } - if (allowance === 0 || (quantity > allowance && allowance > 0)) { + if (allowanceToInt === 0 || (quantity > allowanceToInt && allowanceToInt > 0)) { approveTransactionHash = await this.erc20TokenService.approveTrasferAsync( personalWalletAddress, tradingWalletAddress, quantity, privateKey, ) - this.log.info({ - approveTransactionHash, - fn: 'depositTokenAsync', - }, - 'Approve quantity done successfully.') } const depositTransactionHash = await this.tradingWalletService.depositTokenAsync( @@ -108,7 +105,8 @@ class TradingWalletFacade { tradingWalletAddress, quantity, tokenAddress, - privateKey, + approveToZeroTransactionHash, + approveTransactionHash, depositTransactionHash, }, 'Deposit token completed successfully.') diff --git a/src/lib/TransactionLib/TransactionLib.js b/src/lib/TransactionLib/TransactionLib.js index 4b98383..de490e4 100644 --- a/src/lib/TransactionLib/TransactionLib.js +++ b/src/lib/TransactionLib/TransactionLib.js @@ -201,7 +201,7 @@ class TransactionLib extends ITransactionLib { const privateKeyWithPrefix = ethereumUtil.addHexPrefix(privateKey) const privateKeyBuffered = ethereumUtil.toBuffer(privateKeyWithPrefix) const transactionObject = await getTransactionObject(this, transactionDraftObject, nonce, gas, gasPrice) - this.log.info({ transactionObject }, 'Transaction Object.') + this.log.debug({ transactionObject }, 'Transaction Object.') const rawTransaction = new Tx(transactionObject) rawTransaction.sign(privateKeyBuffered) @@ -245,11 +245,10 @@ class TransactionLib extends ITransactionLib { throw new Error(errors) } const transactionObject = Object.assign({}, transactionObjectDraft) - this.log.info({ transactionObject }, 'Doing transaction call.') const responsePayload = await this.ethApiClient .transactionCallAsync({ transactionObject }) - this.log.info({ transactionObject, responsePayload }, 'Transaction call done.') + this.log.info({ fn: 'call', transactionObject, responsePayload }, 'Transaction call done.') return responsePayload } catch (err) { this.log.error({ err, fn: 'call', transactionObjectDraft }, 'Error executing transaction call.') @@ -262,6 +261,13 @@ class TransactionLib extends ITransactionLib { const { transactions } = await this.ethApiClient.getAccountTxsDetailsAsync(fromAddress) const transactionReceipt = _.find(transactions, item => item.transactionReceipt.transactionHash === hash) + this.log.info({ + fn: 'getTransactionReceipt', + transactionReceipt, + hash, + fromAddress, + }, 'Retrieve transaction receipt.') + return transactionReceipt || null } catch (err) { throw new Error(`Error retriving transaction Receipt: ${hash}`) diff --git a/src/services/Erc20TokenService.js b/src/services/Erc20TokenService.js index 9b046cf..9cf9800 100644 --- a/src/services/Erc20TokenService.js +++ b/src/services/Erc20TokenService.js @@ -18,9 +18,17 @@ class Erc20TokenService extends BaseTransactionService { this.checkEtherumAddress(personalWalletAddress) this.checkEtherumAddress(tradingWalletAddress) + this.log.info({ + fn: 'approveTrasferAsync', + tradingWalletAddress, + quantity, + personalWalletAddress, + }, + 'Approving quantity.') const transactionParams = [personalWalletAddress, tradingWalletAddress, quantity] const transactionDraftBuilderName = 'buildApproveTrasferTransactionDraft' const transactionHash = this.transactionExecutor(privateKey, transactionDraftBuilderName, transactionParams) + return transactionHash } @@ -39,9 +47,16 @@ class Erc20TokenService extends BaseTransactionService { personalWalletAddress, tradingWalletAddress, ) - const allowance = await this.transactionLib.call(transactionObjectDraft) - - return this.web3.toBigNumber(allowance).toString(10) + const allowanceHex = await this.transactionLib.call(transactionObjectDraft) + const allowance = this.web3.toBigNumber(allowanceHex).toString(10) + this.log.info({ + fn: 'getAllowanceAsync', + allowance, + tradingWalletAddress, + personalWalletAddress, + }, + 'Retrieve allowance.') + return allowance } /** @@ -56,8 +71,11 @@ class Erc20TokenService extends BaseTransactionService { const transactionObjectDraft = this.transactionBuilder.buildGetBalanceOfTransactionDraft( personalWalletAddress, ) - const assetBalance = await this.transactionLib.call(transactionObjectDraft) - return this.web3.toBigNumber(assetBalance).toString(10) + const assetBalanceHex = await this.transactionLib.call(transactionObjectDraft) + const assetBalance = this.web3.toBigNumber(assetBalanceHex).toString(10) + this.log.info({ fn: 'getBalanceOfAsync', personalWalletAddress, assetBalance }, 'Retrieve token balanceOf.') + + return assetBalance } }