aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2017-08-17 06:34:09 +0800
committerFabio Berger <me@fabioberger.com>2017-08-17 06:34:09 +0800
commita32b94bac440f127c0b984d7a1e763cf4cb86792 (patch)
tree22aa4b13e067017220c579958bcc8225a5b22a84
parent6ec3c8728e639b74bd94ce222097fd0841bb0399 (diff)
downloaddexon-sol-tools-a32b94bac440f127c0b984d7a1e763cf4cb86792.tar.gz
dexon-sol-tools-a32b94bac440f127c0b984d7a1e763cf4cb86792.tar.zst
dexon-sol-tools-a32b94bac440f127c0b984d7a1e763cf4cb86792.zip
Remove isUserAddressAvailable assertion from getBalanceAsync and add regression test
-rw-r--r--src/contract_wrappers/token_wrapper.ts1
-rw-r--r--src/subproviders/empty_wallet_subprovider.ts25
-rw-r--r--test/token_wrapper_test.ts54
-rw-r--r--test/utils/web3_factory.ts10
4 files changed, 68 insertions, 22 deletions
diff --git a/src/contract_wrappers/token_wrapper.ts b/src/contract_wrappers/token_wrapper.ts
index 9c073f30b..477daec59 100644
--- a/src/contract_wrappers/token_wrapper.ts
+++ b/src/contract_wrappers/token_wrapper.ts
@@ -45,7 +45,6 @@ export class TokenWrapper extends ContractWrapper {
public async getBalanceAsync(tokenAddress: string, ownerAddress: string): Promise<BigNumber.BigNumber> {
assert.isETHAddressHex('ownerAddress', ownerAddress);
assert.isETHAddressHex('tokenAddress', tokenAddress);
- await assert.isUserAddressAvailableAsync(this._web3Wrapper);
const tokenContract = await this._getTokenContractAsync(tokenAddress);
let balance = await tokenContract.balanceOf.call(ownerAddress);
diff --git a/src/subproviders/empty_wallet_subprovider.ts b/src/subproviders/empty_wallet_subprovider.ts
new file mode 100644
index 000000000..40d3d418d
--- /dev/null
+++ b/src/subproviders/empty_wallet_subprovider.ts
@@ -0,0 +1,25 @@
+import * as _ from 'lodash';
+import Web3 = require('web3');
+
+/*
+ * This class implements the web3-provider-engine subprovider interface and returns
+ * that the provider has no addresses when queried.
+ * Source: https://github.com/MetaMask/provider-engine/blob/master/subproviders/subprovider.js
+ */
+export class EmptyWalletSubProvider {
+ public handleRequest(payload: any, next: () => void, end: (err: Error|null, result: any) => void) {
+ switch (payload.method) {
+ case 'eth_accounts':
+ end(null, []);
+ return;
+
+ default:
+ next();
+ return;
+ }
+ }
+ // Required to implement this method despite not needing it for this subprovider
+ public setEngine(engine: any) {
+ // noop
+ }
+}
diff --git a/test/token_wrapper_test.ts b/test/token_wrapper_test.ts
index 52a2507bf..d157efec4 100644
--- a/test/token_wrapper_test.ts
+++ b/test/token_wrapper_test.ts
@@ -149,25 +149,43 @@ describe('TokenWrapper', () => {
});
});
describe('#getBalanceAsync', () => {
- it('should return the balance for an existing ERC20 token', async () => {
- const token = tokens[0];
- const ownerAddress = coinbase;
- const balance = await zeroEx.token.getBalanceAsync(token.address, ownerAddress);
- const expectedBalance = new BigNumber('100000000000000000000000000');
- return expect(balance).to.be.bignumber.equal(expectedBalance);
+ describe('With web3 provider with accounts', () => {
+ it('should return the balance for an existing ERC20 token', async () => {
+ const token = tokens[0];
+ const ownerAddress = coinbase;
+ const balance = await zeroEx.token.getBalanceAsync(token.address, ownerAddress);
+ const expectedBalance = new BigNumber('100000000000000000000000000');
+ return 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 ownerAddress = coinbase;
+ return expect(zeroEx.token.getBalanceAsync(nonExistentTokenAddress, ownerAddress))
+ .to.be.rejectedWith(ZeroExError.ContractDoesNotExist);
+ });
+ it('should return a balance of 0 for a non-existent owner address', async () => {
+ const token = tokens[0];
+ const nonExistentOwner = '0x198C6Ad858F213Fb31b6FE809E25040E6B964593';
+ const balance = await zeroEx.token.getBalanceAsync(token.address, nonExistentOwner);
+ const expectedBalance = new BigNumber(0);
+ return 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 ownerAddress = coinbase;
- return expect(zeroEx.token.getBalanceAsync(nonExistentTokenAddress, ownerAddress))
- .to.be.rejectedWith(ZeroExError.ContractDoesNotExist);
- });
- it('should return a balance of 0 for a non-existent owner address', async () => {
- const token = tokens[0];
- const nonExistentOwner = '0x198C6Ad858F213Fb31b6FE809E25040E6B964593';
- const balance = await zeroEx.token.getBalanceAsync(token.address, nonExistentOwner);
- const expectedBalance = new BigNumber(0);
- return expect(balance).to.be.bignumber.equal(expectedBalance);
+ describe('With web3 provider without accounts', () => {
+ let zeroExWithoutAccounts: ZeroEx;
+ before(async () => {
+ const hasAddresses = false;
+ const web3WithoutAccounts = web3Factory.create(hasAddresses);
+ web3WithoutAccounts.eth.getAccounts(console.log);
+ zeroExWithoutAccounts = new ZeroEx(web3WithoutAccounts.currentProvider);
+ });
+ it('should return balance even when called with Web3 provider instance without addresses', async () => {
+ const token = tokens[0];
+ const ownerAddress = coinbase;
+ const balance = await zeroExWithoutAccounts.token.getBalanceAsync(token.address, ownerAddress);
+ const expectedBalance = new BigNumber('100000000000000000000000000');
+ return expect(balance).to.be.bignumber.equal(expectedBalance);
+ });
});
});
describe('#setAllowanceAsync', () => {
diff --git a/test/utils/web3_factory.ts b/test/utils/web3_factory.ts
index ffdc0e4cf..f89c907f6 100644
--- a/test/utils/web3_factory.ts
+++ b/test/utils/web3_factory.ts
@@ -7,17 +7,21 @@ import ProviderEngine = require('web3-provider-engine');
import RpcSubprovider = require('web3-provider-engine/subproviders/rpc');
import * as Web3 from 'web3';
import {constants} from './constants';
+import {EmptyWalletSubProvider} from '../../src/subproviders/empty_wallet_subprovider';
export const web3Factory = {
- create(): Web3 {
- const provider = this.getRpcProvider();
+ create(hasAddresses: boolean = true): Web3 {
+ const provider = this.getRpcProvider(hasAddresses);
const web3 = new Web3();
web3.setProvider(provider);
return web3;
},
- getRpcProvider(): Web3.Provider {
+ getRpcProvider(hasAddresses: boolean): Web3.Provider {
const provider = new ProviderEngine();
const rpcUrl = `http://${constants.RPC_HOST}:${constants.RPC_PORT}`;
+ if (!hasAddresses) {
+ provider.addProvider(new EmptyWalletSubProvider());
+ }
provider.addProvider(new RpcSubprovider({
rpcUrl,
}));