From 832ce3767c6dcc9f746012a4b5e9e197cc9ec0c3 Mon Sep 17 00:00:00 2001 From: raulk Date: Tue, 2 Apr 2024 21:25:37 +0100 Subject: [PATCH] extras/linked-token: add prettier + make fmt. (#855) --- .gitignore | 1 + extras/linked-token/.prettierignore | 12 ++ extras/linked-token/.prettierrc | 22 +++ extras/linked-token/Makefile | 8 +- extras/linked-token/package-lock.json | 97 ++++++++++++ extras/linked-token/package.json | 7 + extras/linked-token/script/ConfigManager.sol | 7 +- .../script/DeployIpcTokenController.s.sol | 8 +- .../script/DeployIpcTokenReplica.s.sol | 3 +- .../linked-token/script/DeployUSDCTest.s.sol | 4 +- extras/linked-token/src/LinkedToken.sol | 16 +- .../src/LinkedTokenController.sol | 15 +- .../linked-token/src/LinkedTokenReplica.sol | 14 +- .../test/IpcTokenController.t.sol | 147 ++++++------------ .../linked-token/test/IpcTokenReplica.t.sol | 125 +++++---------- .../linked-token/test/MultiSubnetTest.t.sol | 114 ++++---------- 16 files changed, 278 insertions(+), 322 deletions(-) create mode 100644 extras/linked-token/.prettierignore create mode 100644 extras/linked-token/.prettierrc create mode 100644 extras/linked-token/package-lock.json create mode 100644 extras/linked-token/package.json diff --git a/.gitignore b/.gitignore index 15aedc70d..9de367396 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ target/ .idea +node_modules/ \ No newline at end of file diff --git a/extras/linked-token/.prettierignore b/extras/linked-token/.prettierignore new file mode 100644 index 000000000..6ceb6222f --- /dev/null +++ b/extras/linked-token/.prettierignore @@ -0,0 +1,12 @@ +# Ignore everything in the lib folder +lib/ +package.json +package-lock.json +out/ +.out/ +artifacts/ +cache/ +cache_hardhat/ +typechain/ +.storage-layouts/ +node_modules/ \ No newline at end of file diff --git a/extras/linked-token/.prettierrc b/extras/linked-token/.prettierrc new file mode 100644 index 000000000..dc15aa325 --- /dev/null +++ b/extras/linked-token/.prettierrc @@ -0,0 +1,22 @@ +{ + "plugins": ["prettier-plugin-solidity"], + semi: false, + singleQuote: true, + printWidth: 80, + endOfLine: 'auto', + tabWidth: 4, + trailingComma: 'all', + overrides: [ + { + files: '*.sol', + options: { + parser: 'solidity-parse', + printWidth: 120, + tabWidth: 4, + useTabs: false, + singleQuote: false, + bracketSpacing: false, + }, + }, + ], +} \ No newline at end of file diff --git a/extras/linked-token/Makefile b/extras/linked-token/Makefile index 7a8147dd8..b02a6fddc 100644 --- a/extras/linked-token/Makefile +++ b/extras/linked-token/Makefile @@ -1,10 +1,10 @@ # Makefile for USDC Token Bridge on Calibration Network # Include environment variables from .env file -include .env +-include .env export -.PHONY: help deploy-usdctest mint-usdc check-balance deploy-replica deploy-controller initialize-replica initialize-controller deposit-usdc withdraw-usdc check-replica-balance +.PHONY: help fmt deploy-usdctest mint-usdc check-balance deploy-replica deploy-controller initialize-replica initialize-controller deposit-usdc withdraw-usdc check-replica-balance help: @echo "Available targets:" @@ -25,6 +25,10 @@ help: @echo " make - Run a specific target." @echo " make help - Display this help message." +fmt: + npm install --silent --no-save + npx prettier --check -w 'src/**/**/*.sol' 'script/**/**/*.sol' 'test/**/**/*.sol' 'test/**/**/*.t.sol' '**/*.{js,jsx,ts,tsx,json,css}' + install: @echo "Installing dependencies..." forge install diff --git a/extras/linked-token/package-lock.json b/extras/linked-token/package-lock.json new file mode 100644 index 000000000..7bf1e105e --- /dev/null +++ b/extras/linked-token/package-lock.json @@ -0,0 +1,97 @@ +{ + "name": "linked-token", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "devDependencies": { + "@solidity-parser/parser": "^0.18.0", + "prettier": "^3.2.5", + "prettier-plugin-solidity": "^1.3.1" + } + }, + "node_modules/@solidity-parser/parser": { + "version": "0.18.0", + "resolved": "https://registry.npmjs.org/@solidity-parser/parser/-/parser-0.18.0.tgz", + "integrity": "sha512-yfORGUIPgLck41qyN7nbwJRAx17/jAIXCTanHOJZhB6PJ1iAk/84b/xlsVKFSyNyLXIj0dhppoE0+CRws7wlzA==", + "dev": true + }, + "node_modules/lru-cache": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", + "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", + "dev": true, + "dependencies": { + "yallist": "^4.0.0" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/prettier": { + "version": "3.2.5", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.2.5.tgz", + "integrity": "sha512-3/GWa9aOC0YeD7LUfvOG2NiDyhOWRvt1k+rcKhOuYnMY24iiCphgneUfJDyFXd6rZCAnuLBv6UeAULtrhT/F4A==", + "dev": true, + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, + "node_modules/prettier-plugin-solidity": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/prettier-plugin-solidity/-/prettier-plugin-solidity-1.3.1.tgz", + "integrity": "sha512-MN4OP5I2gHAzHZG1wcuJl0FsLS3c4Cc5494bbg+6oQWBPuEamjwDvmGfFMZ6NFzsh3Efd9UUxeT7ImgjNH4ozA==", + "dev": true, + "dependencies": { + "@solidity-parser/parser": "^0.17.0", + "semver": "^7.5.4", + "solidity-comments-extractor": "^0.0.8" + }, + "engines": { + "node": ">=16" + }, + "peerDependencies": { + "prettier": ">=2.3.0" + } + }, + "node_modules/prettier-plugin-solidity/node_modules/@solidity-parser/parser": { + "version": "0.17.0", + "resolved": "https://registry.npmjs.org/@solidity-parser/parser/-/parser-0.17.0.tgz", + "integrity": "sha512-Nko8R0/kUo391jsEHHxrGM07QFdnPGvlmox4rmH0kNiNAashItAilhy4Mv4pK5gQmW5f4sXAF58fwJbmlkGcVw==", + "dev": true + }, + "node_modules/semver": { + "version": "7.6.0", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.0.tgz", + "integrity": "sha512-EnwXhrlwXMk9gKu5/flx5sv/an57AkRplG3hTK68W7FRDN+k+OWBj65M7719OkA82XLBxrcX0KSHj+X5COhOVg==", + "dev": true, + "dependencies": { + "lru-cache": "^6.0.0" + }, + "bin": { + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/solidity-comments-extractor": { + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/solidity-comments-extractor/-/solidity-comments-extractor-0.0.8.tgz", + "integrity": "sha512-htM7Vn6LhHreR+EglVMd2s+sZhcXAirB1Zlyrv5zBuTxieCvjfnRpd7iZk75m/u6NOlEyQ94C6TWbBn2cY7w8g==", + "dev": true + }, + "node_modules/yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", + "dev": true + } + } +} diff --git a/extras/linked-token/package.json b/extras/linked-token/package.json new file mode 100644 index 000000000..de072809a --- /dev/null +++ b/extras/linked-token/package.json @@ -0,0 +1,7 @@ +{ + "devDependencies": { + "@solidity-parser/parser": "^0.18.0", + "prettier": "^3.2.5", + "prettier-plugin-solidity": "^1.3.1" + } +} diff --git a/extras/linked-token/script/ConfigManager.sol b/extras/linked-token/script/ConfigManager.sol index b7565a0ab..0bc804403 100644 --- a/extras/linked-token/script/ConfigManager.sol +++ b/extras/linked-token/script/ConfigManager.sol @@ -5,7 +5,6 @@ pragma solidity 0.8.23; import {Script} from "forge-std/Script.sol"; contract ConfigManager is Script { - string private configPath = "config.json"; // Path to your JSON config file // Reads a value from the JSON config @@ -23,7 +22,6 @@ contract ConfigManager is Script { value = vm.parseJsonAddress(json, key); } - // Writes a value to the JSON config function writeConfig(string memory key, string memory value) internal { // First, check if the file exists and read its contents @@ -33,11 +31,11 @@ contract ConfigManager is Script { jsonData = vm.readFile(path); } else { // If the file doesn't exist, initialize an empty JSON object - jsonData = "{\"LinkedToken\":{\"USDCTest\":{}, \"LinkedTokenReplica\":{}, \"LinkedTokenController\":{}}}"; + jsonData = '{"LinkedToken":{"USDCTest":{}, "LinkedTokenReplica":{}, "LinkedTokenController":{}}}'; vm.writeJson(jsonData, path); } - vm.writeJson( value, path, string.concat(".LinkedToken.", key)); + vm.writeJson(value, path, string.concat(".LinkedToken.", key)); } // Example usage within a script @@ -51,4 +49,3 @@ contract ConfigManager is Script { bytes memory retrievedValue = readConfig(exampleKey); } } - diff --git a/extras/linked-token/script/DeployIpcTokenController.s.sol b/extras/linked-token/script/DeployIpcTokenController.s.sol index b92b5774a..6af34f2f8 100644 --- a/extras/linked-token/script/DeployIpcTokenController.s.sol +++ b/extras/linked-token/script/DeployIpcTokenController.s.sol @@ -6,7 +6,12 @@ import "../src/LinkedTokenController.sol"; import "@ipc/src/structs/Subnet.sol"; contract DeployIpcTokenController is ConfigManager { - function run(address gateway, address tokenContractAddress, uint64 _rootNetChainId, address[] memory _route) external { + function run( + address gateway, + address tokenContractAddress, + uint64 _rootNetChainId, + address[] memory _route + ) external { // Example for setting up the SubnetID, adjust according to your actual setup SubnetID memory destinationSubnet = SubnetID({root: _rootNetChainId, route: _route}); @@ -18,4 +23,3 @@ contract DeployIpcTokenController is ConfigManager { writeConfig("LinkedTokenController", vm.toString(address(controller))); } } - diff --git a/extras/linked-token/script/DeployIpcTokenReplica.s.sol b/extras/linked-token/script/DeployIpcTokenReplica.s.sol index f93fb1128..77bbcd1e3 100644 --- a/extras/linked-token/script/DeployIpcTokenReplica.s.sol +++ b/extras/linked-token/script/DeployIpcTokenReplica.s.sol @@ -7,7 +7,7 @@ import "@ipc/src/structs/Subnet.sol"; contract DeployIpcTokenReplica is ConfigManager { function run(address gateway, address USDCTest, uint64 _rootNetChainId, address[] memory _route) external { - SubnetID memory controllerSubnet = SubnetID({root: _rootNetChainId , route: _route}); + SubnetID memory controllerSubnet = SubnetID({root: _rootNetChainId, route: _route}); vm.startBroadcast(); LinkedTokenReplica replica = new LinkedTokenReplica(gateway, USDCTest, controllerSubnet); @@ -16,4 +16,3 @@ contract DeployIpcTokenReplica is ConfigManager { writeConfig("LinkedTokenReplica", vm.toString(address(replica))); } } - diff --git a/extras/linked-token/script/DeployUSDCTest.s.sol b/extras/linked-token/script/DeployUSDCTest.s.sol index 32dfd3f46..ad1b6be6c 100644 --- a/extras/linked-token/script/DeployUSDCTest.s.sol +++ b/extras/linked-token/script/DeployUSDCTest.s.sol @@ -4,9 +4,8 @@ pragma solidity 0.8.23; import "../src/USDCTest.sol"; import "./ConfigManager.sol"; - contract DeployUSDCTest is ConfigManager { - function run() external override{ + function run() external override { vm.startBroadcast(); USDCTest erc20Token = new USDCTest(); @@ -14,6 +13,5 @@ contract DeployUSDCTest is ConfigManager { vm.stopBroadcast(); writeConfig("USDCTest", vm.toString(address(erc20Token))); - } } diff --git a/extras/linked-token/src/LinkedToken.sol b/extras/linked-token/src/LinkedToken.sol index 5c2311f64..1b46c60c1 100644 --- a/extras/linked-token/src/LinkedToken.sol +++ b/extras/linked-token/src/LinkedToken.sol @@ -13,7 +13,6 @@ import {SubnetIDHelper} from "@ipc/src/lib/SubnetIDHelper.sol"; error InvalidOriginContract(); error InvalidOriginSubnet(); - /** * @title LinkedToken * @notice Contract to handle token transfer from L1, lock them and mint on L2. @@ -64,11 +63,7 @@ abstract contract LinkedToken is IpcExchange { * @param underlyingToken Address of the destination contract for minting * @param linkedSubnet SubnetID of the destination network */ - constructor( - address gateway, - address underlyingToken, - SubnetID memory linkedSubnet - ) IpcExchange(gateway) { + constructor(address gateway, address underlyingToken, SubnetID memory linkedSubnet) IpcExchange(gateway) { _underlying = IERC20(underlyingToken); _linkedSubnet = linkedSubnet; } @@ -77,7 +72,6 @@ abstract contract LinkedToken is IpcExchange { return _linkedSubnet; } - function _captureTokens(address holder, uint256 amount) internal virtual; function _releaseTokens(address beneficiary, uint256 amount) internal virtual; @@ -91,10 +85,7 @@ abstract contract LinkedToken is IpcExchange { return _linkedTransfer(receiver, amount); } - function _linkedTransfer( - address recipient, - uint256 amount - ) internal returns (IpcEnvelope memory committed) { + function _linkedTransfer(address recipient, uint256 amount) internal returns (IpcEnvelope memory committed) { _validateInitialized(); // Validate that the transfer parameters are acceptable. @@ -184,7 +175,7 @@ abstract contract LinkedToken is IpcExchange { OutcomeType outcome = resultMsg.outcome; bool refund = outcome == OutcomeType.SystemErr || outcome == OutcomeType.ActorErr; - _removeUnconfirmedTransfer({ id: resultMsg.id, refund: refund }); + _removeUnconfirmedTransfer({id: resultMsg.id, refund: refund}); } function _receiveLinked(address recipient, uint256 amount) private { @@ -267,5 +258,4 @@ abstract contract LinkedToken is IpcExchange { _releaseTokens(sender, value); } } - } diff --git a/extras/linked-token/src/LinkedTokenController.sol b/extras/linked-token/src/LinkedTokenController.sol index e11ad6bf2..31b008dc3 100644 --- a/extras/linked-token/src/LinkedTokenController.sol +++ b/extras/linked-token/src/LinkedTokenController.sol @@ -1,9 +1,7 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 pragma solidity 0.8.23; -import { - SafeERC20 -} from "openzeppelin-contracts/token/ERC20/utils/SafeERC20.sol"; +import {SafeERC20} from "openzeppelin-contracts/token/ERC20/utils/SafeERC20.sol"; import {IERC20} from "openzeppelin-contracts/token/ERC20/IERC20.sol"; import {LinkedToken} from "./LinkedToken.sol"; import {SubnetID} from "@ipc/src/structs/Subnet.sol"; @@ -18,17 +16,10 @@ contract LinkedTokenController is LinkedToken { ) LinkedToken(gateway, underlyingToken, linkedSubnet) {} function _captureTokens(address holder, uint256 amount) internal override { - _underlying.safeTransferFrom({ - from: msg.sender, - to: address(this), - value: amount - }); + _underlying.safeTransferFrom({from: msg.sender, to: address(this), value: amount}); } - function _releaseTokens(address beneficiary, uint256 amount) - internal - override - { + function _releaseTokens(address beneficiary, uint256 amount) internal override { _underlying.safeTransfer(beneficiary, amount); } } diff --git a/extras/linked-token/src/LinkedTokenReplica.sol b/extras/linked-token/src/LinkedTokenReplica.sol index bab1c42fa..40e9a8ecb 100644 --- a/extras/linked-token/src/LinkedTokenReplica.sol +++ b/extras/linked-token/src/LinkedTokenReplica.sol @@ -3,9 +3,7 @@ pragma solidity 0.8.23; import {IERC20} from "openzeppelin-contracts/token/ERC20/IERC20.sol"; import {ERC20} from "openzeppelin-contracts/token/ERC20/ERC20.sol"; -import { - SafeERC20 -} from "openzeppelin-contracts/token/ERC20/utils/SafeERC20.sol"; +import {SafeERC20} from "openzeppelin-contracts/token/ERC20/utils/SafeERC20.sol"; import {LinkedToken} from "./LinkedToken.sol"; import {SubnetID} from "@ipc/src/structs/Subnet.sol"; @@ -20,19 +18,13 @@ contract LinkedTokenReplica is LinkedToken, ERC20 { address gateway, address underlyingToken, SubnetID memory linkedSubnet - ) - LinkedToken(gateway, underlyingToken, linkedSubnet) - ERC20("USDCTestReplica", "USDCtR") - {} + ) LinkedToken(gateway, underlyingToken, linkedSubnet) ERC20("USDCTestReplica", "USDCtR") {} function _captureTokens(address holder, uint256 amount) internal override { _burn(holder, amount); } - function _releaseTokens(address beneficiary, uint256 amount) - internal - override - { + function _releaseTokens(address beneficiary, uint256 amount) internal override { _mint(beneficiary, amount); } } diff --git a/extras/linked-token/test/IpcTokenController.t.sol b/extras/linked-token/test/IpcTokenController.t.sol index 420b90b44..46229467d 100644 --- a/extras/linked-token/test/IpcTokenController.t.sol +++ b/extras/linked-token/test/IpcTokenController.t.sol @@ -10,10 +10,7 @@ import {SubnetID, IPCAddress} from "@ipc/src/structs/Subnet.sol"; import {FvmAddressHelper} from "@ipc/src/lib/FvmAddressHelper.sol"; import {FvmAddress} from "@ipc/src/structs/FvmAddress.sol"; -import { - InvalidOriginContract, - InvalidOriginSubnet -} from "../src/LinkedToken.sol"; +import {InvalidOriginContract, InvalidOriginSubnet} from "../src/LinkedToken.sol"; import {IpcEnvelope, CallMsg, IpcMsgKind} from "@ipc/src/structs/CrossNet.sol"; import {SubnetActorDiamond} from "@ipc/src/SubnetActorDiamond.sol"; @@ -44,41 +41,21 @@ contract IpcTokenControllerTest is Test, IntegrationTestBase { testUSDC.mint(transferAmount); controllerSubnetUSDC = address(testUSDC); - controllerSubnet = SubnetID({ - root: ROOTNET_CHAINID, - route: new address[](0) - }); + controllerSubnet = SubnetID({root: ROOTNET_CHAINID, route: new address[](0)}); require(controllerSubnet.isRoot(), "not root"); rootGateway = createGatewayDiamond(gatewayParams(controllerSubnet)); gateway = address(rootGateway); - replica = new LinkedTokenReplica( - gateway, - controllerSubnetUSDC, - controllerSubnet - ); - rootNativeSubnetActor = createSubnetActor( - defaultSubnetActorParamsWith(address(rootGateway), controllerSubnet) - ); + replica = new LinkedTokenReplica(gateway, controllerSubnetUSDC, controllerSubnet); + rootNativeSubnetActor = createSubnetActor(defaultSubnetActorParamsWith(address(rootGateway), controllerSubnet)); nativeSubnetPath = new address[](1); nativeSubnetPath[0] = address(rootNativeSubnetActor); - replicaSubnetName = SubnetID({ - root: ROOTNET_CHAINID, - route: nativeSubnetPath - }); + replicaSubnetName = SubnetID({root: ROOTNET_CHAINID, route: nativeSubnetPath}); vm.deal(address(rootNativeSubnetActor), DEFAULT_COLLATERAL_AMOUNT); vm.prank(address(rootNativeSubnetActor)); - registerSubnetGW( - DEFAULT_COLLATERAL_AMOUNT, - address(rootNativeSubnetActor), - rootGateway - ); - - controller = new LinkedTokenController( - gateway, - controllerSubnetUSDC, - replicaSubnetName - ); + registerSubnetGW(DEFAULT_COLLATERAL_AMOUNT, address(rootNativeSubnetActor), rootGateway); + + controller = new LinkedTokenController(gateway, controllerSubnetUSDC, replicaSubnetName); replica.initialize(address(controller)); controller.initialize(address(replica)); @@ -88,61 +65,43 @@ contract IpcTokenControllerTest is Test, IntegrationTestBase { testUSDC.approve(address(controller), transferAmount); vm.deal(address(this), DEFAULT_CROSS_MSG_FEE); - CallMsg memory message = - CallMsg({ - method: abi.encodePacked( - bytes4(keccak256("receiveAndUnlock(address,uint256)")) - ), - params: abi.encode(address(this), transferAmount) - }); - - IpcEnvelope memory validMsg = - IpcEnvelope({ - kind: IpcMsgKind.Call, - from: IPCAddress({ - subnetId: replicaSubnetName, - rawAddress: FvmAddressHelper.from(address(controller)) - }), - to: IPCAddress({ - subnetId: controllerSubnet, - rawAddress: FvmAddressHelper.from(address(replica)) - }), - value: DEFAULT_CROSS_MSG_FEE, - nonce: 0, - message: abi.encode(message) - }); - - IpcEnvelope memory invalidContract = - IpcEnvelope({ - kind: IpcMsgKind.Call, - from: IPCAddress({ - subnetId: replicaSubnetName, - rawAddress: FvmAddressHelper.from(address(this)) /* invalid */ - }), - to: IPCAddress({ - subnetId: controllerSubnet, - rawAddress: FvmAddressHelper.from(address(replica)) - }), - value: DEFAULT_CROSS_MSG_FEE, - nonce: 0, - message: abi.encode(message) - }); - - IpcEnvelope memory invalidSubnet = - IpcEnvelope({ - kind: IpcMsgKind.Call, - from: IPCAddress({ - subnetId: controllerSubnet, /* invalid */ - rawAddress: FvmAddressHelper.from(address(controller)) - }), - to: IPCAddress({ - subnetId: controllerSubnet, - rawAddress: FvmAddressHelper.from(address(replica)) - }), - value: DEFAULT_CROSS_MSG_FEE, - nonce: 0, - message: abi.encode(message) - }); + CallMsg memory message = CallMsg({ + method: abi.encodePacked(bytes4(keccak256("receiveAndUnlock(address,uint256)"))), + params: abi.encode(address(this), transferAmount) + }); + + IpcEnvelope memory validMsg = IpcEnvelope({ + kind: IpcMsgKind.Call, + from: IPCAddress({subnetId: replicaSubnetName, rawAddress: FvmAddressHelper.from(address(controller))}), + to: IPCAddress({subnetId: controllerSubnet, rawAddress: FvmAddressHelper.from(address(replica))}), + value: DEFAULT_CROSS_MSG_FEE, + nonce: 0, + message: abi.encode(message) + }); + + IpcEnvelope memory invalidContract = IpcEnvelope({ + kind: IpcMsgKind.Call, + from: IPCAddress({ + subnetId: replicaSubnetName, + rawAddress: FvmAddressHelper.from(address(this)) /* invalid */ + }), + to: IPCAddress({subnetId: controllerSubnet, rawAddress: FvmAddressHelper.from(address(replica))}), + value: DEFAULT_CROSS_MSG_FEE, + nonce: 0, + message: abi.encode(message) + }); + + IpcEnvelope memory invalidSubnet = IpcEnvelope({ + kind: IpcMsgKind.Call, + from: IPCAddress({ + subnetId: controllerSubnet /* invalid */, + rawAddress: FvmAddressHelper.from(address(controller)) + }), + to: IPCAddress({subnetId: controllerSubnet, rawAddress: FvmAddressHelper.from(address(replica))}), + value: DEFAULT_CROSS_MSG_FEE, + nonce: 0, + message: abi.encode(message) + }); vm.expectRevert(InvalidOriginContract.selector); controller._validateEnvelope(invalidContract); @@ -153,22 +112,12 @@ contract IpcTokenControllerTest is Test, IntegrationTestBase { function testParentSubnetUSDCAddress() public { // Test to check if controllerSubnetUSDC address is correctly set - assertEq( - controller._linkedContract(), - address(replica), - "controllerSubnetUSDC address does not match" - ); + assertEq(controller._linkedContract(), address(replica), "controllerSubnetUSDC address does not match"); } function testParentSubnet() public { - assertTrue( - controllerSubnet.equals(replica.getLinkedSubnet()), - "replica Subnetdoes not match" - ); - assertTrue( - replicaSubnetName.equals(controller.getLinkedSubnet()), - "controller Subnetdoes not match" - ); + assertTrue(controllerSubnet.equals(replica.getLinkedSubnet()), "replica Subnetdoes not match"); + assertTrue(replicaSubnetName.equals(controller.getLinkedSubnet()), "controller Subnetdoes not match"); } function testDepositTokens() public { diff --git a/extras/linked-token/test/IpcTokenReplica.t.sol b/extras/linked-token/test/IpcTokenReplica.t.sol index d2020fa7f..d7b41208d 100644 --- a/extras/linked-token/test/IpcTokenReplica.t.sol +++ b/extras/linked-token/test/IpcTokenReplica.t.sol @@ -16,10 +16,7 @@ import {SubnetActorDiamond} from "@ipc/src/SubnetActorDiamond.sol"; import {LinkedTokenController} from "../src/LinkedTokenController.sol"; //import {InvalidOriginContract, InvalidOriginSubnet} from "@ipc/src/examples/cross-token/IpcCrossTokenErrors.sol"; import {USDCTest} from "../src/USDCTest.sol"; -import { - InvalidOriginContract, - InvalidOriginSubnet -} from "../src/LinkedToken.sol"; +import {InvalidOriginContract, InvalidOriginSubnet} from "../src/LinkedToken.sol"; contract IpcTokenReplicaTest is Test, IntegrationTestBase { using SubnetIDHelper for SubnetID; @@ -43,101 +40,57 @@ contract IpcTokenReplicaTest is Test, IntegrationTestBase { testUSDC.mint(transferAmount); controllerSubnetUSDC = address(testUSDC); - controllerSubnet = SubnetID({ - root: ROOTNET_CHAINID, - route: new address[](0) - }); + controllerSubnet = SubnetID({root: ROOTNET_CHAINID, route: new address[](0)}); require(controllerSubnet.isRoot(), "not root"); rootGateway = createGatewayDiamond(gatewayParams(controllerSubnet)); gateway = address(rootGateway); - replica = new LinkedTokenReplica( - gateway, - controllerSubnetUSDC, - controllerSubnet - ); - rootNativeSubnetActor = createSubnetActor( - defaultSubnetActorParamsWith(address(rootGateway), controllerSubnet) - ); + replica = new LinkedTokenReplica(gateway, controllerSubnetUSDC, controllerSubnet); + rootNativeSubnetActor = createSubnetActor(defaultSubnetActorParamsWith(address(rootGateway), controllerSubnet)); nativeSubnetPath = new address[](1); nativeSubnetPath[0] = address(rootNativeSubnetActor); - replicaSubnetName = SubnetID({ - root: ROOTNET_CHAINID, - route: nativeSubnetPath - }); + replicaSubnetName = SubnetID({root: ROOTNET_CHAINID, route: nativeSubnetPath}); vm.deal(address(rootNativeSubnetActor), DEFAULT_COLLATERAL_AMOUNT); vm.prank(address(rootNativeSubnetActor)); - registerSubnetGW( - DEFAULT_COLLATERAL_AMOUNT, - address(rootNativeSubnetActor), - rootGateway - ); - - controller = new LinkedTokenController( - gateway, - controllerSubnetUSDC, - replicaSubnetName - ); + registerSubnetGW(DEFAULT_COLLATERAL_AMOUNT, address(rootNativeSubnetActor), rootGateway); + + controller = new LinkedTokenController(gateway, controllerSubnetUSDC, replicaSubnetName); replica.initialize(address(controller)); controller.initialize(address(replica)); } function testHandleIpcMessageOrigin() public { - CallMsg memory message = - CallMsg({ - method: abi.encodePacked( - bytes4(keccak256("receiveAndMint(address,uint256)")) - ), - params: abi.encode(address(this), transferAmount) - }); - - IpcEnvelope memory validMsg = - IpcEnvelope({ - kind: IpcMsgKind.Call, - from: IPCAddress({ - subnetId: controllerSubnet, - rawAddress: FvmAddressHelper.from(address(replica)) - }), - to: IPCAddress({ - subnetId: replicaSubnetName, - rawAddress: FvmAddressHelper.from(address(controller)) - }), - value: DEFAULT_CROSS_MSG_FEE, - nonce: 0, - message: abi.encode(message) - }); - - IpcEnvelope memory invalidContract = - IpcEnvelope({ - kind: IpcMsgKind.Call, - from: IPCAddress({ - subnetId: controllerSubnet, - rawAddress: FvmAddressHelper.from(address(replica)) - }), - to: IPCAddress({ - subnetId: replicaSubnetName, - rawAddress: FvmAddressHelper.from(address(replica)) - }), - value: DEFAULT_CROSS_MSG_FEE, - nonce: 0, - message: abi.encode(message) - }); - - IpcEnvelope memory invalidSubnet = - IpcEnvelope({ - kind: IpcMsgKind.Call, - from: IPCAddress({ - subnetId: replicaSubnetName, - rawAddress: FvmAddressHelper.from(address(replica)) - }), - to: IPCAddress({ - subnetId: replicaSubnetName, - rawAddress: FvmAddressHelper.from(address(controller)) - }), - value: DEFAULT_CROSS_MSG_FEE, - nonce: 0, - message: abi.encode(message) - }); + CallMsg memory message = CallMsg({ + method: abi.encodePacked(bytes4(keccak256("receiveAndMint(address,uint256)"))), + params: abi.encode(address(this), transferAmount) + }); + + IpcEnvelope memory validMsg = IpcEnvelope({ + kind: IpcMsgKind.Call, + from: IPCAddress({subnetId: controllerSubnet, rawAddress: FvmAddressHelper.from(address(replica))}), + to: IPCAddress({subnetId: replicaSubnetName, rawAddress: FvmAddressHelper.from(address(controller))}), + value: DEFAULT_CROSS_MSG_FEE, + nonce: 0, + message: abi.encode(message) + }); + + IpcEnvelope memory invalidContract = IpcEnvelope({ + kind: IpcMsgKind.Call, + from: IPCAddress({subnetId: controllerSubnet, rawAddress: FvmAddressHelper.from(address(replica))}), + to: IPCAddress({subnetId: replicaSubnetName, rawAddress: FvmAddressHelper.from(address(replica))}), + value: DEFAULT_CROSS_MSG_FEE, + nonce: 0, + message: abi.encode(message) + }); + + IpcEnvelope memory invalidSubnet = IpcEnvelope({ + kind: IpcMsgKind.Call, + from: IPCAddress({subnetId: replicaSubnetName, rawAddress: FvmAddressHelper.from(address(replica))}), + to: IPCAddress({subnetId: replicaSubnetName, rawAddress: FvmAddressHelper.from(address(controller))}), + value: DEFAULT_CROSS_MSG_FEE, + nonce: 0, + message: abi.encode(message) + }); vm.expectRevert(InvalidOriginContract.selector); replica._validateEnvelope(invalidContract); diff --git a/extras/linked-token/test/MultiSubnetTest.t.sol b/extras/linked-token/test/MultiSubnetTest.t.sol index bb53dd4ea..a0655931a 100644 --- a/extras/linked-token/test/MultiSubnetTest.t.sol +++ b/extras/linked-token/test/MultiSubnetTest.t.sol @@ -11,12 +11,7 @@ import {LinkedTokenController} from "../src/LinkedTokenController.sol"; import {LinkedTokenReplica} from "../src/LinkedTokenReplica.sol"; import {USDCTest} from "../src/USDCTest.sol"; -import { - SubnetID, - Subnet, - IPCAddress, - Validator -} from "@ipc/src/structs/Subnet.sol"; +import {SubnetID, Subnet, IPCAddress, Validator} from "@ipc/src/structs/Subnet.sol"; import {SubnetActorDiamond} from "@ipc/src/SubnetActorDiamond.sol"; import {GatewayDiamond} from "@ipc/src/GatewayDiamond.sol"; import {TopDownFinalityFacet} from "@ipc/src/gateway/router/TopDownFinalityFacet.sol"; @@ -26,15 +21,7 @@ import {GatewayGetterFacet} from "@ipc/src/gateway/GatewayGetterFacet.sol"; import {SubnetActorCheckpointingFacet} from "@ipc/src/subnet/SubnetActorCheckpointingFacet.sol"; import {CheckpointingFacet} from "@ipc/src/gateway/router/CheckpointingFacet.sol"; import {FvmAddressHelper} from "@ipc/src/lib/FvmAddressHelper.sol"; -import { - IpcEnvelope, - BottomUpMsgBatch, - BottomUpCheckpoint, - ParentFinality, - IpcMsgKind, - ResultMsg, - CallMsg -} from "@ipc/src/structs/CrossNet.sol"; +import {IpcEnvelope, BottomUpMsgBatch, BottomUpCheckpoint, ParentFinality, IpcMsgKind, ResultMsg, CallMsg} from "@ipc/src/structs/CrossNet.sol"; import {SubnetIDHelper} from "@ipc/src/lib/SubnetIDHelper.sol"; import {IERC20} from "openzeppelin-contracts/token/ERC20/IERC20.sol"; import {CrossMsgHelper} from "@ipc/src/lib/CrossMsgHelper.sol"; @@ -74,9 +61,7 @@ contract MultiSubnetTest is IntegrationTestBase { rootGateway = createGatewayDiamond(gatewayParams(rootSubnetName)); - rootNativeSubnetActor = createSubnetActor( - defaultSubnetActorParamsWith(address(rootGateway), rootSubnetName) - ); + rootNativeSubnetActor = createSubnetActor(defaultSubnetActorParamsWith(address(rootGateway), rootSubnetName)); rootTokenSubnetActor = createSubnetActor( defaultSubnetActorParamsWith(address(rootGateway), rootSubnetName, address(token)) @@ -129,23 +114,13 @@ contract MultiSubnetTest is IntegrationTestBase { vm.deal(address(rootTokenSubnetActor), DEFAULT_COLLATERAL_AMOUNT); vm.prank(address(rootTokenSubnetActor)); - registerSubnetGW( - DEFAULT_COLLATERAL_AMOUNT, - address(rootTokenSubnetActor), - rootGateway - ); + registerSubnetGW(DEFAULT_COLLATERAL_AMOUNT, address(rootTokenSubnetActor), rootGateway); vm.deal(address(rootNativeSubnetActor), DEFAULT_COLLATERAL_AMOUNT); vm.prank(address(rootNativeSubnetActor)); - registerSubnetGW( - DEFAULT_COLLATERAL_AMOUNT, - address(rootNativeSubnetActor), - rootGateway - ); + registerSubnetGW(DEFAULT_COLLATERAL_AMOUNT, address(rootNativeSubnetActor), rootGateway); - console.log( - "--------------- transfer and mint (top-down) ---------------" - ); + console.log("--------------- transfer and mint (top-down) ---------------"); USDCTest testUSDC = new USDCTest(); @@ -153,10 +128,7 @@ contract MultiSubnetTest is IntegrationTestBase { testUSDC.transfer(holder, holderTotalAmount); require(testUSDC.owner() == owner, "unexpected owner"); - require( - testUSDC.balanceOf(holder) == holderTotalAmount, - "unexpected balance" - ); + require(testUSDC.balanceOf(holder) == holderTotalAmount, "unexpected balance"); // the token replica sits in a native supply child subnet. ipcTokenReplica = new LinkedTokenReplica({ @@ -187,25 +159,17 @@ contract MultiSubnetTest is IntegrationTestBase { ); vm.prank(address(holder)); - IpcEnvelope memory lockAndTransferEnvelope = - ipcTokenController.lockAndTransferWithReturn( - recipient, - transferAmount - ); + IpcEnvelope memory lockAndTransferEnvelope = ipcTokenController.lockAndTransferWithReturn( + recipient, + transferAmount + ); // Check that the message is in unconfirmedTransfers - (address receiptSender, uint256 receiptValue) = - ipcTokenController.getUnconfirmedTransfer( - lockAndTransferEnvelope.toHash() - ); - require( - receiptSender == address(holder), - "Transfer sender incorrect in unconfirmedTransfers" - ); - require( - receiptValue == transferAmount, - "Transfer amount incorrect in unconfirmedTransfers" + (address receiptSender, uint256 receiptValue) = ipcTokenController.getUnconfirmedTransfer( + lockAndTransferEnvelope.toHash() ); + require(receiptSender == address(holder), "Transfer sender incorrect in unconfirmedTransfers"); + require(receiptValue == transferAmount, "Transfer amount incorrect in unconfirmedTransfers"); //confirm that token replica only accept calls to Ipc from the gateway vm.prank(owner); @@ -227,25 +191,16 @@ contract MultiSubnetTest is IntegrationTestBase { }); msgs[0] = expected; - executeTopDownMsgs( - msgs, - nativeSubnetName, - nativeSubnetGateway - ); + executeTopDownMsgs(msgs, nativeSubnetName, nativeSubnetGateway); console.log("fail:"); console.log(IERC20(ipcTokenReplica).balanceOf(recipient)); console.log(transferAmount); //ensure that tokens are delivered on subnet - require( - IERC20(ipcTokenReplica).balanceOf(recipient) == transferAmount, - "incorrect proxy token balance" - ); + require(IERC20(ipcTokenReplica).balanceOf(recipient) == transferAmount, "incorrect proxy token balance"); - console.log( - "--------------- withdraw token (bottom-up)---------------" - ); + console.log("--------------- withdraw token (bottom-up)---------------"); // ensure that USDC holder has initial balance minus tokens previously sent amount of tokens in the root chain require( @@ -257,37 +212,22 @@ contract MultiSubnetTest is IntegrationTestBase { expected = ipcTokenReplica.linkedTransfer(holder, transferAmount); // check that the message is in unconfirmedTransfers - (receiptSender, receiptValue) = ipcTokenReplica.getUnconfirmedTransfer( - expected.toHash() - ); - require( - receiptSender == recipient, - "Transfer sender incorrect in unconfirmedTransfers" - ); - require( - receiptValue == transferAmount, - "Transfer amount incorrect in unconfirmedTransfers" - ); + (receiptSender, receiptValue) = ipcTokenReplica.getUnconfirmedTransfer(expected.toHash()); + require(receiptSender == recipient, "Transfer sender incorrect in unconfirmedTransfers"); + require(receiptValue == transferAmount, "Transfer amount incorrect in unconfirmedTransfers"); console.log("Begin bottom up checkpoint"); - BottomUpCheckpoint memory checkpoint = - callCreateBottomUpCheckpointFromChildSubnet( - nativeSubnetName, - nativeSubnetGateway - ); + BottomUpCheckpoint memory checkpoint = callCreateBottomUpCheckpointFromChildSubnet( + nativeSubnetName, + nativeSubnetGateway + ); submitBottomUpCheckpoint(checkpoint, rootNativeSubnetActor); //ensure that usdc tokens are returned on root net - require( - holderTotalAmount == testUSDC.balanceOf(holder), - "unexpected holder balance after withdrawal" - ); + require(holderTotalAmount == testUSDC.balanceOf(holder), "unexpected holder balance after withdrawal"); //ensure that the tokens in the subnet are minted and the token bridge and the usdc holder does not own any - require( - 0 == ipcTokenReplica.balanceOf(holder), - "unexpected holder balance in ipcTokenReplica" - ); + require(0 == ipcTokenReplica.balanceOf(holder), "unexpected holder balance in ipcTokenReplica"); require( 0 == ipcTokenReplica.balanceOf(address(ipcTokenReplica)), "unexpected ipcTokenReplica balance in ipcTokenReplica"