generated from ZumZoom/solidity-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added some foundry tests for dst chain
- Loading branch information
Showing
11 changed files
with
322 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,7 @@ build | |
.coverage_artifacts | ||
.idea | ||
.env | ||
|
||
# foundry | ||
cache_forge | ||
out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "lib/forge-std"] | ||
path = lib/forge-std | ||
url = https://github.com/foundry-rs/forge-std |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[profile.default] | ||
src = 'contracts' | ||
out = 'out' | ||
libs = ['node_modules', 'lib'] | ||
test = 'test' | ||
cache_path = 'cache_forge' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
@1inch/=node_modules/@1inch/ | ||
@chainlink/=node_modules/@chainlink/ | ||
@eth-optimism/=node_modules/@eth-optimism/ | ||
@openzeppelin/=node_modules/@openzeppelin/ | ||
clones-with-immutable-args/=node_modules/clones-with-immutable-args/ | ||
ds-test/=lib/forge-std/lib/ds-test/src/ | ||
eth-gas-reporter/=node_modules/eth-gas-reporter/ | ||
forge-std/=lib/forge-std/src/ | ||
hardhat-deploy/=node_modules/hardhat-deploy/ | ||
hardhat/=node_modules/hardhat/ | ||
solidity-utils/=node_modules/@1inch/solidity-utils/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.23; | ||
|
||
import { Escrow, IEscrow } from "../../contracts/Escrow.sol"; | ||
import { IEscrowFactory } from "../../contracts/EscrowFactory.sol"; | ||
|
||
import { BaseSetup } from "../utils/BaseSetup.sol"; | ||
|
||
contract EscrowTest is BaseSetup { | ||
function setUp() public virtual override { | ||
BaseSetup.setUp(); | ||
} | ||
|
||
/* solhint-disable func-name-mixedcase */ | ||
|
||
function test_NoWithdrawalDuringFinalityLockDst() public { | ||
( | ||
IEscrowFactory.DstEscrowImmutablesCreation memory immutables, | ||
Escrow dstClone | ||
) = _prepareDataDst(SECRET, TAKING_AMOUNT); | ||
|
||
// deploy escrow | ||
vm.startPrank(bob); | ||
escrowFactory.createEscrow(immutables); | ||
|
||
// withdraw | ||
vm.expectRevert(IEscrow.InvalidWithdrawalTime.selector); | ||
dstClone.withdrawDst(SECRET); | ||
} | ||
|
||
function test_WithdrawByResolverDst() public { | ||
( | ||
IEscrowFactory.DstEscrowImmutablesCreation memory immutables, | ||
Escrow dstClone | ||
) = _prepareDataDst(SECRET, TAKING_AMOUNT); | ||
|
||
// deploy escrow | ||
vm.startPrank(bob); | ||
escrowFactory.createEscrow(immutables); | ||
|
||
uint256 balanceAlice = dai.balanceOf(alice); | ||
uint256 balanceBob = dai.balanceOf(bob); | ||
uint256 balanceEscrow = dai.balanceOf(address(dstClone)); | ||
|
||
// withdraw | ||
vm.warp(block.timestamp + dstTimelocks.finality + 10); | ||
dstClone.withdrawDst(SECRET); | ||
|
||
assertEq(dai.balanceOf(alice), balanceAlice + TAKING_AMOUNT); | ||
assertEq(dai.balanceOf(bob), balanceBob + SAFETY_DEPOSIT); | ||
assertEq(dai.balanceOf(address(dstClone)), balanceEscrow - (TAKING_AMOUNT + SAFETY_DEPOSIT)); | ||
} | ||
|
||
function test_NoWithdrawalWithWrongSecretDst() public { | ||
bytes32 wrongSecret = keccak256(abi.encodePacked("wrong secret")); | ||
|
||
( | ||
IEscrowFactory.DstEscrowImmutablesCreation memory immutables, | ||
Escrow dstClone | ||
) = _prepareDataDst(SECRET, TAKING_AMOUNT); | ||
|
||
// deploy escrow | ||
vm.startPrank(bob); | ||
escrowFactory.createEscrow(immutables); | ||
|
||
// withdraw | ||
vm.warp(block.timestamp + dstTimelocks.finality + 100); | ||
vm.expectRevert(IEscrow.InvalidSecret.selector); | ||
dstClone.withdrawDst(wrongSecret); | ||
} | ||
|
||
/* solhint-enable func-name-mixedcase */ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.23; | ||
|
||
import { Escrow, IEscrow } from "../../contracts/Escrow.sol"; | ||
import { IEscrowFactory } from "../../contracts/EscrowFactory.sol"; | ||
|
||
import { BaseSetup } from "../utils/BaseSetup.sol"; | ||
|
||
contract EscrowFactoryTest is BaseSetup { | ||
function setUp() public virtual override { | ||
BaseSetup.setUp(); | ||
} | ||
|
||
/* solhint-disable func-name-mixedcase */ | ||
|
||
function testFuzz_DeployCloneForTaker(bytes32 secret, uint256 amount) public { | ||
vm.assume(amount > 0.1 ether && amount < 1 ether); | ||
( | ||
IEscrowFactory.DstEscrowImmutablesCreation memory immutables, | ||
Escrow dstClone | ||
) = _prepareDataDst(secret, amount); | ||
uint256 balanceBob = dai.balanceOf(bob); | ||
uint256 balanceEscrow = dai.balanceOf(address(dstClone)); | ||
|
||
// deploy escrow | ||
vm.prank(bob); | ||
escrowFactory.createEscrow(immutables); | ||
|
||
assertEq(dai.balanceOf(bob), balanceBob - (amount + immutables.safetyDeposit)); | ||
assertEq(dai.balanceOf(address(dstClone)), balanceEscrow + amount + immutables.safetyDeposit); | ||
|
||
IEscrow.DstEscrowImmutables memory returnedImmutables = dstClone.dstEscrowImmutables(); | ||
assertEq(returnedImmutables.hashlock, uint256(keccak256(abi.encodePacked(secret)))); | ||
assertEq(returnedImmutables.amount, amount); | ||
} | ||
|
||
function test_NoUnsafeDeploymentForTaker() public { | ||
|
||
(IEscrowFactory.DstEscrowImmutablesCreation memory immutables,) = _prepareDataDst(SECRET, TAKING_AMOUNT); | ||
|
||
vm.warp(immutables.srcCancellationTimestamp + 1); | ||
|
||
// deploy escrow | ||
vm.prank(bob); | ||
vm.expectRevert(IEscrowFactory.InvalidCreationTime.selector); | ||
escrowFactory.createEscrow(immutables); | ||
} | ||
|
||
/* solhint-enable func-name-mixedcase */ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.23; | ||
|
||
import { Test } from "forge-std/Test.sol"; | ||
|
||
import { TokenCustomDecimalsMock } from "solidity-utils/contracts/mocks/TokenCustomDecimalsMock.sol"; | ||
import { TokenMock } from "solidity-utils/contracts/mocks/TokenMock.sol"; | ||
|
||
import { Escrow, IEscrow } from "../../contracts/Escrow.sol"; | ||
import { EscrowFactory, IEscrowFactory } from "../../contracts/EscrowFactory.sol"; | ||
|
||
import { Utils } from "./Utils.sol"; | ||
|
||
contract BaseSetup is Test { | ||
/* solhint-disable private-vars-leading-underscore */ | ||
bytes32 internal constant SECRET = keccak256(abi.encodePacked("secret")); | ||
uint256 internal constant TAKING_AMOUNT = 0.5 ether; | ||
uint256 internal constant SAFETY_DEPOSIT = 0.05 ether; | ||
|
||
Utils internal utils; | ||
address payable[] internal users; | ||
|
||
address internal alice; | ||
address internal bob; | ||
|
||
TokenMock internal dai; | ||
TokenCustomDecimalsMock internal usdc; | ||
|
||
address internal limitOrderProtocol; | ||
EscrowFactory internal escrowFactory; | ||
Escrow internal escrow; | ||
|
||
IEscrow.SrcTimelocks internal srcTimelocks = IEscrow.SrcTimelocks({ | ||
finality: 120, | ||
publicUnlock: 900 | ||
}); | ||
IEscrow.DstTimelocks internal dstTimelocks = IEscrow.DstTimelocks({ | ||
finality: 300, | ||
unlock: 240, | ||
publicUnlock: 360 | ||
}); | ||
/* solhint-enable private-vars-leading-underscore */ | ||
|
||
function setUp() public virtual { | ||
utils = new Utils(); | ||
users = utils.createUsers(2); | ||
|
||
alice = users[0]; | ||
vm.label(alice, "Alice"); | ||
bob = users[1]; | ||
vm.label(bob, "Bob"); | ||
|
||
_deployTokens(); | ||
dai.mint(bob, 1000 ether); | ||
usdc.mint(alice, 1000 ether); | ||
|
||
_deployContracts(); | ||
|
||
vm.prank(bob); | ||
dai.approve(address(escrowFactory), 1000 ether); | ||
vm.prank(alice); | ||
usdc.approve(address(escrowFactory), 1000 ether); | ||
} | ||
|
||
function _deployTokens() internal { | ||
dai = new TokenMock("DAI", "DAI"); | ||
vm.label(address(dai), "DAI"); | ||
usdc = new TokenCustomDecimalsMock("USDC", "USDC", 1000 ether, 6); | ||
vm.label(address(usdc), "USDC"); | ||
} | ||
|
||
function _deployContracts() internal { | ||
limitOrderProtocol = address(this); | ||
escrow = new Escrow(); | ||
vm.label(address(escrow), "Escrow"); | ||
escrowFactory = new EscrowFactory(address(escrow), limitOrderProtocol); | ||
vm.label(address(escrowFactory), "EscrowFactory"); | ||
} | ||
|
||
function _prepareDataDst(bytes32 secret, uint256 amount) internal view returns ( | ||
IEscrowFactory.DstEscrowImmutablesCreation memory, | ||
Escrow | ||
) { | ||
( | ||
IEscrowFactory.DstEscrowImmutablesCreation memory escrowImmutables, | ||
bytes memory data | ||
) = _buildDstEscrowImmutables(secret, amount); | ||
address msgSender = bob; | ||
uint256 deployedAt = block.timestamp; | ||
bytes32 salt = keccak256(abi.encodePacked(deployedAt, data, msgSender)); | ||
Escrow dstClone = Escrow(escrowFactory.addressOfEscrow(salt)); | ||
return (escrowImmutables, dstClone); | ||
} | ||
|
||
function _buildDstEscrowImmutables(bytes32 secret, uint256 amount) internal view returns( | ||
IEscrowFactory.DstEscrowImmutablesCreation memory immutables, | ||
bytes memory data | ||
) { | ||
uint256 hashlock = uint256(keccak256(abi.encodePacked(secret))); | ||
uint256 safetyDeposit = amount * 10 / 100; | ||
uint256 srcCancellationTimestamp = block.timestamp + srcTimelocks.finality + srcTimelocks.publicUnlock; | ||
immutables = IEscrowFactory.DstEscrowImmutablesCreation({ | ||
hashlock: hashlock, | ||
maker: alice, | ||
taker: bob, | ||
token: address(dai), | ||
amount: amount, | ||
safetyDeposit: safetyDeposit, | ||
timelocks: dstTimelocks, | ||
srcCancellationTimestamp: srcCancellationTimestamp | ||
}); | ||
data = abi.encode( | ||
hashlock, | ||
alice, | ||
bob, | ||
block.chainid, | ||
address(dai), | ||
amount, | ||
safetyDeposit, | ||
dstTimelocks.finality, | ||
dstTimelocks.unlock, | ||
dstTimelocks.publicUnlock | ||
); | ||
} | ||
} |
Oops, something went wrong.