From 2c3b718a4fa063277163e72f1be962dab3dfd21a Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 14:48:41 +0200 Subject: rename erc20Wrapper to tokenWrapper to be more generic in case we end up supporting another spec --- test/token_wrapper_test.ts | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 test/token_wrapper_test.ts (limited to 'test/token_wrapper_test.ts') diff --git a/test/token_wrapper_test.ts b/test/token_wrapper_test.ts new file mode 100644 index 000000000..22fb6e052 --- /dev/null +++ b/test/token_wrapper_test.ts @@ -0,0 +1,55 @@ +import 'mocha'; +import * as chai from 'chai'; +import chaiAsPromised = require('chai-as-promised'); +import * as Web3 from 'web3'; +import * as BigNumber from 'bignumber.js'; +import promisify = require('es6-promisify'); +import {web3Factory} from './utils/web3_factory'; +import {ZeroEx} from '../src/0x.js'; +import {ZeroExError, Token} from '../src/types'; +import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; + +const expect = chai.expect; +chai.use(chaiAsPromised); +const blockchainLifecycle = new BlockchainLifecycle(); + +describe('TokenWrapper', () => { + let web3: Web3; + let zeroEx: ZeroEx; + let userAddresses: string[]; + let tokens: Token[]; + before(async () => { + web3 = web3Factory.create(); + zeroEx = new ZeroEx(web3); + userAddresses = await promisify(web3.eth.getAccounts)(); + tokens = await zeroEx.tokenRegistry.getTokensAsync(); + }); + beforeEach(async () => { + await blockchainLifecycle.startAsync(); + }); + afterEach(async () => { + await blockchainLifecycle.revertAsync(); + }); + describe('#getBalanceAsync', () => { + it('should return the balance for an existing ERC20 token', async () => { + const aToken = tokens[0]; + const aOwnerAddress = userAddresses[0]; + const balance = await zeroEx.erc20.getBalanceAsync(aToken.address, aOwnerAddress); + const expectedBalance = new BigNumber('100000000000000000000000000'); + expect(balance).to.be.bignumber.equal(expectedBalance); + }); + it ('should throw a CONTRACT_DOES_NOT_EXIST error for a non-existent token contract', async () => { + const nonExistentTokenAddress = '0x9dd402f14d67e001d8efbe6583e51bf9706aa065'; + const aOwnerAddress = userAddresses[0]; + expect(zeroEx.erc20.getBalanceAsync(nonExistentTokenAddress, aOwnerAddress)) + .to.be.rejectedWith(ZeroExError.CONTRACT_DOES_NOT_EXIST); + }); + it ('should return a balance of 0 for a non-existent owner address', async () => { + const aToken = tokens[0]; + const aNonExistentOwner = '0x198C6Ad858F213Fb31b6FE809E25040E6B964593'; + const balance = await zeroEx.erc20.getBalanceAsync(aToken.address, aNonExistentOwner); + const expectedBalance = new BigNumber('0'); + expect(balance).to.be.bignumber.equal(expectedBalance); + }); + }); +}); -- cgit v1.2.3 From 86cdb6cb83078f9f29f473e0ebb8425a2d90f5ec Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 14:53:25 +0200 Subject: use token instead of erc20 --- test/token_wrapper_test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'test/token_wrapper_test.ts') diff --git a/test/token_wrapper_test.ts b/test/token_wrapper_test.ts index 22fb6e052..a15fab505 100644 --- a/test/token_wrapper_test.ts +++ b/test/token_wrapper_test.ts @@ -34,20 +34,20 @@ describe('TokenWrapper', () => { it('should return the balance for an existing ERC20 token', async () => { const aToken = tokens[0]; const aOwnerAddress = userAddresses[0]; - const balance = await zeroEx.erc20.getBalanceAsync(aToken.address, aOwnerAddress); + const balance = await zeroEx.token.getBalanceAsync(aToken.address, aOwnerAddress); const expectedBalance = new BigNumber('100000000000000000000000000'); expect(balance).to.be.bignumber.equal(expectedBalance); }); it ('should throw a CONTRACT_DOES_NOT_EXIST error for a non-existent token contract', async () => { const nonExistentTokenAddress = '0x9dd402f14d67e001d8efbe6583e51bf9706aa065'; const aOwnerAddress = userAddresses[0]; - expect(zeroEx.erc20.getBalanceAsync(nonExistentTokenAddress, aOwnerAddress)) + expect(zeroEx.token.getBalanceAsync(nonExistentTokenAddress, aOwnerAddress)) .to.be.rejectedWith(ZeroExError.CONTRACT_DOES_NOT_EXIST); }); it ('should return a balance of 0 for a non-existent owner address', async () => { const aToken = tokens[0]; const aNonExistentOwner = '0x198C6Ad858F213Fb31b6FE809E25040E6B964593'; - const balance = await zeroEx.erc20.getBalanceAsync(aToken.address, aNonExistentOwner); + const balance = await zeroEx.token.getBalanceAsync(aToken.address, aNonExistentOwner); const expectedBalance = new BigNumber('0'); expect(balance).to.be.bignumber.equal(expectedBalance); }); -- cgit v1.2.3 From 53e83a7d8168d1feace1959803d3ad5e21dff7b8 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 15:28:21 +0200 Subject: Add first test for getProxyAllowanceAsync --- test/token_wrapper_test.ts | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'test/token_wrapper_test.ts') diff --git a/test/token_wrapper_test.ts b/test/token_wrapper_test.ts index a15fab505..9ce078c15 100644 --- a/test/token_wrapper_test.ts +++ b/test/token_wrapper_test.ts @@ -52,4 +52,13 @@ describe('TokenWrapper', () => { expect(balance).to.be.bignumber.equal(expectedBalance); }); }); + describe('#getProxyAllowanceAsync', () => { + it('should return 0 if no allowance set yet', async () => { + const aToken = tokens[0]; + const aOwner = userAddresses[0]; + const allowance = await zeroEx.token.getProxyAllowanceAsync(aToken.address, aOwner); + const expectedAllowance = new BigNumber('0'); + expect(allowance).to.be.bignumber.equal(expectedAllowance); + }); + }); }); -- cgit v1.2.3 From f066b521438f6e2d4a8c32194a569e59c7079c4f Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 15:28:45 +0200 Subject: remove spaces after it keyword --- test/token_wrapper_test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test/token_wrapper_test.ts') diff --git a/test/token_wrapper_test.ts b/test/token_wrapper_test.ts index 9ce078c15..c9cd08028 100644 --- a/test/token_wrapper_test.ts +++ b/test/token_wrapper_test.ts @@ -38,13 +38,13 @@ describe('TokenWrapper', () => { const expectedBalance = new BigNumber('100000000000000000000000000'); expect(balance).to.be.bignumber.equal(expectedBalance); }); - it ('should throw a CONTRACT_DOES_NOT_EXIST error for a non-existent token contract', async () => { + it('should throw a CONTRACT_DOES_NOT_EXIST error for a non-existent token contract', async () => { const nonExistentTokenAddress = '0x9dd402f14d67e001d8efbe6583e51bf9706aa065'; const aOwnerAddress = userAddresses[0]; expect(zeroEx.token.getBalanceAsync(nonExistentTokenAddress, aOwnerAddress)) .to.be.rejectedWith(ZeroExError.CONTRACT_DOES_NOT_EXIST); }); - it ('should return a balance of 0 for a non-existent owner address', async () => { + it('should return a balance of 0 for a non-existent owner address', async () => { const aToken = tokens[0]; const aNonExistentOwner = '0x198C6Ad858F213Fb31b6FE809E25040E6B964593'; const balance = await zeroEx.token.getBalanceAsync(aToken.address, aNonExistentOwner); -- cgit v1.2.3 From 8df2926a1acf698f469deaf03a0de35e2179ddf2 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 16:31:02 +0200 Subject: Add test for setProxyAllowanceAsync and additional test for getProxyAllowanceAsync --- test/token_wrapper_test.ts | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) (limited to 'test/token_wrapper_test.ts') diff --git a/test/token_wrapper_test.ts b/test/token_wrapper_test.ts index c9cd08028..3a1a3c3a5 100644 --- a/test/token_wrapper_test.ts +++ b/test/token_wrapper_test.ts @@ -53,12 +53,42 @@ describe('TokenWrapper', () => { }); }); describe('#getProxyAllowanceAsync', () => { + it('should get the proxy allowance', async () => { + const aToken = tokens[0]; + const aOwnerAddress = userAddresses[0]; + + const amountInUnits = new BigNumber('50'); + const amountInBaseUnits = ZeroEx.toBaseUnitAmount(amountInUnits, aToken.decimals); + await zeroEx.token.setProxyAllowanceAsync(aToken.address, aOwnerAddress, amountInBaseUnits); + + const allowance = await zeroEx.token.getProxyAllowanceAsync(aToken.address, aOwnerAddress); + const expectedAllowance = amountInBaseUnits; + expect(allowance).to.be.bignumber.equal(expectedAllowance); + }); it('should return 0 if no allowance set yet', async () => { const aToken = tokens[0]; - const aOwner = userAddresses[0]; - const allowance = await zeroEx.token.getProxyAllowanceAsync(aToken.address, aOwner); + const aOwnerAddress = userAddresses[0]; + const allowance = await zeroEx.token.getProxyAllowanceAsync(aToken.address, aOwnerAddress); const expectedAllowance = new BigNumber('0'); expect(allowance).to.be.bignumber.equal(expectedAllowance); }); }); + describe('#setProxyAllowanceAsync', () => { + it('should set the proxy allowance', async () => { + const aToken = tokens[0]; + const aOwnerAddress = userAddresses[0]; + + const allowanceBeforeSet = await zeroEx.token.getProxyAllowanceAsync(aToken.address, aOwnerAddress); + const expectedAllowanceBeforeAllowanceSet = new BigNumber('0'); + expect(allowanceBeforeSet).to.be.bignumber.equal(expectedAllowanceBeforeAllowanceSet); + + const amountInUnits = new BigNumber('50'); + const amountInBaseUnits = ZeroEx.toBaseUnitAmount(amountInUnits, aToken.decimals); + await zeroEx.token.setProxyAllowanceAsync(aToken.address, aOwnerAddress, amountInBaseUnits); + + const allowanceAfterSet = await zeroEx.token.getProxyAllowanceAsync(aToken.address, aOwnerAddress); + const expectedAllowanceAfterAllowanceSet = amountInBaseUnits; + expect(allowanceAfterSet).to.be.bignumber.equal(expectedAllowanceAfterAllowanceSet); + }); + }); }); -- cgit v1.2.3 From 5e6818d3eff89347e704edde4bfee72066caedb1 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 30 May 2017 16:36:38 +0200 Subject: remove unused imports --- test/token_wrapper_test.ts | 2 -- 1 file changed, 2 deletions(-) (limited to 'test/token_wrapper_test.ts') diff --git a/test/token_wrapper_test.ts b/test/token_wrapper_test.ts index 3a1a3c3a5..5ce3efb14 100644 --- a/test/token_wrapper_test.ts +++ b/test/token_wrapper_test.ts @@ -1,6 +1,5 @@ import 'mocha'; import * as chai from 'chai'; -import chaiAsPromised = require('chai-as-promised'); import * as Web3 from 'web3'; import * as BigNumber from 'bignumber.js'; import promisify = require('es6-promisify'); @@ -10,7 +9,6 @@ import {ZeroExError, Token} from '../src/types'; import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; const expect = chai.expect; -chai.use(chaiAsPromised); const blockchainLifecycle = new BlockchainLifecycle(); describe('TokenWrapper', () => { -- cgit v1.2.3 From 9409e0aba1d1fa62a60c25149ae1b7115de9e2c9 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 31 May 2017 11:16:57 +0200 Subject: Add transfer function and tests for it --- test/token_wrapper_test.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'test/token_wrapper_test.ts') diff --git a/test/token_wrapper_test.ts b/test/token_wrapper_test.ts index 5ce3efb14..113dd32d6 100644 --- a/test/token_wrapper_test.ts +++ b/test/token_wrapper_test.ts @@ -28,6 +28,25 @@ describe('TokenWrapper', () => { afterEach(async () => { await blockchainLifecycle.revertAsync(); }); + describe('#transferAsync', () => { + it('should successfully transfer tokens', async () => { + const token = tokens[0]; + const fromAddress = userAddresses[0]; + const toAddress = userAddresses[1]; + const preBalance = await zeroEx.token.getBalanceAsync(token.address, toAddress); + expect(preBalance).to.be.bignumber.equal(0); + await zeroEx.token.transferAsync(token.address, fromAddress, toAddress, new BigNumber(42)); + const postBalance = await zeroEx.token.getBalanceAsync(token.address, toAddress); + expect(postBalance).to.be.bignumber.equal(42); + }); + it('should throw a CONTRACT_DOES_NOT_EXIST error for a non-existent token contract', async () => { + const nonExistentTokenAddress = '0x9dd402f14d67e001d8efbe6583e51bf9706aa065'; + const aOwnerAddress = userAddresses[0]; + expect(zeroEx.token.transferAsync( + nonExistentTokenAddress, userAddresses[0], userAddresses[1], new BigNumber(42), + )).to.be.rejectedWith(ZeroExError.CONTRACT_DOES_NOT_EXIST); + }); + }); describe('#getBalanceAsync', () => { it('should return the balance for an existing ERC20 token', async () => { const aToken = tokens[0]; -- cgit v1.2.3