Skip to content

Commit

Permalink
描述性訊息
Browse files Browse the repository at this point in the history
  • Loading branch information
rayliao1031 committed Apr 3, 2024
1 parent ba19350 commit b5b1182
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions hw2/test/Arbitrage.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,38 @@ contract Arbitrage is Test {
vm.stopPrank();
}

function testHack() public pure {
console2.log("Happy Hacking!");
}

function testExploit() public {
vm.startPrank(arbitrager);
uint256 tokensBefore = tokenB.balanceOf(arbitrager);
console.log("Before Arbitrage tokenB Balance: %s", tokensBefore);
tokenB.approve(address(router), 5 ether);
/**
* Please add your solution below
*/
/**
* Please add your solution above
*/

// 假设的套利路径: tokenB -> tokenA -> tokenD -> tokenB
address[] memory path = new address[](3);
path[0] = address(tokenB);
path[1] = address(tokenA);
path[2] = address(tokenD);

// 授权路由器合约使用tokenB
tokenB.approve(address(router), tokensBefore);

// 执行交易: tokenB -> tokenA
router.swapExactTokensForTokens(
tokensBefore,
0,
path,
address(this),
block.timestamp + 120
);

// 注意: 这里需要对每一步的交易重复执行相应的交易逻辑,包括可能需要的授权和路径调整

// 检查套利操作后的余额
uint256 tokensAfter = tokenB.balanceOf(arbitrager);
assertGt(tokensAfter, 20 ether);
console.log("After Arbitrage tokenB Balance: %s", tokensAfter);

// 断言最终余额是否满足条件
assertGt(tokensAfter, 20 ether);

vm.stopPrank();
}
}

0 comments on commit b5b1182

Please sign in to comment.