Skip to content

Commit

Permalink
#11 Add getBalanceAsync in Erc20TokenService
Browse files Browse the repository at this point in the history
  • Loading branch information
ceres3idoo committed Feb 19, 2019
1 parent f48a407 commit e3854da
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 19 deletions.
19 changes: 19 additions & 0 deletions src/factories/Erc20TokenTransactionBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,25 @@ class Erc20TokenTransactionBuilder extends BaseTransactionBuilder {
)
return transactionDraft
}

buildGetBalanceOfTransactionDraft(personalWalletAddress) {
this.constructor.checkEtherumAddress(personalWalletAddress)

const smartContractMethodName = 'balanceOf'
const smartContractParams = [personalWalletAddress]
const transactionParams = {
from: personalWalletAddress,
to: this.erc20TokenSmartContractAddress,
}

const transactionDraft = this.transactionLib.buildDraft(this.erc20TokenSmartContractInstance,
transactionParams, smartContractMethodName, smartContractParams)
this.log.debug(
{ fn: 'buildGetBalanceOfTransactionDraft', personalWalletAddress },
'Erc20 token get balance of transaction draft builded successfully.',
)
return transactionDraft
}
}

module.exports = Erc20TokenTransactionBuilder
27 changes: 27 additions & 0 deletions tests/unit/factories/Erc20TokenTransactionBuilder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,31 @@ describe('Erc20TokenTransactionBuilder', () => {
}
})
})

describe('buildGetBalanceOfTransactionDraft', () => {
test('returns correct transaction object', () => {
const encodedDataForApproveMethod = '0x70a08231000000000000000000000000d57cb05ef66d2c7c952656ddf5096e02281e3d2e'

const expectedTransactionObjectDraft = MockTransactionDraftFactory
.build({ from: personalWalletAddress, data: encodedDataForApproveMethod, to: erc20TokenSmartContractAddress })

const result = erc20TokenTransactionBuilder.buildGetBalanceOfTransactionDraft(
personalWalletAddress,
tradingWalletSmartContractAddress,
quantity,
)

expect(result).toMatchObject(expectedTransactionObjectDraft)
})

const invalidPersonalWalletAddresses = [{}, [], 'I am not a valid ethereum address', 1, undefined, NaN, '0xNOOP']
test.each(invalidPersonalWalletAddresses)('raise InvalidEthereumAddress error if the personalWalletAddress is not a valid ethereum address %s',
(invalidPersonalWalletAddress) => {
try {
erc20TokenTransactionBuilder.buildGetBalanceOfTransactionDraft(invalidPersonalWalletAddress)
} catch (e) {
expect(e instanceof InvalidEthereumAddress).toBe(true)
}
})
})
})
39 changes: 20 additions & 19 deletions tests/unit/services/Erc20TokenService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,30 @@ describe('approveTrasferAsync', () => {

describe('getAllowanceAsync', () => {
const personalWalletAddress = '0x52daf0caee4cf4e66a9c90dad58c3f0cc4cbf785'
test('calls correctly ethapi sendRawTransaction correctly', async() => {
const expectedTransactionHash = '0xTransactionHash'
const nonceResponse = {
nonce: 4400,
}
const gasEstimationResponse = {
gas: 21000,
gasPrices: {
high: '0',
medium: '25395',
low: '0',
},
}
sandbox.stub(transactionLib, 'sign')
sandbox.stub(ethApiLib, 'sendRawTransactionAsync').returns({ hash: expectedTransactionHash })
sandbox.stub(ethApiLib, 'getAddressNonceAsync').returns(nonceResponse)
sandbox.stub(ethApiLib, 'getEstimateGasAsync').returns(gasEstimationResponse)
const tradingWalletAddress = '0x52daf0caee4cf4e66a9c90dad58c3f0cc4cbf585'

const transactionHash = await erc20TokenService.approveTrasferAsync(
test('calls correctly ethapi transactionCallAsync correctly', async() => {
const expectedAllowed = '10000000000000'

sandbox.stub(ethApiLib, 'transactionCallAsync').returns(expectedAllowed)

const result = await erc20TokenService.getAllowanceAsync(
personalWalletAddress,
tradingWalletAddress,
)
expect(result).toEqual(expectedAllowed)
})
})

expect(transactionHash).toEqual(expectedTransactionHash)
describe('getBalanceOfAsync', () => {
const personalWalletAddress = '0x52daf0caee4cf4e66a9c90dad58c3f0cc4cbf785'

test('calls correctly ethapi transactionCallAsync correctly', async() => {
const expectedBalance = '10000000000000'

sandbox.stub(ethApiLib, 'transactionCallAsync').returns(expectedBalance)

const result = await erc20TokenService.getBalanceOfAsync(personalWalletAddress)
expect(result).toEqual(expectedBalance)
})
})

0 comments on commit e3854da

Please sign in to comment.