Skip to content

Commit

Permalink
Cleanup files (#6)
Browse files Browse the repository at this point in the history
* refactored

* added deploy all script

* added config parser

* fixed deploy all

* fixed token bridge

* fixed tests
  • Loading branch information
RuslanProgrammer authored Jan 5, 2024
1 parent 4befdf1 commit 32b8b27
Show file tree
Hide file tree
Showing 14 changed files with 218 additions and 109 deletions.
2 changes: 1 addition & 1 deletion contracts/Distribution.sol
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ contract Distribution is IDistribution, OwnableUpgradeable, UUPSUpgradeable {

IERC20(depositToken).safeTransfer(l1Sender, overplus_);

return L1Sender(l1Sender).sendDepositToken(gasLimit_, maxFeePerGas_, maxSubmissionCost_);
return L1Sender(l1Sender).sendDepositToken{value: msg.value}(gasLimit_, maxFeePerGas_, maxSubmissionCost_);
}

/**********************************************************************************************/
Expand Down
34 changes: 24 additions & 10 deletions contracts/L1Sender.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,33 @@ import {IL1Sender} from "./interfaces/IL1Sender.sol";

contract L1Sender is IL1Sender, ERC165, OwnableUpgradeable, UUPSUpgradeable {
address public unwrappedDepositToken;
address public distribution;

DepositTokenConfig public depositTokenConfig;
RewardTokenConfig public rewardTokenConfig;

function L1Sender__init() external initializer {
function L1Sender__init(
address distribution_,
RewardTokenConfig calldata rewardTokenConfig_,
DepositTokenConfig calldata depositTokenConfig_
) external initializer {
__Ownable_init();
__UUPSUpgradeable_init();

setDistribution(distribution_);
setRewardTokenConfig(rewardTokenConfig_);
setDepositTokenConfig(depositTokenConfig_);
}

function setDistribution(address distribution_) public onlyOwner {
distribution = distribution_;
}

function setRewardTokenConfig(RewardTokenConfig calldata newConfig_) external onlyOwner {
function setRewardTokenConfig(RewardTokenConfig calldata newConfig_) public onlyOwner {
rewardTokenConfig = newConfig_;
}

function setDepositTokenConfig(DepositTokenConfig calldata newConfig_) external onlyOwner {
function setDepositTokenConfig(DepositTokenConfig calldata newConfig_) public onlyOwner {
require(newConfig_.receiver != address(0), "L1S: invalid receiver");

DepositTokenConfig storage oldConfig = depositTokenConfig;
Expand Down Expand Up @@ -65,15 +78,14 @@ contract L1Sender is IL1Sender, ERC165, OwnableUpgradeable, UUPSUpgradeable {
address oldToken_,
address newToken_
) private {
bool isTokenChanged_ = oldToken_ != newToken_;
bool isGatewayChanged_ = oldGateway_ != newGateway_;
bool isAllowedChanged_ = (oldToken_ != newToken_) || (oldGateway_ != newGateway_);

if (oldGateway_ != address(0) && (isTokenChanged_ || isGatewayChanged_)) {
IERC20(oldToken_).approve(oldGateway_, 0);
if (oldGateway_ != address(0) && isAllowedChanged_) {
IERC20(oldToken_).approve(IGatewayRouter(oldGateway_).getGateway(oldToken_), 0);
}

if (isTokenChanged_ || isGatewayChanged_) {
IERC20(newToken_).approve(newGateway_, type(uint256).max);
if (isAllowedChanged_) {
IERC20(newToken_).approve(IGatewayRouter(newGateway_).getGateway(newToken_), type(uint256).max);
}
}

Expand Down Expand Up @@ -102,7 +114,9 @@ contract L1Sender is IL1Sender, ERC165, OwnableUpgradeable, UUPSUpgradeable {
);
}

function sendMintMessage(address user_, uint256 amount_, address refundTo_) external payable onlyOwner {
function sendMintMessage(address user_, uint256 amount_, address refundTo_) external payable {
require(_msgSender() == distribution, "L1S: invalid sender");

RewardTokenConfig storage config = rewardTokenConfig;

bytes memory receiverAndSenderAddresses_ = abi.encodePacked(config.receiver, address(this));
Expand Down
4 changes: 2 additions & 2 deletions contracts/L2TokenReceiver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ contract L2TokenReceiver is IL2TokenReceiver, ERC165, OwnableUpgradeable, UUPSUp
tokenId: tokenId_,
amount0Desired: amountAdd0_,
amount1Desired: amountAdd1_,
amount0Min: 0,
amount1Min: 0,
amount0Min: amountMin0_,
amount1Min: amountMin1_,
deadline: block.timestamp
});

Expand Down
4 changes: 4 additions & 0 deletions contracts/mock/GatewayRouterMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ contract GatewayRouterMock {

return abi.encode(_token, _to, _amount, _maxGas, _gasPriceBid, _data);
}

function getGateway(address) external view returns (address) {
return address(this);
}
}
20 changes: 13 additions & 7 deletions deploy/1_bridge.migration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DefaultStorage, Deployer, Reporter } from '@solarity/hardhat-migrate';
import { Deployer, Reporter, UserStorage } from '@solarity/hardhat-migrate';

import { parseConfig } from './helpers/config-parser';

Expand All @@ -15,7 +15,7 @@ import {
import { IL2TokenReceiver } from '@/generated-types/ethers/contracts/L2TokenReceiver';

module.exports = async function (deployer: Deployer) {
const config = parseConfig();
const config = parseConfig(await deployer.getChainId());

let WStETH: string;
let swapRouter: string;
Expand All @@ -41,10 +41,11 @@ module.exports = async function (deployer: Deployer) {
}

const MOR = await deployer.deploy(MOR__factory, [config.cap]);
if (!UserStorage.has('MOR')) UserStorage.set('MOR', await MOR.getAddress());

const swapParams: IL2TokenReceiver.SwapParamsStruct = {
tokenIn: WStETH,
tokenOut: MOR.address,
tokenOut: MOR,
fee: config.swapParams.fee,
sqrtPriceLimitX96: config.swapParams.sqrtPriceLimitX96,
};
Expand All @@ -53,20 +54,25 @@ module.exports = async function (deployer: Deployer) {
const l2TokenReceiverProxy = await deployer.deploy(ERC1967Proxy__factory, [l2TokenReceiverImpl, '0x'], {
name: 'L2TokenReceiver Proxy',
});
const l2TokenReceiver = L2TokenReceiver__factory.connect(l2TokenReceiverProxy.address, await deployer.getSigner());
if (!UserStorage.has('L2TokenReceiver Proxy'))
UserStorage.set('L2TokenReceiver Proxy', await l2TokenReceiverProxy.getAddress());
const l2TokenReceiver = L2TokenReceiver__factory.connect(
await l2TokenReceiverProxy.getAddress(),
await deployer.getSigner(),
);
await l2TokenReceiver.L2TokenReceiver__init(swapRouter, nonfungiblePositionManager, swapParams);
DefaultStorage.set('l2TokenReceiver', l2TokenReceiver.address);

const l2MessageReceiverImpl = await deployer.deploy(L2MessageReceiver__factory);
const l2MessageReceiverProxy = await deployer.deploy(ERC1967Proxy__factory, [l2MessageReceiverImpl, '0x'], {
name: 'L2MessageReceiver Proxy',
});
if (!UserStorage.has('L2MessageReceiver Proxy'))
UserStorage.set('L2MessageReceiver Proxy', await l2MessageReceiverProxy.getAddress());
const l2MessageReceiver = L2MessageReceiver__factory.connect(
l2MessageReceiverProxy.address,
await l2MessageReceiverProxy.getAddress(),
await deployer.getSigner(),
);
await l2MessageReceiver.L2MessageReceiver__init();
DefaultStorage.set('l2MessageReceiver', l2MessageReceiver.address);

await MOR.transferOwnership(l2MessageReceiver);

Expand Down
34 changes: 14 additions & 20 deletions deploy/2_token.migration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DefaultStorage, Deployer, Reporter } from '@solarity/hardhat-migrate';
import { Deployer, Reporter, UserStorage } from '@solarity/hardhat-migrate';

import { parseConfig } from './helpers/config-parser';

Expand All @@ -11,10 +11,10 @@ import {
WStETHMock__factory,
} from '@/generated-types/ethers';
import { IL1Sender } from '@/generated-types/ethers/contracts/L1Sender';
import { ZERO_ADDR } from '@/scripts/utils/constants';
import { ETHER_ADDR } from '@/scripts/utils/constants';

module.exports = async function (deployer: Deployer) {
const config = parseConfig();
const config = parseConfig(await deployer.getChainId());

let stETH: string;
let wStEth: string;
Expand Down Expand Up @@ -46,39 +46,33 @@ module.exports = async function (deployer: Deployer) {
if (config.arbitrumConfig) {
arbitrumBridgeGatewayRouter = config.arbitrumConfig.arbitrumBridgeGatewayRouter;
} else {
arbitrumBridgeGatewayRouter = ZERO_ADDR;
arbitrumBridgeGatewayRouter = ETHER_ADDR;
}

const distributionImpl = await deployer.deploy(Distribution__factory);
const distributionProxy = await deployer.deploy(ERC1967Proxy__factory, [distributionImpl, '0x'], {
name: 'Distribution Proxy',
});
const distribution = Distribution__factory.connect(distributionProxy.address, await deployer.getSigner());

const l1SenderImpl = await deployer.deploy(L1Sender__factory);
const l1SenderProxy = await deployer.deploy(ERC1967Proxy__factory, [l1SenderImpl, '0x'], {
name: 'L1Sender Proxy',
});
const l1Sender = L1Sender__factory.connect(l1SenderProxy.address, await deployer.getSigner());
await l1Sender.L1Sender__init();
const distribution = Distribution__factory.connect(await distributionProxy.getAddress(), await deployer.getSigner());

const rewardTokenConfig: IL1Sender.RewardTokenConfigStruct = {
gateway: lzEndpointL1,
receiver: DefaultStorage.get('l2MessageReceiver'),
// receiver: '0xc37fF39e5A50543AD01E42C4Cd88c2939dD13002',
receiver: UserStorage.get('L2MessageReceiver Proxy'),
receiverChainId: config.chainsConfig.receiverChainId,
};
await l1Sender.setRewardTokenConfig(rewardTokenConfig);

const depositTokenConfig: IL1Sender.DepositTokenConfigStruct = {
token: wStEth,
gateway: arbitrumBridgeGatewayRouter,
receiver: DefaultStorage.get('l2TokenReceiver'),
// receiver: '0x56c7db3D200c92eAAb8a2c4a9C1DcB8c50D4041F',
receiver: UserStorage.get('L2TokenReceiver Proxy'),
};
await l1Sender.setDepositTokenConfig(depositTokenConfig);

await l1Sender.transferOwnership(await distribution.getAddress());
const l1SenderImpl = await deployer.deploy(L1Sender__factory);
const l1SenderProxy = await deployer.deploy(ERC1967Proxy__factory, [l1SenderImpl, '0x'], {
name: 'L1Sender Proxy',
});
if (!UserStorage.has('L1Sender Proxy')) UserStorage.set('L1Sender Proxy', await l1SenderProxy.getAddress());
const l1Sender = L1Sender__factory.connect(await l1SenderProxy.getAddress(), await deployer.getSigner());
await l1Sender.L1Sender__init(distribution, rewardTokenConfig, depositTokenConfig);

await distribution.Distribution_init(stETH, l1Sender, config.pools || []);

Expand Down
20 changes: 9 additions & 11 deletions deploy/3_init_bridge.migration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Deployer } from '@solarity/hardhat-migrate';
import { Deployer, UserStorage } from '@solarity/hardhat-migrate';

import { parseConfig } from './helpers/config-parser';

Expand All @@ -11,7 +11,7 @@ import {
import { IL2MessageReceiver } from '@/generated-types/ethers/contracts/L2MessageReceiver';

module.exports = async function (deployer: Deployer) {
const config = parseConfig();
const config = parseConfig(await deployer.getChainId());

let lzEndpointL2: string;
if (config.lzConfig) {
Expand All @@ -24,22 +24,20 @@ module.exports = async function (deployer: Deployer) {
lzEndpointL2 = await lzEndpointL2Mock.getAddress();
}

const l2MessageReceiver = await deployer.deployed(
L2MessageReceiver__factory,
// '0xc37fF39e5A50543AD01E42C4Cd88c2939dD13002',
const l2MessageReceiver = L2MessageReceiver__factory.connect(
UserStorage.get('L2MessageReceiver Proxy'),
await deployer.getSigner(),
);

const l1SenderAddress = (await deployer.deployed(L1Sender__factory)).address;
// const l1SenderAddress = '0xEec0DF0991458274fF0ede917E9827fFc67a8332';
const l1Sender = L1Sender__factory.connect(UserStorage.get('L1Sender Proxy'), await deployer.getSigner());

const morAddress = (await deployer.deployed(MOR__factory)).address;
// const morAddress = '0x26BCDEb3E4e7EDf5657daF543132cAF792728908';
const mor = MOR__factory.connect(UserStorage.get('MOR'), await deployer.getSigner());

const l2MessageReceiverConfig: IL2MessageReceiver.ConfigStruct = {
gateway: lzEndpointL2,
sender: l1SenderAddress,
sender: l1Sender,
senderChainId: config.chainsConfig.senderChainId,
};

await l2MessageReceiver.setParams(morAddress, l2MessageReceiverConfig);
await l2MessageReceiver.setParams(mor, l2MessageReceiverConfig);
};
4 changes: 2 additions & 2 deletions deploy/data/config_goerli.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"pools": [
{
"payoutStart": 1703618736,
"payoutStart": 1735925178,
"decreaseInterval": 86400,
"withdrawLockPeriod": 120,
"claimLockPeriod": 60,
Expand All @@ -16,7 +16,7 @@
"isPublic": true
},
{
"payoutStart": 1703618736,
"payoutStart": 1735925178,
"decreaseInterval": 60,
"withdrawLockPeriod": 1,
"claimLockPeriod": 1,
Expand Down
8 changes: 4 additions & 4 deletions deploy/data/config_sepolia.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
},
"pools": [
{
"payoutStart": 1703672149,
"payoutStart": 1704360948,
"decreaseInterval": 86400,
"withdrawLockPeriod": 120,
"claimLockPeriod": 60,
"initialReward": "14400000000000000000000",
"rewardDecrease": "2468994701000000000",
"minimalStake": "1000000000000000",
"minimalStake": "10000000000",
"isPublic": true
},
{
"payoutStart": 1703672149,
"payoutStart": 1704360948,
"decreaseInterval": 60,
"withdrawLockPeriod": 1,
"claimLockPeriod": 1,
"initialReward": "100000000000000000000",
"rewardDecrease": "100000000000000000000",
"minimalStake": "1000000000000000",
"minimalStake": "10000000000",
"isPublic": false,
"whitelistedUsers": [
"0x901F2d23823730fb7F2356920e0E273EFdCdFe17",
Expand Down
9 changes: 9 additions & 0 deletions deploy/deploy-all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
set -e

verifyL1=$([ '$1' = 'localhost' ] && echo --verify || echo '')
verifyL2=$([ '$2' = 'localhost' ] && echo --verify || echo '')

npx hardhat migrate --network $2 --only 1 $verifyL2 $3
npx hardhat migrate --network $1 --only 2 $verifyL1 --continue
npx hardhat migrate --network $2 --only 3 $verifyL2 --continue
14 changes: 13 additions & 1 deletion deploy/helpers/config-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,19 @@ type PoolInitInfo = IDistribution.PoolStruct & {
amounts: BigNumberish[];
};

export function parseConfig(configPath: string = 'deploy/data/config_goerli.json'): Config {
export function parseConfig(chainId: bigint): Config {
let configPath: string;

if (chainId === 31337n) {
configPath = `deploy/data/config.json`;
} else if (chainId === 5n || chainId === 421613n) {
configPath = `deploy/data/config_goerli.json`;
} else if (chainId === 11155111n || chainId === 421614n) {
configPath = `deploy/data/config_sepolia.json`;
} else {
throw new Error(`Invalid chainId`);
}

const config: Config = JSON.parse(readFileSync(configPath, 'utf-8')) as Config;

if (config.cap == undefined) {
Expand Down
Loading

0 comments on commit 32b8b27

Please sign in to comment.