Skip to content

Commit

Permalink
#11 Adjust log level in TransactionLib
Browse files Browse the repository at this point in the history
  • Loading branch information
ceres3idoo committed Feb 22, 2019
1 parent 4b582b4 commit e28f04b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
30 changes: 14 additions & 16 deletions src/facades/TradingWalletFacade.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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!')
Expand All @@ -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(
Expand All @@ -108,7 +105,8 @@ class TradingWalletFacade {
tradingWalletAddress,
quantity,
tokenAddress,
privateKey,
approveToZeroTransactionHash,
approveTransactionHash,
depositTransactionHash,
},
'Deposit token completed successfully.')
Expand Down
12 changes: 9 additions & 3 deletions src/lib/TransactionLib/TransactionLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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.')
Expand All @@ -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}`)
Expand Down
28 changes: 23 additions & 5 deletions src/services/Erc20TokenService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

/**
Expand All @@ -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
}
}

Expand Down

0 comments on commit e28f04b

Please sign in to comment.