diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-08-24 21:58:22 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-08-25 04:48:51 +0800 |
commit | 79dcc1660e5201a6c8738ed6117ba80e96d7c225 (patch) | |
tree | 5c273ca14d8b139a60569e4b54a4f7c6bede2d65 /test | |
parent | 6f227c152d49b6266fab3c8eb054c16e3c9cef45 (diff) | |
download | dexon-0x-contracts-79dcc1660e5201a6c8738ed6117ba80e96d7c225.tar.gz dexon-0x-contracts-79dcc1660e5201a6c8738ed6117ba80e96d7c225.tar.zst dexon-0x-contracts-79dcc1660e5201a6c8738ed6117ba80e96d7c225.zip |
Add a test that unlimited allowance reduces gas cost
Diffstat (limited to 'test')
-rw-r--r-- | test/token_wrapper_test.ts | 28 |
1 files changed, 28 insertions, 0 deletions
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', () => { |