From 79dcc1660e5201a6c8738ed6117ba80e96d7c225 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 24 Aug 2017 15:58:22 +0200 Subject: Add a test that unlimited allowance reduces gas cost --- test/token_wrapper_test.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'test/token_wrapper_test.ts') diff --git a/test/token_wrapper_test.ts b/test/token_wrapper_test.ts index e466cc0ad..22e1ff12d 100644 --- a/test/token_wrapper_test.ts +++ b/test/token_wrapper_test.ts @@ -16,6 +16,7 @@ import { ApprovalContractEventArgs, } from '../src'; import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; +import {TokenUtils} from './utils/token_utils'; import {DoneCallback} from '../src/types'; chaiSetup.configure(); @@ -27,6 +28,7 @@ describe('TokenWrapper', () => { let zeroEx: ZeroEx; let userAddresses: string[]; let tokens: Token[]; + let tokenUtils: TokenUtils; let coinbase: string; let addressWithoutFunds: string; before(async () => { @@ -34,6 +36,7 @@ describe('TokenWrapper', () => { zeroEx = new ZeroEx(web3.currentProvider); userAddresses = await promisify(web3.eth.getAccounts)(); tokens = await zeroEx.tokenRegistry.getTokensAsync(); + tokenUtils = new TokenUtils(tokens); coinbase = userAddresses[0]; addressWithoutFunds = userAddresses[1]; }); @@ -216,6 +219,31 @@ describe('TokenWrapper', () => { const allowance = await zeroEx.token.getAllowanceAsync(token.address, ownerAddress, spenderAddress); return expect(allowance).to.be.bignumber.equal(zeroEx.token.UNLIMITED_ALLOWANCE_IN_BASE_UNITS); }); + it('should reduce the gas cost for transfers including tokens with unlimited allowance support', async () => { + const transferAmount = new BigNumber(5); + const zrx = tokenUtils.getProtocolTokenOrThrow(); + const [, userWithNormalAllowance, userWithUnlimitedAllowance] = userAddresses; + await zeroEx.token.setAllowanceAsync(zrx.address, coinbase, userWithNormalAllowance, transferAmount); + await zeroEx.token.setUnlimitedAllowanceAsync(zrx.address, coinbase, userWithUnlimitedAllowance); + + const initBalanceWithNormalAllowance = await promisify(web3.eth.getBalance)(userWithNormalAllowance); + const initBalanceWithUnlimitedAllowance = await promisify(web3.eth.getBalance)(userWithUnlimitedAllowance); + + await zeroEx.token.transferFromAsync( + zrx.address, coinbase, userWithNormalAllowance, userWithNormalAllowance, transferAmount, + ); + await zeroEx.token.transferFromAsync( + zrx.address, coinbase, userWithUnlimitedAllowance, userWithUnlimitedAllowance, transferAmount, + ); + + const finalBalanceWithNormalAllowance = await promisify(web3.eth.getBalance)(userWithNormalAllowance); + const finalBalanceWithUnlimitedAllowance = await promisify(web3.eth.getBalance)(userWithUnlimitedAllowance); + + const normalGasCost = initBalanceWithNormalAllowance.minus(finalBalanceWithNormalAllowance); + const unlimitedGasCost = initBalanceWithUnlimitedAllowance.minus(finalBalanceWithUnlimitedAllowance); + + expect(normalGasCost.toNumber()).to.be.gt(unlimitedGasCost.toNumber()); + }); }); describe('#getAllowanceAsync', () => { describe('With web3 provider with accounts', () => { -- cgit v1.2.3