Skip to content

Commit

Permalink
#9 Add service factories
Browse files Browse the repository at this point in the history
  • Loading branch information
ceres3idoo authored and andreafspeziale committed Feb 18, 2019
1 parent 9f980e7 commit cd57cc4
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 154 deletions.
24 changes: 5 additions & 19 deletions examples/lib/tradingWallet/allowance/getAllowance.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
(async () => {
const { Erc20TokenService } = require('@eidoo/hybrid-exchange-sdk').services
const { TransactionLib } = require('@eidoo/hybrid-exchange-sdk').lib.TransactionLib
const { Erc20TokenTransactionBuilder } = require('@eidoo/hybrid-exchange-sdk').factories

const Web3 = require('web3')

const providerUrl = 'providerUrl'
const web3 = new Web3(new Web3.providers.HttpProvider(providerUrl))
const { Erc20TokenServiceBuilder } = require('@eidoo/hybrid-exchange-sdk').factories

const tokenAddress = '0x9727e2fb13f7f42d5a6f1a4a9877d4a7e0404d6a'
const personalWalletAddress = '0xf6ccfafbe3ac8b6c82f39f54530a5ff7c0a1ed52'
const tradingWalletAddress = '0xa3725d5a71db4550770d5c96b0104e40e8be8d23'

const transactionLib = new TransactionLib()

const erc20TokenTransactionBuilder = new Erc20TokenTransactionBuilder(
web3,
{ erc20TokenSmartContractAddress: tokenAddress,
transactionLib },
)
const personalWalletAddress = '0xcf4b07a79b5d29988f488f30c4a676ecaad35c02'
const tradingWalletAddress = '0x9c6d1840381cc570235a4ed867bf8465e32ce753'

const erc20TokenService = new Erc20TokenService(web3, transactionLib, erc20TokenTransactionBuilder)
const erc20TokenServiceBuilder = new Erc20TokenServiceBuilder(tokenAddress)
const erc20TokenService = erc20TokenServiceBuilder.build()
const allowance = await erc20TokenService.getAllowanceAsync(
personalWalletAddress,
tradingWalletAddress,
Expand Down
27 changes: 7 additions & 20 deletions examples/lib/tradingWallet/approve/approve.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
(async () => {
const { Erc20TokenService } = require('@eidoo/hybrid-exchange-sdk').services
const { TransactionLib } = require('@eidoo/hybrid-exchange-sdk').lib.TransactionLib
const { Erc20TokenTransactionBuilder } = require('@eidoo/hybrid-exchange-sdk').factories
const { Erc20TokenServiceBuilder } = require('@eidoo/hybrid-exchange-sdk').factories

const Web3 = require('web3')

const providerUrl = 'providerUrl'
const web3 = new Web3(new Web3.providers.HttpProvider(providerUrl))
const tokenAddress = '0x9727e2fb13f7f42d5a6f1a4a9877d4a7e0404d6a'

const transactionLib = new TransactionLib()

const erc20TokenTransactionBuilder = new Erc20TokenTransactionBuilder(
web3,
{ erc20TokenSmartContractAddress: tokenAddress,
transactionLib },
)

const erc20TokenService = new Erc20TokenService(web3, transactionLib, erc20TokenTransactionBuilder)

const personalWalletAddress = '0xf6ccfafbe3ac8b6c82f39f54530a5ff7c0a1ed52'
const tradingWalletAddress = '0xa3725d5a71db4550770d5c96b0104e40e8be8d23'
const personalWalletAddress = '0xcf4b07a79b5d29988f488f30c4a676ecaad35c02'
const tradingWalletAddress = '0x9c6d1840381cc570235a4ed867bf8465e32ce753'
const toDeposit = '500000000000000000'
const privateKey = 'bf177acb6470f78c01209c1461fb0a104b2b0dbff9607d0b95b7e02f450b4142'
const privateKey = '0x4c7ee440ad699493b22732031e4a3277d2d8aa834b727aa0b358e3310aa37384'

const erc20TokenServiceBuilder = new Erc20TokenServiceBuilder(tokenAddress)
const erc20TokenService = erc20TokenServiceBuilder.build()

const transactionHash = await erc20TokenService.approveTrasferAsync(
personalWalletAddress,
Expand Down
10 changes: 3 additions & 7 deletions examples/lib/tradingWallet/balances/getEthBalance.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
(async () => {
const { TradingWalletService } = require('@eidoo/hybrid-exchange-sdk').services
const Web3 = require('web3')
const { TradingWalletServiceBuilder } = require('@eidoo/hybrid-exchange-sdk').factories

const providerUrl = 'providerUrl'
const web3 = new Web3(new Web3.providers.HttpProvider(providerUrl))
const tradingWalletService = TradingWalletServiceBuilder.build()

const tradingWalletService = new TradingWalletService(web3)

const personalWalletAddress = '0xf6ccfafbe3ac8b6c82f39f54530a5ff7c0a1ed52'
const personalWalletAddress = '0xcf4b07a79b5d29988f488f30c4a676ecaad35c02'
const tokenAddress = '0x0000000000000000000000000000000000000000'
const assetBalance = await tradingWalletService.getAssetBalanceAsync(personalWalletAddress, tokenAddress)
console.log(assetBalance)
Expand Down
11 changes: 4 additions & 7 deletions examples/lib/tradingWallet/createWallet/createWallet.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
(async () => {
const { TradingWalletService } = require('@eidoo/hybrid-exchange-sdk').services
const Web3 = require('web3')
const { TradingWalletServiceBuilder } = require('@eidoo/hybrid-exchange-sdk').factories

const providerUrl = 'providerUrl'
const web3 = new Web3(new Web3.providers.HttpProvider(providerUrl))
const tradingWalletService = new TradingWalletService(web3)
const tradingWalletService = TradingWalletServiceBuilder.build()

const personalWalletAddress = '0xf6ccfafbe3ac8b6c82f39f54530a5ff7c0a1ed52'
const privateKey = '0xbf177acb6470f78c01209c1461fb0a104b2b0dbff9607d0b95b7e02f450b4142'
const personalWalletAddress = '0xcf4b07a79b5d29988f488f30c4a676ecaad35c02'
const privateKey = '0x4c7ee440ad699493b22732031e4a3277d2d8aa834b727aa0b358e3310aa37384'

const transactionHash = await tradingWalletService.createWalletAsync(personalWalletAddress, privateKey)
console.log(transactionHash)
Expand Down
15 changes: 6 additions & 9 deletions examples/lib/tradingWallet/depositEth/depositEther.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
(async () => {
const { TradingWalletService } = require('@eidoo/hybrid-exchange-sdk').services
const Web3 = require('web3')
const { TradingWalletServiceBuilder } = require('@eidoo/hybrid-exchange-sdk').factories

const providerUrl = 'providerUrl'
const web3 = new Web3(new Web3.providers.HttpProvider(providerUrl))
const tradingWalletService = new TradingWalletService(web3)
const tradingWalletService = TradingWalletServiceBuilder.build()

const personalWalletAddress = '0xf6ccfafbe3ac8b6c82f39f54530a5ff7c0a1ed52'
const tradingWalletAddress = '0xa3725d5a71db4550770d5c96b0104e40e8be8d23'
const privateKey = 'bf177acb6470f78c01209c1461fb0a104b2b0dbff9607d0b95b7e02f450b4142'
const quantityToDeposit = '500000000000000000'
const personalWalletAddress = '0xcf4b07a79b5d29988f488f30c4a676ecaad35c02'
const tradingWalletAddress = '0x9c6d1840381cc570235a4ed867bf8465e32ce753'
const privateKey = '0x4c7ee440ad699493b22732031e4a3277d2d8aa834b727aa0b358e3310aa37384'
const quantityToDeposit = '100000000000000000'

const transactionHash = await tradingWalletService.depositEtherAsync(
personalWalletAddress,
Expand Down
15 changes: 6 additions & 9 deletions examples/lib/tradingWallet/depositToken/depositToken.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
(async () => {
const { TradingWalletService } = require('@eidoo/hybrid-exchange-sdk').services
const Web3 = require('web3')
const { TradingWalletServiceBuilder } = require('@eidoo/hybrid-exchange-sdk').factories

const tradingWalletService = TradingWalletServiceBuilder.build()

const providerUrl = 'providerUrl'
const web3 = new Web3(new Web3.providers.HttpProvider(providerUrl))
const tokenAddress = '0x9727e2fb13f7f42d5a6f1a4a9877d4a7e0404d6a'
const tradingWalletService = new TradingWalletService(web3)
const personalWalletAddress = '0xf6ccfafbe3ac8b6c82f39f54530a5ff7c0a1ed52'
const tradingWalletAddress = '0xa3725d5a71db4550770d5c96b0104e40e8be8d23'
const personalWalletAddress = '0xcf4b07a79b5d29988f488f30c4a676ecaad35c02'
const tradingWalletAddress = '0x9c6d1840381cc570235a4ed867bf8465e32ce753'
const privateKey = '0x4c7ee440ad699493b22732031e4a3277d2d8aa834b727aa0b358e3310aa37384'
const quantityToDeposit = '500000000000000000'
const privateKey = '0xbf177acb6470f78c01209c1461fb0a104b2b0dbff9607d0b95b7e02f450b4142'


const transactionHash = await tradingWalletService.depositTokenAsync(
personalWalletAddress,
Expand Down
10 changes: 3 additions & 7 deletions examples/lib/tradingWallet/getAddress/getAddress.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
(async () => {
const { TradingWalletService } = require('@eidoo/hybrid-exchange-sdk').services
const Web3 = require('web3')
const { TradingWalletServiceBuilder } = require('@eidoo/hybrid-exchange-sdk').factories

const providerUrl = 'providerUrl'
const web3 = new Web3(new Web3.providers.HttpProvider(providerUrl))
const tradingWalletService = new TradingWalletService(web3)

const personalWalletAddress = '0x70742a2530069fd1fa302766e2e58f7c03d63e4e'
const tradingWalletService = TradingWalletServiceBuilder.build()

const personalWalletAddress = '0xcf4b07a79b5d29988f488f30c4a676ecaad35c02'
const tradingWalletAddress = await tradingWalletService.getTradingWalletAddressAsync(personalWalletAddress)
console.log(tradingWalletAddress)
})()
13 changes: 5 additions & 8 deletions examples/lib/tradingWallet/withdraw/withdraw.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
(async () => {
const { TradingWalletService } = require('@eidoo/hybrid-exchange-sdk').services
const Web3 = require('web3')
const { TradingWalletServiceBuilder } = require('@eidoo/hybrid-exchange-sdk').factories

const providerUrl = 'providerUrl'
const web3 = new Web3(new Web3.providers.HttpProvider(providerUrl))
const tradingWalletService = new TradingWalletService(web3)
const tradingWalletService = TradingWalletServiceBuilder.build()

const personalWalletAddress = '0xf6ccfafbe3ac8b6c82f39f54530a5ff7c0a1ed52'
const tradingWalletAddress = '0xa3725d5a71db4550770d5c96b0104e40e8be8d23'
const personalWalletAddress = '0xcf4b07a79b5d29988f488f30c4a676ecaad35c02'
const tradingWalletAddress = '0x9c6d1840381cc570235a4ed867bf8465e32ce753'
const tokenAddress = '0x9727e2fb13f7f42d5a6f1a4a9877d4a7e0404d6a'
const privateKey = 'bf177acb6470f78c01209c1461fb0a104b2b0dbff9607d0b95b7e02f450b4142'
const privateKey = '0x4c7ee440ad699493b22732031e4a3277d2d8aa834b727aa0b358e3310aa37384'
const toWithdraw = '500000000000000000'

const transactionHash = await tradingWalletService.withdrawAsync(
Expand Down
8 changes: 2 additions & 6 deletions src/commands/commandList.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const Web3 = require('web3')

const logger = require('../logger')
const GetAddressCommandValidator = require('../validators/GetAddressCommandValidator')
const GetAddressCommand = require('../commands/GetAddressCommand')
Expand All @@ -10,17 +8,15 @@ const { PrivateKeyService } = require('../services/PrivateKeyService')
const PrivateKeyValidator = require('../validators/PrivateKeyValidator')
const OrderSignCommandValidator = require('../validators/OrderSignCommandValidator')
const OrderSignerHelper = require('../helpers/OrderSignerHelper')
const TradingWalletService = require('../services/TradingWalletService')
const TradingWalletServiceBuilder = require('../factories/TradingWalletServiceBuilder')

const providerUrl = 'urlToProvider'
const web3 = new Web3(new Web3.providers.HttpProvider(providerUrl))

const createWalletCommandValidator = new CreateWalletCommandValidator(logger)
const getAddressCommandValidator = new GetAddressCommandValidator(logger)
const privateKeyValidator = new PrivateKeyValidator(logger)
const orderSignCommandValidator = new OrderSignCommandValidator(logger)

const tradingWalletService = new TradingWalletService(web3)
const tradingWalletService = TradingWalletServiceBuilder.build()
const privateKeyService = new PrivateKeyService(logger)
const ordersignerHelper = new OrderSignerHelper(logger)

Expand Down
39 changes: 39 additions & 0 deletions src/factories/Erc20TokenServiceBuilder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const Web3 = require('web3')

const logger = require('../logger')
const Erc20TokenTransactionBuilder = require('./Erc20TokenTransactionBuilder')

const { TransactionLib } = require('../lib/TransactionLib')
const { Erc20TokenService } = require('../services')

const providerUrl = 'urlToProvider'
const web3 = new Web3(new Web3.providers.HttpProvider(providerUrl))

const transactionLibInstance = new TransactionLib(web3, logger)

/**
* Class representing a simple factory to build Erc20TokenService object.
*/

class Erc20TokenServiceBuilder {
constructor(tokenAddress) {
if (!tokenAddress) {
const errorMessage = `Invalid "tokenAddress" value: ${tokenAddress}`
throw new TypeError(errorMessage)
}

this.tokenAddress = tokenAddress
}

build() {
const erc20TokenTransactionBuilder = new Erc20TokenTransactionBuilder(
web3,
{ erc20TokenSmartContractAddress: this.tokenAddress,
transactionLib: transactionLibInstance },
logger,
)
return new Erc20TokenService(web3, transactionLibInstance, erc20TokenTransactionBuilder, logger)
}
}

module.exports = Erc20TokenServiceBuilder
24 changes: 24 additions & 0 deletions src/factories/TradingWalletServiceBuilder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const Web3 = require('web3')

const logger = require('../logger')
const TradingWalletTransactionBuilder = require('./TradingWalletTransactionBuilder')

const { TransactionLib } = require('../lib/TransactionLib')
const { TradingWalletService } = require('../services')

const providerUrl = 'urlToProvider'
const web3 = new Web3(new Web3.providers.HttpProvider(providerUrl))
const transactionLibInstance = new TransactionLib(web3, logger)

/**
* Class representing a simple factory to build TradingWalletService object.
*/

class TradingWalletServiceBuilder {
static build() {
const tradingWalletTransactionBuilder = new TradingWalletTransactionBuilder(web3)
return new TradingWalletService(web3, transactionLibInstance, tradingWalletTransactionBuilder, logger)
}
}

module.exports = TradingWalletServiceBuilder
4 changes: 4 additions & 0 deletions src/factories/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const Erc20TokenTransactionBuilder = require('./Erc20TokenTransactionBuilder')
const Erc20TokenServiceBuilder = require('./Erc20TokenServiceBuilder')
const OrderPayloadBuilder = require('./OrderPayloadBuilder')
const TradingWalletServiceBuilder = require('./TradingWalletServiceBuilder')
const TradingWalletTransactionBuilder = require('./TradingWalletTransactionBuilder')

module.exports = {
Erc20TokenTransactionBuilder,
Erc20TokenServiceBuilder,
OrderPayloadBuilder,
TradingWalletServiceBuilder,
TradingWalletTransactionBuilder,
}
26 changes: 0 additions & 26 deletions src/services/Erc20TokenService.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,10 @@
const Web3 = require('web3')

const BaseTransactionService = require('./BaseTransactionService')
const log = require('../logger')

const { TransactionLib } = require('../lib/TransactionLib')

const providerUrl = 'urlToProvider'
const web3 = new Web3(new Web3.providers.HttpProvider(providerUrl))

const transactionLibInstance = new TransactionLib(web3, log)

/**
* Class representing a service to build sign and execute transaction related to a Erc20 token.
* @extends BaseTransactionService
*/
class Erc20TokenService extends BaseTransactionService {
/**
* Create a new instance of Erc20TokenService.
*
* @param {Object} web3 The web3 instance.
* @param {Object} erc20TokenSmartContract The exchange smart contract object.
* @param {Object} erc20TokenSmartContract.erc20TokenSmartContractAddress The exchange smart contract address.
* @param {Object} [erc20TokenSmartContract.erc20TokenSmartContractAbi] The exchange smart contract abi interface.
* @param {Object} [transactionLib] The transaction lib istance.
* @param {Object} [logger] The logger instance.
* @throws {TypeError} If exchangeSmart contract objecs is not initialized as expected.
*/
constructor(web3, transactionLib = transactionLibInstance,
transactionBuilder, logger = log) {
super(web3, transactionLib, transactionBuilder, logger)
}

/**
* It approves the trasfer of token quantity from trading to pesonal wallet.
*
Expand Down
26 changes: 0 additions & 26 deletions src/services/TradingWalletService.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,15 @@
const Web3 = require('web3')

const { TransactionLib } = require('../lib/TransactionLib')
const { TradingWalletNotFoundError } = require('../utils/errors')
const BaseTransactionService = require('./BaseTransactionService')
const log = require('../logger')
const TradingWalletTransactionBuilder = require('../factories/TradingWalletTransactionBuilder')

const ETH_ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'
const ETH_ADDRESS_LENGTH = 40
const ETH_ADDRESS_LENGTH_256B = 66

const providerUrl = 'urlToProvider'
const web3 = new Web3(new Web3.providers.HttpProvider(providerUrl))

const transactionLibInstance = new TransactionLib(web3, log)

const tradingWalletTransactionBuilderInstance = new TradingWalletTransactionBuilder(web3)

/**
* Class representing a service to create trading, deposit and withdraw token or ether.
* @extends BaseTransactionService
*/
class TradingWalletService extends BaseTransactionService {
/**
* Create a new instance of TradingWalletService.
*
* @param {Object} web3 The web3 instance.
* @param {Object} [transactionLib] The transaction lib istance.
* @param {Object} [transactionBuilder] The trading wallet transaction builder lib istance.
* @param {Object} [logger] The logger instance.
* @throws {TypeError} If exchangeSmart contract objecs is not initialized as expected.
*/
constructor(web3, transactionLib = transactionLibInstance,
transactionBuilder = tradingWalletTransactionBuilderInstance, logger = log) {
super(web3, transactionLib, transactionBuilder, logger)
}

/**
* It gets the signed transaction data to execute the transaction.
*
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/commands/createWalletCommand.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ const ethApiLibConf = {
useTLS: false,
}
const ethApiLib = new EidooEthApiLib(ethApiLibConf)
const transactionLibInstance = new TransactionLib(web3, logger, ethApiLib)
const transactionLib = new TransactionLib(web3, logger, ethApiLib)
const tradingWalletTransactionBuilder = new TradingWalletTransactionBuilder(
web3, { exchangeSmartContractAddress }, transactionLibInstance,
web3, { exchangeSmartContractAddress, transactionLib, logger },
)
const tradingWalletService = new TradingWalletService(web3, transactionLibInstance, tradingWalletTransactionBuilder)

const tradingWalletService = new TradingWalletService(web3, transactionLib, tradingWalletTransactionBuilder, logger)

const privateKeyValidator = new PrivateKeyValidator(logger)
const privateKeyService = new PrivateKeyService(logger)
Expand Down
Loading

0 comments on commit cd57cc4

Please sign in to comment.