aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contracts/test/tokens/erc721_token.ts
blob: 72407748f87f4a2791b9df989b0c0bcef582beb3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import { BlockchainLifecycle } from '@0x/dev-utils';
import { RevertReason } from '@0x/types';
import { BigNumber } from '@0x/utils';
import * as chai from 'chai';
import { LogWithDecodedArgs } from 'ethereum-types';

import {
    DummyERC721ReceiverContract,
    DummyERC721ReceiverTokenReceivedEventArgs,
} from '../../generated-wrappers/dummy_erc721_receiver';
import {
    DummyERC721TokenContract,
    DummyERC721TokenTransferEventArgs,
} from '../../generated-wrappers/dummy_erc721_token';
import { InvalidERC721ReceiverContract } from '../../generated-wrappers/invalid_erc721_receiver';
import { artifacts } from '../../src/artifacts';
import { expectTransactionFailedAsync, expectTransactionFailedWithoutReasonAsync } 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);
// tslint:disable:no-unnecessary-type-assertion
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);
        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 expectTransactionFailedAsync(
                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 expectTransactionFailedAsync(
                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 expectTransactionFailedAsync(
                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 expectTransactionFailedAsync(
                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 if 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 expectTransactionFailedWithoutReasonAsync(
                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 expectTransactionFailedAsync(
                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<DummyERC721ReceiverTokenReceivedEventArgs>;
            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 expectTransactionFailedWithoutReasonAsync(
                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 expectTransactionFailedAsync(
                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<DummyERC721ReceiverTokenReceivedEventArgs>;
            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);
        });
    });
});
// tslint:enable:no-unnecessary-type-assertion