-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24d0eb0
commit 0612ec5
Showing
2 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
const log = require('../logger') | ||
const { QuantityNotAllowedError, TransactionNotMinedError } = require('../utils').errors | ||
const { TransactionLibBuilder } = require('../factories') | ||
|
||
/** | ||
* Class representing a facade of tradingWallet utilities. | ||
*/ | ||
|
||
class TradingWalletFacade { | ||
constructor(tradingWalletService, erc20TokenService, 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}` | ||
throw new TypeError(errorMessage) | ||
} | ||
this.tradingWalletService = tradingWalletService | ||
|
||
if (!erc20TokenService) { | ||
const errorMessage = `Invalid "erc20TokenService" value: ${erc20TokenService}` | ||
throw new TypeError(errorMessage) | ||
} | ||
this.erc20TokenService = erc20TokenService | ||
|
||
if (!transactionLib) { | ||
const errorMessage = `Invalid "transactionLib" value: ${transactionLib}` | ||
throw new TypeError(errorMessage) | ||
} | ||
this.transactionLib = transactionLib | ||
} | ||
|
||
async depositTokenAsync(personalWalletAddress, tradingWalletAddress, quantity, tokenAddress, privateKey) { | ||
const approveTransactionHash = await this.erc20TokenService.approveTrasferAsync( | ||
personalWalletAddress, | ||
tradingWalletAddress, | ||
quantity, | ||
privateKey, | ||
) | ||
const isApproveMined = await this.transactionLib.isTransactionMined(approveTransactionHash, tradingWalletAddress) | ||
|
||
if (!isApproveMined) { | ||
const errMsg = 'Approve transaction not mined.' | ||
this.log.error({ | ||
fn: 'depositTokenAsync', | ||
isApproveMined, | ||
personalWalletAddress, | ||
tradingWalletAddress, | ||
quantity, | ||
}, errMsg) | ||
throw new TransactionNotMinedError(errMsg) | ||
} | ||
|
||
const allowedQuantity = await this.erc20TokenService.getAllowanceAsync(personalWalletAddress, tradingWalletAddress) | ||
|
||
if (quantity <= allowedQuantity) { | ||
const errorMessage = 'The quantity to deposit is not allowed!' | ||
this.log.error({ | ||
personalWalletAddress, | ||
tradingWalletAddress, | ||
quantity, | ||
tokenAddress, | ||
allowedQuantity, | ||
}, | ||
errorMessage) | ||
throw new QuantityNotAllowedError(errorMessage) | ||
} | ||
const depositTransactionHash = await this.tradingWalletService.depositTokenAsync( | ||
personalWalletAddress, | ||
tradingWalletAddress, | ||
quantity, | ||
tokenAddress, | ||
privateKey, | ||
) | ||
const isDepositMined = await this.transactionLib.isTransactionMined(depositTransactionHash, tradingWalletAddress) | ||
|
||
if (!isDepositMined) { | ||
const errMsg = 'Deposit transaction not mined.' | ||
this.log.error({ | ||
fn: 'depositTokenAsync', | ||
isDepositMined, | ||
personalWalletAddress, | ||
tradingWalletAddress, | ||
quantity, | ||
}, errMsg) | ||
throw new TransactionNotMinedError(errMsg) | ||
} | ||
|
||
this.log.info({ | ||
fn: 'depositTokenAsync', | ||
personalWalletAddress, | ||
tradingWalletAddress, | ||
quantity, | ||
tokenAddress, | ||
privateKey, | ||
}, | ||
'Deposit token completed successfully.') | ||
} | ||
} | ||
|
||
module.exports = TradingWalletFacade |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const TradingWalletFacade = require('./TradingWalletFacade') | ||
|
||
module.exports = { | ||
TradingWalletFacade, | ||
} |