-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDecentBridgeAdapter.sol
159 lines (138 loc) · 4.51 KB
/
DecentBridgeAdapter.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import {IERC20} from "forge-std/interfaces/IERC20.sol";
import {IUTB} from "../interfaces/IUTB.sol";
import {IBridgeAdapter} from "../interfaces/IBridgeAdapter.sol";
import {SwapInstructions} from "../CommonTypes.sol";
import {SwapParams} from "../swappers/SwapParams.sol";
import {IDecentEthRouter} from "decent-bridge/src/interfaces/IDecentEthRouter.sol";
import {BaseAdapter} from "./BaseAdapter.sol";
contract DecentBridgeAdapter is BaseAdapter, IBridgeAdapter {
uint8 public constant BRIDGE_ID = 0;
mapping(uint256 => address) public destinationBridgeAdapter;
IDecentEthRouter public router;
mapping(uint256 => uint16) lzIdLookup;
mapping(uint16 => uint256) chainIdLookup;
bool gasIsEth;
address bridgeToken;
constructor(bool _gasIsEth, address _bridgeToken) BaseAdapter() {
gasIsEth = _gasIsEth;
bridgeToken = _bridgeToken;
}
function setRouter(address _router) public onlyOwner {
router = IDecentEthRouter(payable(_router));
}
function getId() public pure returns (uint8) {
return BRIDGE_ID;
}
function registerRemoteBridgeAdapter(
uint256 dstChainId,
uint16 dstLzId,
address decentBridgeAdapter
) public onlyOwner {
lzIdLookup[dstChainId] = dstLzId;
chainIdLookup[dstLzId] = dstChainId;
destinationBridgeAdapter[dstChainId] = decentBridgeAdapter;
}
function estimateFees(
SwapInstructions memory postBridge,
uint256 dstChainId,
address target,
uint64 dstGas,
bytes memory payload
) public view returns (uint nativeFee, uint zroFee) {
SwapParams memory swapParams = abi.decode(
postBridge.swapPayload,
(SwapParams)
);
return
router.estimateSendAndCallFee(
router.MT_ETH_TRANSFER_WITH_PAYLOAD(),
lzIdLookup[dstChainId],
target,
swapParams.amountIn,
dstGas,
false,
payload
);
}
function getBridgeToken(
bytes calldata /*additionalArgs*/
) external view returns (address) {
return bridgeToken;
}
function getBridgedAmount(
uint256 amt2Bridge,
address /*tokenIn*/,
address /*tokenOut*/
) external pure returns (uint256) {
return amt2Bridge;
}
function bridge(
uint256 amt2Bridge,
SwapInstructions memory postBridge,
uint256 dstChainId,
address target,
address paymentOperator,
bytes memory payload,
bytes calldata additionalArgs,
address payable refund
) public payable onlyUtb returns (bytes memory bridgePayload) {
require(
destinationBridgeAdapter[dstChainId] != address(0),
string.concat("dst chain address not set ")
);
uint64 dstGas = abi.decode(additionalArgs, (uint64));
bridgePayload = abi.encodeCall(
this.receiveFromBridge,
(postBridge, target, paymentOperator, payload, refund)
);
SwapParams memory swapParams = abi.decode(
postBridge.swapPayload,
(SwapParams)
);
if (!gasIsEth) {
IERC20(bridgeToken).transferFrom(
msg.sender,
address(this),
amt2Bridge
);
IERC20(bridgeToken).approve(address(router), amt2Bridge);
}
router.bridgeWithPayload{value: msg.value}(
lzIdLookup[dstChainId],
destinationBridgeAdapter[dstChainId],
swapParams.amountIn,
false,
dstGas,
bridgePayload
);
}
function receiveFromBridge(
SwapInstructions memory postBridge,
address target,
address paymentOperator,
bytes memory payload,
address payable refund
) public onlyExecutor {
SwapParams memory swapParams = abi.decode(
postBridge.swapPayload,
(SwapParams)
);
IERC20(swapParams.tokenIn).transferFrom(
msg.sender,
address(this),
swapParams.amountIn
);
IERC20(swapParams.tokenIn).approve(utb, swapParams.amountIn);
IUTB(utb).receiveFromBridge(
postBridge,
target,
paymentOperator,
payload,
refund
);
}
receive() external payable {}
fallback() external payable {}
}