-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathevm-provider.e2e.test.ts
123 lines (100 loc) · 4.12 KB
/
evm-provider.e2e.test.ts
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
/*
Copyright (c) 2019 xf00f
This file is part of web3x and is released under the MIT License.
https://opensource.org/licenses/MIT
*/
import levelup from 'levelup';
import memdown from 'memdown';
import { Eth } from 'web3x/eth';
import { toWei, utf8ToHex } from 'web3x/utils';
import { Wallet } from 'web3x/wallet';
import { EvmProvider } from './evm-provider';
import { DaiContract } from './fixtures/DaiContract';
describe('evm provider e2e tests', () => {
const wallet = new Wallet(10);
const account1 = wallet.get(0)!.address;
const account2 = wallet.get(1)!.address;
const gasPrice = 50000;
let provider!: EvmProvider;
let eth!: Eth;
let daiContract!: DaiContract;
beforeEach(async () => {
provider = await EvmProvider.fromDb(levelup(memdown()));
await provider.loadWallet(wallet);
eth = new Eth(provider);
daiContract = new DaiContract(eth, undefined, { gasPrice });
eth.defaultFromAddress = account1;
});
it('should execute contract code', async () => {
const deployReceipt = await daiContract
.deploy(utf8ToHex('xf00f'))
.send()
.getReceipt();
expect(deployReceipt.events!.LogSetOwner[0].returnValues.owner).toEqual(account1);
// Mint some DAI into account1.
await daiContract.methods
.mint(toWei('1000', 'ether'))
.send()
.getReceipt();
expect(await daiContract.methods.balanceOf(account1).call()).toBe(toWei('1000', 'ether'));
expect(await daiContract.methods.allowance(account1, account2).call()).toBe(toWei('0', 'ether'));
// Approve account2 to transfer the minted funds to itself.
await daiContract.methods
.approve(account2, toWei('1000', 'ether'))
.send()
.getReceipt();
expect(await daiContract.methods.allowance(account1, account2).call()).toBe(toWei('1000', 'ether'));
// Transfer 600 to account2.
await daiContract.methods
.transferFrom(account1, account2, toWei('600', 'ether'))
.send({ from: account2 })
.getReceipt();
expect(await daiContract.methods.allowance(account1, account2).call()).toBe(toWei('400', 'ether'));
expect(await daiContract.methods.balanceOf(account1).call()).toBe(toWei('400', 'ether'));
expect(await daiContract.methods.balanceOf(account2).call()).toBe(toWei('600', 'ether'));
// Transfer 400 to account2.
const transferReceipt = await daiContract.methods
.transferFrom(account1, account2, toWei('400', 'ether'))
.send({ from: account2 })
.getReceipt();
const transferEvent = transferReceipt.events!.Transfer[0].returnValues;
expect(transferEvent.src).toEqual(account1);
expect(transferEvent.dst).toEqual(account2);
expect(transferEvent.wad).toEqual(toWei('400', 'ether'));
expect(await daiContract.methods.allowance(account1, account2).call()).toBe(toWei('0', 'ether'));
expect(await daiContract.methods.balanceOf(account1).call()).toBe('0');
expect(await daiContract.methods.balanceOf(account2).call()).toBe(toWei('1000', 'ether'));
const logs = await daiContract.getPastEvents('Transfer', { fromBlock: 0 });
expect(logs).toHaveLength(2);
expect(logs[0].returnValues.src).toEqual(account1);
expect(logs[0].returnValues.dst).toEqual(account2);
expect(logs[0].returnValues.wad).toEqual(toWei('600', 'ether'));
expect(logs[1].returnValues.src).toEqual(account1);
expect(logs[1].returnValues.dst).toEqual(account2);
expect(logs[1].returnValues.wad).toEqual(toWei('400', 'ether'));
}, 10000);
it('should receive Transfer event', async done => {
await daiContract
.deploy(utf8ToHex('xf00f'))
.send()
.getReceipt();
await daiContract.methods
.mint(toWei('1000', 'ether'))
.send()
.getReceipt();
const sub = daiContract.events.Transfer({}, (err, log) => {
if (err) {
done(err);
}
expect(log.returnValues.src).toEqual(account1);
expect(log.returnValues.dst).toEqual(account2);
expect(log.returnValues.wad).toEqual(toWei('600', 'ether'));
sub.unsubscribe();
done();
});
await daiContract.methods
.transfer(account2, toWei('600', 'ether'))
.send()
.getReceipt();
});
});