diff options
author | Amir Bandeali <abandeali1@gmail.com> | 2018-08-13 08:34:03 +0800 |
---|---|---|
committer | Amir Bandeali <abandeali1@gmail.com> | 2018-08-17 08:31:21 +0800 |
commit | 0b9a9d92afc48745b2e4b438f31640c362882f78 (patch) | |
tree | 671cc12d1241f9ea9f32fabae47cc5854ea01083 /packages | |
parent | a82e36c1d473d7fde5af52683f7a04f74a1c63c1 (diff) | |
download | dexon-0x-contracts-0b9a9d92afc48745b2e4b438f31640c362882f78.tar.gz dexon-0x-contracts-0b9a9d92afc48745b2e4b438f31640c362882f78.tar.zst dexon-0x-contracts-0b9a9d92afc48745b2e4b438f31640c362882f78.zip |
Add tests for ERC721Token
Diffstat (limited to 'packages')
-rw-r--r-- | packages/contracts/test/tokens/erc721_token.ts | 278 | ||||
-rw-r--r-- | packages/contracts/test/tokens/unlimited_allowance_token.ts | 2 | ||||
-rw-r--r-- | packages/contracts/test/utils/artifacts.ts | 2 | ||||
-rw-r--r-- | packages/contracts/test/utils/log_decoder.ts | 1 |
4 files changed, 281 insertions, 2 deletions
diff --git a/packages/contracts/test/tokens/erc721_token.ts b/packages/contracts/test/tokens/erc721_token.ts new file mode 100644 index 000000000..0981e0f15 --- /dev/null +++ b/packages/contracts/test/tokens/erc721_token.ts @@ -0,0 +1,278 @@ +import { BlockchainLifecycle } from '@0xproject/dev-utils'; +import { RevertReason } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; +import * as chai from 'chai'; +import { LogWithDecodedArgs } from 'ethereum-types'; + +import { DummyERC721ReceiverContract } from '../../generated_contract_wrappers/dummy_erc721_receiver'; +import { + DummyERC721TokenContract, + DummyERC721TokenTransferEventArgs, +} from '../../generated_contract_wrappers/dummy_erc721_token'; +import { + InvalidERC721ReceiverContract, + InvalidERC721ReceiverTokenReceivedEventArgs, +} from '../../generated_contract_wrappers/invalid_erc721_receiver'; +import { artifacts } from '../utils/artifacts'; +import { expectContractCallFailed, expectContractCallFailedWithoutReasonAsync } from '../utils/assertions'; +import { chaiSetup } from '../utils/chai_setup'; +import { constants } from '../utils/constants'; +import { LogDecoder } from '../utils/log_decoder'; +import { provider, txDefaults, web3Wrapper } from '../utils/web3_wrapper'; + +chaiSetup.configure(); +const expect = chai.expect; +const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper); + +describe('ERC721Token', () => { + let owner: string; + let spender: string; + let token: DummyERC721TokenContract; + let erc721Receiver: DummyERC721ReceiverContract; + let logDecoder: LogDecoder; + const tokenId = new BigNumber(1); + before(async () => { + await blockchainLifecycle.startAsync(); + }); + after(async () => { + await blockchainLifecycle.revertAsync(); + }); + before(async () => { + const accounts = await web3Wrapper.getAvailableAddressesAsync(); + owner = accounts[0]; + spender = accounts[1]; + token = await DummyERC721TokenContract.deployFrom0xArtifactAsync( + artifacts.DummyERC721Token, + provider, + txDefaults, + constants.DUMMY_TOKEN_NAME, + constants.DUMMY_TOKEN_SYMBOL, + ); + erc721Receiver = await DummyERC721ReceiverContract.deployFrom0xArtifactAsync( + artifacts.DummyERC721Receiver, + provider, + txDefaults, + ); + logDecoder = new LogDecoder(web3Wrapper, token.address); + await web3Wrapper.awaitTransactionSuccessAsync( + await token.mint.sendTransactionAsync(owner, tokenId, { from: owner }), + constants.AWAIT_TRANSACTION_MINED_MS, + ); + }); + beforeEach(async () => { + await blockchainLifecycle.startAsync(); + }); + afterEach(async () => { + await blockchainLifecycle.revertAsync(); + }); + + describe('transferFrom', () => { + it('should revert if the tokenId is not owner', async () => { + const from = owner; + const to = erc721Receiver.address; + const unownedTokenId = new BigNumber(2); + await expectContractCallFailed( + token.transferFrom.sendTransactionAsync(from, to, unownedTokenId), + RevertReason.Erc721ZeroOwner, + ); + }); + it('should revert if transferring to a null address', async () => { + const from = owner; + const to = constants.NULL_ADDRESS; + await expectContractCallFailed( + token.transferFrom.sendTransactionAsync(from, to, tokenId), + RevertReason.Erc721ZeroToAddress, + ); + }); + it('should revert if the from address does not own the token', async () => { + const from = spender; + const to = erc721Receiver.address; + await expectContractCallFailed( + token.transferFrom.sendTransactionAsync(from, to, tokenId), + RevertReason.Erc721OwnerMismatch, + ); + }); + it('should revert if spender does not own the token, is not approved, and is not approved for all', async () => { + const from = owner; + const to = erc721Receiver.address; + await expectContractCallFailed( + token.transferFrom.sendTransactionAsync(from, to, tokenId, { from: spender }), + RevertReason.Erc721InvalidSpender, + ); + }); + it('should transfer the token if called by owner', async () => { + const from = owner; + const to = erc721Receiver.address; + const txReceipt = await logDecoder.getTxWithDecodedLogsAsync( + await token.transferFrom.sendTransactionAsync(from, to, tokenId), + ); + const newOwner = await token.ownerOf.callAsync(tokenId); + expect(newOwner).to.be.equal(to); + const log = txReceipt.logs[0] as LogWithDecodedArgs<DummyERC721TokenTransferEventArgs>; + expect(log.args._from).to.be.equal(from); + expect(log.args._to).to.be.equal(to); + expect(log.args._tokenId).to.be.bignumber.equal(tokenId); + }); + it('should transfer the token if spender is approved for all', async () => { + const isApproved = true; + await web3Wrapper.awaitTransactionSuccessAsync( + await token.setApprovalForAll.sendTransactionAsync(spender, isApproved), + constants.AWAIT_TRANSACTION_MINED_MS, + ); + + const from = owner; + const to = erc721Receiver.address; + const txReceipt = await logDecoder.getTxWithDecodedLogsAsync( + await token.transferFrom.sendTransactionAsync(from, to, tokenId), + ); + const newOwner = await token.ownerOf.callAsync(tokenId); + expect(newOwner).to.be.equal(to); + const log = txReceipt.logs[0] as LogWithDecodedArgs<DummyERC721TokenTransferEventArgs>; + expect(log.args._from).to.be.equal(from); + expect(log.args._to).to.be.equal(to); + expect(log.args._tokenId).to.be.bignumber.equal(tokenId); + }); + it('should transfer the token is spender is individually approved', async () => { + await web3Wrapper.awaitTransactionSuccessAsync( + await token.approve.sendTransactionAsync(spender, tokenId), + constants.AWAIT_TRANSACTION_MINED_MS, + ); + + const from = owner; + const to = erc721Receiver.address; + const txReceipt = await logDecoder.getTxWithDecodedLogsAsync( + await token.transferFrom.sendTransactionAsync(from, to, tokenId), + ); + const newOwner = await token.ownerOf.callAsync(tokenId); + expect(newOwner).to.be.equal(to); + + const approvedAddress = await token.getApproved.callAsync(tokenId); + expect(approvedAddress).to.be.equal(constants.NULL_ADDRESS); + const log = txReceipt.logs[0] as LogWithDecodedArgs<DummyERC721TokenTransferEventArgs>; + expect(log.args._from).to.be.equal(from); + expect(log.args._to).to.be.equal(to); + expect(log.args._tokenId).to.be.bignumber.equal(tokenId); + }); + }); + describe('safeTransferFrom without data', () => { + it('should transfer token to a non-contract address if called by owner', async () => { + const from = owner; + const to = spender; + const txReceipt = await logDecoder.getTxWithDecodedLogsAsync( + await token.safeTransferFrom1.sendTransactionAsync(from, to, tokenId), + ); + const newOwner = await token.ownerOf.callAsync(tokenId); + expect(newOwner).to.be.equal(to); + const log = txReceipt.logs[0] as LogWithDecodedArgs<DummyERC721TokenTransferEventArgs>; + expect(log.args._from).to.be.equal(from); + expect(log.args._to).to.be.equal(to); + expect(log.args._tokenId).to.be.bignumber.equal(tokenId); + }); + it('should revert if transferring to a contract address without onERC721Received', async () => { + const contract = await DummyERC721TokenContract.deployFrom0xArtifactAsync( + artifacts.DummyERC721Token, + provider, + txDefaults, + constants.DUMMY_TOKEN_NAME, + constants.DUMMY_TOKEN_SYMBOL, + ); + const from = owner; + const to = contract.address; + await expectContractCallFailedWithoutReasonAsync( + token.safeTransferFrom1.sendTransactionAsync(from, to, tokenId), + ); + }); + it('should revert if onERC721Received does not return the correct value', async () => { + const invalidErc721Receiver = await InvalidERC721ReceiverContract.deployFrom0xArtifactAsync( + artifacts.InvalidERC721Receiver, + provider, + txDefaults, + ); + const from = owner; + const to = invalidErc721Receiver.address; + await expectContractCallFailed( + token.safeTransferFrom1.sendTransactionAsync(from, to, tokenId), + RevertReason.Erc721InvalidSelector, + ); + }); + it('should transfer to contract and call onERC721Received with correct return value', async () => { + const from = owner; + const to = erc721Receiver.address; + const txReceipt = await logDecoder.getTxWithDecodedLogsAsync( + await token.safeTransferFrom1.sendTransactionAsync(from, to, tokenId), + ); + const newOwner = await token.ownerOf.callAsync(tokenId); + expect(newOwner).to.be.equal(to); + const transferLog = txReceipt.logs[0] as LogWithDecodedArgs<DummyERC721TokenTransferEventArgs>; + const receiverLog = txReceipt.logs[1] as LogWithDecodedArgs<InvalidERC721ReceiverTokenReceivedEventArgs>; + expect(transferLog.args._from).to.be.equal(from); + expect(transferLog.args._to).to.be.equal(to); + expect(transferLog.args._tokenId).to.be.bignumber.equal(tokenId); + expect(receiverLog.args.operator).to.be.equal(owner); + expect(receiverLog.args.from).to.be.equal(from); + expect(receiverLog.args.tokenId).to.be.bignumber.equal(tokenId); + expect(receiverLog.args.data).to.be.equal(constants.NULL_BYTES); + }); + }); + describe('safeTransferFrom with data', () => { + const data = '0x0102030405060708090a0b0c0d0e0f'; + it('should transfer token to a non-contract address if called by owner', async () => { + const from = owner; + const to = spender; + const txReceipt = await logDecoder.getTxWithDecodedLogsAsync( + await token.safeTransferFrom2.sendTransactionAsync(from, to, tokenId, data), + ); + const newOwner = await token.ownerOf.callAsync(tokenId); + expect(newOwner).to.be.equal(to); + const log = txReceipt.logs[0] as LogWithDecodedArgs<DummyERC721TokenTransferEventArgs>; + expect(log.args._from).to.be.equal(from); + expect(log.args._to).to.be.equal(to); + expect(log.args._tokenId).to.be.bignumber.equal(tokenId); + }); + it('should revert if transferring to a contract address without onERC721Received', async () => { + const contract = await DummyERC721TokenContract.deployFrom0xArtifactAsync( + artifacts.DummyERC721Token, + provider, + txDefaults, + constants.DUMMY_TOKEN_NAME, + constants.DUMMY_TOKEN_SYMBOL, + ); + const from = owner; + const to = contract.address; + await expectContractCallFailedWithoutReasonAsync( + token.safeTransferFrom2.sendTransactionAsync(from, to, tokenId, data), + ); + }); + it('should revert if onERC721Received does not return the correct value', async () => { + const invalidErc721Receiver = await InvalidERC721ReceiverContract.deployFrom0xArtifactAsync( + artifacts.InvalidERC721Receiver, + provider, + txDefaults, + ); + const from = owner; + const to = invalidErc721Receiver.address; + await expectContractCallFailed( + token.safeTransferFrom2.sendTransactionAsync(from, to, tokenId, data), + RevertReason.Erc721InvalidSelector, + ); + }); + it('should transfer to contract and call onERC721Received with correct return value', async () => { + const from = owner; + const to = erc721Receiver.address; + const txReceipt = await logDecoder.getTxWithDecodedLogsAsync( + await token.safeTransferFrom2.sendTransactionAsync(from, to, tokenId, data), + ); + const newOwner = await token.ownerOf.callAsync(tokenId); + expect(newOwner).to.be.equal(to); + const transferLog = txReceipt.logs[0] as LogWithDecodedArgs<DummyERC721TokenTransferEventArgs>; + const receiverLog = txReceipt.logs[1] as LogWithDecodedArgs<InvalidERC721ReceiverTokenReceivedEventArgs>; + expect(transferLog.args._from).to.be.equal(from); + expect(transferLog.args._to).to.be.equal(to); + expect(transferLog.args._tokenId).to.be.bignumber.equal(tokenId); + expect(receiverLog.args.operator).to.be.equal(owner); + expect(receiverLog.args.from).to.be.equal(from); + expect(receiverLog.args.tokenId).to.be.bignumber.equal(tokenId); + expect(receiverLog.args.data).to.be.equal(data); + }); + }); +}); diff --git a/packages/contracts/test/tokens/unlimited_allowance_token.ts b/packages/contracts/test/tokens/unlimited_allowance_token.ts index 81d931fc5..f2725b408 100644 --- a/packages/contracts/test/tokens/unlimited_allowance_token.ts +++ b/packages/contracts/test/tokens/unlimited_allowance_token.ts @@ -17,7 +17,7 @@ const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper); describe('UnlimitedAllowanceToken', () => { let owner: string; let spender: string; - const MAX_MINT_VALUE = new BigNumber(100000000000000000000); + const MAX_MINT_VALUE = new BigNumber(10000000000000000000000); let token: DummyERC20TokenContract; before(async () => { diff --git a/packages/contracts/test/utils/artifacts.ts b/packages/contracts/test/utils/artifacts.ts index 65f72015f..e86ad5406 100644 --- a/packages/contracts/test/utils/artifacts.ts +++ b/packages/contracts/test/utils/artifacts.ts @@ -11,6 +11,7 @@ import * as Exchange from '../../artifacts/Exchange.json'; import * as ExchangeWrapper from '../../artifacts/ExchangeWrapper.json'; import * as Forwarder from '../../artifacts/Forwarder.json'; import * as IAssetProxy from '../../artifacts/IAssetProxy.json'; +import * as InvalidERC721Receiver from '../../artifacts/InvalidERC721Receiver.json'; import * as MixinAuthorizable from '../../artifacts/MixinAuthorizable.json'; import * as MultiSigWallet from '../../artifacts/MultiSigWallet.json'; import * as MultiSigWalletWithTimeLock from '../../artifacts/MultiSigWalletWithTimeLock.json'; @@ -41,6 +42,7 @@ export const artifacts = { EtherToken: (EtherToken as any) as ContractArtifact, Forwarder: (Forwarder as any) as ContractArtifact, IAssetProxy: (IAssetProxy as any) as ContractArtifact, + InvalidERC721Receiver: (InvalidERC721Receiver as any) as ContractArtifact, MixinAuthorizable: (MixinAuthorizable as any) as ContractArtifact, MultiSigWallet: (MultiSigWallet as any) as ContractArtifact, MultiSigWalletWithTimeLock: (MultiSigWalletWithTimeLock as any) as ContractArtifact, diff --git a/packages/contracts/test/utils/log_decoder.ts b/packages/contracts/test/utils/log_decoder.ts index 5a4801319..31e8679be 100644 --- a/packages/contracts/test/utils/log_decoder.ts +++ b/packages/contracts/test/utils/log_decoder.ts @@ -48,7 +48,6 @@ export class LogDecoder { } public async getTxWithDecodedLogsAsync(txHash: string): Promise<TransactionReceiptWithDecodedLogs> { const tx = await this._web3Wrapper.awaitTransactionSuccessAsync(txHash, constants.AWAIT_TRANSACTION_MINED_MS); - tx.logs = _.filter(tx.logs, log => log.address === this._contractAddress); tx.logs = _.map(tx.logs, log => this.decodeLogOrThrow(log)); return tx; } |