-
Notifications
You must be signed in to change notification settings - Fork 353
/
DumbContract.spec.ts
187 lines (147 loc) 路 7.18 KB
/
DumbContract.spec.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
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
import { DumbContract } from "./types/ethers-contracts/DumbContract";
import { DumbContractFactory } from "./types/ethers-contracts/DumbContractFactory";
import { BigNumber } from "ethers/utils";
import { expect } from "chai";
import { Event } from "ethers";
import { arrayify } from "ethers/utils/bytes";
import { getTestSigner } from "./ethers";
describe("DumbContract", () => {
function deployDumbContract(): Promise<DumbContract> {
const factory = new DumbContractFactory(getTestSigner());
return factory.deploy(0);
}
it("should work", async () => {
const contract = await deployDumbContract();
const res = await contract.functions.returnAll();
expect(res).to.be.deep.eq([new BigNumber("0"), new BigNumber("5")]);
});
it("should have an address", async () => {
const contract = await deployDumbContract();
expect(contract.address).to.be.string;
});
it("should allow passing a contructor argument in multiple ways", async () => {
const factory = new DumbContractFactory(getTestSigner());
const contract1 = await factory.deploy(42);
expect(await contract1.functions.counter()).to.be.deep.eq(new BigNumber("42"));
const contract2 = await factory.deploy("1234123412341234123");
expect(await contract2.functions.counter()).to.be.deep.eq(new BigNumber("1234123412341234123"));
const contract3 = await factory.deploy(new BigNumber("5678567856785678567"));
expect(await contract3.functions.counter()).to.be.deep.eq(new BigNumber("5678567856785678567"));
});
it("should allow to pass unsigned values in multiple ways", async () => {
const contract = await deployDumbContract();
await contract.functions.countup(2);
expect(await contract.functions.counter()).to.be.deep.eq(new BigNumber("2"));
await contract.functions.countup("2");
expect(await contract.functions.counter()).to.be.deep.eq(new BigNumber("4"));
await contract.functions.countup(new BigNumber(2));
expect(await contract.functions.counter()).to.be.deep.eq(new BigNumber("6"));
});
it("should allow to pass signed values in multiple ways", async () => {
const contract = await deployDumbContract();
expect(await contract.functions.returnSigned(2)).to.be.deep.eq(new BigNumber("2"));
expect(await contract.functions.returnSigned("2")).to.be.deep.eq(new BigNumber("2"));
expect(await contract.functions.returnSigned(new BigNumber(2))).to.be.deep.eq(
new BigNumber("2"),
);
});
it("should allow to pass address values", async () => {
const contract = await deployDumbContract();
expect(
await contract.functions.testAddress("0x0000000000000000000000000000000000000123"),
).to.be.eq("0x0000000000000000000000000000000000000123");
});
it("should allow to pass bytes values in multiple ways", async () => {
const contract = await deployDumbContract();
const bytes32Str = "0x0201030700000000000000000000000000000000000000000000000000004200";
await contract.functions.callWithBytes(bytes32Str);
expect(await contract.functions.byteArray()).to.eq(bytes32Str);
await contract.functions.callWithBytes(Buffer.from(bytes32Str.slice(2), "hex"));
expect(await contract.functions.byteArray()).to.eq(bytes32Str);
await contract.functions.callWithBytes(arrayify(bytes32Str));
expect(await contract.functions.byteArray()).to.eq(bytes32Str);
});
it("should allow to pass dynamic byte arrays in multiple ways", async () => {
const contract = await deployDumbContract();
const bytesStr = "0x02010307000000000000000000000000133700000000000000000000000000000000004200";
await contract.functions.callWithDynamicByteArray(bytesStr);
expect(await contract.functions.dynamicByteArray()).to.eq(bytesStr);
await contract.functions.callWithDynamicByteArray(Buffer.from(bytesStr.slice(2), "hex"));
expect(await contract.functions.dynamicByteArray()).to.eq(bytesStr);
await contract.functions.callWithDynamicByteArray(arrayify(bytesStr));
expect(await contract.functions.dynamicByteArray()).to.eq(bytesStr);
});
it("should allow to pass boolean values", async () => {
const contract = await deployDumbContract();
const res = await contract.functions.callWithBoolean(true);
expect(res).to.be.deep.eq(true);
});
it("should allow to pass numeric arrays values in multiple ways", async () => {
const contract = await deployDumbContract();
const res = await contract.functions.callWithArray2(["1", 2]);
expect(res).to.be.deep.eq([new BigNumber("1"), new BigNumber("2")]);
});
it("should allow to pass strings ", async () => {
const contract = await deployDumbContract();
expect(await contract.functions.testString("abc")).to.be.deep.eq("abc");
});
it("should allow to pass overrides", async () => {
const contract = await deployDumbContract();
const value = 1;
const gasPrice = 33;
const tx = await contract.functions.countupForEther({ value, gasPrice });
expect(tx.value).to.be.deep.eq(new BigNumber(value));
expect(tx.gasPrice).to.be.deep.eq(new BigNumber(gasPrice));
});
it("should return number for small ints", async () => {
const contract = await deployDumbContract();
expect(await contract.functions.returnSmallUint()).to.be.eq(18);
});
it("should .attach to another contract instance", async () => {
const contract1 = await deployDumbContract();
const contract2 = await deployDumbContract();
await contract1.functions.countup(2);
const reconnectedContract = contract2.attach(contract1.address);
expect(await reconnectedContract.functions.counter()).to.be.deep.eq(new BigNumber("2"));
});
it("should estimate tx gas cost", async () => {
const contract = await deployDumbContract();
expect((await contract.estimate.countup(2)).toNumber()).to.be.greaterThan(22000);
});
it("should encode a tx", async () => {
const contract = await deployDumbContract();
expect(await contract.interface.functions.countup.encode([2])).to.eq(
"0x7916df080000000000000000000000000000000000000000000000000000000000000002",
);
});
it("should encode event topics", async () => {
const contract = await deployDumbContract();
expect(
await contract.interface.events.Deposit.encodeTopics([
"0x0000000000000000000000000000000000000123",
null,
]),
).to.deep.eq([
"0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c",
"0x0000000000000000000000000000000000000000000000000000000000000123",
]);
});
it("should listen for an event", async function(this) {
this.timeout(10000); // the listener isn't called within the default 2000ms
const contract = await deployDumbContract();
const value = 42;
const signerAddress = await contract.signer.getAddress();
let eventListenedResolveFn: () => void;
contract.on("Deposit", (eventFrom: string, eventValue: BigNumber, event: Event) => {
expect(eventFrom).to.eq(signerAddress);
expect(eventValue.toNumber()).to.eq(value);
event.removeListener();
eventListenedResolveFn();
});
await contract.functions.countupForEther({ value });
// tslint:disable-next-line:promise-must-complete
await new Promise(resolve => {
eventListenedResolveFn = resolve;
});
});
});