diff --git a/contracts/asset-proxy/contracts/src/bridges/Eth2DaiBridge.sol b/contracts/asset-proxy/contracts/src/bridges/Eth2DaiBridge.sol index 3c4b3e634f..2d12c1f729 100644 --- a/contracts/asset-proxy/contracts/src/bridges/Eth2DaiBridge.sol +++ b/contracts/asset-proxy/contracts/src/bridges/Eth2DaiBridge.sol @@ -20,8 +20,8 @@ pragma solidity ^0.5.9; pragma experimental ABIEncoderV2; import "@0x/contracts-erc20/contracts/src/interfaces/IERC20Token.sol"; +import "@0x/contracts-erc20/contracts/src/LibERC20Token.sol"; import "@0x/contracts-exchange-libs/contracts/src/IWallet.sol"; -import "@0x/contracts-utils/contracts/src/LibERC20Token.sol"; import "../interfaces/IERC20Bridge.sol"; import "../interfaces/IEth2Dai.sol"; diff --git a/contracts/asset-proxy/contracts/src/bridges/UniswapBridge.sol b/contracts/asset-proxy/contracts/src/bridges/UniswapBridge.sol index 587882a21f..2f5327b36d 100644 --- a/contracts/asset-proxy/contracts/src/bridges/UniswapBridge.sol +++ b/contracts/asset-proxy/contracts/src/bridges/UniswapBridge.sol @@ -21,8 +21,8 @@ pragma experimental ABIEncoderV2; import "@0x/contracts-erc20/contracts/src/interfaces/IERC20Token.sol"; import "@0x/contracts-erc20/contracts/src/interfaces/IEtherToken.sol"; +import "@0x/contracts-erc20/contracts/src/LibERC20Token.sol"; import "@0x/contracts-exchange-libs/contracts/src/IWallet.sol"; -import "@0x/contracts-utils/contracts/src/LibERC20Token.sol"; import "../interfaces/IUniswapExchangeFactory.sol"; import "../interfaces/IUniswapExchange.sol"; import "../interfaces/IERC20Bridge.sol"; diff --git a/contracts/erc20/CHANGELOG.json b/contracts/erc20/CHANGELOG.json index 74e072b7b5..85f06d6968 100644 --- a/contracts/erc20/CHANGELOG.json +++ b/contracts/erc20/CHANGELOG.json @@ -1,4 +1,13 @@ [ + { + "version": "2.3.0-beta.1", + "changes": [ + { + "note": "Create `LibERC20Token`", + "pr": 2309 + } + ] + }, { "version": "2.3.0-beta.0", "changes": [ diff --git a/contracts/utils/contracts/src/LibERC20Token.sol b/contracts/erc20/contracts/src/LibERC20Token.sol similarity index 88% rename from contracts/utils/contracts/src/LibERC20Token.sol rename to contracts/erc20/contracts/src/LibERC20Token.sol index 97df57b0d9..e41a2875e8 100644 --- a/contracts/utils/contracts/src/LibERC20Token.sol +++ b/contracts/erc20/contracts/src/LibERC20Token.sol @@ -18,6 +18,10 @@ pragma solidity ^0.5.9; +import "@0x/contracts-utils/contracts/src/LibRichErrors.sol"; +import "@0x/contracts-utils/contracts/src/LibBytes.sol"; +import "../src/interfaces/IERC20Token.sol"; + library LibERC20Token { @@ -35,7 +39,7 @@ library LibERC20Token { internal { bytes memory callData = abi.encodeWithSelector( - 0x095ea7b3, + IERC20Token(0).approve.selector, spender, allowance ); @@ -56,7 +60,7 @@ library LibERC20Token { internal { bytes memory callData = abi.encodeWithSelector( - 0xa9059cbb, + IERC20Token(0).transfer.selector, to, amount ); @@ -79,7 +83,7 @@ library LibERC20Token { internal { bytes memory callData = abi.encodeWithSelector( - 0x23b872dd, + IERC20Token(0).transferFrom.selector, from, to, amount @@ -104,13 +108,12 @@ library LibERC20Token { return; } if (resultData.length == 32) { - uint256 result; - assembly { result := mload(add(resultData, 0x20)) } + uint256 result = LibBytes.readUint256(resultData, 0); if (result == 1) { return; } } } - assembly { revert(add(resultData, 0x20), mload(resultData)) } + LibRichErrors.rrevert(resultData); } } diff --git a/contracts/utils/contracts/test/TestLibERC20Token.sol b/contracts/erc20/contracts/test/TestLibERC20Token.sol similarity index 100% rename from contracts/utils/contracts/test/TestLibERC20Token.sol rename to contracts/erc20/contracts/test/TestLibERC20Token.sol diff --git a/contracts/utils/contracts/test/TestLibERC20TokenTarget.sol b/contracts/erc20/contracts/test/TestLibERC20TokenTarget.sol similarity index 100% rename from contracts/utils/contracts/test/TestLibERC20TokenTarget.sol rename to contracts/erc20/contracts/test/TestLibERC20TokenTarget.sol diff --git a/contracts/erc20/package.json b/contracts/erc20/package.json index bd851a7056..9c3a20355d 100644 --- a/contracts/erc20/package.json +++ b/contracts/erc20/package.json @@ -34,7 +34,7 @@ "lint-contracts": "solhint -c ../.solhint.json contracts/**/**/**/**/*.sol" }, "config": { - "abis": "./generated-artifacts/@(DummyERC20Token|DummyMultipleReturnERC20Token|DummyNoReturnERC20Token|ERC20Token|IERC20Token|IEtherToken|MintableERC20Token|UnlimitedAllowanceERC20Token|UntransferrableDummyERC20Token|WETH9|ZRXToken).json", + "abis": "./generated-artifacts/@(DummyERC20Token|DummyMultipleReturnERC20Token|DummyNoReturnERC20Token|ERC20Token|IERC20Token|IEtherToken|LibERC20Token|MintableERC20Token|TestLibERC20Token|TestLibERC20TokenTarget|UnlimitedAllowanceERC20Token|UntransferrableDummyERC20Token|WETH9|ZRXToken).json", "abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually." }, "repository": { diff --git a/contracts/erc20/src/artifacts.ts b/contracts/erc20/src/artifacts.ts index 6d0e08c3a7..8d48dc2753 100644 --- a/contracts/erc20/src/artifacts.ts +++ b/contracts/erc20/src/artifacts.ts @@ -11,13 +11,17 @@ import * as DummyNoReturnERC20Token from '../generated-artifacts/DummyNoReturnER import * as ERC20Token from '../generated-artifacts/ERC20Token.json'; import * as IERC20Token from '../generated-artifacts/IERC20Token.json'; import * as IEtherToken from '../generated-artifacts/IEtherToken.json'; +import * as LibERC20Token from '../generated-artifacts/LibERC20Token.json'; import * as MintableERC20Token from '../generated-artifacts/MintableERC20Token.json'; +import * as TestLibERC20Token from '../generated-artifacts/TestLibERC20Token.json'; +import * as TestLibERC20TokenTarget from '../generated-artifacts/TestLibERC20TokenTarget.json'; import * as UnlimitedAllowanceERC20Token from '../generated-artifacts/UnlimitedAllowanceERC20Token.json'; import * as UntransferrableDummyERC20Token from '../generated-artifacts/UntransferrableDummyERC20Token.json'; import * as WETH9 from '../generated-artifacts/WETH9.json'; import * as ZRXToken from '../generated-artifacts/ZRXToken.json'; export const artifacts = { ERC20Token: ERC20Token as ContractArtifact, + LibERC20Token: LibERC20Token as ContractArtifact, MintableERC20Token: MintableERC20Token as ContractArtifact, UnlimitedAllowanceERC20Token: UnlimitedAllowanceERC20Token as ContractArtifact, WETH9: WETH9 as ContractArtifact, @@ -27,5 +31,7 @@ export const artifacts = { DummyERC20Token: DummyERC20Token as ContractArtifact, DummyMultipleReturnERC20Token: DummyMultipleReturnERC20Token as ContractArtifact, DummyNoReturnERC20Token: DummyNoReturnERC20Token as ContractArtifact, + TestLibERC20Token: TestLibERC20Token as ContractArtifact, + TestLibERC20TokenTarget: TestLibERC20TokenTarget as ContractArtifact, UntransferrableDummyERC20Token: UntransferrableDummyERC20Token as ContractArtifact, }; diff --git a/contracts/erc20/src/wrappers.ts b/contracts/erc20/src/wrappers.ts index 9a921ccfbd..e38e07af17 100644 --- a/contracts/erc20/src/wrappers.ts +++ b/contracts/erc20/src/wrappers.ts @@ -9,7 +9,10 @@ export * from '../generated-wrappers/dummy_no_return_erc20_token'; export * from '../generated-wrappers/erc20_token'; export * from '../generated-wrappers/i_erc20_token'; export * from '../generated-wrappers/i_ether_token'; +export * from '../generated-wrappers/lib_erc20_token'; export * from '../generated-wrappers/mintable_erc20_token'; +export * from '../generated-wrappers/test_lib_erc20_token'; +export * from '../generated-wrappers/test_lib_erc20_token_target'; export * from '../generated-wrappers/unlimited_allowance_erc20_token'; export * from '../generated-wrappers/untransferrable_dummy_erc20_token'; export * from '../generated-wrappers/weth9'; diff --git a/contracts/utils/test/lib_erc20_token.ts b/contracts/erc20/test/lib_erc20_token.ts similarity index 93% rename from contracts/utils/test/lib_erc20_token.ts rename to contracts/erc20/test/lib_erc20_token.ts index da625963ec..b843867a8c 100644 --- a/contracts/utils/test/lib_erc20_token.ts +++ b/contracts/erc20/test/lib_erc20_token.ts @@ -2,21 +2,14 @@ import { blockchainTests, constants, expect, - filterLogsToArguments, getRandomInteger, hexLeftPad, randomAddress, + verifyEventsFromLogs, } from '@0x/contracts-test-utils'; import { RawRevertError, StringRevertError } from '@0x/utils'; -import { - artifacts, - TestLibERC20TokenContract, - TestLibERC20TokenTargetApproveCalledEventArgs as ApproveCalled, - TestLibERC20TokenTargetEvents, - TestLibERC20TokenTargetTransferCalledEventArgs as TransferCalled, - TestLibERC20TokenTargetTransferFromCalledEventArgs as TransferFromCalled, -} from '../src'; +import { artifacts, TestLibERC20TokenContract, TestLibERC20TokenTargetEvents } from '../src'; blockchainTests('LibERC20Token', env => { let testContract: TestLibERC20TokenContract; @@ -52,11 +45,7 @@ blockchainTests('LibERC20Token', env => { allowance, ); expect(logs).to.be.length(1); - const [event] = filterLogsToArguments(logs, TestLibERC20TokenTargetEvents.ApproveCalled); - expect(event).to.deep.eq({ - spender, - allowance, - }); + verifyEventsFromLogs(logs, [{ spender, allowance }], TestLibERC20TokenTargetEvents.ApproveCalled); }); it('succeeds if the target returns true', async () => { @@ -178,11 +167,7 @@ blockchainTests('LibERC20Token', env => { amount, ); expect(logs).to.be.length(1); - const [event] = filterLogsToArguments(logs, TestLibERC20TokenTargetEvents.TransferCalled); - expect(event).to.deep.eq({ - to, - amount, - }); + verifyEventsFromLogs(logs, [{ to, amount }], TestLibERC20TokenTargetEvents.TransferCalled); }); it('succeeds if the target returns true', async () => { @@ -306,15 +291,7 @@ blockchainTests('LibERC20Token', env => { amount, ); expect(logs).to.be.length(1); - const [event] = filterLogsToArguments( - logs, - TestLibERC20TokenTargetEvents.TransferFromCalled, - ); - expect(event).to.deep.eq({ - from: owner, - to, - amount, - }); + verifyEventsFromLogs(logs, [{ from: owner, to, amount }], TestLibERC20TokenTargetEvents.TransferFromCalled); }); it('succeeds if the target returns true', async () => { diff --git a/contracts/erc20/tsconfig.json b/contracts/erc20/tsconfig.json index 26a14799e4..5c706d141a 100644 --- a/contracts/erc20/tsconfig.json +++ b/contracts/erc20/tsconfig.json @@ -9,7 +9,10 @@ "generated-artifacts/ERC20Token.json", "generated-artifacts/IERC20Token.json", "generated-artifacts/IEtherToken.json", + "generated-artifacts/LibERC20Token.json", "generated-artifacts/MintableERC20Token.json", + "generated-artifacts/TestLibERC20Token.json", + "generated-artifacts/TestLibERC20TokenTarget.json", "generated-artifacts/UnlimitedAllowanceERC20Token.json", "generated-artifacts/UntransferrableDummyERC20Token.json", "generated-artifacts/WETH9.json", diff --git a/contracts/exchange-forwarder/contracts/src/MixinAssets.sol b/contracts/exchange-forwarder/contracts/src/MixinAssets.sol index 2fef16220b..6813c45e98 100644 --- a/contracts/exchange-forwarder/contracts/src/MixinAssets.sol +++ b/contracts/exchange-forwarder/contracts/src/MixinAssets.sol @@ -19,10 +19,10 @@ pragma solidity ^0.5.9; import "@0x/contracts-utils/contracts/src/LibBytes.sol"; -import "@0x/contracts-utils/contracts/src/LibERC20Token.sol"; import "@0x/contracts-utils/contracts/src/LibRichErrors.sol"; import "@0x/contracts-utils/contracts/src/Ownable.sol"; import "@0x/contracts-erc20/contracts/src/interfaces/IERC20Token.sol"; +import "@0x/contracts-erc20/contracts/src/LibERC20Token.sol"; import "@0x/contracts-erc721/contracts/src/interfaces/IERC721Token.sol"; import "./libs/LibConstants.sol"; import "./libs/LibForwarderRichErrors.sol"; diff --git a/contracts/utils/CHANGELOG.json b/contracts/utils/CHANGELOG.json index f295d3cc07..dc3f8d93a4 100644 --- a/contracts/utils/CHANGELOG.json +++ b/contracts/utils/CHANGELOG.json @@ -1,13 +1,4 @@ [ - { - "version": "3.3.0-beta.1", - "changes": [ - { - "note": "Create `LibERC20Token`", - "pr": 2309 - } - ] - }, { "version": "3.3.0-beta.0", "changes": [ diff --git a/contracts/utils/package.json b/contracts/utils/package.json index b63c57d863..a1d1f697dd 100644 --- a/contracts/utils/package.json +++ b/contracts/utils/package.json @@ -36,7 +36,7 @@ }, "config": { "abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually.", - "abis": "./generated-artifacts/@(Authorizable|IAuthorizable|IOwnable|LibAddress|LibAddressArray|LibAddressArrayRichErrors|LibAuthorizableRichErrors|LibBytes|LibBytesRichErrors|LibEIP1271|LibEIP712|LibERC20Token|LibFractions|LibOwnableRichErrors|LibReentrancyGuardRichErrors|LibRichErrors|LibSafeMath|LibSafeMathRichErrors|Ownable|ReentrancyGuard|Refundable|TestLibAddress|TestLibAddressArray|TestLibBytes|TestLibEIP712|TestLibERC20Token|TestLibERC20TokenTarget|TestLibRichErrors|TestLibSafeMath|TestLogDecoding|TestLogDecodingDownstream|TestOwnable|TestReentrancyGuard|TestRefundable|TestRefundableReceiver).json" + "abis": "./generated-artifacts/@(Authorizable|IAuthorizable|IOwnable|LibAddress|LibAddressArray|LibAddressArrayRichErrors|LibAuthorizableRichErrors|LibBytes|LibBytesRichErrors|LibEIP1271|LibEIP712|LibFractions|LibOwnableRichErrors|LibReentrancyGuardRichErrors|LibRichErrors|LibSafeMath|LibSafeMathRichErrors|Ownable|ReentrancyGuard|Refundable|TestLibAddress|TestLibAddressArray|TestLibBytes|TestLibEIP712|TestLibRichErrors|TestLibSafeMath|TestLogDecoding|TestLogDecodingDownstream|TestOwnable|TestReentrancyGuard|TestRefundable|TestRefundableReceiver).json" }, "repository": { "type": "git", diff --git a/contracts/utils/src/artifacts.ts b/contracts/utils/src/artifacts.ts index bcbdd49ca1..983f2951ed 100644 --- a/contracts/utils/src/artifacts.ts +++ b/contracts/utils/src/artifacts.ts @@ -16,7 +16,6 @@ import * as LibBytes from '../generated-artifacts/LibBytes.json'; import * as LibBytesRichErrors from '../generated-artifacts/LibBytesRichErrors.json'; import * as LibEIP1271 from '../generated-artifacts/LibEIP1271.json'; import * as LibEIP712 from '../generated-artifacts/LibEIP712.json'; -import * as LibERC20Token from '../generated-artifacts/LibERC20Token.json'; import * as LibFractions from '../generated-artifacts/LibFractions.json'; import * as LibOwnableRichErrors from '../generated-artifacts/LibOwnableRichErrors.json'; import * as LibReentrancyGuardRichErrors from '../generated-artifacts/LibReentrancyGuardRichErrors.json'; @@ -30,8 +29,6 @@ import * as TestLibAddress from '../generated-artifacts/TestLibAddress.json'; import * as TestLibAddressArray from '../generated-artifacts/TestLibAddressArray.json'; import * as TestLibBytes from '../generated-artifacts/TestLibBytes.json'; import * as TestLibEIP712 from '../generated-artifacts/TestLibEIP712.json'; -import * as TestLibERC20Token from '../generated-artifacts/TestLibERC20Token.json'; -import * as TestLibERC20TokenTarget from '../generated-artifacts/TestLibERC20TokenTarget.json'; import * as TestLibRichErrors from '../generated-artifacts/TestLibRichErrors.json'; import * as TestLibSafeMath from '../generated-artifacts/TestLibSafeMath.json'; import * as TestLogDecoding from '../generated-artifacts/TestLogDecoding.json'; @@ -50,7 +47,6 @@ export const artifacts = { LibBytesRichErrors: LibBytesRichErrors as ContractArtifact, LibEIP1271: LibEIP1271 as ContractArtifact, LibEIP712: LibEIP712 as ContractArtifact, - LibERC20Token: LibERC20Token as ContractArtifact, LibFractions: LibFractions as ContractArtifact, LibOwnableRichErrors: LibOwnableRichErrors as ContractArtifact, LibReentrancyGuardRichErrors: LibReentrancyGuardRichErrors as ContractArtifact, @@ -66,8 +62,6 @@ export const artifacts = { TestLibAddressArray: TestLibAddressArray as ContractArtifact, TestLibBytes: TestLibBytes as ContractArtifact, TestLibEIP712: TestLibEIP712 as ContractArtifact, - TestLibERC20Token: TestLibERC20Token as ContractArtifact, - TestLibERC20TokenTarget: TestLibERC20TokenTarget as ContractArtifact, TestLibRichErrors: TestLibRichErrors as ContractArtifact, TestLibSafeMath: TestLibSafeMath as ContractArtifact, TestLogDecoding: TestLogDecoding as ContractArtifact, diff --git a/contracts/utils/src/wrappers.ts b/contracts/utils/src/wrappers.ts index 86e083de14..da266a03dd 100644 --- a/contracts/utils/src/wrappers.ts +++ b/contracts/utils/src/wrappers.ts @@ -14,7 +14,6 @@ export * from '../generated-wrappers/lib_bytes'; export * from '../generated-wrappers/lib_bytes_rich_errors'; export * from '../generated-wrappers/lib_e_i_p1271'; export * from '../generated-wrappers/lib_e_i_p712'; -export * from '../generated-wrappers/lib_erc20_token'; export * from '../generated-wrappers/lib_fractions'; export * from '../generated-wrappers/lib_ownable_rich_errors'; export * from '../generated-wrappers/lib_reentrancy_guard_rich_errors'; @@ -28,8 +27,6 @@ export * from '../generated-wrappers/test_lib_address'; export * from '../generated-wrappers/test_lib_address_array'; export * from '../generated-wrappers/test_lib_bytes'; export * from '../generated-wrappers/test_lib_e_i_p712'; -export * from '../generated-wrappers/test_lib_erc20_token'; -export * from '../generated-wrappers/test_lib_erc20_token_target'; export * from '../generated-wrappers/test_lib_rich_errors'; export * from '../generated-wrappers/test_lib_safe_math'; export * from '../generated-wrappers/test_log_decoding'; diff --git a/contracts/utils/tsconfig.json b/contracts/utils/tsconfig.json index 20d0a8b185..58803aad69 100644 --- a/contracts/utils/tsconfig.json +++ b/contracts/utils/tsconfig.json @@ -14,7 +14,6 @@ "generated-artifacts/LibBytesRichErrors.json", "generated-artifacts/LibEIP1271.json", "generated-artifacts/LibEIP712.json", - "generated-artifacts/LibERC20Token.json", "generated-artifacts/LibFractions.json", "generated-artifacts/LibOwnableRichErrors.json", "generated-artifacts/LibReentrancyGuardRichErrors.json", @@ -28,8 +27,6 @@ "generated-artifacts/TestLibAddressArray.json", "generated-artifacts/TestLibBytes.json", "generated-artifacts/TestLibEIP712.json", - "generated-artifacts/TestLibERC20Token.json", - "generated-artifacts/TestLibERC20TokenTarget.json", "generated-artifacts/TestLibRichErrors.json", "generated-artifacts/TestLibSafeMath.json", "generated-artifacts/TestLogDecoding.json",