-
Notifications
You must be signed in to change notification settings - Fork 143
/
ExampleBridge.sol
92 lines (82 loc) · 3.84 KB
/
ExampleBridge.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// SPDX-License-Identifier: Apache-2.0
// Copyright 2022 Aztec.
pragma solidity >=0.8.4;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {AztecTypes} from "rollup-encoder/libraries/AztecTypes.sol";
import {ErrorLib} from "../base/ErrorLib.sol";
import {BridgeBase} from "../base/BridgeBase.sol";
/**
* @title An example bridge contract.
* @author Aztec Team
* @notice You can use this contract to immediately get back what you've deposited.
* @dev This bridge demonstrates the flow of assets in the convert function. This bridge simply returns what has been
* sent to it.
*/
contract ExampleBridge is BridgeBase {
/**
* @notice Set address of rollup processor
* @param _rollupProcessor Address of rollup processor
*/
constructor(address _rollupProcessor) BridgeBase(_rollupProcessor) {
address dai = 0x6B175474E89094C44Da98b954EedeAC495271d0F;
address usdc = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
uint256[] memory criterias = new uint256[](2);
uint32[] memory gasUsage = new uint32[](2);
uint32[] memory minGasPerMinute = new uint32[](2);
criterias[0] = uint256(keccak256(abi.encodePacked(dai, dai)));
criterias[1] = uint256(keccak256(abi.encodePacked(usdc, usdc)));
gasUsage[0] = 72896;
gasUsage[1] = 80249;
minGasPerMinute[0] = 100;
minGasPerMinute[1] = 150;
// We set gas usage in the subsidy contract
// We only want to incentivize the bridge when input and output token is Dai or USDC
SUBSIDY.setGasUsageAndMinGasPerMinute(criterias, gasUsage, minGasPerMinute);
}
/**
* @notice A function which returns an _totalInputValue amount of _inputAssetA
* @param _inputAssetA - Arbitrary ERC20 token
* @param _outputAssetA - Equal to _inputAssetA
* @param _rollupBeneficiary - Address of the contract which receives subsidy in case subsidy was set for a given
* criteria
* @return outputValueA - the amount of output asset to return
* @dev In this case _outputAssetA equals _inputAssetA
*/
function convert(
AztecTypes.AztecAsset calldata _inputAssetA,
AztecTypes.AztecAsset calldata _inputAssetB,
AztecTypes.AztecAsset calldata _outputAssetA,
AztecTypes.AztecAsset calldata _outputAssetB,
uint256 _totalInputValue,
uint256,
uint64 _auxData,
address _rollupBeneficiary
) external payable override(BridgeBase) onlyRollup returns (uint256 outputValueA, uint256, bool) {
// Check the input asset is ERC20
if (_inputAssetA.assetType != AztecTypes.AztecAssetType.ERC20) revert ErrorLib.InvalidInputA();
if (_outputAssetA.erc20Address != _inputAssetA.erc20Address) revert ErrorLib.InvalidOutputA();
// Return the input value of input asset
outputValueA = _totalInputValue;
// Approve rollup processor to take input value of input asset
IERC20(_outputAssetA.erc20Address).approve(ROLLUP_PROCESSOR, _totalInputValue);
// Pay out subsidy to the rollupBeneficiary
SUBSIDY.claimSubsidy(
computeCriteria(_inputAssetA, _inputAssetB, _outputAssetA, _outputAssetB, _auxData), _rollupBeneficiary
);
}
/**
* @notice Computes the criteria that is passed when claiming subsidy.
* @param _inputAssetA The input asset
* @param _outputAssetA The output asset
* @return The criteria
*/
function computeCriteria(
AztecTypes.AztecAsset calldata _inputAssetA,
AztecTypes.AztecAsset calldata,
AztecTypes.AztecAsset calldata _outputAssetA,
AztecTypes.AztecAsset calldata,
uint64
) public view override(BridgeBase) returns (uint256) {
return uint256(keccak256(abi.encodePacked(_inputAssetA.erc20Address, _outputAssetA.erc20Address)));
}
}