Skip to content

Commit

Permalink
fix a bug in Bank.sol
Browse files Browse the repository at this point in the history
  • Loading branch information
XieJunhua committed May 12, 2024
1 parent 7a63bdc commit 675515b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
12 changes: 7 additions & 5 deletions src/Week4/Bank.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,25 @@ contract Bank {
} else {
Node memory n = nodes[head];

if (n.value < msg.value) {
nodes[msg.sender] = Node(head, msg.value, msg.sender);
uint256 amountValue = nodes[msg.sender].value + msg.value;

if (n.value < amountValue) {
nodes[msg.sender] = Node(head, amountValue, msg.sender);
head = msg.sender;
return;
}

while (n.next != address(0)) {
if (msg.value > nodes[n.next].value) {
nodes[msg.sender] = Node(n.next, msg.value, msg.sender);
if (amountValue > nodes[n.next].value) {
nodes[msg.sender] = Node(n.next, amountValue, msg.sender);
nodes[n.depositer].next = msg.sender;
return;
}
n = nodes[n.next];
}

if (n.next == address(0)) {
nodes[msg.sender] = Node(address(0), msg.value, msg.sender);
nodes[msg.sender] = Node(address(0), amountValue, msg.sender);
nodes[n.depositer].next = msg.sender;
return;
}
Expand Down
5 changes: 3 additions & 2 deletions test/Bank.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ contract BankTest is Test {
_deposit(kate, 111);
// _deposit(linda, 12);
// _deposit(minnie, 13);
_deposit(nancy, 14);
_deposit(nancy, 13);
_deposit(dave, 10);

bank.nodes(bank.head());
// assertEq(deposit, charlie);
Expand All @@ -52,7 +53,7 @@ contract BankTest is Test {

(address next1, uint256 value1, address deposit1) = bank.nodes(jessie);
// assertEq(deposit, 15);
assertEq(next1, nancy);
assertEq(next1, dave);
assertEq(value1, 15);
}

Expand Down

0 comments on commit 675515b

Please sign in to comment.