Skip to content

Commit 28fc105

Browse files
committed
🌯 wrapped ether tests added
1 parent 51e569a commit 28fc105

File tree

5 files changed

+239
-3
lines changed

5 files changed

+239
-3
lines changed

‎contracts/WrappedEther.sol

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// SPDX-License-Identifier: BCOM
2+
3+
pragma solidity ^0.8.8;
4+
5+
contract WrappedEther {
6+
7+
string public name = "Wrapped Ether";
8+
string public symbol = "WETH";
9+
uint8 public decimals = 18;
10+
11+
event Approval(
12+
address indexed src,
13+
address indexed guy,
14+
uint256 wad
15+
);
16+
17+
event Transfer(
18+
address indexed src,
19+
address indexed dst,
20+
uint256 wad
21+
);
22+
23+
event Deposit(
24+
address indexed dst,
25+
uint256 wad
26+
);
27+
28+
event Withdrawal(
29+
address indexed src,
30+
uint256 wad
31+
);
32+
33+
mapping (address => uint256) public balanceOf;
34+
mapping (address => mapping (address => uint256)) public allowance;
35+
36+
receive() external payable {
37+
deposit();
38+
}
39+
40+
function deposit()
41+
public
42+
payable
43+
{
44+
balanceOf[msg.sender] =
45+
balanceOf[msg.sender] + msg.value;
46+
47+
emit Deposit(
48+
msg.sender,
49+
msg.value
50+
);
51+
}
52+
53+
function withdraw(
54+
uint256 _wad
55+
)
56+
public
57+
{
58+
balanceOf[msg.sender] =
59+
balanceOf[msg.sender] - _wad;
60+
61+
payable(msg.sender).transfer(_wad);
62+
63+
emit Withdrawal(
64+
msg.sender,
65+
_wad
66+
);
67+
}
68+
69+
function totalSupply()
70+
public
71+
view
72+
returns (uint256)
73+
{
74+
return address(this).balance;
75+
}
76+
77+
function approve(
78+
address _guy,
79+
uint256 _wad
80+
)
81+
public
82+
returns (bool)
83+
{
84+
allowance[msg.sender][_guy] = _wad;
85+
emit Approval(msg.sender, _guy, _wad);
86+
return true;
87+
}
88+
89+
function transfer(
90+
address dst,
91+
uint wad
92+
)
93+
public
94+
returns (bool)
95+
{
96+
return transferFrom(
97+
msg.sender,
98+
dst,
99+
wad
100+
);
101+
}
102+
103+
function transferFrom(
104+
address src,
105+
address dst,
106+
uint256 wad
107+
)
108+
public
109+
returns (bool)
110+
{
111+
allowance[src][msg.sender] =
112+
allowance[src][msg.sender] - wad;
113+
114+
balanceOf[src] =
115+
balanceOf[src] - wad;
116+
117+
balanceOf[dst] =
118+
balanceOf[dst] - wad;
119+
120+
emit Transfer(
121+
src,
122+
dst,
123+
wad
124+
);
125+
126+
return true;
127+
}
128+
}

‎coverage.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"compile": "truffle compile",
2323
"test-token": "npx truffle test test/token.test.js",
2424
"test-swaps": "npx truffle test test/swaps.test.js",
25+
"test-wraps": "npx truffle test test/wraps.test.js",
2526
"test-coverage": "npx truffle run coverage --network development",
2627
"flat-file": "cd contracts && truffle-flattener fileName.sol > ../flats/fileName.sol",
2728
"docs": "oz-docs",

‎test/wraps.test.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
const Weth = artifacts.require("WrappedEther");
2+
const catchRevert = require("./exceptionsHelpers.js").catchRevert;
3+
require("./utils");
4+
const BN = web3.utils.BN;
5+
6+
const FOUR_ETH = web3.utils.toWei("4");
7+
const FIVE_ETH = web3.utils.toWei("5");
8+
const NINE_ETH = web3.utils.toWei("9");
9+
10+
const getLastEvent = async (eventName, instance) => {
11+
const events = await instance.getPastEvents(eventName, {
12+
fromBlock: 0,
13+
toBlock: "latest",
14+
});
15+
return events.pop().returnValues;
16+
};
17+
18+
contract("Weth", ([owner, alice, bob, random]) => {
19+
20+
let weth;
21+
22+
before(async () => {
23+
weth = await Weth.new();
24+
});
25+
26+
describe("Deposit functionality", () => {
27+
28+
it("should credit correct amount when deposited", async () => {
29+
30+
const amount = FOUR_ETH;
31+
32+
await weth.deposit({
33+
value: amount,
34+
from: alice
35+
});
36+
37+
balance = await weth.balanceOf(alice);
38+
39+
assert.equal(
40+
balance,
41+
amount
42+
);
43+
});
44+
45+
it("should emit correct deposit event", async () => {
46+
47+
const amount = FIVE_ETH;
48+
49+
await weth.deposit({
50+
value: amount,
51+
from: alice
52+
});
53+
54+
const { dst, wad } = await getLastEvent(
55+
"Deposit",
56+
weth
57+
);
58+
59+
assert.equal(
60+
alice,
61+
dst
62+
);
63+
64+
assert.equal(
65+
amount,
66+
wad
67+
);
68+
});
69+
70+
it("should allow to deposit through direct transfer", async () => {
71+
72+
const amount = NINE_ETH;
73+
74+
await weth.send(amount, {
75+
from: bob
76+
});
77+
78+
balance = await weth.balanceOf(bob);
79+
80+
assert.equal(
81+
balance,
82+
amount
83+
);
84+
});
85+
});
86+
87+
describe("Withdraw functionality", () => {
88+
89+
it("should give back correct amount", async () => {
90+
});
91+
92+
it("should emit withdraw event", async () => {
93+
});
94+
});
95+
96+
describe.skip("Transfer functionality", () => {
97+
98+
it("should allow to transfer WETH token between accounts", async () => {
99+
});
100+
101+
it("should be able to use approve/allowance", async () => {
102+
});
103+
104+
it("should be able to use transferFrom", async () => {
105+
});
106+
});
107+
});

‎weth/contracts/WETH9.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
pragma solidity ^0.4.18;
22

33
contract WETH9 {
4-
string public name = "Wrapped Ether";
5-
string public symbol = "WETH";
4+
string public name = "Wrapped Ether";
5+
string public symbol = "WETH";
66
uint8 public decimals = 18;
77

88
event Approval(address indexed src, address indexed guy, uint wad);

0 commit comments

Comments
 (0)