Skip to content

Commit

Permalink
#11 Get gas estimation to set gas in deposit token transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
ceres3idoo committed Feb 22, 2019
1 parent 1804bc6 commit 8f48340
Showing 1 changed file with 55 additions and 30 deletions.
85 changes: 55 additions & 30 deletions src/facades/TradingWalletFacade.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,54 @@ const { TransactionLibBuilder } = require('../factories')
*/

class TradingWalletFacade {
constructor(tradingWalletService, erc20TokenService, transactionLib = TransactionLibBuilder.build(), logger = log) {
constructor(tradingWalletTransactionBuilder, erc20TokenService, erc20TokenTransactionBuilder,
transactionLib = TransactionLibBuilder.build(), logger = log) {
if (!logger) {
throw new TypeError(`Invalid "logger" value: ${logger}`)
}
this.log = logger.child({ module: this.constructor.name })

if (!tradingWalletService) {
const errorMessage = `Invalid "tradingWalletService" value: ${tradingWalletService}`
if (!tradingWalletTransactionBuilder) {
const errorMessage = `Invalid "tradingWalletTransactionBuilder" value: ${tradingWalletTransactionBuilder}`
throw new TypeError(errorMessage)
}
this.tradingWalletService = tradingWalletService
this.tradingWalletTransactionBuilder = tradingWalletTransactionBuilder

if (!erc20TokenService) {
const errorMessage = `Invalid "erc20TokenService" value: ${erc20TokenService}`
throw new TypeError(errorMessage)
}
this.erc20TokenService = erc20TokenService

if (!erc20TokenTransactionBuilder) {
const errorMessage = `Invalid "erc20TokenTransactionBuilder" value: ${erc20TokenTransactionBuilder}`
throw new TypeError(errorMessage)
}
this.erc20TokenTransactionBuilder = erc20TokenTransactionBuilder

if (!transactionLib) {
const errorMessage = `Invalid "transactionLib" value: ${transactionLib}`
throw new TypeError(errorMessage)
}
this.transactionLib = transactionLib
}

async throwIfTokenWalletEnought(personalWalletAddress, quantity) {
const assetBalance = await this.erc20TokenService.getBalanceOfAsync(personalWalletAddress)

if (assetBalance < quantity) {
const errorMessage = 'The asset balance is < than the quantity to depoist!'
this.log.info({
personalWalletAddress,
quantity,
assetBalance,
fn: 'checkIfTokenWalletEnought',
},
errorMessage)
throw new QuantityNotEnoughError(errorMessage)
}
}

async depositTokenAsync(personalWalletAddress, tradingWalletAddress, quantity, tokenAddress, privateKey) {
this.log.info({
fn: 'depositTokenAsync',
Expand All @@ -44,31 +67,18 @@ class TradingWalletFacade {
let approveToZeroTransactionHash = null
let approveTransactionHash = null

const assetBalance = this.erc20TokenService.getBalanceOfAsync(personalWalletAddress)

if (assetBalance < quantity) {
const errorMessage = 'The asset balance is < than the quantity to depoist!'
this.log.info({
personalWalletAddress,
tradingWalletAddress,
quantity,
tokenAddress,
assetBalance,
fn: 'depositTokenAsync',
},
errorMessage)
throw new QuantityNotEnoughError(errorMessage)
}
await this.throwIfTokenWalletEnought(personalWalletAddress, quantity)

const allowance = await this.erc20TokenService.getAllowanceAsync(personalWalletAddress, tradingWalletAddress)
const allowanceToInt = +allowance
if (quantity > allowanceToInt && allowanceToInt > 0) {

if (quantity > allowance && allowance > 0) {
this.log.info({
personalWalletAddress,
tradingWalletAddress,
quantity,
tokenAddress,
allowanceToInt,
allowance,
fn: 'depositTokenAsync',
},
'The quantity to deposit is not completely allowed!')
Expand All @@ -82,23 +92,38 @@ class TradingWalletFacade {
)
}

const approveTransactionDraftObject = await this.erc20TokenTransactionBuilder.buildApproveTrasferTransactionDraft(
personalWalletAddress,
tradingWalletAddress,
quantity,
)
const gasEstimationForApprove = await this.transactionLib.getGasEstimation(approveTransactionDraftObject)

if (allowanceToInt === 0 || (quantity > allowanceToInt && allowanceToInt > 0)) {
approveTransactionHash = await this.erc20TokenService.approveTrasferAsync(
personalWalletAddress,
tradingWalletAddress,
quantity,
const signedApproveData = await this.transactionLib.sign(
approveTransactionDraftObject,
privateKey,
null,
gasEstimationForApprove.gas,
gasEstimationForApprove.gasPrice,
)
approveTransactionHash = await this.transactionLib.execute(signedApproveData)
}

const depositTransactionHash = await this.tradingWalletService.depositTokenAsync(
personalWalletAddress,
tradingWalletAddress,
quantity,
tokenAddress,
const depositTokenTransactionDraft = await this.tradingWalletTransactionBuilder
.buildDepositTokenTransactionDraft(personalWalletAddress, tradingWalletAddress, quantity, tokenAddress)
const depositTokenFixedGasEstimation = 100000

const signedTransactionDataForDeposit = await this.transactionLib.sign(
depositTokenTransactionDraft,
privateKey,
depositTokenFixedGasEstimation,
gasEstimationForApprove.gasPrice,
)

const depositTransactionHash = await this.transactionLib
.execute(signedTransactionDataForDeposit, privateKey)

this.log.info({
fn: 'depositTokenAsync',
personalWalletAddress,
Expand Down

0 comments on commit 8f48340

Please sign in to comment.