diff options
author | Fabio Berger <me@fabioberger.com> | 2017-06-02 22:29:12 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2017-06-02 22:29:12 +0800 |
commit | 21292ad15ec9490acb8d3089286f3d81e83967b7 (patch) | |
tree | 1f08ab21da3f38b4573c623a87cb44c21c3fcbfc | |
parent | aed96d593d2658f022a257d00124c244bb73bf92 (diff) | |
download | dexon-0x-contracts-21292ad15ec9490acb8d3089286f3d81e83967b7.tar.gz dexon-0x-contracts-21292ad15ec9490acb8d3089286f3d81e83967b7.tar.zst dexon-0x-contracts-21292ad15ec9490acb8d3089286f3d81e83967b7.zip |
Add balance check to `transferAsync` and related test
-rw-r--r-- | src/contract_wrappers/token_wrapper.ts | 6 | ||||
-rw-r--r-- | test/token_wrapper_test.ts | 17 |
2 files changed, 20 insertions, 3 deletions
diff --git a/src/contract_wrappers/token_wrapper.ts b/src/contract_wrappers/token_wrapper.ts index 6cb71c7e8..55e1e364b 100644 --- a/src/contract_wrappers/token_wrapper.ts +++ b/src/contract_wrappers/token_wrapper.ts @@ -102,6 +102,12 @@ export class TokenWrapper extends ContractWrapper { assert.isBigNumber('amountInBaseUnits', amountInBaseUnits); const tokenContract = await this.getTokenContractAsync(tokenAddress); + + const fromAddressBalance = await this.getBalanceAsync(tokenAddress, fromAddress); + if (fromAddressBalance.lessThan(amountInBaseUnits)) { + throw new Error(ZeroExError.INSUFFICIENT_BALANCE_FOR_TRANSFER); + } + await tokenContract.transfer(toAddress, amountInBaseUnits, { from: fromAddress, }); diff --git a/test/token_wrapper_test.ts b/test/token_wrapper_test.ts index e8d2199e9..3b9071bde 100644 --- a/test/token_wrapper_test.ts +++ b/test/token_wrapper_test.ts @@ -33,23 +33,34 @@ describe('TokenWrapper', () => { await blockchainLifecycle.revertAsync(); }); describe('#transferAsync', () => { + let token: Token; + let transferAmount: BigNumber.BigNumber; + before(() => { + token = tokens[0]; + transferAmount = new BigNumber(42); + }); it('should successfully transfer tokens', async () => { - const token = tokens[0]; const fromAddress = coinbase; const toAddress = addressWithoutFunds; const preBalance = await zeroEx.token.getBalanceAsync(token.address, toAddress); expect(preBalance).to.be.bignumber.equal(0); - const transferAmount = new BigNumber(42); await zeroEx.token.transferAsync(token.address, fromAddress, toAddress, transferAmount); const postBalance = await zeroEx.token.getBalanceAsync(token.address, toAddress); return expect(postBalance).to.be.bignumber.equal(transferAmount); }); + it('should fail to transfer tokens if fromAddress has an insufficient balance', async () => { + const fromAddress = addressWithoutFunds; + const toAddress = coinbase; + return expect(zeroEx.token.transferAsync( + token.address, fromAddress, toAddress, transferAmount, + )).to.be.rejectedWith(ZeroExError.INSUFFICIENT_BALANCE_FOR_TRANSFER); + }); it('should throw a CONTRACT_DOES_NOT_EXIST error for a non-existent token contract', async () => { const nonExistentTokenAddress = '0x9dd402f14d67e001d8efbe6583e51bf9706aa065'; const fromAddress = coinbase; const toAddress = coinbase; return expect(zeroEx.token.transferAsync( - nonExistentTokenAddress, fromAddress, toAddress, new BigNumber(42), + nonExistentTokenAddress, fromAddress, toAddress, transferAmount, )).to.be.rejectedWith(ZeroExError.CONTRACT_DOES_NOT_EXIST); }); }); |