aboutsummaryrefslogtreecommitdiffstats
path: root/packages/metacoin/test/metacoin_test.ts
blob: 78de57a2ae10ea33b3efe964487eb42f6246183d (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
import { BlockchainLifecycle, devConstants } from '@0xproject/dev-utils';
import { ContractArtifact } from '@0xproject/sol-compiler';
import { BigNumber } from '@0xproject/utils';
import * as chai from 'chai';
import { LogWithDecodedArgs } from 'ethereum-types';
import 'make-promises-safe';

import * as MetacoinArtifact from '../artifacts/Metacoin.json';
import { MetacoinContract, TransferContractEventArgs } from '../src/contract_wrappers/metacoin';

import { chaiSetup } from './utils/chai_setup';
import { config } from './utils/config';
// Comment out the next line enable profiling
// import { profiler } from './utils/profiler';
import { provider, web3Wrapper } from './utils/web3_wrapper';

const artifact: ContractArtifact = MetacoinArtifact as any;

chaiSetup.configure();
const { expect } = chai;
const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
// tslint:disable:no-unnecessary-type-assertion
describe('Metacoin', () => {
    let metacoin: MetacoinContract;
    const ownerAddress = devConstants.TESTRPC_FIRST_ADDRESS;
    const INITIAL_BALANCE = new BigNumber(10000);
    before(async () => {
        metacoin = await MetacoinContract.deployFrom0xArtifactAsync(artifact, provider, config.txDefaults);
        web3Wrapper.abiDecoder.addABI(metacoin.abi);
    });
    beforeEach(async () => {
        await blockchainLifecycle.startAsync();
    });
    afterEach(async () => {
        await blockchainLifecycle.revertAsync();
    });
    describe('#constructor', () => {
        it(`should initialy give ${INITIAL_BALANCE} tokens to the creator`, async () => {
            const balance = await metacoin.balances.callAsync(ownerAddress);
            expect(balance).to.be.bignumber.equal(INITIAL_BALANCE);
        });
    });
    describe('#transfer', () => {
        it(`should successfully transfer tokens (via transfer1)`, async () => {
            const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
            const amount = INITIAL_BALANCE.div(2);
            const oldBalance = await metacoin.balances.callAsync(ZERO_ADDRESS);
            expect(oldBalance).to.be.bignumber.equal(0);
            // profiler.start();
            const txHash = await metacoin.transfer1.sendTransactionAsync(
                {
                    to: ZERO_ADDRESS,
                    amount,
                },
                { from: devConstants.TESTRPC_FIRST_ADDRESS },
            );
            // profiler.stop();
            const txReceipt = await web3Wrapper.awaitTransactionSuccessAsync(txHash);
            const transferLogs = txReceipt.logs[0] as LogWithDecodedArgs<TransferContractEventArgs>;
            expect(transferLogs.args).to.be.deep.equal({
                _to: ZERO_ADDRESS,
                _from: devConstants.TESTRPC_FIRST_ADDRESS,
                _value: amount,
            });
            const newBalance = await metacoin.balances.callAsync(ZERO_ADDRESS);
            expect(newBalance).to.be.bignumber.equal(amount);
        });

        it(`should successfully transfer tokens (via transfer2)`, async () => {
            const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
            const amount = INITIAL_BALANCE.div(2);
            const oldBalance = await metacoin.balances.callAsync(ZERO_ADDRESS);
            expect(oldBalance).to.be.bignumber.equal(0);
            const callback = 59;
            const txHash = await metacoin.transfer2.sendTransactionAsync(
                {
                    to: ZERO_ADDRESS,
                    amount,
                },
                callback,
                { from: devConstants.TESTRPC_FIRST_ADDRESS },
            );
            const txReceipt = await web3Wrapper.awaitTransactionSuccessAsync(txHash);
            const transferLogs = txReceipt.logs[0] as LogWithDecodedArgs<TransferContractEventArgs>;
            expect(transferLogs.args).to.be.deep.equal({
                _to: ZERO_ADDRESS,
                _from: devConstants.TESTRPC_FIRST_ADDRESS,
                _value: amount,
            });
            const newBalance = await metacoin.balances.callAsync(ZERO_ADDRESS);
            expect(newBalance).to.be.bignumber.equal(amount);
        });

        it(`should successfully transfer tokens (via transfer3)`, async () => {
            const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
            const amount = INITIAL_BALANCE.div(2);
            const oldBalance = await metacoin.balances.callAsync(ZERO_ADDRESS);
            expect(oldBalance).to.be.bignumber.equal(0);
            const callback = 59;
            const txHash = await metacoin.transfer3.sendTransactionAsync(
                {
                    transferData: {
                        to: ZERO_ADDRESS,
                        amount,
                    },
                    callback,
                },
                { from: devConstants.TESTRPC_FIRST_ADDRESS },
            );
            const txReceipt = await web3Wrapper.awaitTransactionSuccessAsync(txHash);
            const transferLogs = txReceipt.logs[0] as LogWithDecodedArgs<TransferContractEventArgs>;
            expect(transferLogs.args).to.be.deep.equal({
                _to: ZERO_ADDRESS,
                _from: devConstants.TESTRPC_FIRST_ADDRESS,
                _value: amount,
            });
            const newBalance = await metacoin.balances.callAsync(ZERO_ADDRESS);
            expect(newBalance).to.be.bignumber.equal(amount);
        });
    });
});
// tslint:enable:no-unnecessary-type-assertion