Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add husky #33

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx lint-staged
2 changes: 1 addition & 1 deletion contracts/INativeOFT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ import "@layerzerolabs/solidity-examples/contracts/token/oft/IOFTCore.sol";

interface INativeOFT is IOFTCore {
function deposit() external payable;
}
}
5 changes: 3 additions & 2 deletions contracts/IWETH.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ pragma solidity ^0.8.0;

interface IWETH {
function deposit() external payable;
function approve(address spender, uint256 amount) external returns (bool);
}

function approve(address spender, uint amount) external returns (bool);
}
2 changes: 1 addition & 1 deletion contracts/MinSendAmountNativeOFT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ contract MinSendAmountNativeOFT is NativeOFT {
require(_amount >= minSendAmount, "MinSendAmountNativeOFT: amount is less than minimum");
super._send(_from, _dstChainId, _toAddress, _amount, _refundAddress, _zroPaymentAddress, _adapterParams);
}
}
}
2 changes: 1 addition & 1 deletion contracts/MinSendAmountOFT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ contract MinSendAmountOFT is OFT {
require(_amount >= minSendAmount, "MinSendAmountOFT: amount is less than minimum");
super._send(_from, _dstChainId, _toAddress, _amount, _refundAddress, _zroPaymentAddress, _adapterParams);
}
}
}
2 changes: 1 addition & 1 deletion contracts/SwappableBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ contract SwappableBridge {
nativeOft.deposit{value: amountIn}();
nativeOft.sendFrom{value: msg.value - amountIn}(address(this), dstChainId, abi.encodePacked(to), amountIn, refundAddress, zroPaymentAddress, adapterParams);
}
}
}
18 changes: 4 additions & 14 deletions contracts/SwappableBridgeUniswapV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import "@layerzerolabs/solidity-examples/contracts/token/oft/IOFTCore.sol";
import "./IWETH.sol";
import "./INativeOFT.sol";

contract SwappableBridgeUniswapV3 {
contract SwappableBridgeUniswapV3 {
uint24 public constant poolFee = 3000; // 0.3%

IWETH public immutable weth;
IOFTCore public immutable oft;
IOFTCore public immutable oft;
ISwapRouter public immutable swapRouter;

constructor(address _weth, address _oft, address _swapRouter) {
Expand All @@ -30,19 +30,9 @@ contract SwappableBridgeUniswapV3 {
weth.deposit{value: amountIn}();
weth.approve(address(swapRouter), amountIn);

ISwapRouter.ExactInputSingleParams memory params =
ISwapRouter.ExactInputSingleParams({
tokenIn: address(weth),
tokenOut: address(oft),
fee: poolFee,
recipient: address(this),
deadline: block.timestamp,
amountIn: amountIn,
amountOutMinimum: amountOutMin,
sqrtPriceLimitX96: 0
});
ISwapRouter.ExactInputSingleParams memory params = ISwapRouter.ExactInputSingleParams({tokenIn: address(weth), tokenOut: address(oft), fee: poolFee, recipient: address(this), deadline: block.timestamp, amountIn: amountIn, amountOutMinimum: amountOutMin, sqrtPriceLimitX96: 0});

uint amountOut = swapRouter.exactInputSingle(params);
oft.sendFrom{value: msg.value - amountIn}(address(this), dstChainId, abi.encodePacked(to), amountOut, refundAddress, zroPaymentAddress, adapterParams);
}
}
}
2 changes: 1 addition & 1 deletion contracts/mocks/LayerZeroEndpoint.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import "@layerzerolabs/solidity-examples/contracts/mocks/LZEndpointMock.sol";

contract LayerZeroEndpoint is LZEndpointMock {
constructor(uint16 _chainId) LZEndpointMock(_chainId) {}
}
}
2 changes: 1 addition & 1 deletion contracts/mocks/MintableMinSendAmountOFTMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ contract MintableMinSendAmountOFTMock is MinSendAmountOFT {
function mint(address _to, uint _amount) public {
_mint(_to, _amount);
}
}
}
2 changes: 1 addition & 1 deletion contracts/mocks/WETH9.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ contract WETH9 {

return true;
}
}
}
2 changes: 1 addition & 1 deletion deploy/NativeOFT.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ module.exports = async function ({ deployments, getNamedAccounts }) {
})
}

module.exports.tags = ["NativeOFT"]
module.exports.tags = ["NativeOFT"]
6 changes: 3 additions & 3 deletions deploy/OFT.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ module.exports = async function ({ deployments, getNamedAccounts }) {
const lzEndpointAddress = LZ_ENDPOINTS[hre.network.name]
console.log(`[${hre.network.name}] Endpoint Address: ${lzEndpointAddress}`)
const oftArgs = OFT_ARGS[hre.network.name]
const constructorArgs = oftArgs.useMinAmount
? [oftArgs.name, oftArgs.symbol, lzEndpointAddress, ethers.utils.parseEther(oftArgs.minAmount)]
const constructorArgs = oftArgs.useMinAmount
? [oftArgs.name, oftArgs.symbol, lzEndpointAddress, ethers.utils.parseEther(oftArgs.minAmount)]
: [oftArgs.name, oftArgs.symbol, lzEndpointAddress]

await deploy(oftArgs.contractName, {
Expand All @@ -21,4 +21,4 @@ module.exports = async function ({ deployments, getNamedAccounts }) {
})
}

module.exports.tags = ["OFT"]
module.exports.tags = ["OFT"]
2 changes: 1 addition & 1 deletion deploy/SwappableBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ module.exports = async function ({ deployments, getNamedAccounts }) {
})
}

module.exports.tags = ["SwappableBridge"]
module.exports.tags = ["SwappableBridge"]
32 changes: 16 additions & 16 deletions deploy/SwappableBridgeUniswapV3.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ const ROUTERS = require("../constants/swapRouters.json")
const WETHS = require("../constants/weths.json")

module.exports = async function ({ deployments, getNamedAccounts }) {
const { deploy } = deployments
const { deployer } = await getNamedAccounts()
console.log(`Deployer Address: ${deployer}`)
const { deploy } = deployments
const { deployer } = await getNamedAccounts()
console.log(`Deployer Address: ${deployer}`)

const routerAddress = ROUTERS[hre.network.name]
console.log(`[${hre.network.name}] Uniswap V3 SwapRouter Address: ${routerAddress}`)
const routerAddress = ROUTERS[hre.network.name]
console.log(`[${hre.network.name}] Uniswap V3 SwapRouter Address: ${routerAddress}`)

const wethAddress = WETHS[hre.network.name]
console.log(`[${hre.network.name}] WETH Address: ${wethAddress}`)
const wethAddress = WETHS[hre.network.name]
console.log(`[${hre.network.name}] WETH Address: ${wethAddress}`)

const oft = await ethers.getContract("OFT")
console.log(`[${hre.network.name}] OFT Address: ${oft.address}`)
const oft = await ethers.getContract("OFT")
console.log(`[${hre.network.name}] OFT Address: ${oft.address}`)

await deploy("SwappableBridgeUniswapV3", {
from: deployer,
args: [wethAddress, oft.address, routerAddress],
log: true,
waitConfirmations: 1,
})
await deploy("SwappableBridgeUniswapV3", {
from: deployer,
args: [wethAddress, oft.address, routerAddress],
log: true,
waitConfirmations: 1,
})
}

module.exports.tags = ["SwappableBridgeUniswapV3"]
module.exports.tags = ["SwappableBridgeUniswapV3"]
15 changes: 14 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@
"test": "hardhat test",
"coverage": "hardhat coverage",
"prettier": "prettier --write test/**/*.js && prettier --write deploy/*.js && prettier --write tasks/*.js && prettier --write contracts/**/*.sol && prettier --write contracts/**/**/*.sol && prettier --write contracts/**/**/**/*.sol",
"lint": "yarn prettier && solhint 'contracts/*.sol' && solhint 'contracts/**/*.sol' && solhint 'contracts/**/**/*.sol' && solhint 'contracts/**/**/**/*.sol'"
"lint": "yarn prettier && solhint 'contracts/*.sol' && solhint 'contracts/**/*.sol' && solhint 'contracts/**/**/*.sol' && solhint 'contracts/**/**/**/*.sol'",
"prepare": "husky"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{js,sol}": [
"yarn lint --fix"
]
},
"dependencies": {
"@layerzerolabs/solidity-examples": "0.0.6",
Expand Down Expand Up @@ -41,6 +52,8 @@
"hardhat-deploy": "^0.10.5",
"hardhat-deploy-ethers": "^0.3.0-beta.13",
"hardhat-gas-reporter": "^1.0.6",
"husky": "^9.0.11",
"lint-staged": "^15.2.2",
"prettier": "^2.4.1",
"solhint": "^3.3.6",
"solidity-coverage": "^0.8.2",
Expand Down
7 changes: 5 additions & 2 deletions tasks/addLiquidity.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ module.exports = async function (taskArgs, hre) {
const block = await ethers.provider.getBlock(blockNumber)
const deadline = block.timestamp + 5 * 60 // 5 minutes from the current time

tx = await uniswapRouter.addLiquidityETH(oft.address, oftAmount, oftAmount, ethAmount, owner.address, deadline, { value: ethAmount, gasPrice: finalGasPrice })
tx = await uniswapRouter.addLiquidityETH(oft.address, oftAmount, oftAmount, ethAmount, owner.address, deadline, {
value: ethAmount,
gasPrice: finalGasPrice,
})
console.log(`Add liquidity tx: ${tx.hash}`)
await tx.wait()
}
}
9 changes: 6 additions & 3 deletions tasks/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const NATIVE_OFT_ARGS = require("../constants/nativeOftArgs.json")
module.exports = async function (taskArgs, hre) {
const signers = await ethers.getSigners()
const owner = signers[0]
const dstChainId = CHAIN_IDS[taskArgs.targetNetwork]
const dstChainId = CHAIN_IDS[taskArgs.targetNetwork]
const amount = ethers.utils.parseEther(taskArgs.amount)
const nativeOft = await ethers.getContract(NATIVE_OFT_ARGS[hre.network.name].contractName)
const bridge = await ethers.getContract("SwappableBridge")
Expand All @@ -13,7 +13,10 @@ module.exports = async function (taskArgs, hre) {
const gasPrice = await hre.ethers.provider.getGasPrice()
const finalGasPrice = gasPrice.mul(2)

const tx = await bridge.bridge(amount, dstChainId, owner.address, owner.address, ethers.constants.AddressZero, "0x", { value: amount.add(increasedNativeFee), gasPrice: finalGasPrice })
const tx = await bridge.bridge(amount, dstChainId, owner.address, owner.address, ethers.constants.AddressZero, "0x", {
value: amount.add(increasedNativeFee),
gasPrice: finalGasPrice,
})
console.log(`bridge tx: ${tx.hash}\n`)
await tx.wait()
}
}
32 changes: 15 additions & 17 deletions tasks/getPrice.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@ const ROUTERS = require("../constants/uniswapRoutes.json")
const WETHS = require("../constants/weths.json")
const OFT_ARGS = require("../constants/oftArgs.json")

const routerAbi = [
"function getAmountsOut(uint amountIn, address[] calldata path) view returns (uint[] memory amounts)"
]
const routerAbi = ["function getAmountsOut(uint amountIn, address[] calldata path) view returns (uint[] memory amounts)"]
module.exports = async function (taskArgs, hre) {
const uniswapRouterAddress = ROUTERS[hre.network.name]
const uniswapRouter = await hre.ethers.getContractAt(routerAbi, uniswapRouterAddress)
const wethAddress = WETHS[hre.network.name]
const oft = await ethers.getContract(OFT_ARGS[hre.network.name].contractName)
const uniswapRouterAddress = ROUTERS[hre.network.name]
const uniswapRouter = await hre.ethers.getContractAt(routerAbi, uniswapRouterAddress)
const wethAddress = WETHS[hre.network.name]
const oft = await ethers.getContract(OFT_ARGS[hre.network.name].contractName)

// NOTE: using a small amount just to get a ratio
const precision = 6;
const amountIn = ethers.utils.parseUnits("1", precision)
// NOTE: using a small amount just to get a ratio
const precision = 6
const amountIn = ethers.utils.parseUnits("1", precision)

let amountsOut = await uniswapRouter.getAmountsOut(amountIn, [wethAddress, oft.address])
const wethPriceInOFT = amountsOut[1]
console.log(ethers.utils.formatUnits(wethPriceInOFT, precision))
let amountsOut = await uniswapRouter.getAmountsOut(amountIn, [wethAddress, oft.address])
const wethPriceInOFT = amountsOut[1]
console.log(ethers.utils.formatUnits(wethPriceInOFT, precision))

amountsOut = await uniswapRouter.getAmountsOut(amountIn, [oft.address, wethAddress])
const oftPriceInWETH = amountsOut[1]
console.log(ethers.utils.formatUnits(oftPriceInWETH, precision))
}
amountsOut = await uniswapRouter.getAmountsOut(amountIn, [oft.address, wethAddress])
const oftPriceInWETH = amountsOut[1]
console.log(ethers.utils.formatUnits(oftPriceInWETH, precision))
}
9 changes: 5 additions & 4 deletions tasks/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
task("setTrustedRemote", "sets trusted remotes for OFT and NativeOFT", require("./setTrustedRemote"))
.addParam("targetNetwork", "the destination chainId")
task("setTrustedRemote", "sets trusted remotes for OFT and NativeOFT", require("./setTrustedRemote")).addParam(
"targetNetwork",
"the destination chainId"
)

task("bridge", "bridges native tokens", require("./bridge"))
.addParam("targetNetwork", "the destination chainId")
Expand All @@ -17,5 +19,4 @@ task("getPrice", "info", require("./getPrice"))

task("info", "info", require("./info"))

task("getSigners", "show the signers of the current mnemonic", require("./getSigners"))
.addOptionalParam("n", "how many to show", 3, types.int)
task("getSigners", "show the signers of the current mnemonic", require("./getSigners")).addOptionalParam("n", "how many to show", 3, types.int)
6 changes: 3 additions & 3 deletions tasks/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ module.exports = async function (taskArgs, hre) {
console.log(`OFT owner balance: ${ethers.utils.formatEther(await oft.balanceOf(owner.address))}`)
console.log(`ETH owner balance: ${ethers.utils.formatEther(await ethers.provider.getBalance(owner.address))}`)

console.log(`OFT pool balance: ${ethers.utils.formatEther(await oft.balanceOf(poolAddress))}`);
console.log(`ETH pool balance: ${ethers.utils.formatEther(await weth.balanceOf(poolAddress))}`);
}
console.log(`OFT pool balance: ${ethers.utils.formatEther(await oft.balanceOf(poolAddress))}`)
console.log(`ETH pool balance: ${ethers.utils.formatEther(await weth.balanceOf(poolAddress))}`)
}
4 changes: 2 additions & 2 deletions tasks/removeLiquidity.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = async function (taskArgs, hre) {

const blockNumber = await ethers.provider.getBlockNumber()
const block = await ethers.provider.getBlock(blockNumber)
const deadline = block.timestamp + 5 * 60; // 5 minutes from the current time
const deadline = block.timestamp + 5 * 60 // 5 minutes from the current time
const gasPrice = await hre.ethers.provider.getGasPrice()
const finalGasPrice = gasPrice.mul(5).div(4)

Expand All @@ -24,4 +24,4 @@ module.exports = async function (taskArgs, hre) {
tx = await uniswapRouter.removeLiquidityETH(oft.address, lpBalance, "0", "0", owner.address, deadline, { gasPrice: finalGasPrice })
console.log(`[${hre.network.name}] remove liquidity tx: ${tx.hash}`)
await tx.wait()
}
}
6 changes: 3 additions & 3 deletions tasks/setTrustedRemote.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const OFT_ARGS = require("../constants/oftArgs.json")
const NATIVE_OFT_ARGS = require("../constants/nativeOftArgs.json")

module.exports = async function (taskArgs, hre) {
const localChain = hre.network.name;
const remoteChain = taskArgs.targetNetwork;
const localChain = hre.network.name
const remoteChain = taskArgs.targetNetwork

const remoteChainId = CHAIN_IDS[remoteChain]

Expand All @@ -28,4 +28,4 @@ module.exports = async function (taskArgs, hre) {
tx = await oft.setTrustedRemoteAddress(remoteChainId, remoteNativeOft)
console.log(`OFT setTrustedRemoteAddress tx: ${tx.hash}`)
await tx.wait()
}
}
7 changes: 5 additions & 2 deletions tasks/swapAndBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ module.exports = async function (taskArgs, hre) {
const gasPrice = await hre.ethers.provider.getGasPrice()
const finalGasPrice = gasPrice.mul(5).div(4)

let tx = await bridge.swapAndBridge(amount, "0", dstChainId, owner.address, owner.address, ethers.constants.AddressZero, "0x", { value: amount.add(increasedNativeFee), gasPrice: finalGasPrice })
let tx = await bridge.swapAndBridge(amount, "0", dstChainId, owner.address, owner.address, ethers.constants.AddressZero, "0x", {
value: amount.add(increasedNativeFee),
gasPrice: finalGasPrice,
})
console.log(`swapAndBridge tx ${tx.hash}`)
await tx.wait()
}
}
15 changes: 12 additions & 3 deletions test/MinSendAmountNativeOFT.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe("MinSendAmountNativeOFT", () => {
let localNativeOft, remoteOft

before(async () => {
[owner, user] = await ethers.getSigners()
;[owner, user] = await ethers.getSigners()
ownerAddressBytes32 = utils.solidityPack(["address"], [owner.address])
})

Expand All @@ -45,7 +45,16 @@ describe("MinSendAmountNativeOFT", () => {

it("reverts when the amount sent is less than minimum allowed", async () => {
await expect(
localNativeOft.sendFrom(owner.address, remoteChainId, ownerAddressBytes32, utils.parseEther("0.9"), owner.address, constants.AddressZero, "0x", { value: nativeFee })
localNativeOft.sendFrom(
owner.address,
remoteChainId,
ownerAddressBytes32,
utils.parseEther("0.9"),
owner.address,
constants.AddressZero,
"0x",
{ value: nativeFee }
)
).to.be.revertedWith("MinSendAmountNativeOFT: amount is less than minimum")
})

Expand Down Expand Up @@ -74,4 +83,4 @@ describe("MinSendAmountNativeOFT", () => {
it("reverts when a non-owner calls setMinSendAmount", async () => {
await expect(localNativeOft.connect(user).setMinSendAmount(utils.parseEther("2"))).to.be.revertedWith("Ownable: caller is not the owner")
})
})
})
Loading