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

update bridge script #327

Open
wants to merge 2 commits into
base: master
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
98 changes: 72 additions & 26 deletions src/gas/liquity/TroveBridgeGas.s.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright 2022 Aztec.
pragma solidity >=0.8.4;

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
Expand All @@ -15,91 +14,138 @@ interface IRead {
}

contract TroveBridgeMeasure is LiquityTroveDeployment {
ISubsidy private constant SUBSIDY = ISubsidy(0xABc30E831B5Cc173A9Ed5941714A7845c909e7fA);
address private constant BENEFICIARY = address(uint160(uint256(keccak256(abi.encodePacked("_BENEFICIARY")))));
ISubsidy private constant SUBSIDY =
ISubsidy(0xABc30E831B5Cc173A9Ed5941714A7845c909e7fA);
address private constant BENEFICIARY =
address(uint160(uint256(keccak256(abi.encodePacked("_BENEFICIARY")))));
uint256 private constant MAX_FEE = 1e18; // Set appropriate fee limit.

GasBase internal gasBase;
TroveBridge internal bridge;

AztecTypes.AztecAsset internal emptyAsset;
AztecTypes.AztecAsset internal ethAsset;
AztecTypes.AztecAsset internal lusdAsset;
AztecTypes.AztecAsset internal tbAsset; // Accounting token
AztecTypes.AztecAsset internal tbAsset;

function setUp() public override(BaseDeployment) {
super.setUp();

address defiProxy = IRead(ROLLUP_PROCESSOR).defiBridgeProxy();
vm.label(defiProxy, "DefiProxy");

vm.broadcast();
gasBase = new GasBase(defiProxy);

// Deploy TroveBridge
address temp = ROLLUP_PROCESSOR;
ROLLUP_PROCESSOR = address(gasBase);
bridge = TroveBridge(payable(deploy(400)));
ROLLUP_PROCESSOR = temp;

ethAsset = AztecTypes.AztecAsset({id: 0, erc20Address: address(0), assetType: AztecTypes.AztecAssetType.ETH});
// Asset Initialization
ethAsset = AztecTypes.AztecAsset({
id: 0,
erc20Address: address(0),
assetType: AztecTypes.AztecAssetType.ETH
});
lusdAsset = AztecTypes.AztecAsset({
id: 1,
erc20Address: 0x5f98805A4E8be255a32880FDeC7F6728C6568bA0,
assetType: AztecTypes.AztecAssetType.ERC20
});
tbAsset =
AztecTypes.AztecAsset({id: 2, erc20Address: address(bridge), assetType: AztecTypes.AztecAssetType.ERC20});
tbAsset = AztecTypes.AztecAsset({
id: 2,
erc20Address: address(bridge),
assetType: AztecTypes.AztecAssetType.ERC20
});

vm.label(lusdAsset.erc20Address, "LUSD");
vm.label(tbAsset.erc20Address, "TB");

// Fund subsidy
// Fund Subsidy
vm.startBroadcast();
SUBSIDY.subsidize{value: 1e17}(address(bridge), 0, 500);
SUBSIDY.registerBeneficiary(BENEFICIARY);
SUBSIDY.subsidize{value: 1e17}(address(bridge), 1, 300);
SUBSIDY.registerBeneficiary(BENEFICIARY);
vm.stopBroadcast();

// Warp time to increase subsidy
uint256 subsidyBalance = SUBSIDY.claimableAmount(BENEFICIARY);
require(subsidyBalance > 0, "Subsidy funding failed");

// Increase Subsidy Time
vm.warp(block.timestamp + 10 days);
}

function measureETH() public {
openTrove(address(bridge));
vm.broadcast();
address(gasBase).call{value: 2 ether}("");
emit log_named_uint("ETH balance of gasBase", address(gasBase).balance);

// Borrow
// Transfer ETH
{
(bool success, ) = address(gasBase).call{value: 2 ether}("");
require(success, "ETH transfer failed");
emit log_named_uint(
"ETH balance of gasBase",
address(gasBase).balance
);
}

// Borrow LUSD
{
vm.broadcast();
gasBase.convert(
address(bridge), ethAsset, emptyAsset, tbAsset, lusdAsset, 1 ether, 0, MAX_FEE, BENEFICIARY, 630000
address(bridge),
ethAsset,
emptyAsset,
tbAsset,
lusdAsset,
1 ether,
0,
MAX_FEE,
BENEFICIARY,
630000
);
}

uint256 claimableSubsidyAfterDeposit = SUBSIDY.claimableAmount(BENEFICIARY);
assertGt(claimableSubsidyAfterDeposit, 0, "Subsidy was not claimed during deposit");
emit log_named_uint("Claimable subsidy after deposit", claimableSubsidyAfterDeposit);
uint256 subsidyAfterBorrow = SUBSIDY.claimableAmount(BENEFICIARY);
require(subsidyAfterBorrow > 0, "Subsidy not claimed during borrow");
emit log_named_uint(
"Claimable subsidy after borrow",
subsidyAfterBorrow
);

// Repay
// Repay Debt
{
uint256 lusdBalance = IERC20(lusdAsset.erc20Address).balanceOf(address(gasBase));
uint256 tbBalance = IERC20(tbAsset.erc20Address).balanceOf(address(gasBase));
uint256 lusdBalance = IERC20(lusdAsset.erc20Address).balanceOf(
address(gasBase)
);
uint256 tbBalance = IERC20(tbAsset.erc20Address).balanceOf(
address(gasBase)
);

emit log_named_uint("LUSD balance", lusdBalance);
emit log_named_uint("TB balance", tbBalance);

vm.broadcast();
gasBase.convert(
address(bridge), tbAsset, lusdAsset, ethAsset, lusdAsset, lusdBalance / 2, 0, 0, BENEFICIARY, 480000
address(bridge),
tbAsset,
lusdAsset,
ethAsset,
lusdAsset,
lusdBalance / 2,
0,
0,
BENEFICIARY,
480000
);
}

uint256 claimableSubsidyAfterRepayment = SUBSIDY.claimableAmount(BENEFICIARY);
assertGt(
claimableSubsidyAfterRepayment, claimableSubsidyAfterDeposit, "Subsidy was not claimed during repayment"
uint256 subsidyAfterRepay = SUBSIDY.claimableAmount(BENEFICIARY);
require(
subsidyAfterRepay > subsidyAfterBorrow,
"Subsidy not claimed during repay"
);
emit log_named_uint("Claimable subsidy after repayment", claimableSubsidyAfterRepayment);
emit log_named_uint("Claimable subsidy after repay", subsidyAfterRepay);
}
}
77 changes: 52 additions & 25 deletions src/gas/uniswap/UniswapGas.s.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright 2022 Aztec.
pragma solidity >=0.8.4;

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
Expand All @@ -18,6 +17,11 @@ interface IRead {
contract UniswapMeasure is UniswapDeployment {
ISubsidy private constant SUBSIDY = ISubsidy(0xABc30E831B5Cc173A9Ed5941714A7845c909e7fA);
address private constant BENEFICIARY = address(uint160(uint256(keccak256(abi.encodePacked("_BENEFICIARY")))));
address private constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;

uint256 private constant SUBSIDY_AMOUNT = 1e17; // 0.1 ETH for subsidy
uint256 private constant GAS_LIMIT_1_SPLIT = 300000;
uint256 private constant GAS_LIMIT_MAX_COMPLEX = 660000;

address public constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
address public constant USDT = 0xdAC17F958D2ee523a2206206994597C13D831ec7;
Expand All @@ -36,65 +40,79 @@ contract UniswapMeasure is UniswapDeployment {
function setUp() public override(BaseDeployment) {
super.setUp();

// Initialize defiProxy
address defiProxy = IRead(ROLLUP_PROCESSOR).defiBridgeProxy();
vm.label(defiProxy, "DefiProxy");

vm.broadcast();
gasBase = new GasBase(defiProxy);

// Deploy the UniswapBridge contract
address temp = ROLLUP_PROCESSOR;
ROLLUP_PROCESSOR = address(gasBase);
bridge = UniswapBridge(payable(deploy()));
ROLLUP_PROCESSOR = temp;

// Initialize assets
ethAsset = AztecTypes.AztecAsset({id: 0, erc20Address: address(0), assetType: AztecTypes.AztecAssetType.ETH});
wethAsset =
AztecTypes.AztecAsset({id: 2, erc20Address: address(WETH), assetType: AztecTypes.AztecAssetType.ERC20});
wethAsset = AztecTypes.AztecAsset({id: 2, erc20Address: WETH, assetType: AztecTypes.AztecAssetType.ERC20});
daiAsset = AztecTypes.AztecAsset({
id: 1,
erc20Address: 0x6B175474E89094C44Da98b954EedeAC495271d0F,
assetType: AztecTypes.AztecAssetType.ERC20
});

// List vaults and fund subsidy
// Register vaults and fund subsidies
vm.startBroadcast();
bridge.registerSubsidyCriteria(ethAsset.erc20Address, daiAsset.erc20Address);
bridge.registerSubsidyCriteria(daiAsset.erc20Address, ethAsset.erc20Address);
SUBSIDY.subsidize{value: 1e17}(

SUBSIDY.subsidize{value: SUBSIDY_AMOUNT}(
address(bridge), bridge.computeCriteria(ethAsset, emptyAsset, daiAsset, emptyAsset, 0), 500
);
SUBSIDY.subsidize{value: 1e17}(
SUBSIDY.subsidize{value: SUBSIDY_AMOUNT}(
address(bridge), bridge.computeCriteria(daiAsset, emptyAsset, ethAsset, emptyAsset, 0), 500
);
SUBSIDY.registerBeneficiary(BENEFICIARY);
vm.stopBroadcast();

// Warp time to increase subsidy
// Increase time to allow subsidy accumulation
vm.warp(block.timestamp + 10 days);
}

function measure1SplitPathSwap() public {
// Encode path
uint64 path = bridge.encodePath(
1 ether, 1e20, WETH, UniswapBridge.SplitPath(100, 500, USDC, 100, address(0), 100), emptySplitPath
);

vm.broadcast();
address(gasBase).call{value: 2 ether}("");
// Transfer ETH
(bool success, ) = address(gasBase).call{value: 2 ether}("");
require(success, "ETH transfer failed");
emit log_named_uint("ETH balance of gasBase", address(gasBase).balance);

{
vm.broadcast();
gasBase.convert(
address(bridge), ethAsset, emptyAsset, daiAsset, emptyAsset, 1 ether, 0, path, BENEFICIARY, 300000
);
}
// Perform the swap
vm.broadcast();
gasBase.convert(
address(bridge),
ethAsset,
emptyAsset,
daiAsset,
emptyAsset,
1 ether,
0,
path,
BENEFICIARY,
GAS_LIMIT_1_SPLIT
);

uint256 claimableSubsidyAfterDeposit = SUBSIDY.claimableAmount(BENEFICIARY);
assertGt(claimableSubsidyAfterDeposit, 0, "Subsidy was not claimed during deposit");
require(claimableSubsidyAfterDeposit > 0, "Subsidy was not claimed during deposit");
emit log_named_uint("Claimable subsidy after deposit", claimableSubsidyAfterDeposit);
}

function measureMaxComplexPath() public {
// Encode complex path
uint64 path = bridge.encodePath(
1 ether,
1e20,
Expand All @@ -103,19 +121,28 @@ contract UniswapMeasure is UniswapDeployment {
UniswapBridge.SplitPath(50, 3000, USDT, 100, USDC, 500)
);

vm.broadcast();
address(gasBase).call{value: 2 ether}("");
// Transfer ETH
(bool success, ) = address(gasBase).call{value: 2 ether}("");
require(success, "ETH transfer failed");
emit log_named_uint("ETH balance of gasBase", address(gasBase).balance);

{
vm.broadcast();
gasBase.convert(
address(bridge), ethAsset, emptyAsset, daiAsset, emptyAsset, 1 ether, 0, path, BENEFICIARY, 660000
);
}
// Perform the complex swap
vm.broadcast();
gasBase.convert(
address(bridge),
ethAsset,
emptyAsset,
daiAsset,
emptyAsset,
1 ether,
0,
path,
BENEFICIARY,
GAS_LIMIT_MAX_COMPLEX
);

uint256 claimableSubsidyAfterDeposit = SUBSIDY.claimableAmount(BENEFICIARY);
assertGt(claimableSubsidyAfterDeposit, 0, "Subsidy was not claimed during deposit");
require(claimableSubsidyAfterDeposit > 0, "Subsidy was not claimed during deposit");
emit log_named_uint("Claimable subsidy after deposit", claimableSubsidyAfterDeposit);
}
}