Skip to content

Commit b93c176

Browse files
add unit test for ChiDiscount (#2170)
1 parent d3b73da commit b93c176

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright 2017 Loopring Technology Limited.
3+
pragma solidity ^0.7.0;
4+
pragma experimental ABIEncoderV2;
5+
6+
import "../../aux/gas/ChiDiscount.sol";
7+
8+
contract DummyWriteContract is ChiDiscount {
9+
mapping (address => uint) data;
10+
address public chiToken;
11+
12+
constructor(address _chiToken) {
13+
chiToken = _chiToken;
14+
}
15+
16+
function write(ChiConfig calldata config, uint offset)
17+
external
18+
discountCHI(chiToken, config)
19+
{
20+
for (uint i = 0; i < 20; i++) {
21+
data[address(i + offset)] = i + offset;
22+
}
23+
}
24+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { expectThrow } from "./expectThrow";
2+
3+
const ChiToken = artifacts.require("ChiToken");
4+
const DummyWriteContract = artifacts.require("DummyWriteContract");
5+
6+
contract("ChiDiscount", (accounts: string[]) => {
7+
const owner1 = accounts[0];
8+
const owner2 = accounts[1];
9+
const owner3 = accounts[2];
10+
const emptyAddr = "0x0000000000000000000000000000000000000000";
11+
12+
let chiToken: any;
13+
let dummyWriteContract: any;
14+
15+
beforeEach(async () => {
16+
chiToken = await ChiToken.new();
17+
dummyWriteContract = await DummyWriteContract.new(chiToken.address);
18+
});
19+
20+
describe("ChiDiscount", () => {
21+
it("should be able to refund gas", async () => {
22+
// mint chiToken:
23+
const tx0 = await chiToken.mint(100, { gas: 6700000 });
24+
const gasAmount0 = tx0.receipt.gasUsed;
25+
console.log("mint 100 chiToken, gasUsed:", gasAmount0);
26+
27+
const balance1 = await chiToken.balanceOf(accounts[0]);
28+
assert.equal(100, balance1.toNumber(), "unexpected balance");
29+
30+
let offset = 0;
31+
// write without chiToken burning:
32+
const tx1 = await dummyWriteContract.write(
33+
{
34+
gasTokenVault: "0x" + "00".repeat(20),
35+
maxToBurn: 100,
36+
expectedGasRefund: 0,
37+
calldataCost: 0
38+
},
39+
offset
40+
);
41+
console.log("gas used without chiToken:", tx1.receipt.gasUsed);
42+
43+
offset += 100;
44+
// transfer chiToken to writeContract:
45+
await chiToken.transfer(dummyWriteContract.address, 100);
46+
// write with chiToken burning:
47+
const tx2 = await dummyWriteContract.write(
48+
{
49+
gasTokenVault: "0x" + "00".repeat(20),
50+
maxToBurn: 100,
51+
expectedGasRefund: 0,
52+
calldataCost: 0
53+
},
54+
offset
55+
);
56+
console.log("gas used with chiToken:", tx2.receipt.gasUsed);
57+
const chiTokenBalanceAfter = await chiToken.balanceOf(
58+
dummyWriteContract.address
59+
);
60+
const chiTokenUsed = 100 - chiTokenBalanceAfter.toNumber();
61+
console.log(
62+
"chiTokenUsed:",
63+
chiTokenUsed,
64+
" minted gas amount:",
65+
(gasAmount0 * chiTokenUsed) / 100
66+
);
67+
console.log("gas saved:", tx1.receipt.gasUsed - tx2.receipt.gasUsed);
68+
69+
offset += 100;
70+
// freeFrom test:
71+
await chiToken.mint(100, { gas: 6700000 });
72+
await chiToken.approve(dummyWriteContract.address, 1000);
73+
const chiTokenBalanceBefore3 = (await chiToken.balanceOf(
74+
accounts[0]
75+
)).toNumber();
76+
const tx3 = await dummyWriteContract.write(
77+
{
78+
gasTokenVault: accounts[0],
79+
maxToBurn: 100,
80+
expectedGasRefund: 0,
81+
calldataCost: 0
82+
},
83+
offset
84+
);
85+
console.log("--- freeFrom test ---");
86+
console.log("gas used with chiToken (freeFrom):", tx3.receipt.gasUsed);
87+
const chiTokenBalanceAfter3 = (await chiToken.balanceOf(
88+
accounts[0]
89+
)).toNumber();
90+
const chiTokenUsed3 = chiTokenBalanceBefore3 - chiTokenBalanceAfter3;
91+
console.log(
92+
"chiTokenUsed:",
93+
chiTokenUsed3,
94+
" minted gas amount:",
95+
(gasAmount0 * chiTokenUsed3) / 100
96+
);
97+
console.log("gas saved:", tx1.receipt.gasUsed - tx3.receipt.gasUsed);
98+
99+
assert(true);
100+
});
101+
});
102+
});

0 commit comments

Comments
 (0)