Skip to content

Commit

Permalink
extras/linked-token: add prettier + make fmt. (#855)
Browse files Browse the repository at this point in the history
  • Loading branch information
raulk authored Apr 2, 2024
1 parent 8b563d7 commit 832ce37
Show file tree
Hide file tree
Showing 16 changed files with 278 additions and 322 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target/
.idea
node_modules/
12 changes: 12 additions & 0 deletions extras/linked-token/.prettierignore
Original file line number Diff line number Diff line change
@@ -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/
22 changes: 22 additions & 0 deletions extras/linked-token/.prettierrc
Original file line number Diff line number Diff line change
@@ -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,
},
},
],
}
8 changes: 6 additions & 2 deletions extras/linked-token/Makefile
Original file line number Diff line number Diff line change
@@ -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:"
Expand All @@ -25,6 +25,10 @@ help:
@echo " make <target> - 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
Expand Down
97 changes: 97 additions & 0 deletions extras/linked-token/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions extras/linked-token/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"devDependencies": {
"@solidity-parser/parser": "^0.18.0",
"prettier": "^3.2.5",
"prettier-plugin-solidity": "^1.3.1"
}
}
7 changes: 2 additions & 5 deletions extras/linked-token/script/ConfigManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -51,4 +49,3 @@ contract ConfigManager is Script {
bytes memory retrievedValue = readConfig(exampleKey);
}
}

8 changes: 6 additions & 2 deletions extras/linked-token/script/DeployIpcTokenController.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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});

Expand All @@ -18,4 +23,3 @@ contract DeployIpcTokenController is ConfigManager {
writeConfig("LinkedTokenController", vm.toString(address(controller)));
}
}

3 changes: 1 addition & 2 deletions extras/linked-token/script/DeployIpcTokenReplica.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -16,4 +16,3 @@ contract DeployIpcTokenReplica is ConfigManager {
writeConfig("LinkedTokenReplica", vm.toString(address(replica)));
}
}

4 changes: 1 addition & 3 deletions extras/linked-token/script/DeployUSDCTest.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ 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();

vm.stopBroadcast();

writeConfig("USDCTest", vm.toString(address(erc20Token)));

}
}
16 changes: 3 additions & 13 deletions extras/linked-token/src/LinkedToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -267,5 +258,4 @@ abstract contract LinkedToken is IpcExchange {
_releaseTokens(sender, value);
}
}

}
15 changes: 3 additions & 12 deletions extras/linked-token/src/LinkedTokenController.sol
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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);
}
}
14 changes: 3 additions & 11 deletions extras/linked-token/src/LinkedTokenReplica.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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);
}
}
Loading

0 comments on commit 832ce37

Please sign in to comment.